From 148129344094de432f9ebbb56abc3dfd859fa8c2 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 12 Jun 2018 12:59:21 -0400 Subject: [PATCH 01/33] Adds a schema validator for the new battles collection. --- db/migrations/migration-00003.js | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 db/migrations/migration-00003.js diff --git a/db/migrations/migration-00003.js b/db/migrations/migration-00003.js new file mode 100644 index 0000000..cce7e80 --- /dev/null +++ b/db/migrations/migration-00003.js @@ -0,0 +1,45 @@ +db = new Mongo().getDB('psg'); +db.createCollection('battles', { + validationLevel: "strict", + validator: { + $jsonSchema: { + bsonType: "object", + required: ["active","currentTurn","turnTimer","combatants"], + additionalProperties: false, + properties: { + _id: { + bsonType: "objectId" + }, + //Going to skimp out on the descriptions for now, they have no purpose. + active: { + bsonType: "bool" + }, + currentTurn: { + bsonType: "string" + }, + turnTimer: { + bsonType: "int" + }, + combatants: { + bsonType: "array", + items: { + bsonType: "object", + required: ["mobId", "turnCount", "team"], + additionalProperties: false, + properties: { + mobId: { + bsonType: "string" + }, + turnCount: { + bsonType: "int" + }, + team: { + bsonType: "int" + } + } + } + } + } + } + } +}); From e966066672858840ec1e7a4450fed89fc4682042 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 12 Jun 2018 22:21:01 -0400 Subject: [PATCH 02/33] Adds a rudimentary battle model to the fray. --- models/battle.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 models/battle.js diff --git a/models/battle.js b/models/battle.js new file mode 100644 index 0000000..3b55481 --- /dev/null +++ b/models/battle.js @@ -0,0 +1,58 @@ +//Import the database +const db = require('../db/config'); + +//Tmport modelHelper object +const modelHelper = require('./_model-helper'); + +//Create the battle object +const Battle = {}; + +//XXX Do we need this?? +//finding all battles +Battle.findAll = function(){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .find({}) + .toArray() + .then(response => modelHelper.serverLog('Battle.findAll', response)) + .then(response => { + connection.close(); + return response; + }) + }) +} + +//finding a single battle +Battle.findById = function(id){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .findOne({"_id": db.objectId.createFromHexString(id)}) + .then(response => modelHelper.serverLog('Battle.findById', response)) + .then(response => { + connection.close(); + return response; + }) + }) +} + +//creating a single battle +Battle.create = function(battle){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .insertOne(battle) + .then(response => modelHelper.serverLog('Battle.create', response.ops[0])) + .then(response => { + connection.close(); + return response; + }) + }) +} + +//Export it +module.exports = Battle; From c228a1bb034899980ffb5f69bd8d60cb2e41d687 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 12 Jun 2018 23:01:49 -0400 Subject: [PATCH 03/33] Adds the battle-controller to the fray --- controllers/battle-controller.js | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 controllers/battle-controller.js diff --git a/controllers/battle-controller.js b/controllers/battle-controller.js new file mode 100644 index 0000000..fb7d9a3 --- /dev/null +++ b/controllers/battle-controller.js @@ -0,0 +1,58 @@ +//Import the battle model +const Battle = require('../models/battle'); +//And the Mob model too. +const Mob = require('../models/mob'); +//Might need the log model too +const Log = require('../models/log'); + +//Create the battlesController object +const battlesController = {}; + +//TODO: Maybe we can have a controller helper in the same way we have a model helper to avoid CRUD function regurgitation?? +//Short information about all battles +battlesController.index = function(req, res){ + Battle.findAll() + .then(battles => { + res.status(200) + .json({ + message: "Battle index retrieved successfully!", + battles + }) + }).catch(err => { + console.log(err); + res.status(500).json({error: err}); + }) +} + +//in depth information about a single battle +battlesController.show = function(req, res){ + Battle.findById(req.params.id) + .then(battle => { + res.status(200) + .json({ + message: "Battle retrieved successfully!", + battle + }) + }).catch(err => { + console.log(err); + res.status(500).json({error: err}); + }) +} + +//Creating a new battle +battlesController.create = function(req, res){ + Battle.create(req.body) + .then(battle => { + res.status(201) + .json({ + message: "Battle created successfully!", + battle + }) + }).catch(err => { + console.log(err); + res.status(500).json({error: err}); + }) +} + +//Export the file +module.exports = battlesController From bc86f51e6d9fdbc4bd45d7e0720ff6c45963227b Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 12 Jun 2018 23:14:11 -0400 Subject: [PATCH 04/33] Adds the battle router to the fray. --- routes/battle-routes.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 routes/battle-routes.js diff --git a/routes/battle-routes.js b/routes/battle-routes.js new file mode 100644 index 0000000..9104c3a --- /dev/null +++ b/routes/battle-routes.js @@ -0,0 +1,17 @@ +//Import the controller +const battlesController = require('../controllers/battle-controller'); + +//Create the router instance +const battleRoutes = require('express').Router(); + +//Index +battleRoutes.get('/', battlesController.index); + +//Create single +battleRoutes.post('/', battlesController.create); + +//Get single +battleRoutes.get('/:id', battlesController.show); + +//Export +module.exports = battleRoutes From dd7bbfd6ec4de08fd299ac4b50e3ad479d710f40 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 12 Jun 2018 23:14:11 -0400 Subject: [PATCH 05/33] Plugs the battle router into the server for some basic CR-- functionalitygit status! --- server.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server.js b/server.js index ea2d326..7dce207 100644 --- a/server.js +++ b/server.js @@ -33,6 +33,9 @@ server.use('/action', require('./routes/action-routes')); //"/log" server.use('/log', require('./routes/log-routes')); +//"/battle" +server.use('/battle', require('./routes/battle-routes')); + //Root server.use('/', (req, res) => { res.status(200).json({ From 9db3c0482b307838d1bbcf4e67086bb1ec8aeeb5 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 13 Jun 2018 09:54:04 -0400 Subject: [PATCH 06/33] Allows the battle model to insert a combatant into it's combatants array. --- models/battle.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/models/battle.js b/models/battle.js index 3b55481..5f7a34d 100644 --- a/models/battle.js +++ b/models/battle.js @@ -54,5 +54,23 @@ Battle.create = function(battle){ }) } +//Adding combatants +Battle.insertIntoCombatants = function(id, combatant){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .findOneAndUpdate( + {"_id": db.objectId.createFromHexString(id)}, + {$push: {combatants: combatant}}, + {returnOriginal: false} + ).then(response => modelHelper.serverLog('Battle.insertIntoCombatants', response.value)) + .then(response => { + connection.close(); + return response; + }) + }) +} + //Export it module.exports = Battle; From e747917856c87cbbe2140e29c21466f1987c0645 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 13 Jun 2018 09:54:04 -0400 Subject: [PATCH 07/33] Allows the battle controller to tell it's model to add a combatant. --- controllers/battle-controller.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/controllers/battle-controller.js b/controllers/battle-controller.js index fb7d9a3..4fd7557 100644 --- a/controllers/battle-controller.js +++ b/controllers/battle-controller.js @@ -54,5 +54,20 @@ battlesController.create = function(req, res){ }) } +//Adding combatants to an existing battle +battlesController.addToBattle = function(req, res){ + Battle.insertIntoCombatants(req.params.id, req.body) + .then(battle => { + res.status(200) + .json({ + message: "Combatant added to battle successfully!", + battle + }) + }).catch(err => { + console.log(err); + res.status(500).json({error: err}); + }) +} + //Export the file module.exports = battlesController From 573aeafee3bbb8ddccde697e26679519a5ad0a40 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 13 Jun 2018 09:54:04 -0400 Subject: [PATCH 08/33] Adds a route for adding combatants to a battle. --- routes/battle-routes.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/routes/battle-routes.js b/routes/battle-routes.js index 9104c3a..5a9320d 100644 --- a/routes/battle-routes.js +++ b/routes/battle-routes.js @@ -13,5 +13,8 @@ battleRoutes.post('/', battlesController.create); //Get single battleRoutes.get('/:id', battlesController.show); +//Add combatant +battleRoutes.post('/:id', battlesController.addToBattle); + //Export module.exports = battleRoutes From 0a3f092a11d460e8a4351124393e5188f3a82104 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 13 Jun 2018 11:46:16 -0400 Subject: [PATCH 09/33] adds a battle model function for seeing if two combatants are in the same battle. --- models/battle.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/models/battle.js b/models/battle.js index 5f7a34d..099626f 100644 --- a/models/battle.js +++ b/models/battle.js @@ -72,5 +72,29 @@ Battle.insertIntoCombatants = function(id, combatant){ }) } +//Confirming the presence of two combatants in the same battle +Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .find({ + $and: [{ + "combatants.mobId": combatantA + }, + { + "combatants.mobId": combatantB + } + ] + }) + .toArray() + .then(response => modelHelper.serverLog('Battle.confirmCompatibleCombatants', response)) + .then(response => { + connection.close(); + return response; + }) + }) +} + //Export it module.exports = Battle; From c2450101fd574130d1e83f63b5829ae1a968572e Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 13 Jun 2018 13:16:56 -0400 Subject: [PATCH 10/33] The battle controller will now reject attacks from entities in different battles. --- controllers/actions-controller.js | 72 ++++++++++++++++++------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 88b8ee3..7bd19b7 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -4,43 +4,55 @@ const Mob = require('../models/mob'); const mobsController = require('./mobs-controller'); ////And the log model const Log = require('../models/log'); +////And the battle model +const Battle = require('../models/battle'); + ////Create the actions controller object const actionsController = {}; ////Give it logic //attacking actionsController.attack = function(req, res){ - Mob.findById(req.params.attacker) - .then(attacker => { - return Mob.takeDamage(req.params.defender, attacker.attribute.strength) - .then(defender => { - let content = ""; - if (defender.attribute.living){ - content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage! // ${attacker._id} =${attacker.attribute.strength}=> ${defender._id} //`; - } - else if (defender.attackFailed){ - console.log('ERROR, CATCH!!!'); - let error = {}; - error.message = "Attack rejected: target is already downed and can't recieve anymore damage." - throw error; - } - else { - content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage, knocking them to the ground! // ${attacker._id} =x!${attacker.attribute.strength}!x=> ${defender._id} //`; - } - let newLog = { - content, - timestamp: Date.now(), - type: "action", - room: null + Battle.confirmCompatibleCombatants(req.params.attacker, req.params.defender) + .then(response => { + if(response.length === 0){ + let error = { + message: "Attack rejected: You and your target are not in a battle or in different battles." } - return Log.create(newLog) - .then(logResponse => { - res.status(200) - .json({ - message: "Attack completed successfully!", - log: logResponse, - attacker, - defender, + throw error; + } + Mob.findById(req.params.attacker) + .then(attacker => { + return Mob.takeDamage(req.params.defender, attacker.attribute.strength) + .then(defender => { + let content = ""; + if (defender.attribute.living){ + content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage! // ${attacker._id} =${attacker.attribute.strength}=> ${defender._id} //`; + } + else if (defender.attackFailed){ + console.log('ERROR, CATCH!!!'); + let error = {}; + error.message = "Attack rejected: target is already downed and can't recieve anymore damage." + throw error; + } + else { + content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage, knocking them to the ground! // ${attacker._id} =x!${attacker.attribute.strength}!x=> ${defender._id} //`; + } + let newLog = { + content, + timestamp: Date.now(), + type: "action", + room: null + } + return Log.create(newLog) + .then(logResponse => { + res.status(200) + .json({ + message: "Attack completed successfully!", + log: logResponse, + attacker, + defender, + }) }) }) }) From 5dccb063addae1c7fc82603ebb2f28fff433ab26 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 25 Jun 2018 16:49:35 -0400 Subject: [PATCH 11/33] Converts the existing Battle.confirm method into an aggregate function with a scaffolded filter in place. --- models/battle.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/models/battle.js b/models/battle.js index 099626f..2195147 100644 --- a/models/battle.js +++ b/models/battle.js @@ -78,14 +78,37 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ .then(connection => { let selectedDb = connection.db(db.name); return selectedDb.collection('battles') - .find({ - $and: [{ + //.find({ + // $and: [{ + // "combatants.mobId": combatantA + // }, + // { + // "combatants.mobId": combatantB + // } + // ] + //}) + //.toArray() + .aggregate({ + $match: { $and: [{ "combatants.mobId": combatantA }, { "combatants.mobId": combatantB } - ] + ]}, + $addFields : { "combatants": { $filter: { + input: "$combatants", + as: "combatant", + cond: { $or: [{ + "$$combatant.mobId": combatantA + }, + { + "$$combatant.mobId": combatantB + } + ]} + } + } + } }) .toArray() .then(response => modelHelper.serverLog('Battle.confirmCompatibleCombatants', response)) From cfb56a2714c9ab65b4b17021ad095cf89bfb4317 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 25 Jun 2018 16:57:56 -0400 Subject: [PATCH 12/33] Corrects the abyssmal syntax in the aggregation pipeline in the confirm model method (Battle) --- models/battle.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/models/battle.js b/models/battle.js index 2195147..fb62fb3 100644 --- a/models/battle.js +++ b/models/battle.js @@ -95,8 +95,10 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ { "combatants.mobId": combatantB } - ]}, - $addFields : { "combatants": { $filter: { + ]}}, + + { + $addFields : { "peepee": { $filter: { input: "$combatants", as: "combatant", cond: { $or: [{ @@ -106,9 +108,9 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ "$$combatant.mobId": combatantB } ]} + } - } - } + }} }) .toArray() .then(response => modelHelper.serverLog('Battle.confirmCompatibleCombatants', response)) From 10668332f459b4e2960ed8116868e50677b9a6d2 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Sat, 21 Jul 2018 12:47:23 -0400 Subject: [PATCH 13/33] confirms finding battles containing specified combatants works. --- models/battle.js | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/models/battle.js b/models/battle.js index fb62fb3..9b89180 100644 --- a/models/battle.js +++ b/models/battle.js @@ -88,30 +88,13 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ // ] //}) //.toArray() - .aggregate({ - $match: { $and: [{ - "combatants.mobId": combatantA - }, - { - "combatants.mobId": combatantB - } - ]}}, - - { - $addFields : { "peepee": { $filter: { - input: "$combatants", - as: "combatant", - cond: { $or: [{ - "$$combatant.mobId": combatantA - }, - { - "$$combatant.mobId": combatantB - } - ]} - - } - }} - }) + .aggregate([ + //TODO: On confirmation, return involved mob's data to be modified. + { $match: { $and: [ + { "combatants.mobId": combatantA }, + { "combatants.mobId": combatantB } + ]}} + ]) .toArray() .then(response => modelHelper.serverLog('Battle.confirmCompatibleCombatants', response)) .then(response => { From fc716df7a1d6a2a714209c36a86898e302b68b6e Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Sat, 21 Jul 2018 13:26:13 -0400 Subject: [PATCH 14/33] Scaffolds out a lookup stage in the aggregation pipeline along with a note as to why it wouldn't work. --- models/battle.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/models/battle.js b/models/battle.js index 9b89180..edd123c 100644 --- a/models/battle.js +++ b/models/battle.js @@ -90,10 +90,20 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ //.toArray() .aggregate([ //TODO: On confirmation, return involved mob's data to be modified. + //XXX: Due to poor code practices we have no reliable way to grab data from another collection + // by the object id. To solve this, I'd need to change how the object ids are stored in the + // database itself which would require a lot of refactoring. I'll do that later. { $match: { $and: [ { "combatants.mobId": combatantA }, { "combatants.mobId": combatantB } - ]}} + ]}}//, + // { $unwind: "$combatants" }, + // { $lookup: { + // from: "mobs", + // localField: "combatants.mobId", + // foreignField: "_id", + // as: "fetchedMob" + // }} ]) .toArray() .then(response => modelHelper.serverLog('Battle.confirmCompatibleCombatants', response)) From f8a7d2532a67a504b31b2926ec0b99ebdf805b11 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Thu, 2 Aug 2018 18:13:15 -0400 Subject: [PATCH 15/33] Adds some commentary to the attack controller. --- controllers/actions-controller.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 7bd19b7..39abc7f 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -13,6 +13,7 @@ const actionsController = {}; ////Give it logic //attacking actionsController.attack = function(req, res){ + //First make sure that both parties are in the same battle... Battle.confirmCompatibleCombatants(req.params.attacker, req.params.defender) .then(response => { if(response.length === 0){ @@ -21,6 +22,7 @@ actionsController.attack = function(req, res){ } throw error; } + //...Then we grab the attacker(?) and request that the defender takes damage according to specified calculations... Mob.findById(req.params.attacker) .then(attacker => { return Mob.takeDamage(req.params.defender, attacker.attribute.strength) @@ -29,6 +31,7 @@ actionsController.attack = function(req, res){ if (defender.attribute.living){ content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage! // ${attacker._id} =${attacker.attribute.strength}=> ${defender._id} //`; } + //...But if the defender is already dead then don't do anything and throw and error. else if (defender.attackFailed){ console.log('ERROR, CATCH!!!'); let error = {}; @@ -38,6 +41,7 @@ actionsController.attack = function(req, res){ else { content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage, knocking them to the ground! // ${attacker._id} =x!${attacker.attribute.strength}!x=> ${defender._id} //`; } + //Log the action for future reference... let newLog = { content, timestamp: Date.now(), @@ -57,6 +61,7 @@ actionsController.attack = function(req, res){ }) }) }) + //General catchall .catch(err => { console.log(err) res.status(500).json({error: err}) From 3e897802485f259e9128c7184edd6a7bb2d65ad6 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Fri, 3 Aug 2018 12:59:47 -0400 Subject: [PATCH 16/33] Adds some comments to the take damage method in the mob model --- models/mob.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/models/mob.js b/models/mob.js index 289aaca..f0d37d8 100644 --- a/models/mob.js +++ b/models/mob.js @@ -102,17 +102,23 @@ Mob.takeDamage = function(id, damage){ .then(target => { //Forgive me for the following 14 lines... //TODO: Refactor this shit + //Create an empty object that will tell the database how to update involved parties in the future... let updateInstructions = {}; + //...If the target has less health than damage about to be inflicted... if (target.attribute.health < damage){ + //...And the target is already dead, then do nothing and tell the controller the attack failed. if (!target.attribute.living){ updateInstructions.invalidate = true; damage = 0; } + //Otherwise set the target to 'dead'... updateInstructions['attribute.living'] = false; } + //If the target is alive and will survive the coming attack (or was dead and is being brought back to life, set the target to 'alive'... else { updateInstructions['attribute.living'] = true; } + //apply the damage... updateInstructions['attribute.health'] = target.attribute.health - damage; return selectedDb.collection('mobs') .findOneAndUpdate( @@ -121,6 +127,7 @@ Mob.takeDamage = function(id, damage){ { returnOriginal: false } ).then(response => modelHelper.serverLog('Mob.takeDamage', response.value)) .then(response => { + //...If the target didn't take damage then the attack failed if (target.attribute.health === response.attribute.health){ response.attackFailed = true; } From af547d6e79ffb78f28ac4f2bc314153d76df5074 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Fri, 3 Aug 2018 16:54:30 -0400 Subject: [PATCH 17/33] Begins a rewrite of the attack action in the actions-controller. --- controllers/actions-controller.js | 113 +++++++++++++++++------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 39abc7f..46ba3c8 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -11,62 +11,79 @@ const Battle = require('../models/battle'); const actionsController = {}; ////Give it logic +/* XXX Trying this shit again.*/ //attacking +//actionsController.attack = function(req, res){ +// //First make sure that both parties are in the same battle... +// Battle.confirmCompatibleCombatants(req.params.attacker, req.params.defender) +// .then(response => { +// if(response.length === 0){ +// let error = { +// message: "Attack rejected: You and your target are not in a battle or in different battles." +// } +// throw error; +// } +// //...Then we grab the attacker(?) and request that the defender takes damage according to specified calculations... +// Mob.findById(req.params.attacker) +// .then(attacker => { +// return Mob.takeDamage(req.params.defender, attacker.attribute.strength) +// .then(defender => { +// let content = ""; +// if (defender.attribute.living){ +// content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage! // ${attacker._id} =${attacker.attribute.strength}=> ${defender._id} //`; +// } +// //...But if the defender is already dead then don't do anything and throw and error. +// else if (defender.attackFailed){ +// console.log('ERROR, CATCH!!!'); +// let error = {}; +// error.message = "Attack rejected: target is already downed and can't recieve anymore damage." +// throw error; +// } +// else { +// content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage, knocking them to the ground! // ${attacker._id} =x!${attacker.attribute.strength}!x=> ${defender._id} //`; +// } +// //Log the action for future reference... +// let newLog = { +// content, +// timestamp: Date.now(), +// type: "action", +// room: null +// } +// return Log.create(newLog) +// .then(logResponse => { +// res.status(200) +// .json({ +// message: "Attack completed successfully!", +// log: logResponse, +// attacker, +// defender, +// }) +// }) +// }) +// }) +// }) +// //General catchall +// .catch(err => { +// console.log(err) +// res.status(500).json({error: err}) +// }) +//} + actionsController.attack = function(req, res){ - //First make sure that both parties are in the same battle... + //See if the combatants are in the same battle. Battle.confirmCompatibleCombatants(req.params.attacker, req.params.defender) .then(response => { - if(response.length === 0){ + if (response.length === 0){ let error = { - message: "Attack rejected: You and your target are not in a battle or in different battles." + message: "characters are not in the same battle" } throw error; } - //...Then we grab the attacker(?) and request that the defender takes damage according to specified calculations... - Mob.findById(req.params.attacker) - .then(attacker => { - return Mob.takeDamage(req.params.defender, attacker.attribute.strength) - .then(defender => { - let content = ""; - if (defender.attribute.living){ - content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage! // ${attacker._id} =${attacker.attribute.strength}=> ${defender._id} //`; - } - //...But if the defender is already dead then don't do anything and throw and error. - else if (defender.attackFailed){ - console.log('ERROR, CATCH!!!'); - let error = {}; - error.message = "Attack rejected: target is already downed and can't recieve anymore damage." - throw error; - } - else { - content = `${attacker.name} attacks ${defender.name} for ${attacker.attribute.strength} damage, knocking them to the ground! // ${attacker._id} =x!${attacker.attribute.strength}!x=> ${defender._id} //`; - } - //Log the action for future reference... - let newLog = { - content, - timestamp: Date.now(), - type: "action", - room: null - } - return Log.create(newLog) - .then(logResponse => { - res.status(200) - .json({ - message: "Attack completed successfully!", - log: logResponse, - attacker, - defender, - }) - }) - }) - }) - }) - //General catchall - .catch(err => { - console.log(err) - res.status(500).json({error: err}) - }) -} + else{ + promise.all([Mob.findById(req.params.attacker),Mob.findById(req.params.defender)]) + .then(promiseResponse => { + //actual attack logic that included the attack used and the target's damage calculations. + ////Export it. module.exports = actionsController; From f37b4f0d4d1876445a0ae44dd23a77a277b56a81 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Fri, 3 Aug 2018 22:29:32 -0400 Subject: [PATCH 18/33] Adds more commentary guide to the actions controller --- controllers/actions-controller.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 46ba3c8..3cd21cd 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -79,11 +79,16 @@ actionsController.attack = function(req, res){ } throw error; } - else{ + else { + //Check if the attacker has zero for a turn timer then... + + //Grab both the attacker and the defender promise.all([Mob.findById(req.params.attacker),Mob.findById(req.params.defender)]) .then(promiseResponse => { - //actual attack logic that included the attack used and the target's damage calculations. + //Deal damage based on the attacker's strength + //Then increase the attacker's turn timer by 50 - their speed + ////Export it. module.exports = actionsController; From 9056a68ae6a91ad6cf9a7d01d52cf846f6f40efe Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 6 Aug 2018 19:37:40 -0400 Subject: [PATCH 19/33] progresses the attack action towards completion --- controllers/actions-controller.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 3cd21cd..fbbb5bb 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -79,16 +79,35 @@ actionsController.attack = function(req, res){ } throw error; } - else { + else { //Check if the attacker has zero for a turn timer then... //Grab both the attacker and the defender - promise.all([Mob.findById(req.params.attacker),Mob.findById(req.params.defender)]) + return promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)]) .then(promiseResponse => { //Deal damage based on the attacker's strength - - //Then increase the attacker's turn timer by 50 - their speed - + return Mob.takeDamage(req.params.defender, promiseResponse[0].attribute.strength) + .then(postDamageResponse => { + //Check if the attack failed + if (postDamageResponse.attackFailed){ + console.log('ERROR, CATCH!!!'); + let error = {}; + error.message = "Attack rejected: target is already downed and can't recieve anymore damage."; + throw error; + } + //Then increase the attacker's turn timer by 50 - their speed + for (combatant in response.combatants){ + if (combatant.mobId === req.params.attacker){ + console.log("Ding! turn count increased."); + } + } + //Build the log message + let content = ""; + if (promiseResponse[1].attribute.living){ + content = `${promiseResponse[0].name} attacks ${promiseResponse[1].name} for ${promiseResponse[0].attribute.strength} damage! // ${promiseResponse[0]._id} =${promiseResponse[0].attribute.strength}=> ${promiseResponse[1]._id} //`; + } + else { + content = `${promiseResponse[0].name} attacks ${promiseResponse[1].name} for ${promiseResponse[0].attribute.strength} damage, knocking them to the ground!! // ${promiseResponse[0]._id} =x${promiseResponse[0].attribute.strength}x=> ${promiseResponse[1]._id} //`; ////Export it. module.exports = actionsController; From 1de62bfc434f46b5e545778f5c33bd98fc0e2133 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 6 Aug 2018 19:48:13 -0400 Subject: [PATCH 20/33] completes the attack logic with a bit for modifying the turn count. --- controllers/actions-controller.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index fbbb5bb..1167626 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -108,6 +108,32 @@ actionsController.attack = function(req, res){ } else { content = `${promiseResponse[0].name} attacks ${promiseResponse[1].name} for ${promiseResponse[0].attribute.strength} damage, knocking them to the ground!! // ${promiseResponse[0]._id} =x${promiseResponse[0].attribute.strength}x=> ${promiseResponse[1]._id} //`; - + } + //build the log + let newLog = { + content, + timestamp: Date.now(), + type: "action", + room: null + } + return Log.create(newLog) + .then(logResponse => { + res.status(200) + .json({ + message: "Attack completed successfully!", + log: logResponse, + attacker: promiseResponse[0], + defender: promiseResponse[1] + }) + }) + }) + }) + .catch(err => { + console.log(err) + res.status(500).json({error: err}) + }) + } + }) +} ////Export it. module.exports = actionsController; From 3978d1b7936c79e2e655633ceac11ed6bf76a854 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 6 Aug 2018 20:04:37 -0400 Subject: [PATCH 21/33] prepares file for beautify --- controllers/actions-controller.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 1167626..9432214 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -126,14 +126,15 @@ actionsController.attack = function(req, res){ defender: promiseResponse[1] }) }) - }) - }) + .catch(err => { console.log(err) res.status(500).json({error: err}) }) + }) + }) } }) -} + ////Export it. module.exports = actionsController; From 5a97274bbf90b515cf6e466bc262d3cc4247102d Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 6 Aug 2018 20:38:48 -0400 Subject: [PATCH 22/33] fixes and updates the attack method to properly trigger a turn count increase event. --- controllers/actions-controller.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 9432214..9070974 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -83,7 +83,7 @@ actionsController.attack = function(req, res){ //Check if the attacker has zero for a turn timer then... //Grab both the attacker and the defender - return promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)]) + return Promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)]) .then(promiseResponse => { //Deal damage based on the attacker's strength return Mob.takeDamage(req.params.defender, promiseResponse[0].attribute.strength) @@ -96,9 +96,12 @@ actionsController.attack = function(req, res){ throw error; } //Then increase the attacker's turn timer by 50 - their speed - for (combatant in response.combatants){ - if (combatant.mobId === req.params.attacker){ + console.log('beep...'); + for (let combatant in response[0].combatants){ + console.log('...buzz..'); + if (response[0].combatants[combatant].mobId === req.params.attacker){ console.log("Ding! turn count increased."); + break; } } //Build the log message @@ -126,15 +129,14 @@ actionsController.attack = function(req, res){ defender: promiseResponse[1] }) }) - - .catch(err => { - console.log(err) - res.status(500).json({error: err}) - }) - }) + }) }) } + }).catch(err => { + console.log(err) + throw err }) +} ////Export it. module.exports = actionsController; From 38906a5683afe6660402e2b3c580ac31390bd1ba Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 7 Aug 2018 16:38:24 -0400 Subject: [PATCH 23/33] Adds a function for updating the battle's turn timer. --- models/battle.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/models/battle.js b/models/battle.js index edd123c..0dce7c1 100644 --- a/models/battle.js +++ b/models/battle.js @@ -114,5 +114,27 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ }) } +//Increasing a combatant's turn timer +Battle.increaseTurnTimer = function(updatedBattle){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .findOneAndUpdate( + { + '_id': updatedBattle._id, + }, + { $set: updatedBattle }, + { returnOriginal: false } + ).then(response => modelHelper.serverLog('Battle.increaseTurnTimer', response)) + .then(response => { + console.log(updatedBattle) + connection.close(); + return response; + }) + }) +} + + //Export it module.exports = Battle; From 3a8bb5cfe8d3a67545bad3693469ff8d9f4fa38a Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 7 Aug 2018 16:38:55 -0400 Subject: [PATCH 24/33] Plugs in the function for updating battles --- controllers/actions-controller.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 9070974..1bb6334 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -100,10 +100,13 @@ actionsController.attack = function(req, res){ for (let combatant in response[0].combatants){ console.log('...buzz..'); if (response[0].combatants[combatant].mobId === req.params.attacker){ + response[0].combatants[combatant].turnCount += 50 - (promiseResponse[0].attribute.agility * 2) console.log("Ding! turn count increased."); + Battle.increaseTurnTimer(response[0]) break; } } + //Build the log message let content = ""; if (promiseResponse[1].attribute.living){ From ba5368a0158cb95e563f28e6ad68dbe4eed402df Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Tue, 7 Aug 2018 19:04:19 -0400 Subject: [PATCH 25/33] Prevents characters from making turns if thier turn timer isn't 0. --- controllers/actions-controller.js | 34 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 1bb6334..1323f71 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -75,16 +75,33 @@ actionsController.attack = function(req, res){ .then(response => { if (response.length === 0){ let error = { - message: "characters are not in the same battle" + message: "Attack rejected: characters are not in the same battle" } throw error; } else { //Check if the attacker has zero for a turn timer then... - + //Grab both the attacker and the defender return Promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)]) .then(promiseResponse => { + //Then increase the attacker's turn timer by 50 - their speed + console.log('beep...'); + for (let combatant in response[0].combatants){ + console.log('...buzz..'); + if (response[0].combatants[combatant].mobId === req.params.attacker){ + if (response[0].combatants[combatant].turnTimer !== 0){ + let error = { + message: "Attack rejected: not the character's turn" + } + throw error; + } + response[0].combatants[combatant].turnCount += 50 - (promiseResponse[0].attribute.agility * 2) + console.log("Ding! turn count increased."); + Battle.increaseTurnTimer(response[0]) + break; + } + } //Deal damage based on the attacker's strength return Mob.takeDamage(req.params.defender, promiseResponse[0].attribute.strength) .then(postDamageResponse => { @@ -95,17 +112,6 @@ actionsController.attack = function(req, res){ error.message = "Attack rejected: target is already downed and can't recieve anymore damage."; throw error; } - //Then increase the attacker's turn timer by 50 - their speed - console.log('beep...'); - for (let combatant in response[0].combatants){ - console.log('...buzz..'); - if (response[0].combatants[combatant].mobId === req.params.attacker){ - response[0].combatants[combatant].turnCount += 50 - (promiseResponse[0].attribute.agility * 2) - console.log("Ding! turn count increased."); - Battle.increaseTurnTimer(response[0]) - break; - } - } //Build the log message let content = ""; @@ -137,7 +143,7 @@ actionsController.attack = function(req, res){ } }).catch(err => { console.log(err) - throw err + res.status(500).json(err) }) } From 5af0d93eb353469b28635bd4dfd769d9e5797baf Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 8 Aug 2018 15:18:59 -0400 Subject: [PATCH 26/33] properly rolls over a combatant's turnCount on attack. --- controllers/actions-controller.js | 34 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/controllers/actions-controller.js b/controllers/actions-controller.js index 1323f71..22ca2cd 100644 --- a/controllers/actions-controller.js +++ b/controllers/actions-controller.js @@ -79,30 +79,44 @@ actionsController.attack = function(req, res){ } throw error; } - else { - //Check if the attacker has zero for a turn timer then... - + else { //Grab both the attacker and the defender return Promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)]) .then(promiseResponse => { - //Then increase the attacker's turn timer by 50 - their speed - console.log('beep...'); + //Find the attacker in the battle... for (let combatant in response[0].combatants){ - console.log('...buzz..'); if (response[0].combatants[combatant].mobId === req.params.attacker){ - if (response[0].combatants[combatant].turnTimer !== 0){ + //If it's not their turn, stop the attack + if (response[0].combatants[combatant].turnCount !== 0){ let error = { message: "Attack rejected: not the character's turn" } throw error; } + //If it is, then we update the battle with an increased turn count for the attacker response[0].combatants[combatant].turnCount += 50 - (promiseResponse[0].attribute.agility * 2) - console.log("Ding! turn count increased."); - Battle.increaseTurnTimer(response[0]) + //Battle.update(response[0]) break; } } - //Deal damage based on the attacker's strength + //Then we find who is next in line for attacking + let lowest = response[0].combatants[0].turnCount; + console.log('finding next in line...'); + for (let combatant in response[0].combatants){ + if (response[0].combatants[combatant].turnCount < lowest){ + lowest = response[0].combatants[combatant].turnCount; + console.log('...new lowest found...'); + } + } + //...And finally progress everyone's turn timer + for (let combatant in response[0].combatants){ + response[0].combatants[combatant].turnCount -= lowest; + console.log('buzz!'); + } + //Apply the update + Battle.update(response[0]) + + //Deal damage based on the attacker's strength return Mob.takeDamage(req.params.defender, promiseResponse[0].attribute.strength) .then(postDamageResponse => { //Check if the attack failed From 125c0121cfb1dcf45ace39ca3036c18d375af4a8 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 8 Aug 2018 15:23:43 -0400 Subject: [PATCH 27/33] renames the turnCount function in the battle model to an update function as it's more fitting. --- models/battle.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/models/battle.js b/models/battle.js index 0dce7c1..3c0f5fb 100644 --- a/models/battle.js +++ b/models/battle.js @@ -114,27 +114,26 @@ Battle.confirmCompatibleCombatants = function(combatantA, combatantB){ }) } -//Increasing a combatant's turn timer -Battle.increaseTurnTimer = function(updatedBattle){ +//updating a battle +Battle.update = function(updatedBattle){ return db.client.connect(db.url) .then(connection => { let selectedDb = connection.db(db.name); return selectedDb.collection('battles') .findOneAndUpdate( - { - '_id': updatedBattle._id, - }, + { '_id': updatedBattle._id }, { $set: updatedBattle }, { returnOriginal: false } - ).then(response => modelHelper.serverLog('Battle.increaseTurnTimer', response)) + ).then(response => modelHelper.serverLog('Battle.update', response)) .then(response => { - console.log(updatedBattle) connection.close(); return response; }) }) } +//Progressing the turn timers to allow the next person to go +//XXX Maybe this can be done inside the turn timer increase? //Export it module.exports = Battle; From 51756f03ff42ab8360878751ba0e488df1b74274 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 8 Aug 2018 15:38:33 -0400 Subject: [PATCH 28/33] Adds an emergency turn progress function to the battle model --- models/battle.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/models/battle.js b/models/battle.js index 3c0f5fb..d110818 100644 --- a/models/battle.js +++ b/models/battle.js @@ -133,7 +133,26 @@ Battle.update = function(updatedBattle){ } //Progressing the turn timers to allow the next person to go -//XXX Maybe this can be done inside the turn timer increase? +Battle.kickStart = function(id){ + Battle.findById(id) + .then( battle => { + let lowest = battle.combatants[0].turnCount; + console.log('finding next in line...'); + for (let combatant in battle.combatants){ + if (battle.combatants[combatant].turnCount < lowest){ + lowest = battle.combatants[combatant].turnCount; + console.log('...new lowest found...'); + } + } + //...And finally progress everyone's turn timer + for (let combatant in battle.combatants){ + battle.combatants[combatant].turnCount -= lowest; + console.log('buzz!'); + } + //Apply the update + Battle.update(battle) + }) +} //Export it module.exports = Battle; From bd154b5fc3f7542f0af345a7782d1657cc77a055 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 8 Aug 2018 19:53:48 -0400 Subject: [PATCH 29/33] prevents non-existent mobs from being inserted into battles. --- models/battle.js | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/models/battle.js b/models/battle.js index d110818..44b9af1 100644 --- a/models/battle.js +++ b/models/battle.js @@ -1,9 +1,12 @@ //Import the database const db = require('../db/config'); -//Tmport modelHelper object +//Import modelHelper object const modelHelper = require('./_model-helper'); +//Import the mob model +const Mob = require('./mob'); + //Create the battle object const Battle = {}; @@ -54,8 +57,35 @@ Battle.create = function(battle){ }) } +//Confirming the validity of a potential +Battle.confirmEligibleCombatant = function(combatantId){ + return Mob.findById(combatantId) + .then(response => { + if (response){ + return response.mob; + } + else { + return null; + } + }) + .then(response => { + console.log(response); + return response; + }) +} + //Adding combatants Battle.insertIntoCombatants = function(id, combatant){ + return Battle.confirmEligibleCombatant(combatant.mobId) + .then(confirmationResponse => { + console.log(confirmationResponse) + if (confirmationResponse === null){ + let error = { + message: "Mob not found, cancelling operation." + } + throw error; + } + Battle.confirmEligibleCombatant(combatant.mobId).then(test => {console.log(test)}) return db.client.connect(db.url) .then(connection => { let selectedDb = connection.db(db.name); @@ -70,6 +100,8 @@ Battle.insertIntoCombatants = function(id, combatant){ return response; }) }) + +}) } //Confirming the presence of two combatants in the same battle @@ -134,7 +166,7 @@ Battle.update = function(updatedBattle){ //Progressing the turn timers to allow the next person to go Battle.kickStart = function(id){ - Battle.findById(id) + return Battle.findById(id) .then( battle => { let lowest = battle.combatants[0].turnCount; console.log('finding next in line...'); From a1ba73c66af371c03d8a6bd59f1092240005a5d6 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Wed, 8 Aug 2018 19:53:48 -0400 Subject: [PATCH 30/33] prevents non-existent mobs from being inserted into battles. --- models/battle.js | 57 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/models/battle.js b/models/battle.js index d110818..d0d2231 100644 --- a/models/battle.js +++ b/models/battle.js @@ -1,9 +1,12 @@ //Import the database const db = require('../db/config'); -//Tmport modelHelper object +//Import modelHelper object const modelHelper = require('./_model-helper'); +//Import the mob model +const Mob = require('./mob'); + //Create the battle object const Battle = {}; @@ -54,20 +57,46 @@ Battle.create = function(battle){ }) } +//Confirming the validity of a potential combatant +Battle.confirmEligibleCombatant = function(combatantId){ + return Mob.findById(combatantId) + .then(response => { + if (response){ + return response.mob; + } + else { + return null; + } + }) + .then(response => { + console.log(response); + return response; + }) +} + //Adding combatants Battle.insertIntoCombatants = function(id, combatant){ - return db.client.connect(db.url) - .then(connection => { - let selectedDb = connection.db(db.name); - return selectedDb.collection('battles') - .findOneAndUpdate( - {"_id": db.objectId.createFromHexString(id)}, - {$push: {combatants: combatant}}, - {returnOriginal: false} - ).then(response => modelHelper.serverLog('Battle.insertIntoCombatants', response.value)) - .then(response => { - connection.close(); - return response; + return Battle.confirmEligibleCombatant(combatant.mobId) + .then(confirmationResponse => { + if (confirmationResponse === null){ + let error = { + message: "Mob not found, cancelling operation." + } + throw error; + } + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .findOneAndUpdate( + {"_id": db.objectId.createFromHexString(id)}, + {$push: {combatants: combatant}}, + {returnOriginal: false} + ).then(response => modelHelper.serverLog('Battle.insertIntoCombatants', response.value)) + .then(response => { + connection.close(); + return response; + }) }) }) } @@ -134,7 +163,7 @@ Battle.update = function(updatedBattle){ //Progressing the turn timers to allow the next person to go Battle.kickStart = function(id){ - Battle.findById(id) + return Battle.findById(id) .then( battle => { let lowest = battle.combatants[0].turnCount; console.log('finding next in line...'); From a2b4a33d70618de4fa9787e52432468982c103ef Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Fri, 10 Aug 2018 18:29:07 -0400 Subject: [PATCH 31/33] Adds a function to if see a combatant is already in a battle. --- models/battle.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/models/battle.js b/models/battle.js index d0d2231..4d47b5f 100644 --- a/models/battle.js +++ b/models/battle.js @@ -42,6 +42,22 @@ Battle.findById = function(id){ }) } +//finding a single battle by combatant +Battle.findByCombatant = function(combatantId){ + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + findOne({"combatants.mobId": combatantId}) + .then(response => modelHelper.serverLog('Battle.findByCombatant', response)) + .then(response => { + connection.close(); + return response; + }) + }) +} + + //creating a single battle Battle.create = function(battle){ return db.client.connect(db.url) From 7b8b238fa62ed829815bb3c8d42ec2c38ce0348a Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Fri, 10 Aug 2018 18:55:28 -0400 Subject: [PATCH 32/33] Haphazardly plugs in the combatant confirmation function into the add combatant function. --- models/battle.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/models/battle.js b/models/battle.js index 6585ecd..09c9143 100644 --- a/models/battle.js +++ b/models/battle.js @@ -48,7 +48,8 @@ Battle.findByCombatant = function(combatantId){ .then(connection => { let selectedDb = connection.db(db.name); return selectedDb.collection('battles') - findOne({"combatants.mobId": combatantId}) + find({"combatants.mobId": combatantId}) + .toArray() .then(response => modelHelper.serverLog('Battle.findByCombatant', response)) .then(response => { connection.close(); @@ -99,18 +100,27 @@ Battle.insertIntoCombatants = function(id, combatant){ } throw error; } - return db.client.connect(db.url) - .then(connection => { - let selectedDb = connection.db(db.name); - return selectedDb.collection('battles') - .findOneAndUpdate( - {"_id": db.objectId.createFromHexString(id)}, - {$push: {combatants: combatant}}, - {returnOriginal: false} - ).then(response => modelHelper.serverLog('Battle.insertIntoCombatants', response.value)) - .then(response => { - connection.close(); - return response; + return Battle.findByCombatant(combatant.mobId) + .then(combatantConfirmationResponse => { + if (combatantConfirmationResponse.length == 0){ + let error = { + message: "Mob already in a battle, cancelling operation." + } + throw error; + } + return db.client.connect(db.url) + .then(connection => { + let selectedDb = connection.db(db.name); + return selectedDb.collection('battles') + .findOneAndUpdate( + {"_id": db.objectId.createFromHexString(id)}, + {$push: {combatants: combatant}}, + {returnOriginal: false} + ).then(response => modelHelper.serverLog('Battle.insertIntoCombatants', response.value)) + .then(response => { + connection.close(); + return response; + }) }) }) }) From 40ea410bd34e3f08ca9bb78702423b0e6820a837 Mon Sep 17 00:00:00 2001 From: Patrick Shannon Date: Mon, 20 Aug 2018 20:33:59 -0400 Subject: [PATCH 33/33] Properly prevents mobs from being entered into more than one battle and prevents mobs from being entered into the same battle twice. --- models/battle.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/battle.js b/models/battle.js index 09c9143..7dc4b3a 100644 --- a/models/battle.js +++ b/models/battle.js @@ -42,13 +42,13 @@ Battle.findById = function(id){ }) } -//finding a single battle by combatant +//Finding a single battle by combatant Battle.findByCombatant = function(combatantId){ return db.client.connect(db.url) .then(connection => { let selectedDb = connection.db(db.name); return selectedDb.collection('battles') - find({"combatants.mobId": combatantId}) + .find({"combatants.mobId": combatantId}) .toArray() .then(response => modelHelper.serverLog('Battle.findByCombatant', response)) .then(response => { @@ -102,7 +102,7 @@ Battle.insertIntoCombatants = function(id, combatant){ } return Battle.findByCombatant(combatant.mobId) .then(combatantConfirmationResponse => { - if (combatantConfirmationResponse.length == 0){ + if (combatantConfirmationResponse.length){ let error = { message: "Mob already in a battle, cancelling operation." }