Locked off training and untrained while attacking
This commit is contained in:
+4
-1
@@ -17,7 +17,10 @@ let excluded = [ //messages that should not be logged
|
|||||||
'attacking',
|
'attacking',
|
||||||
'idle',
|
'idle',
|
||||||
|
|
||||||
'Combat log sent'
|
'Combat log sent',
|
||||||
|
|
||||||
|
'Can\'t train while attacking',
|
||||||
|
'Can\'t untrain while attacking'
|
||||||
];
|
];
|
||||||
|
|
||||||
const log = (msg, ...args) => {
|
const log = (msg, ...args) => {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ So changes I've made today:
|
|||||||
* Google analytics implemented.
|
* Google analytics implemented.
|
||||||
* The game ladder can now point to a specific rank with the URL: [https://kingdombattles.net/ladder?rank=0](ladder?rank=0)
|
* The game ladder can now point to a specific rank with the URL: [https://kingdombattles.net/ladder?rank=0](ladder?rank=0)
|
||||||
* The same goes for the profile's combat log: [https://kingdombattles.net/profile?log=0](profile?log=0)
|
* The same goes for the profile's combat log: [https://kingdombattles.net/profile?log=0](profile?log=0)
|
||||||
|
* You can no longer train and untrain while attacking.
|
||||||
|
|
||||||
Known Bugs:
|
Known Bugs:
|
||||||
|
|
||||||
|
|||||||
+41
-11
@@ -48,8 +48,10 @@ const attackRequest = (connection) => (req, res) => {
|
|||||||
let attackingUnits = results[0].soldiers;
|
let attackingUnits = results[0].soldiers;
|
||||||
|
|
||||||
//verify that the attacker is not already attacking someone
|
//verify that the attacker is not already attacking someone
|
||||||
isAttacking(connection, req.body.attacker, (isAttacking) => {
|
isAttacking(connection, req.body.attacker, (err, attacking) => {
|
||||||
if (isAttacking) {
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (attacking) {
|
||||||
res.status(400).write(log('You are already attacking someone', req.body.attacker, req.body.defender));
|
res.status(400).write(log('You are already attacking someone', req.body.attacker, req.body.defender));
|
||||||
res.end();
|
res.end();
|
||||||
return;
|
return;
|
||||||
@@ -75,9 +77,11 @@ const attackRequest = (connection) => (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const attackStatusRequest = (connection) => (req, res) => {
|
const attackStatusRequest = (connection) => (req, res) => {
|
||||||
isAttacking(connection, req.body.username, (isAttacking, defender) => {
|
isAttacking(connection, req.body.username, (err, attacking, defender) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
status: log(isAttacking ? 'attacking' : 'idle', req.body.username, defender),
|
status: log(attacking ? 'attacking' : 'idle', req.body.username, defender),
|
||||||
defender: defender
|
defender: defender
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -103,8 +107,25 @@ const runCombatTick = (connection) => {
|
|||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
results.forEach((pendingCombat) => {
|
results.forEach((pendingCombat) => {
|
||||||
|
//check that the attacker still has enough soliders
|
||||||
|
let query = 'SELECT soldiers FROM profiles WHERE accountId = ?;';
|
||||||
|
connection.query(query, [pendingCombat.attackerId], (err, results) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (results[0].soldiers < pendingCombat.attackingUnits) {
|
||||||
|
//delete the failed combat
|
||||||
|
let query = 'DELETE FROM pendingCombat WHERE id = ?;';
|
||||||
|
connection.query(query, [pendingCombat.id], (err) => {
|
||||||
|
if (err) throw err;
|
||||||
|
log('Not enough soldiers for attack', pendingCombat.attackerId, results[0].soldiers, pendingCombat.attackingUnits);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//get the defender's undefended status
|
//get the defender's undefended status
|
||||||
isAttacking(connection, pendingCombat.defenderId, (undefended) => {
|
isAttacking(connection, pendingCombat.defenderId, (err, undefended) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
//get the defending unit count, gold
|
//get the defending unit count, gold
|
||||||
let query = 'SELECT soldiers, recruits, gold FROM profiles WHERE accountId = ?;';
|
let query = 'SELECT soldiers, recruits, gold FROM profiles WHERE accountId = ?;';
|
||||||
|
|
||||||
@@ -161,30 +182,38 @@ const runCombatTick = (connection) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
combatTick.start();
|
combatTick.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isNormalInteger = (str) => {
|
||||||
|
let n = Math.floor(Number(str));
|
||||||
|
return n !== Infinity && String(n) == str && n >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
const isAttacking = (connection, user, cb) => {
|
const isAttacking = (connection, user, cb) => {
|
||||||
let query;
|
let query;
|
||||||
|
|
||||||
if (typeof(user) === 'string') {
|
if (isNormalInteger(user)) {
|
||||||
query = 'SELECT * FROM pendingCombat WHERE attackerId IN (SELECT id FROM accounts WHERE username = ?);';
|
|
||||||
} else if (typeof(user) === 'number') {
|
|
||||||
query = 'SELECT * FROM pendingCombat WHERE attackerId = ?;';
|
query = 'SELECT * FROM pendingCombat WHERE attackerId = ?;';
|
||||||
|
} else if (typeof(user) === 'string') {
|
||||||
|
query = 'SELECT * FROM pendingCombat WHERE attackerId IN (SELECT id FROM accounts WHERE username = ?);';
|
||||||
|
} else {
|
||||||
|
return cb('Unknown argument type for user');
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.query(query, [user], (err, results) => {
|
connection.query(query, [user], (err, results) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
if (results.length === 0) {
|
if (results.length === 0) {
|
||||||
cb(false);
|
return cb(undefined, false);
|
||||||
} else {
|
} else {
|
||||||
//get the username of the person being attacked
|
//get the username of the person being attacked
|
||||||
let query = 'SELECT username FROM accounts WHERE id = ?;';
|
let query = 'SELECT username FROM accounts WHERE id = ?;';
|
||||||
connection.query(query, [results[0].defenderId], (err, results) => {
|
connection.query(query, [results[0].defenderId], (err, results) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
cb(true, results[0].username);
|
return cb(undefined, true, results[0].username);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -194,7 +223,8 @@ module.exports = {
|
|||||||
attackRequest: attackRequest,
|
attackRequest: attackRequest,
|
||||||
attackStatusRequest: attackStatusRequest,
|
attackStatusRequest: attackStatusRequest,
|
||||||
combatLogRequest: combatLogRequest,
|
combatLogRequest: combatLogRequest,
|
||||||
runCombatTick: runCombatTick
|
runCombatTick: runCombatTick,
|
||||||
|
isAttacking: isAttacking
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ require('dotenv').config();
|
|||||||
let formidable = require('formidable');
|
let formidable = require('formidable');
|
||||||
let CronJob = require('cron').CronJob;
|
let CronJob = require('cron').CronJob;
|
||||||
|
|
||||||
|
let { isAttacking } = require('./combat.js');
|
||||||
|
|
||||||
//utilities
|
//utilities
|
||||||
let { log } = require('../common/utilities.js');
|
let { log } = require('../common/utilities.js');
|
||||||
|
|
||||||
@@ -204,6 +206,16 @@ const trainRequest = (connection) => (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//can't train while attacking
|
||||||
|
isAttacking(connection, fields.id, (err, attacking) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (attacking) {
|
||||||
|
res.status(400).write(log('Can\'t train while attacking', fields.id));
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//determine the cost of the training TODO: make these global for the client too
|
//determine the cost of the training TODO: make these global for the client too
|
||||||
let cost = 0;
|
let cost = 0;
|
||||||
switch(fields.role) {
|
switch(fields.role) {
|
||||||
@@ -270,6 +282,7 @@ const trainRequest = (connection) => (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const untrainRequest = (connection) => (req, res) => {
|
const untrainRequest = (connection) => (req, res) => {
|
||||||
@@ -298,6 +311,16 @@ const untrainRequest = (connection) => (req, res) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//can't untrain while attacking
|
||||||
|
isAttacking(connection, fields.id, (err, attacking) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
if (attacking) {
|
||||||
|
res.status(400).write(log('Can\'t untrain while attacking', fields.id));
|
||||||
|
res.end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//verify that the user has a high enough balance
|
//verify that the user has a high enough balance
|
||||||
let query = 'SELECT soldiers, spies, scientists FROM profiles WHERE accountId = ?;';
|
let query = 'SELECT soldiers, spies, scientists FROM profiles WHERE accountId = ?;';
|
||||||
connection.query(query, [fields.id], (err, results) => {
|
connection.query(query, [fields.id], (err, results) => {
|
||||||
@@ -354,6 +377,7 @@ const untrainRequest = (connection) => (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const ladderRequest = (connection) => (req, res) => {
|
const ladderRequest = (connection) => (req, res) => {
|
||||||
|
|||||||
@@ -324,8 +324,7 @@ class Profile extends React.Component {
|
|||||||
|
|
||||||
ProfileNotFoundMainPanel() {
|
ProfileNotFoundMainPanel() {
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className='page' />
|
||||||
</div>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user