Skip to content

Commit

Permalink
feat(combat): combat can now drop items. closes #106
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Sep 13, 2023
1 parent 57c3478 commit 7ac4312
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/src/app/pages/explore/explore.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@

<ion-card
class="item-card action-card"
*ngIf="actionInfo.action === 'item' || actionInfo.action === 'collectible' || actionInfo.action === 'resource'"
*ngIf="actionInfo.action === 'item' || actionInfo.action === 'collectible' || actionInfo.action === 'resource' || actionInfo.action === 'fightreward'"
>
<ion-card-content>
<div class="intro">
Expand Down
39 changes: 36 additions & 3 deletions server/src/modules/fight/fight.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
import { EventEmitter2 } from '@nestjs/event-emitter';
import { getPatchesAfterPropChanges } from '@utils/patches';
import { Operation } from 'fast-json-patch';
import { sample, sampleSize, sum } from 'lodash';
import { random, sample, sampleSize, sum } from 'lodash';
import { Logger } from 'nestjs-pino';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -276,7 +276,9 @@ export class FightService {
const player = await this.playerService.getPlayerForUser(playerId);
if (!player) throw new NotFoundException('Player not found');

this.playerService.setPlayerAction(player, undefined);
if (player.action?.action === 'fight') {
this.playerService.setPlayerAction(player, undefined);
}

this.emit(playerId, {
fight: null,
Expand Down Expand Up @@ -341,8 +343,27 @@ export class FightService {
coinDelta = coinsGained;
}

const randomDropPossibility = sample(
fight.defenders.map((c) => c.killRewards?.items ?? []).flat(),
);

let randomDrop;

if (
randomDropPossibility &&
random(0, 100) <= randomDropPossibility.chance
) {
randomDrop = this.contentService.getItem(randomDropPossibility.item);
addStatusMessage(fight, 'Fight', `You found "${randomDrop.name}"!`);
}

await this.updateFightForAllPlayers(fight);
await delayTime(1000);

this.logger.verbose(
`Fight ${fight._id} rewarded ${xpDelta} XP and ${coinDelta} coins.`,
`Fight ${fight._id} rewarded ${xpDelta} XP and ${coinDelta} coins and ${
randomDrop?.item ?? 'no'
} item drop.`,
);

await Promise.all(
Expand Down Expand Up @@ -372,6 +393,18 @@ export class FightService {
async (player) => {
this.playerHelperService.gainXp(player, xpDelta);
this.playerHelperService.gainCoins(player, coinDelta);

if (isWin && randomDrop) {
this.playerService.setPlayerAction(player, {
text: 'Take',
action: 'fightreward',
actionData: {
item: randomDrop,
},
url: 'gameplay/item/take',
urlData: {},
});
}
},
);

Expand Down

0 comments on commit 7ac4312

Please sign in to comment.