From 24e6b3b737b612b368c06e2abb42181c9b1136f1 Mon Sep 17 00:00:00 2001 From: xangelo Date: Fri, 25 Aug 2023 11:57:09 -0400 Subject: [PATCH] fix: properly increment skill level --- src/server/fight.ts | 2 +- src/server/skills.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/server/fight.ts b/src/server/fight.ts index 855589a..3ceb706 100644 --- a/src/server/fight.ts +++ b/src/server/fight.ts @@ -167,7 +167,7 @@ export async function fightRound(player: Player, monster: MonsterWithFaction, d }); }); - await updatePlayerSkills(player.id, skillsUsed); + await updatePlayerSkills(player.id, playerSkills, skillsUsed); const playerFinalDamage = (data.action === 'cast' && !anyDamageSpells) ? 0 : Math.floor(playerDamage + playerDamageAfterMasteries); const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries); diff --git a/src/server/skills.ts b/src/server/skills.ts index 131c141..80a6405 100644 --- a/src/server/skills.ts +++ b/src/server/skills.ts @@ -1,4 +1,4 @@ -import {Skill, SkillID} from '../shared/skills'; +import {Skills, Skill, SkillID} from '../shared/skills'; import { db } from './lib/db'; import { each } from 'lodash'; @@ -20,11 +20,21 @@ export async function getPlayerSkillsAsObject(playerId: string): Promise) { +export async function updatePlayerSkills(playerId: string, playerSkills:Map, skillExpDiff: Record) { const sql = []; - each(skills, (val, skillId) => { - sql.push(`update player_skills set exp = exp + ${val} where id = '${skillId}' and player_id = '${playerId}'`); + each(skillExpDiff, (val, skillId: SkillID) => { + const skill = playerSkills.get(skillId); + const def = Skills.get(skillId); + if(skill && def) { + skill.exp += val; + if(skill.exp >= def.expToLevel(skill.level + 1)) { + skill.level++; + skill.exp -= def.expToLevel(skill.level); + sql.push(`update player_skills set exp = ${skill.exp}, level = ${skill.level} where id = '${skillId}' and player_id = '${playerId}'`); + } + } + }); if(sql.length) { -- 2.25.1