Commit cac03cce authored by George Peter Banyard's avatar George Peter Banyard

Fixed a bug that would drain energy when receiver is already full health.

Added some fields to retrieve and pass to the resolve data object in preparation for embedded messages for combat actions.
parent 71f67831
......@@ -106,7 +106,7 @@ const attackProfile = (attackerId, defenderId) => {
if (attackerData['energy_current'] < rpg.ACTION_COST) {
reject(rpg.CODE_NOT_ENOUGH_ENERGY);
}
sqlite.get('SELECT userId, health_current FROM player WHERE userId = $defenderId',
sqlite.get('SELECT userId, health_current, health_max FROM player WHERE userId = $defenderId',
{$defenderId:String(defenderId)}).
then(defenderData => {
if (defenderData === undefined) {
......@@ -123,7 +123,8 @@ const attackProfile = (attackerId, defenderId) => {
{$newEnergy:newEnergy, $attackerId:String(attackerId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
resolve({damage:attackerData.attack,health:newHealth,energy:newEnergy});
resolve({damage:attackerData.attack,health:newHealth,healthMax:defenderData['health_max'],
energy:newEnergy});
}
}).catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); })
}).catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); })
......@@ -153,20 +154,22 @@ const heal = (casterId, receiverId) => {
}
if (receiverData['health_current'] === receiverData['health_max']) {
reject(rpg.CODE_RECEIVER_ALREADY_MAX_HEALTH);
}
let newHealth = receiverData['health_current'] + casterData['magic'];
if (newHealth > receiverData['health_max']) {
newHealth = receiverData['health_max'];
}
let amountHealed = newHealth - receiverData['health_current'];
sqlite.run('UPDATE player SET health_current = $newHealth WHERE userId = $receiverId',
{$newHealth:newHealth,$receiverId:String(receiverId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
sqlite.run('UPDATE player SET energy_current = $newEnergy WHERE userId = $casterId',
{$newEnergy:newEnergy,$casterId:String(casterId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
} else {
let newHealth = receiverData['health_current'] + casterData['magic'];
if (newHealth > receiverData['health_max']) {
newHealth = receiverData['health_max'];
}
let amountHealed = newHealth - receiverData['health_current'];
sqlite.run('UPDATE player SET health_current = $newHealth WHERE userId = $receiverId',
{$newHealth:newHealth,$receiverId:String(receiverId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
sqlite.run('UPDATE player SET energy_current = $newEnergy WHERE userId = $casterId',
{$newEnergy:newEnergy,$casterId:String(casterId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
resolve({amountHealed:amountHealed,health:newHealth,energy:newEnergy,self:self});
resolve({amountHealed:amountHealed,health:newHealth,healthMax:receiverData['health_max'],
energy:newEnergy,self:self});
}
}).catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
} else {
if (casterData['health_current'] === casterData['health_max']) {
......@@ -182,7 +185,8 @@ const heal = (casterId, receiverId) => {
{$newHealth:newHealth,$newEnergy:newEnergy,$casterId:String(casterId)})
.catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
resolve({amountHealed:amountHealed,health:newHealth,energy:newEnergy,self:self});
resolve({amountHealed:amountHealed,health:newHealth,healthMax:casterData['health_max'],
energy:newEnergy,self:self});
}
}
}).catch((e) => { console.error(e); reject(rpg.CODE_DB_ERROR); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment