1 import {FightRound} from '../shared/fight';
2 import { clearFight, loadMonster, getMonsterList, saveFightState, loadMonsterFromFight } from './monster';
3 import { Player, expToLevel, maxHp, totalDefence, maxVigor } from '../shared/player';
4 import { clearTravelPlan } from './map';
5 import { updatePlayer } from './player';
6 import { getEquippedItems, updateAp } from './inventory';
7 import { EquippedItemDetails } from '../shared/equipped';
8 import { EquipmentSlot } from '../shared/inventory';
9 import { MonsterWithFaction, MonsterForFight, Fight } from '../shared/monsters';
10 import { getPlayerSkillsAsObject, updatePlayerSkills } from './skills';
11 import { SkillID, Skills } from '../shared/skills';
12 import { Request, Response } from 'express';
13 import * as Alert from './views/alert';
15 export async function blockPlayerInFight(req: Request, res: Response, next: any) {
16 const fight = await loadMonsterFromFight(req.player.id);
22 res.send(Alert.ErrorAlert(`You are currently in a fight with a ${fight.name}`));
25 function exponentialExp(exp: number, monsterLevel: number, playerLevel: number): number {
28 if((monsterLevel+3) < playerLevel) {
29 finalExp = Math.floor(exp * Math.pow(Math.E, ((monsterLevel + 3) - playerLevel)/5));
31 else if(monsterLevel > (playerLevel + 3)) {
32 finalExp = Math.floor(exp * Math.pow(Math.E, ((playerLevel + 3) - monsterLevel)/5));
35 return Math.floor(finalExp);
38 export async function fightRound(player: Player, monster: Fight, data: {action: 'attack' | 'cast' | 'flee'}) {
39 const playerSkills = await getPlayerSkillsAsObject(player.id);
40 const roundData: FightRound = {
43 winner: 'in-progress',
44 fightTrigger: monster.fight_trigger,
52 const equippedItems = await getEquippedItems(player.id);
54 // we only use this if the player successfully defeated the monster
55 // they were fighting, then we load the other monsters in this area
56 // so they can "fight again"
57 let potentialMonsters: MonsterForFight[] = [];
60 defence: totalDefence(equippedItems, player),
69 const equipment: Map<EquipmentSlot, EquippedItemDetails> = new Map<EquipmentSlot, EquippedItemDetails>();
70 const weapons: EquippedItemDetails[] = [];
71 let anyDamageSpells: boolean = false;
72 equippedItems.forEach(item => {
73 if(item.type === 'ARMOUR') {
74 equipment.set(item.equipment_slot, item);
76 else if(item.type === 'WEAPON') {
79 else if(item.type === 'SPELL') {
80 if(item.affectedSkills.includes('destruction_magic')) {
81 anyDamageSpells = true;
86 boost.strength += item.boosts.strength;
87 boost.constitution += item.boosts.constitution;
88 boost.dexterity += item.boosts.dexterity;
89 boost.intelligence += item.boosts.intelligence;
91 if(item.type === 'SPELL' && item.affectedSkills.includes('restoration_magic')) {
92 boost.hp += item.boosts.damage;
95 boost.damage += item.boosts.damage;
99 // @TODO implement flee based on dex + vigor
100 if(data.action === 'flee') {
101 roundData.roundDetails.push(`You managed to escape from the ${monster.name}!`)
102 roundData.winner = 'monster';
103 await clearFight(player.id);
105 return { roundData, monsters: [], player };
108 const attackType = data.action === 'attack' ? 'physical' : 'magical';
109 const primaryStat = attackType === 'physical' ? player.strength : player.intelligence;
110 const boostStat = attackType === 'physical' ? boost.strength : boost.intelligence;
112 const playerDamage = Math.floor(((primaryStat + boostStat) * 1.3) + boost.damage);
113 const skillsUsed: Record<SkillID | any, number> = {};
114 let hpHealAfterMasteries: number = -1;
115 let playerDamageAfterMasteries: number = 0;
117 weapons.forEach(item => {
118 item.affectedSkills.forEach(id => {
119 if(id === 'restoration_magic') {
120 if(hpHealAfterMasteries < 0) {
121 hpHealAfterMasteries = 0;
123 const skill = Skills.get(id);
125 const playerSkill = playerSkills.get(id);
127 hpHealAfterMasteries += skill.effect(playerSkill);
132 const skill = Skills.get(id);
134 const playerSkill = playerSkills.get(id);
136 playerDamageAfterMasteries += playerDamage * skill.effect(playerSkill);
141 if(!skillsUsed[id]) {
148 await updatePlayerSkills(player.id, playerSkills, skillsUsed);
150 const playerFinalDamage = (data.action === 'cast' && !anyDamageSpells) ? 0 : Math.floor(playerDamage + playerDamageAfterMasteries);
151 const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries);
153 let monsterTakesDamage = playerFinalDamage - monster.defence;
154 if(monsterTakesDamage < 0) {
155 monsterTakesDamage = 0;
157 roundData.roundDetails.push(`You dealt ${monsterTakesDamage} damage to the ${monster.name}!`);
159 monster.hp -= monsterTakesDamage;
161 if(monster.hp <= 0) {
162 roundData.monster.hp = 0;
163 roundData.winner = 'player';
165 const expGained = exponentialExp(monster.exp, monster.level, player.level);
167 roundData.rewards.exp = expGained;
168 roundData.rewards.gold = monster.gold;
170 player.gold += monster.gold;
171 player.exp += expGained;
173 if(player.exp >= expToLevel(player.level + 1)) {
174 player.exp -= expToLevel(player.level + 1)
176 roundData.rewards.levelIncrease = true;
177 let statPointsGained = 1;
179 if(player.profession !== 'Wanderer') {
180 statPointsGained = 2;
183 player.stat_points += statPointsGained;
185 roundData.roundDetails.push(`You gained ${statPointsGained} stat points!`);
187 player.hp = maxHp(player.constitution, player.level);
188 player.vigor = maxVigor(player.constitution, player.level);
190 // get the monster location if it was an EXPLORED fight
191 if(roundData.fightTrigger === 'explore') {
192 const rawMonster = await loadMonster(monster.ref_id);
193 const monsterList = await getMonsterList(rawMonster.location_id);
194 potentialMonsters = monsterList.map(monster => {
198 minLevel: monster.minLevel,
199 maxLevel: monster.maxLevel,
201 maxHp: monster.maxHp,
202 fight_trigger: 'explore'
208 if(player.vigor < 0) {
212 const unequippedItems = await updateAp(player.id, 1, equippedItems.map(i => i.item_id));
213 await clearFight(player.id);
214 await updatePlayer(player);
216 if(unequippedItems.length) {
217 unequippedItems.forEach(i => {
218 roundData.roundDetails.push(`Your ${i.name} was too damaged and was unequipped!`);
222 return { roundData, monsters: potentialMonsters, player };
225 let monsterDamage = (monster.strength*2) - boost.defence;
226 if(monsterDamage < 0) {
230 roundData.roundDetails.push(`The ${monster.name} hit you for ${monsterDamage} damage`);
231 player.hp -= monsterDamage;
233 if(playerFinalHeal > 0) {
234 player.hp += playerFinalHeal;
235 if(player.hp > maxHp(player.constitution, player.level)) {
236 player.hp = maxHp(player.constitution, player.level);
238 roundData.roundDetails.push(`You healed for ${playerFinalHeal} HP`);
244 roundData.winner = 'monster';
246 roundData.roundDetails.push(`You were killed by the ${monster.name}`);
248 await clearFight(player.id);
249 const unequippedItems = await updateAp(player.id, 5, equippedItems.map(i => i.item_id));
250 await updatePlayer(player);
251 await clearTravelPlan(player.id);
253 if(unequippedItems.length) {
254 unequippedItems.forEach(i => {
255 roundData.roundDetails.push(`Your ${i.name} was too damaged and was unequipped!`);
259 return { roundData, monsters: [], player};
262 await updatePlayer(player);
263 await saveFightState(player.id, monster);
265 return { roundData, monsters: [], player};