Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/IHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IHandler {
canHandle(): boolean | void;
handle(): void;
}
15 changes: 15 additions & 0 deletions src/common/base.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { IHandler } from './IHandler';

export abstract class BaseHandler<T> implements IHandler {
constructor(
protected analyzer: T,
private handler?: () => void
) {}

abstract canHandle(): boolean | void;
handle() {
if (this.handler) {
this.handler();
}
}
}
17 changes: 17 additions & 0 deletions src/common/handlerChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IHandler } from './IHandler';

export class HandlerChain {
private handlers: IHandler[] = [];
addHandlers(...handlers: IHandler[]): void {
this.handlers.push(...handlers);
}

process(): unknown | Promise<unknown> {
for (const handler of this.handlers) {
if (handler.canHandle()) {
return handler.handle();
}
}
console.log('No handler found for this data');
}
}
47 changes: 47 additions & 0 deletions src/field/FieldAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GamePayload } from 'src/game/game.repository';
import { PlayerPayload } from 'src/player/player.repository';
import { PlayerService } from 'src/player/player.service';
import { FieldDocument } from 'src/schema/Field.schema';

export class FieldAnalyzer {
readonly currentPlayer: Partial<PlayerPayload>;

constructor(
readonly field: FieldDocument,
readonly game: Partial<GamePayload>,
private playerService: PlayerService
) {
this.currentPlayer = this.playerService.findPlayerWithTurn(game);
}
isOwnedByCurrentUser(): boolean {
return (
this.field.ownedBy === this.currentPlayer.userId && this.field.price > 0
);
}
isOwnedByOtherAndNotPledged(): boolean {
return (
this.field.ownedBy &&
this.field.ownedBy !== this.currentPlayer.userId &&
!this.field.isPledged
);
}
isNotOwned(): boolean {
return !this.field.ownedBy && this.field.price > 0;
}
isAffordableForSomeone(): boolean {
return this.game.players.some(
(player) =>
player.userId !== this.currentPlayer.userId &&
player.money > this.field.price
);
}
isSpecialField(): boolean {
return !this.field.price;
}
isSkipable(): boolean {
return (
(this.field?.specialField && !this.field.secret && !this.field.toPay) ||
this.field.isPledged
);
}
}
Loading
Loading