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 } from '../shared/monsters';
10 import { getPlayerSkillsAsObject, updatePlayerSkills } from './skills';
11 import { SkillID, Skills } from '../shared/skills';
12 import { AuthRequest } from './auth';
13 import { Response } from 'express';
14 import * as Alert from './views/alert';
16 export async function blockPlayerInFight(req: AuthRequest, res: Response, next: any) {
17 const fight = await loadMonsterFromFight(req.player.id);
23 res.send(Alert.ErrorAlert(`You are currently in a fight with a ${fight.name}`));
26 function exponentialExp(exp: number, monsterLevel: number, playerLevel: number): number {
29 if((monsterLevel+3) < playerLevel) {
30 finalExp = Math.floor(exp * Math.pow(Math.E, ((monsterLevel + 3) - playerLevel)/5));
32 else if(monsterLevel > (playerLevel + 3)) {
33 finalExp = Math.floor(exp * Math.pow(Math.E, ((playerLevel + 3) - monsterLevel)/5));
36 return Math.floor(finalExp);
39 export async function fightRound(player: Player, monster: MonsterWithFaction, data: {action: 'attack' | 'cast' | 'flee', target: 'head' | 'body' | 'arms' | 'legs'}) {
40 const playerSkills = await getPlayerSkillsAsObject(player.id);
41 const roundData: FightRound = {
44 winner: 'in-progress',
45 fightTrigger: monster.fight_trigger,
53 const equippedItems = await getEquippedItems(player.id);
55 // we only use this if the player successfully defeated the monster
56 // they were fighting, then we load the other monsters in this area
57 // so they can "fight again"
58 let potentialMonsters: MonsterForFight[] = [];
61 defence: totalDefence(equippedItems, player),
70 const equipment: Map<EquipmentSlot, EquippedItemDetails> = new Map<EquipmentSlot, EquippedItemDetails>();
71 const weapons: EquippedItemDetails[] = [];
72 let anyDamageSpells: boolean = false;
73 equippedItems.forEach(item => {
74 if(item.type === 'ARMOUR') {
75 equipment.set(item.equipment_slot, item);
77 else if(item.type === 'WEAPON') {
80 else if(item.type === 'SPELL') {
81 if(item.affectedSkills.includes('destruction_magic')) {
82 anyDamageSpells = true;
87 boost.strength += item.boosts.strength;
88 boost.constitution += item.boosts.constitution;
89 boost.dexterity += item.boosts.dexterity;
90 boost.intelligence += item.boosts.intelligence;
92 if(item.type === 'SPELL' && item.affectedSkills.includes('restoration_magic')) {
93 boost.hp += item.boosts.damage;
96 boost.damage += item.boosts.damage;
100 // @TODO implement flee based on dex + vigor
101 if(data.action === 'flee') {
102 roundData.roundDetails.push(`You managed to escape from the ${monster.name}!`)
103 roundData.winner = 'monster';
104 await clearFight(player.id);
106 return { roundData, monsters: [], player };
109 const attackType = data.action === 'attack' ? 'physical' : 'magical';
110 const primaryStat = attackType === 'physical' ? player.strength : player.intelligence;
111 const boostStat = attackType === 'physical' ? boost.strength : boost.intelligence;
113 const playerDamage = Math.floor(((primaryStat + boostStat) * 1.3) + boost.damage);
114 const skillsUsed: Record<SkillID | any, number> = {};
115 let hpHealAfterMasteries: number = -1;
116 let playerDamageAfterMasteries: number = 0;
118 weapons.forEach(item => {
119 item.affectedSkills.forEach(id => {
120 if(id === 'restoration_magic') {
121 if(hpHealAfterMasteries < 0) {
122 hpHealAfterMasteries = 0;
124 const skill = Skills.get(id);
126 const playerSkill = playerSkills.get(id);
128 hpHealAfterMasteries += skill.effect(playerSkill);
133 const skill = Skills.get(id);
135 const playerSkill = playerSkills.get(id);
137 playerDamageAfterMasteries += playerDamage * skill.effect(playerSkill);
142 if(!skillsUsed[id]) {
149 await updatePlayerSkills(player.id, playerSkills, skillsUsed);
151 const playerFinalDamage = (data.action === 'cast' && !anyDamageSpells) ? 0 : Math.floor(playerDamage + playerDamageAfterMasteries);
152 const playerFinalHeal = Math.floor(boost.hp + hpHealAfterMasteries);
154 let monsterTakesDamage = playerFinalDamage - monster.defence;
155 if(monsterTakesDamage < 0) {
156 monsterTakesDamage = 0;
158 roundData.roundDetails.push(`You dealt ${monsterTakesDamage} damage to the ${monster.name}!`);
160 monster.hp -= monsterTakesDamage;
162 if(monster.hp <= 0) {
163 roundData.monster.hp = 0;
164 roundData.winner = 'player';
166 const expGained = exponentialExp(monster.exp, monster.level, player.level);
168 roundData.rewards.exp = expGained;
169 roundData.rewards.gold = monster.gold;
171 player.gold += monster.gold;
172 player.exp += expGained;
174 if(player.exp >= expToLevel(player.level + 1)) {
175 player.exp -= expToLevel(player.level + 1)
177 roundData.rewards.levelIncrease = true;
178 let statPointsGained = 1;
180 if(player.profession !== 'Wanderer') {
181 statPointsGained = 2;
184 player.stat_points += statPointsGained;
186 roundData.roundDetails.push(`You gained ${statPointsGained} stat points!`);
188 player.hp = maxHp(player.constitution, player.level);
189 player.vigor = maxVigor(player.constitution, player.level);
191 // get the monster location if it was an EXPLORED fight
192 if(roundData.fightTrigger === 'explore') {
193 const rawMonster = await loadMonster(monster.ref_id);
194 const monsterList = await getMonsterList(rawMonster.location_id);
195 potentialMonsters = monsterList.map(monster => {
199 level: monster.level,
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};