Began work on spying code
This commit is contained in:
+20
-9
@@ -77,18 +77,29 @@ const attackRequest = (connection) => (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
const attackStatusRequest = (connection) => (req, res) => { //TODO: proper credentials
|
||||
isAttacking(connection, req.body.attacker, (err, attacking, defender) => {
|
||||
const attackStatusRequest = (connection) => (req, res) => {
|
||||
//verify the credentials
|
||||
let query = 'SELECT COUNT(*) AS total FROM sessions WHERE accountId = ? AND token = ?;';
|
||||
connection.query(query, [req.body.id, req.body.token], (err, results) => {
|
||||
if (err) throw err;
|
||||
|
||||
res.status(200).json({
|
||||
status: attacking ? 'attacking' : 'idle',
|
||||
attacker: req.body.attacker,
|
||||
defender: defender,
|
||||
msg: null
|
||||
});
|
||||
if (results[0].total !== 1) {
|
||||
res.status(400).write(log('Invalid attack status request credentials', req.body.id, req.body.token));
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
res.end();
|
||||
isAttacking(connection, req.body.id, (err, attacking, defender) => {
|
||||
if (err) throw err;
|
||||
|
||||
res.status(200).json({
|
||||
status: attacking ? 'attacking' : 'idle',
|
||||
defender: defender,
|
||||
msg: null
|
||||
});
|
||||
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -51,6 +51,12 @@ app.post('/attackstatusrequest', combat.attackStatusRequest(connection));
|
||||
app.post('/combatlogrequest', combat.combatLogRequest(connection));
|
||||
combat.runCombatTick(connection);
|
||||
|
||||
let spying = require('./spying.js');
|
||||
app.post('/spyrequest', spying.spyRequest(connection));
|
||||
app.post('/spystatusrequest', spying.spyStatusRequest(connection));
|
||||
app.post('/spylogrequest', spying.spyLogRequest(connection));
|
||||
spying.runSpyTick(connection);
|
||||
|
||||
let equipment = require('./equipment.js');
|
||||
app.post('/equipmentrequest', equipment.equipmentRequest(connection));
|
||||
app.post('/equipmentpurchaserequest', equipment.purchaseRequest(connection));
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
//environment variables
|
||||
require('dotenv').config();
|
||||
|
||||
//libraries
|
||||
let CronJob = require('cron').CronJob;
|
||||
|
||||
//utilities
|
||||
let { logDiagnostics } = require('./diagnostics.js');
|
||||
let { log } = require('../common/utilities.js');
|
||||
|
||||
let { isSpying } = require('./utilities.js');
|
||||
|
||||
const spyRequest = (connection) => (req, res) => {
|
||||
//TODO
|
||||
res.status(400).write(log('Not yet implemented', 'spyRequest'));
|
||||
res.end();
|
||||
};
|
||||
|
||||
const spyStatusRequest = (connection) => (req, res) => {
|
||||
//verify the credentials
|
||||
let query = 'SELECT COUNT(*) AS total FROM sessions WHERE accountId = ? AND token = ?;';
|
||||
connection.query(query, [req.body.id, req.body.token], (err, results) => {
|
||||
if (err) throw err;
|
||||
|
||||
if (results[0].total !== 1) {
|
||||
res.status(400).write(log('Invalid spy status request credentials', req.body.id, req.body.token));
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
isSpying(connection, req.body.id, (err, spying, defender) => {
|
||||
if (err) throw err;
|
||||
|
||||
res.status(200).json({
|
||||
status: spying ? 'spying' : 'idle',
|
||||
attacker: req.body.attacker,
|
||||
defender: defender,
|
||||
msg: null
|
||||
});
|
||||
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const spyLogRequest = (connection) => (req, res) => {
|
||||
//TODO
|
||||
res.status(400).write(log('Not yet implemented', 'spyLogRequest'));
|
||||
res.end();
|
||||
};
|
||||
|
||||
const runSpyTick = (connection) => {
|
||||
//TODO
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
spyRequest: spyRequest,
|
||||
spyStatusRequest: spyStatusRequest,
|
||||
spyLogRequest: spyLogRequest,
|
||||
runSpyTick: runSpyTick
|
||||
};
|
||||
|
||||
+30
-2
@@ -40,7 +40,7 @@ const isAttacking = (connection, user, cb) => {
|
||||
} 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: ${typeof(user)}`);
|
||||
return cb(`isAttacking: Unknown argument type for user: ${typeof(user)}`);
|
||||
}
|
||||
|
||||
connection.query(query, [user], (err, results) => {
|
||||
@@ -59,8 +59,36 @@ const isAttacking = (connection, user, cb) => {
|
||||
});
|
||||
};
|
||||
|
||||
const isSpying = (connection, user, cb) => {
|
||||
let query;
|
||||
|
||||
if (isNormalInteger(user)) {
|
||||
query = 'SELECT * FROM pendingSpying WHERE attackerId = ?;';
|
||||
} else if (typeof(user) === 'string') {
|
||||
query = 'SELECT * FROM pendingSpying WHERE attackerId IN (SELECT id FROM accounts WHERE username = ?);';
|
||||
} else {
|
||||
return cb(`isSpying: Unknown argument type for user: ${typeof(user)}`);
|
||||
}
|
||||
|
||||
connection.query(query, [user], (err, results) => {
|
||||
if (err) throw err;
|
||||
|
||||
if (results.length === 0) {
|
||||
return cb(undefined, false);
|
||||
} else {
|
||||
//get the username of the person being spied on
|
||||
let query = 'SELECT username FROM accounts WHERE id = ?;';
|
||||
connection.query(query, [results[0].defenderId], (err, results) => {
|
||||
if (err) throw err;
|
||||
return cb(undefined, true, results[0].username);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getStatistics: getStatistics,
|
||||
getOwned: getOwned,
|
||||
isAttacking: isAttacking
|
||||
isAttacking: isAttacking,
|
||||
isSpying: isSpying
|
||||
};
|
||||
Reference in New Issue
Block a user