cleanup and check for enemy team in pvp

This commit is contained in:
ntr 2018-12-03 13:33:54 +11:00
parent ba1e909d89
commit b38be6d7bb
3 changed files with 82 additions and 79 deletions

View File

@ -195,6 +195,8 @@ class CombatCryps extends Phaser.GameObjects.Group {
if (addKeys) this.scene.crypKeyHandler(crypObj, crypObj.iter); if (addKeys) this.scene.crypKeyHandler(crypObj, crypObj.iter);
}; };
allyTeam.cryps.forEach((cryp, i) => renderTeam(cryp, i, 0)); allyTeam.cryps.forEach((cryp, i) => renderTeam(cryp, i, 0));
if (!enemyTeam) return false;
enemyTeam.cryps.forEach((cryp, i) => renderTeam(cryp, i, 1)); enemyTeam.cryps.forEach((cryp, i) => renderTeam(cryp, i, 1));
return true; return true;
} }

View File

@ -15,86 +15,10 @@ function errorToast(err) {
function createSocket(events) { function createSocket(events) {
let ws; let ws;
function connect() {
ws = new WebSocket(SOCKET_URL);
ws.binaryType = 'arraybuffer';
// Connection opened
ws.addEventListener('open', (event) => {
toast.info({
message: 'connected',
position: 'topRight',
});
events.loginPrompt();
if (process.env.NODE_ENV !== 'production') {
send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } });
}
});
// Listen for messages
ws.addEventListener('message', onMessage);
ws.addEventListener('error', (event) => {
console.error('WebSocket error', event);
account = null;
// return setTimeout(connect, 5000);
});
ws.addEventListener('close', (event) => {
console.error('WebSocket closed', event);
account = null;
return setTimeout(connect, 5000);
});
return ws;
}
// handle account auth within the socket itself // handle account auth within the socket itself
// https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html // https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html
let account = null; let account = null;
// -------------
// Incoming
// -------------
function accountLogin(res) {
const [struct, login] = res;
account = login;
events.setAccount(login);
sendAccountItems();
sendAccountCryps();
sendGameJoinableList();
}
function accountCryps(response) {
const [structName, cryps] = response;
events.setCryps(cryps);
}
function gameState(response) {
const [structName, game] = response;
events.setGame(game);
}
function gameJoinableList(response) {
const [structName, gameList] = response;
events.setGameList(gameList);
}
function crypSpawn(response) {
const [structName, cryp] = response;
}
function gamePve(response) {
const [structName, game] = response;
}
function accountItems(response) {
const [structName, items] = response;
events.setItems(items);
}
// ------------- // -------------
// Outgoing // Outgoing
// ------------- // -------------
@ -175,8 +99,50 @@ function createSocket(events) {
} }
// ------------- // -------------
// Handling // Incoming
// ------------- // -------------
function accountLogin(res) {
const [struct, login] = res;
account = login;
events.setAccount(login);
sendAccountItems();
sendAccountCryps();
sendGameJoinableList();
}
function accountCryps(response) {
const [structName, cryps] = response;
events.setCryps(cryps);
}
function gameState(response) {
const [structName, game] = response;
events.setGame(game);
}
function gameJoinableList(response) {
const [structName, gameList] = response;
events.setGameList(gameList);
}
function crypSpawn(response) {
const [structName, cryp] = response;
}
function gamePve(response) {
const [structName, game] = response;
}
function accountItems(response) {
const [structName, items] = response;
events.setItems(items);
}
// -------------
// Setup
// -------------
// when the server sends a reply it will have one of these message types // when the server sends a reply it will have one of these message types
// this object wraps the reply types to a function // this object wraps the reply types to a function
const handlers = { const handlers = {
@ -206,6 +172,41 @@ function createSocket(events) {
return handlers[method](params); return handlers[method](params);
} }
function connect() {
ws = new WebSocket(SOCKET_URL);
ws.binaryType = 'arraybuffer';
// Connection opened
ws.addEventListener('open', () => {
toast.info({
message: 'connected',
position: 'topRight',
});
events.loginPrompt();
if (process.env.NODE_ENV !== 'production') {
send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } });
}
});
// Listen for messages
ws.addEventListener('message', onMessage);
ws.addEventListener('error', (event) => {
console.error('WebSocket error', event);
// account = null;
// return setTimeout(connect, 5000);
});
ws.addEventListener('close', (event) => {
console.error('WebSocket closed', event);
// account = null;
return setTimeout(connect, 5000);
});
return ws;
}
return { return {
sendAccountLogin, sendAccountLogin,
sendAccountCreate, sendAccountCreate,

View File

@ -156,12 +156,12 @@ impl Cryp {
let mut rng = thread_rng(); let mut rng = thread_rng();
let max = match self.lvl == 64 { let max = match self.lvl == 64 {
true => u64::max_value(), true => u64::max_value(),
false => 2_u64.pow(self.lvl.into()), false => 2_u64.pow(self.lvl.saturating_sub(1).into()),
}; };
let min = match self.lvl == 1 { let min = match self.lvl == 1 {
true => 2_u64, true => 2_u64,
false => 2_u64.pow(self.lvl.saturating_sub(1).into()), false => 2_u64.pow(self.lvl.saturating_sub(2).into()),
}; };
self.xp = max; self.xp = max;