Spying is done, just need spy logging

This commit is contained in:
2019-06-05 12:58:46 +10:00
parent a656a4de5a
commit e1c5d20289
3 changed files with 126 additions and 60 deletions
+1 -1
View File
@@ -197,7 +197,7 @@ const trainRequest = (connection) => (req, res) => {
break; break;
case 'spy': case 'spy':
cost = 200; cost = 300;
break; break;
case 'scientist': case 'scientist':
+124 -57
View File
@@ -155,10 +155,10 @@ const spyGameplayLogic = (connection, pendingSpying) => {
let totalEyes = results[0].recruits + results[0].soldiers * !defenderIsAttacking + results[0].spies * !defenderIsSpying + results[0].scientists; let totalEyes = results[0].recruits + results[0].soldiers * !defenderIsAttacking + results[0].spies * !defenderIsSpying + results[0].scientists;
//more spies reduces the chances of being seen? Counter intuitive //more spies reduces the chances of being seen? Counter intuitive
let chanceSeen = totalEyes / pendingSpying.attackingUnits; let chanceSeen = totalEyes / (pendingSpying.attackingUnits * 10); //it takes 10 eyes to guarantee the capture of 1 spy, 50% chance to capture 2 spies, etc.
//if seen //if seen
if (Math.random() * 100 <= chanceSeen) { //TODO: balance this if (Math.random() <= chanceSeen) {
let query = 'INSERT INTO pastSpying (eventTime, attackerId, defenderId, attackingUnits, success, spoilsGold) VALUES (?, ?, ?, ?, "failure", 0);'; let query = 'INSERT INTO pastSpying (eventTime, attackerId, defenderId, attackingUnits, success, spoilsGold) VALUES (?, ?, ?, ?, "failure", 0);';
connection.query(query, [pendingSpying.eventTime, pendingSpying.attackerId, pendingSpying.defenderId, pendingSpying.attackingUnits], (err) => { connection.query(query, [pendingSpying.eventTime, pendingSpying.attackerId, pendingSpying.defenderId, pendingSpying.attackingUnits], (err) => {
if (err) throw err; if (err) throw err;
@@ -199,7 +199,7 @@ const spyGameplayLogic = (connection, pendingSpying) => {
log('Spy succeeded', pendingSpying.attackerId, pendingSpying.defenderId, pendingSpying.attackingUnits, totalEyes, spoilsGold); log('Spy succeeded', pendingSpying.attackerId, pendingSpying.defenderId, pendingSpying.attackingUnits, totalEyes, spoilsGold);
// spyStealEquipment(connection, pendingSpying, spoilsGold); spyStealEquipment(connection, pendingSpying, spoilsGold);
}); });
}); });
}); });
@@ -209,7 +209,7 @@ const spyGameplayLogic = (connection, pendingSpying) => {
});; });;
}); });
}; };
/*
const spyStealEquipment = (connection, pendingSpying, spoilsGold) => { const spyStealEquipment = (connection, pendingSpying, spoilsGold) => {
let query = 'SELECT id FROM pastSpying WHERE eventTime = ? AND attackerId = ? AND defenderId = ? AND spoilsGold = ?;'; //make it VERY hard to grab the wrong one let query = 'SELECT id FROM pastSpying WHERE eventTime = ? AND attackerId = ? AND defenderId = ? AND spoilsGold = ?;'; //make it VERY hard to grab the wrong one
connection.query(query, [pendingSpying.eventTime, pendingSpying.attackerId, pendingSpying.defenderId, spoilsGold], (err, results) => { connection.query(query, [pendingSpying.eventTime, pendingSpying.attackerId, pendingSpying.defenderId, spoilsGold], (err, results) => {
@@ -219,7 +219,7 @@ const spyStealEquipment = (connection, pendingSpying, spoilsGold) => {
for (let i = 0; i < pendingSpying.attackingUnits; i++) { for (let i = 0; i < pendingSpying.attackingUnits; i++) {
//50% chance of stealing equipment //50% chance of stealing equipment
if (Math.random() >= 0.5 || true) { //DEBUG if (Math.random() >= 0.5) {
successfulSpies += 1; successfulSpies += 1;
} }
} }
@@ -237,7 +237,7 @@ const spyStealEquipmentInner = (connection, attackerId, defenderId, attackingUni
//if he's not attacking, skip to the next step //if he's not attacking, skip to the next step
if (!attacking) { if (!attacking) {
return spyStealEquipmentInnerInner(connection, attackerId, defenderId, attackingUnits, results, pastSpyingId); return spyStealEquipmentSelectItemsToSteal(connection, attackerId, defenderId, attackingUnits, results, pastSpyingId);
} }
//count the number of weapons/consumable items to be skipped, from strongest to weakest //count the number of weapons/consumable items to be skipped, from strongest to weakest
@@ -252,7 +252,7 @@ const spyStealEquipmentInner = (connection, attackerId, defenderId, attackingUni
connection.query(query, [defenderId], (err, armourResults) => { connection.query(query, [defenderId], (err, armourResults) => {
if (err) throw err; if (err) throw err;
//NOTE: Armour stays at home - it's never carried by soldiers //NOTE: Armour stays at home - it's never carried by soldiers (don't call removeForEachSoldier)
//weapons //weapons
let query = 'SELECT * FROM equipment WHERE accountId = ? AND type = "Weapon";'; let query = 'SELECT * FROM equipment WHERE accountId = ? AND type = "Weapon";';
@@ -273,7 +273,7 @@ const spyStealEquipmentInner = (connection, attackerId, defenderId, attackingUni
//splice the two arrays back together //splice the two arrays back together
let results = weaponResults.concat(consumableResults, armourResults); let results = weaponResults.concat(consumableResults, armourResults);
spyStealEquipmentInnerInner(connection, attackerId, defenderId, attackingUnits, results, pastSpyingId); spyStealEquipmentSelectItemsToSteal(connection, attackerId, defenderId, attackingUnits, results, pastSpyingId);
}); });
}); });
}); });
@@ -308,7 +308,7 @@ const removeForEachSoldier = (results, soldiers, cb) => {
}); });
} }
const spyStealEquipmentInnerInner = (connection, attackerId, defenderId, attackingUnits, results, pastSpyingId) => { const spyStealEquipmentSelectItemsToSteal = (connection, attackerId, defenderId, attackingUnits, results, pastSpyingId) => {
//count the total items //count the total items
let totalItems = 0; let totalItems = 0;
results.forEach((item) => totalItems += item.quantity); results.forEach((item) => totalItems += item.quantity);
@@ -318,72 +318,139 @@ const spyStealEquipmentInnerInner = (connection, attackerId, defenderId, attacki
for (let i = 0; i < attackingUnits; i++) { for (let i = 0; i < attackingUnits; i++) {
//select the specific item to steal //select the specific item to steal
let selection = Math.floor(Math.random() * totalItems); let selection = Math.floor(Math.random() * totalItems);
totalItems -= 1;
//find the exact item that will be stolen //find the exact item that will be stolen (records[0])
items.push(results.filter((item) => { let records = results.filter((item) => {
selection -= item.quantity; selection -= item.quantity;
if (selection < 0) { return selection < 0;
return item; });
}
})[0]);
log(results.indexOf(items[items.length-1]));
results[results.indexOf(items[items.length-1])].quantity -= 1;
if (results[results.indexOf(items[items.length-1])].quantity === 0) { //move to items (quantity = 1)
// log(items[items.length-1].name); if (records.length > 0) {
// log(items[items.length-1].quantity); items.unshift({
log(results[results.indexOf(items[items.length-1])].name); id: records[0].id,
log(results[results.indexOf(items[items.length-1])].quantity); name: records[0].name,
// results[results.indexOf(items[items.length-1])].splice(1); type: records[0].type,
quantity: 1
});
}
//remove it from results (decrement and/or delete)
for (let i = 0; i < results.length; i++) {
if (results[i].id === items[0].id) {
results[i].quantity -= 1;
if (results[i].quantity <= 0) {
results.splice(i, 1);
}
break;
}
}
//skip the rest
if (results.length <= 0) {
break;
} }
} }
return; //collapse the {quantity:1} into {quantity:n}
//NOTE: this is glacially slow let collapsedItems = [];
//insert a new record - will clean up duplicates later items.forEach((item) => {
let query = 'INSERT INTO equipment (accountId, name, type, quantity) VALUES (?, ?, ?, 1);'; if (!collapsedItems[item.id]) {
connection.query(query, [attackerId, item.name, item.type], (err) => { collapsedItems[item.id] = { ...item };
if (err) throw err; } else {
collapsedItems[item.id].quantity += item.quantity;
spyStealEquipmentInnerInnerInner(connection, attackerId, defenderId, item, pastSpyingId); }
}); });
items = []; //clear
collapsedItems.forEach((record) => {
items.push(record);
});
//next steps
spyStealEquipmentIncrementItemsToInventory(connection, attackerId, items);
spyStealEquipmentDecrementItemsFromInventory(connection, defenderId, items);
recordEquipmentStolen(connection, items, pastSpyingId);
}; };
const spyStealEquipmentInnerInnerInner = (connection, attackerId, defenderId, item, pastSpyingId) => { const spyStealEquipmentIncrementItemsToInventory = (connection, accountId, items) => {
//decrement or remove the item //add the items to the players's inventory
if (item.quantity > 1) { items.forEach((item) => {
let query = 'UPDATE equipment SET quantity = quantity - 1 WHERE id = ? AND quantity > 0;'; let query = 'SELECT * FROM equipment WHERE accountId = ? AND name = ? AND type = ?;';
connection.query(query, [item.id], (err) => { connection.query(query, [accountId, item.name, item.type], (err, results) => {
if (err) throw err; if (err) throw err;
//move on to the next step let query;
// spyStealEquipmentInnerInnerInnerInner(connection, attackerId, defenderId, item, pastSpyingId);
log('MARK 1'); //if the player has this item, or not
if (results.length > 0) {
query = 'UPDATE equipment SET quantity = quantity + ? WHERE accountId = ? AND name = ? AND type = ?;';
} else {
query = 'INSERT INTO equipment (quantity, accountId, name, type) VALUES (?, ?, ?, ?);';
}
connection.query(query, [item.quantity, accountId, item.name, item.type], (err) => {
if (err) throw err;
});
}); });
} else { });
let query = 'DELETE FROM equipment WHERE id = ?;';
connection.query(query, [item.id], (err) => { //error checking
items.forEach((item) => {
let query = 'SELECT * FROM equipment WHERE accountId = ? AND name = ? AND type = ?;';
connection.query(query, [accountId, item.name, item.type], (err, results) => {
if (err) throw err; if (err) throw err;
// if (results.length > 1) {
// spyStealEquipmentInnerInnerInnerInner(connection, attackerId, defenderId, item, pastSpyingId); log('WARNING: Duplicate items detected', JSON.stringify(results));
log('MARK 2'); }
}) })
}
};
const spyStealEquipmentInnerInnerInnerInner = (connection, attackerId, defenderId, item, pastSpyingId) => {
//insert into items stolen
let query = 'INSERT INTO equipmentStolen (pastSpyingId, name, type, quantity) VALUES (?, ?, ?, 1);';
connection.query(query, [pastSpyingId, item.name, item.type], (err) => {
if (err) throw err;
//Holy nesting, batman!
log('equipment stolen', attackerId, defenderId, item.id, item.name, item.type, pastSpyingId);
}); });
}; };
*/
const spyStealEquipmentDecrementItemsFromInventory = (connection, accountId, items) => {
//remove these items from the player's inventory
items.forEach((item) => {
let query = 'UPDATE equipment SET quantity = quantity - ? WHERE accountId = ? AND id = ?;';
connection.query(query, [item.quantity, accountId, item.id], (err) => {
if (err) throw err;
});
});
//check to see if any quantities are negative
let query = 'SELECT * FROM equipment WHERE quantity < 0;';
connection.query(query, (err, results) => {
if (err) throw err;
if (results.length !== 0) {
log('WARNING: equipment quantity below zero', JSON.stringify(results));
}
});
//clean the database from quantities of 0
query = 'DELETE FROM equipment WHERE accountId = ? AND quantity = 0;';
connection.query(query, [accountId], (err) => {
if (err) throw err;
log('Cleaned database', 'equipment decrement');
});
};
const recordEquipmentStolen = (connection, items, pastSpyingId) => {
//record in the database
let query = 'INSERT INTO equipmentStolen (pastSpyingId, name, type, quantity) VALUES (?, ?, ?, ?);';
items.forEach((item) => {
connection.query(query, [pastSpyingId, item.name, item.type, item.quantity], (err) => {
if (err) throw err;
log('Items stolen', pastSpyingId, JSON.stringify(item));
});
});
};
module.exports = { module.exports = {
spyRequest: spyRequest, spyRequest: spyRequest,
spyStatusRequest: spyStatusRequest, spyStatusRequest: spyStatusRequest,
+1 -2
View File
@@ -176,7 +176,7 @@ class Profile extends React.Component {
<p className='col'>Spies:</p> <p className='col'>Spies:</p>
<p className='col'>{this.props.profile.spies}</p> <p className='col'>{this.props.profile.spies}</p>
<button className='col' onClick={ () => this.sendRequest('/trainrequest', {role: 'spy'}) }>Train Spy (200 gold)</button> <button className='col' onClick={ () => this.sendRequest('/trainrequest', {role: 'spy'}) }>Train Spy (300 gold)</button>
<button className='col' onClick={ () => this.sendRequest('/untrainrequest', {role: 'spy'}) }>Untrain Spy</button> <button className='col' onClick={ () => this.sendRequest('/untrainrequest', {role: 'spy'}) }>Untrain Spy</button>
</div> </div>
</div> </div>
@@ -256,7 +256,6 @@ class Profile extends React.Component {
pendingStatus={'spying'} pendingStatus={'spying'}
pendingMsg={'Your spies are spying on'} pendingMsg={'Your spies are spying on'}
parseUnits={(json) => json.spies} parseUnits={(json) => json.spies}
disabled={true}
>Send Spies</AttackButton> >Send Spies</AttackButton>
</div> </div>