Skip to content

Commit

Permalink
feat(core): add logic to service paths, closes #104
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Aug 29, 2023
1 parent 8180dac commit c628d71
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/src/modules/aggregator/aggregator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { PlayerService } from '@modules/player/player.service';
import { StatsService } from '@modules/stats/stats.service';
import { UserService } from '@modules/user/user.service';
import { Injectable } from '@nestjs/common';
import { Logger } from 'nestjs-pino';

@Injectable()
export class AggregatorService {
constructor(
private readonly logger: Logger,
private readonly contentService: ContentService,
private readonly userService: UserService,
private readonly playerService: PlayerService,
Expand Down Expand Up @@ -73,10 +75,13 @@ export class AggregatorService {
) as IEquipment;
if (!checkItem) return;

const currentItem = JSON.stringify(equippedItems[slot]);
const currentItem = JSON.stringify(currentEquippedItem);
const newItem = JSON.stringify(checkItem);

if (currentItem && newItem && currentItem !== newItem) {
this.logger.verbose(
`Migrating item ${currentEquippedItem.name} for ${user.user.username}.`,
);
await this.inventoryService.updateEquippedItem(user.user.id, slot, {
...checkItem,
instanceId: currentEquippedItem.instanceId,
Expand Down
6 changes: 6 additions & 0 deletions server/src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import { sample } from 'lodash';
import { IFullUser, IHasAccessToken } from '@interfaces';
import { AggregatorService } from '@modules/aggregator/aggregator.service';
import { ContentService } from '@modules/content/content.service';
import { Logger } from 'nestjs-pino';
import { User } from '../user/user.schema';
import { UserService } from '../user/user.service';

@Injectable()
export class AuthService {
constructor(
private readonly logger: Logger,
private readonly aggregatorService: AggregatorService,
private readonly userService: UserService,
private readonly jwtService: JwtService,
Expand Down Expand Up @@ -59,6 +61,10 @@ export class AuthService {

if (!createdUser) throw new BadRequestException('Failed to create user.');

this.logger.verbose(
`Registered new user: ${createdUser.username}#${createdUser.discriminator}`,
);

return this.aggregatorService.getAllUserInformation(
createdUser._id.toString(),
);
Expand Down
27 changes: 27 additions & 0 deletions server/src/modules/discoveries/discoveries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export class DiscoveriesService {
discoverLocation(discoveries: Discoveries, locationName: string): boolean {
if (discoveries.locations[locationName]) return false;

this.logger.verbose(
`Discovered ${locationName} for ${discoveries.userId}.`,
);
discoveries.locations = { ...discoveries.locations, [locationName]: true };
return true;
}
Expand Down Expand Up @@ -137,6 +140,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Discovered collectible ${itemDefinition.name} for ${userId}.`,
);

return {
discoveries: discoveryPatches,
actions: [
Expand Down Expand Up @@ -183,6 +190,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Discovered equipment ${itemDefinition.name} for ${userId}.`,
);

return {
discoveries: discoveryPatches,
actions: [
Expand Down Expand Up @@ -231,6 +242,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Claimed unique collectible reward for ${userId} (${totalCollectiblesFound} total).`,
);

return {
discoveries: discoveryPatches,
player: playerPatches,
Expand Down Expand Up @@ -276,6 +291,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Claimed total collectible reward for ${userId} (${totalCollectiblesFound} total).`,
);

return {
discoveries: discoveryPatches,
player: playerPatches,
Expand Down Expand Up @@ -321,6 +340,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Claimed unique equipment reward for ${userId} (${totalItemsFound} total).`,
);

return {
discoveries: discoveryPatches,
player: playerPatches,
Expand Down Expand Up @@ -366,6 +389,10 @@ export class DiscoveriesService {
},
);

this.logger.verbose(
`Claimed total equipment reward for ${userId} (${totalItemsFound} total).`,
);

return {
discoveries: discoveryPatches,
player: playerPatches,
Expand Down
39 changes: 39 additions & 0 deletions server/src/modules/fight/fight.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ export class FightService {
this.logger.error(e);
}

this.logger.verbose(
`Created PvE fight for player ${player.userId} (${fight._id})`,
);

return fight;
}

async endFight(fight: Fight, actions: any[] = []): Promise<void> {
this.logger.verbose(`Ending fight ${fight._id}`);

await this.removeFight(fight._id.toHexString());

await Promise.all(
Expand Down Expand Up @@ -284,6 +290,10 @@ export class FightService {
coinDelta = coinsGained;
}

this.logger.verbose(
`Fight ${fight._id} rewarded ${xpDelta} XP and ${coinDelta} coins.`,
);

await Promise.all(
fight.involvedPlayers.map(async (playerId) => {
const player = await this.playerService.getPlayerForUser(playerId);
Expand All @@ -309,6 +319,8 @@ export class FightService {
async setAndTakeNextTurn(fight: Fight): Promise<void> {
if (isFightOver(fight)) return;

this.logger.verbose(`Setting and taking next turn for fight ${fight._id}`);

await this.setNextTurn(fight);
await this.takeNextTurn(fight);
}
Expand Down Expand Up @@ -338,6 +350,10 @@ export class FightService {

if (!currentCharacter) throw new BadRequestException('Character not found');

this.logger.verbose(
`Setting next turn for fight ${fight._id} - ${fight.currentTurn}`,
);

reduceAllCooldownsForCharacter(currentCharacter);

fight.currentTurn = fight.turnOrder[nextTurnIndex];
Expand All @@ -353,11 +369,17 @@ export class FightService {
if (!nextCharacter) throw new BadRequestException('Character not found');

if (isDead(nextCharacter)) {
this.logger.verbose(
`Skipping turn for dead character ${nextCharacter.characterId} in fight ${fight._id}`,
);
await this.setAndTakeNextTurn(fight);
return;
}

if (nextCharacter.monsterId) {
this.logger.verbose(
`Taking next turn for monster ${nextCharacter.monsterId} in fight ${fight._id}`,
);
await this.aiTakeAction(fight, fight.currentTurn);
}
}
Expand All @@ -379,6 +401,10 @@ export class FightService {
const character = getCharacterFromFightForUserId(fight, userId);
if (!character) throw new BadRequestException('Character not found');

this.logger.verbose(
`Taking action ${actionId} for character ${character.characterId} in fight ${fight._id}`,
);

await this.processActionIntoAbility(fight, character, action, targetParams);
await this.setAndTakeNextTurn(fight);
}
Expand Down Expand Up @@ -439,6 +465,9 @@ export class FightService {
doDamageToTargetForAbility(fight, character, target, damage);
});

this.logger.verbose(
`Handling ability ${action.itemId} for character ${character.characterId} in fight ${fight._id}`,
);
await this.saveAndUpdateFight(fight);
}

Expand Down Expand Up @@ -510,6 +539,10 @@ export class FightService {
finalizedTargetParams = { tile: chosenTile };
}

this.logger.verbose(
`AI taking action ${abilityRef.itemId} for character ${characterRef.characterId} in fight ${fight._id}`,
);

await this.processActionIntoAbility(
fight,
characterRef,
Expand Down Expand Up @@ -547,6 +580,10 @@ export class FightService {
if (side !== targetAffiliation)
throw new BadRequestException('Cannot move to enemy side');

this.logger.verbose(
`Moving character ${character.characterId} to tile ${newTile.x},${newTile.y} in fight ${fight._id}`,
);

moveCharacterBetweenTiles(character.characterId, tile, newTile);

await this.saveAndUpdateFight(fight);
Expand All @@ -555,6 +592,8 @@ export class FightService {
async flee(fight: Fight): Promise<void> {
fight.defenders = [];

this.logger.verbose(`Fleeing fight ${fight._id}`);

await this.endFight(fight, [
{
type: 'Notify',
Expand Down
34 changes: 34 additions & 0 deletions server/src/modules/gameplay/gameplay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { getPatchesAfterPropChanges } from '@utils/patches';
import { sample } from 'lodash';
import { Logger } from 'nestjs-pino';

type ExploreResult =
| 'Nothing'
Expand All @@ -45,6 +46,7 @@ const createFilledArray = (length: number, fill: ExploreResult) =>
@Injectable()
export class GameplayService {
constructor(
private readonly logger: Logger,
private readonly playerService: PlayerService,
private readonly discoveriesService: DiscoveriesService,
private readonly statsService: StatsService,
Expand Down Expand Up @@ -243,6 +245,10 @@ export class GameplayService {
},
);

this.logger.verbose(
`Explore result: ${exploreResult} for player ${userId} in ${player.location.current}.`,
);

return { player: playerPatches, discoveries: discoveriesPatches };
}

Expand Down Expand Up @@ -294,6 +300,8 @@ export class GameplayService {
},
);

this.logger.verbose(`Player ${userId} is walking to ${locationName}.`);

return { player: playerPatches };
}

Expand Down Expand Up @@ -350,6 +358,8 @@ export class GameplayService {
},
);

this.logger.verbose(`Player ${userId} is traveling to ${locationName}.`);

return { player: playerPatches };
}

Expand Down Expand Up @@ -379,6 +389,10 @@ export class GameplayService {
if (!notificationAction)
throw new ForbiddenException('Notification has no actions');

this.logger.verbose(
`Player ${userId} is waving to ${notificationAction.urlData.targetUserId} from notification.`,
);

return this.waveToPlayer(userId, player, notificationAction);
}

Expand Down Expand Up @@ -464,6 +478,8 @@ export class GameplayService {

this.analyticsService.sendDesignEvent(userId, `Gameplay:Wave`);

this.logger.verbose(`Player ${userId} waved to ${targetUserId}.`);

return { player: playerPatches };
}

Expand Down Expand Up @@ -491,6 +507,10 @@ export class GameplayService {
},
);

this.logger.verbose(
`Player ${userId} took item ${player.action?.actionData.item.itemId}.`,
);

const playerPatches = await getPatchesAfterPropChanges<Player>(
player,
async (playerRef) => {
Expand Down Expand Up @@ -572,6 +592,8 @@ export class GameplayService {
},
);

this.logger.verbose(`Player ${userId} sold item ${item.itemId}.`);

return {
player: playerPatches,
actions: [
Expand Down Expand Up @@ -661,6 +683,8 @@ export class GameplayService {
},
);

this.logger.verbose(`Player ${userId} equipped item ${item.itemId}.`);

return {
inventory: inventoryPatches,
actions: [
Expand Down Expand Up @@ -695,6 +719,8 @@ export class GameplayService {
[equipmentSlot]: undefined,
};

this.logger.verbose(`Player ${userId} unequipped item ${item.itemId}.`);

return {};
}

Expand Down Expand Up @@ -769,6 +795,10 @@ export class GameplayService {
},
);

this.logger.verbose(
`Player ${userId} started crafting item ${item.itemId}.`,
);

return { inventory: inventoryPatches, crafting: craftingPatches };
}

Expand Down Expand Up @@ -842,6 +872,10 @@ export class GameplayService {
'gameplay/item/craft/take',
);

this.logger.verbose(
`Player ${userId} collected crafted item ${item.itemId}.`,
);

return {
inventory: inventoryPatches,
crafting: craftingPatches,
Expand Down
Loading

0 comments on commit c628d71

Please sign in to comment.