X-Git-Url: https://git.xangelo.ca/?p=sketchy-heroes.git;a=blobdiff_plain;f=src%2Froutes%2Ffight%2Ffight.ts;fp=src%2Froutes%2Ffight%2Ffight.ts;h=038ab97f2bde0e376020c191f711e9938423e8e8;hp=8c3ca0b51d175601dc4638bf2ea1d5601895df5c;hb=b5d3cc37fddebff8dcdf1ef0cdd3a626811f14d3;hpb=20dc560a75cfd6ddc8a66956315a30001779ec24 diff --git a/src/routes/fight/fight.ts b/src/routes/fight/fight.ts index 8c3ca0b..038ab97 100644 --- a/src/routes/fight/fight.ts +++ b/src/routes/fight/fight.ts @@ -2,9 +2,9 @@ import { server } from '../../lib/server'; import { Static, Type } from '@sinclair/typebox'; import { prisma } from '../../lib/db'; import { ForbiddenError } from '../../lib/http-errors'; -import {Fight, Player} from '@prisma/client'; +import {Fight, LootTable, Player, WorldDrop} from '@prisma/client'; import {maxExp, maxHp} from '../../formulas'; - +import {weighted} from '../../lib/weighted'; const FightRoundInput = Type.Object({ params: Type.Object({ @@ -29,7 +29,14 @@ export type FightRoundOutput = { monster: Fight, winner: 'monster' | 'player' | 'flee', roundData: RoundData[], - reward: any + reward: { + exp?: number, + currency?: number + worldDrop?: { + id: string, + name: string + } + } } function round(player: Player, monster: Fight): FightRoundOutput { @@ -114,6 +121,9 @@ export const fightRound = server.post('/v prisma.player.findUnique({ where: { id: req.params.playerId + }, + include: { + zoneBiome: true } }) ]); @@ -175,7 +185,24 @@ export const fightRound = server.post('/v player.hp = maxHp(player.level, player.zest); } - await prisma.$transaction([ + // we should figure out if you got an item! + + const [monster, ,] = await prisma.$transaction([ + prisma.monsterBiome.findUnique({ + where: { + monsterId_biome: { + monsterId: fight.monsterId, + biome: player.zoneBiome.biome + } + }, + include: { + lootTable: { + include: { + item: true + } + } + } + }), prisma.fight.delete({ where: { id: fight.id @@ -196,6 +223,29 @@ export const fightRound = server.post('/v }) ]); + // since the player won, we can give them an item if they've earned it + if(monster && monster.lootTable.length > 0) { + const item = weighted(monster.lootTable, (i) => i.dropRate.toNumber()); + + if(item) { + // we can give the player this item, so lets place it in the "world-drop-table". + // if a player doesn't pick it up, it will remain in the world drop table for + // 24 hours.. after that it's gone forever. + // if the player does pick it up then it's moved to their inventory + const worldDrop = await prisma.worldDrop.create({ + data: { + itemId: item.itemId, + droppedById: player.id + } + }); + + output.reward['worldDrop'] = { + id: worldDrop.id, + name: item.item.name + }; + } + } + // update the player with the new data output.player = player; }