Locked off training and untrained while attacking

This commit is contained in:
2019-05-30 06:53:49 +10:00
parent 9d9ae45ba0
commit daf7c9b157
5 changed files with 194 additions and 137 deletions
+4 -1
View File
@@ -17,7 +17,10 @@ let excluded = [ //messages that should not be logged
'attacking',
'idle',
'Combat log sent'
'Combat log sent',
'Can\'t train while attacking',
'Can\'t untrain while attacking'
];
const log = (msg, ...args) => {
+1
View File
@@ -8,6 +8,7 @@ So changes I've made today:
* 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 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:
+41 -11
View File
@@ -48,8 +48,10 @@ const attackRequest = (connection) => (req, res) => {
let attackingUnits = results[0].soldiers;
//verify that the attacker is not already attacking someone
isAttacking(connection, req.body.attacker, (isAttacking) => {
if (isAttacking) {
isAttacking(connection, req.body.attacker, (err, attacking) => {
if (err) throw err;
if (attacking) {
res.status(400).write(log('You are already attacking someone', req.body.attacker, req.body.defender));
res.end();
return;
@@ -75,9 +77,11 @@ const attackRequest = (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({
status: log(isAttacking ? 'attacking' : 'idle', req.body.username, defender),
status: log(attacking ? 'attacking' : 'idle', req.body.username, defender),
defender: defender
});
@@ -103,8 +107,25 @@ const runCombatTick = (connection) => {
if (err) throw err;
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
isAttacking(connection, pendingCombat.defenderId, (undefended) => {
isAttacking(connection, pendingCombat.defenderId, (err, undefended) => {
if (err) throw err;
//get the defending unit count, gold
let query = 'SELECT soldiers, recruits, gold FROM profiles WHERE accountId = ?;';
@@ -161,30 +182,38 @@ const runCombatTick = (connection) => {
});
});
});
});
combatTick.start();
}
const isNormalInteger = (str) => {
let n = Math.floor(Number(str));
return n !== Infinity && String(n) == str && n >= 0;
}
const isAttacking = (connection, user, cb) => {
let query;
if (typeof(user) === 'string') {
query = 'SELECT * FROM pendingCombat WHERE attackerId IN (SELECT id FROM accounts WHERE username = ?);';
} else if (typeof(user) === 'number') {
if (isNormalInteger(user)) {
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) => {
if (err) throw err;
if (results.length === 0) {
cb(false);
return cb(undefined, false);
} else {
//get the username of the person being attacked
let query = 'SELECT username FROM accounts WHERE id = ?;';
connection.query(query, [results[0].defenderId], (err, results) => {
if (err) throw err;
cb(true, results[0].username);
return cb(undefined, true, results[0].username);
});
}
});
@@ -194,7 +223,8 @@ module.exports = {
attackRequest: attackRequest,
attackStatusRequest: attackStatusRequest,
combatLogRequest: combatLogRequest,
runCombatTick: runCombatTick
runCombatTick: runCombatTick,
isAttacking: isAttacking
}
/*
+24
View File
@@ -5,6 +5,8 @@ require('dotenv').config();
let formidable = require('formidable');
let CronJob = require('cron').CronJob;
let { isAttacking } = require('./combat.js');
//utilities
let { log } = require('../common/utilities.js');
@@ -204,6 +206,16 @@ const trainRequest = (connection) => (req, res) => {
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
let cost = 0;
switch(fields.role) {
@@ -270,6 +282,7 @@ const trainRequest = (connection) => (req, res) => {
});
});
});
});
}
const untrainRequest = (connection) => (req, res) => {
@@ -298,6 +311,16 @@ const untrainRequest = (connection) => (req, res) => {
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
let query = 'SELECT soldiers, spies, scientists FROM profiles WHERE accountId = ?;';
connection.query(query, [fields.id], (err, results) => {
@@ -354,6 +377,7 @@ const untrainRequest = (connection) => (req, res) => {
});
});
});
});
}
const ladderRequest = (connection) => (req, res) => {
+1 -2
View File
@@ -324,8 +324,7 @@ class Profile extends React.Component {
ProfileNotFoundMainPanel() {
return (
<div>
</div>
<div className='page' />
);
}