From f334e342b2e00ff00ffef0e80656e615a3c64ae4 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 30 May 2019 21:43:32 +1000 Subject: [PATCH] Fixed network latency bug --- server/combat.js | 15 --------------- server/equipment_prices.js | 19 +++++++++++++++++++ server/profiles.js | 24 +++++++++++++++++++----- sql/create_database_structure.sql | 2 +- 4 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 server/equipment_prices.js diff --git a/server/combat.js b/server/combat.js index e655902..ba1dae5 100644 --- a/server/combat.js +++ b/server/combat.js @@ -226,18 +226,3 @@ module.exports = { runCombatTick: runCombatTick, isAttacking: isAttacking } - -/* -> You can attack another player using your soldiers (it doesn't work without soldiers). -> Doing so takes time, up to 10 seconds for every soldier you have. -> Combat takes place at the end of the time delay, at which point you can attack people again (after reloading the page). -> While attacking, you are undefended. -> While undefended, your recruits act as combatants, otherwise your soldiers do. -> The chance of success is determined by the ratio of each side's combatant strength. -> Recruits have a strength equal to 0.25 times that of a soldier. -> On a success, you steal 10% of the target's gold. On a failure, you steal 2% of the target's gold. -> The attacking force will lose a percentage, rounded down, of their units - 5% on a success, 10% on a failure (edit: excluding the first 10 units). -> If the server resets (which happens alot) combat still progresses as expected. -* All combat is logged and presented to the player. -*/ - diff --git a/server/equipment_prices.js b/server/equipment_prices.js new file mode 100644 index 0000000..af5d8e4 --- /dev/null +++ b/server/equipment_prices.js @@ -0,0 +1,19 @@ +let weapons = [ + { name: 'Stick', cost: 50, boost: 0.02, level: 1 }, + { name: 'Dagger', cost: 75, boost: 0.03, level: 2 }, + { name: 'Sword', cost: 100, boost: 0.04, level: 3 }, + { name: 'Longsword', cost: 150, boost: 0.05, level: 4 }, + { name: 'Frying Pan', cost: 200, boost: 0.06, level: 5 }, +]; + +let armour = [ + { name: 'leather', cost: 75, boost: 0.02, level: 2 }, + { name: 'gambeson', cost: 100, boost: 0.03, level: 3 }, + { name: 'chainmail', cost: 150, boost: 0.04, level: 4 }, + { name: 'platemail', cost: 200, boost: 0.05, level: 5 }, +]; + +module.exports = { + weapons: weapons, + armour: armour +}; \ No newline at end of file diff --git a/server/profiles.js b/server/profiles.js index 3f2af7b..8a58dd6 100644 --- a/server/profiles.js +++ b/server/profiles.js @@ -250,8 +250,8 @@ const trainRequest = (connection) => (req, res) => { } //update the profile with new values - let query = 'UPDATE profiles SET gold = gold - ?, recruits = recruits - 1, soldiers = soldiers + ?, spies = spies + ?, scientists = scientists + ? WHERE accountId = ?;'; - connection.query(query, [cost, fields.role === 'soldier' ? 1 : 0, fields.role === 'spy' ? 1 : 0, fields.role === 'scientist' ? 1 : 0, fields.id], (err) => { + let query = 'UPDATE profiles SET gold = gold - ?, recruits = recruits - 1, soldiers = soldiers + ?, spies = spies + ?, scientists = scientists + ? WHERE accountId = ? AND gold >= ? AND recruits > 0;'; + connection.query(query, [cost, fields.role === 'soldier' ? 1 : 0, fields.role === 'spy' ? 1 : 0, fields.role === 'scientist' ? 1 : 0, fields.id, cost], (err) => { if (err) throw err; //send the new profile data as JSON (NOTE: possible duplication) @@ -276,7 +276,7 @@ const trainRequest = (connection) => (req, res) => { scientists: results[0].scientists }); res.end(); - log('Train successful', fields.username, fields.role, fields.id, fields.token); + log('Train executed', fields.username, fields.role, fields.id, fields.token); }); }); }); @@ -344,8 +344,22 @@ const untrainRequest = (connection) => (req, res) => { return; } + //hacky + let role = null; + if (fields.role === 'soldier') { + role = 'soldiers'; + } else if (fields.role === 'spy') { + role = 'spies'; + } else if (fields.role === 'scientist') { + role = 'scientists'; + } else { + res.status(400).write(log('Unknown role found', fields.role)); + res.end(); + return; + } + //update the profile with new values - let query = 'UPDATE profiles SET recruits = recruits + 1, soldiers = soldiers - ?, spies = spies - ?, scientists = scientists - ? WHERE accountId = ?;'; + let query = `UPDATE profiles SET recruits = recruits + 1, soldiers = soldiers - ?, spies = spies - ?, scientists = scientists - ? WHERE accountId = ? AND ${role} > 0;`; connection.query(query, [fields.role === 'soldier' ? 1 : 0, fields.role === 'spy' ? 1 : 0, fields.role === 'scientist' ? 1 : 0, fields.id], (err) => { if (err) throw err; @@ -371,7 +385,7 @@ const untrainRequest = (connection) => (req, res) => { scientists: results[0].scientists }); res.end(); - log('Untrain successful', fields.username, fields.role, fields.id, fields.token); + log('Untrain executed', fields.username, fields.role, fields.id, fields.token); }); }); }); diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index d6aa9b5..ae0b929 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -63,7 +63,7 @@ CREATE TABLE IF NOT EXISTS pendingCombat ( eventTime TIMESTAMP, - attackerId INTEGER UNSIGNED, + attackerId INTEGER UNSIGNED UNIQUE, defenderId INTEGER UNSIGNED, attackingUnits INTEGER UNSIGNED,