Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1481293
Adds a schema validator for the new battles collection.
p-shannon Jun 12, 2018
e966066
Adds a rudimentary battle model to the fray.
p-shannon Jun 13, 2018
c228a1b
Adds the battle-controller to the fray
p-shannon Jun 13, 2018
bc86f51
Adds the battle router to the fray.
p-shannon Jun 13, 2018
dd7bbfd
Plugs the battle router into the server for some basic CR-- functiona…
p-shannon Jun 13, 2018
9db3c04
Allows the battle model to insert a combatant into it's combatants ar…
p-shannon Jun 13, 2018
e747917
Allows the battle controller to tell it's model to add a combatant.
p-shannon Jun 13, 2018
573aeaf
Adds a route for adding combatants to a battle.
p-shannon Jun 13, 2018
0a3f092
adds a battle model function for seeing if two combatants are in the …
p-shannon Jun 13, 2018
c245010
The battle controller will now reject attacks from entities in differ…
p-shannon Jun 13, 2018
5dccb06
Converts the existing Battle.confirm method into an aggregate functio…
p-shannon Jun 25, 2018
cfb56a2
Corrects the abyssmal syntax in the aggregation pipeline in the confi…
p-shannon Jun 25, 2018
1066833
confirms finding battles containing specified combatants works.
p-shannon Jul 21, 2018
fc716df
Scaffolds out a lookup stage in the aggregation pipeline along with a…
p-shannon Jul 21, 2018
f8a7d25
Adds some commentary to the attack controller.
p-shannon Aug 2, 2018
3e89780
Adds some comments to the take damage method in the mob model
p-shannon Aug 3, 2018
af547d6
Begins a rewrite of the attack action in the actions-controller.
p-shannon Aug 3, 2018
f37b4f0
Adds more commentary guide to the actions controller
p-shannon Aug 4, 2018
9056a68
progresses the attack action towards completion
p-shannon Aug 6, 2018
1de62bf
completes the attack logic with a bit for modifying the turn count.
p-shannon Aug 6, 2018
3978d1b
prepares file for beautify
p-shannon Aug 7, 2018
5a97274
fixes and updates the attack method to properly trigger a turn count …
p-shannon Aug 7, 2018
38906a5
Adds a function for updating the battle's turn timer.
p-shannon Aug 7, 2018
3a8bb5c
Plugs in the function for updating battles
p-shannon Aug 7, 2018
ba5368a
Prevents characters from making turns if thier turn timer isn't 0.
p-shannon Aug 7, 2018
5af0d93
properly rolls over a combatant's turnCount on attack.
p-shannon Aug 8, 2018
125c012
renames the turnCount function in the battle model to an update funct…
p-shannon Aug 8, 2018
51756f0
Adds an emergency turn progress function to the battle model
p-shannon Aug 8, 2018
bd154b5
prevents non-existent mobs from being inserted into battles.
p-shannon Aug 8, 2018
a1ba73c
prevents non-existent mobs from being inserted into battles.
p-shannon Aug 8, 2018
a2b4a33
Adds a function to if see a combatant is already in a battle.
p-shannon Aug 10, 2018
5236161
solves mergie
p-shannon Aug 10, 2018
7b8b238
Haphazardly plugs in the combatant confirmation function into the add…
p-shannon Aug 10, 2018
40ea410
Properly prevents mobs from being entered into more than one battle a…
p-shannon Aug 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 144 additions & 34 deletions controllers/actions-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,160 @@ 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
/* 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){
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;
//See if the combatants are in the same battle.
Battle.confirmCompatibleCombatants(req.params.attacker, req.params.defender)
.then(response => {
if (response.length === 0){
let error = {
message: "Attack rejected: characters are not in the same battle"
}
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,
throw error;
}
else {
//Grab both the attacker and the defender
return Promise.all([Mob.findById(req.params.attacker), Mob.findById(req.params.defender)])
.then(promiseResponse => {
//Find the attacker in the battle...
for (let combatant in response[0].combatants){
if (response[0].combatants[combatant].mobId === req.params.attacker){
//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)
//Battle.update(response[0])
break;
}
}
//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
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;
}

//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} //`;
}
//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 => {
}
}).catch(err => {
console.log(err)
res.status(500).json({error: err})
res.status(500).json(err)
})
}

Expand Down
73 changes: 73 additions & 0 deletions controllers/battle-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//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});
})
}

//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
45 changes: 45 additions & 0 deletions db/migrations/migration-00003.js
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
}
});
Loading