Skip to content

Commit

Permalink
Change event to TURN_START and update merc role and ability speed acc…
Browse files Browse the repository at this point in the history
…ording to tags
  • Loading branch information
piedpiper358 committed Nov 9, 2022
1 parent 2a8c870 commit 7f8b3b9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
23 changes: 22 additions & 1 deletion core/src/js/models/mercenaries/mercenaries-battle-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class MercenariesBattleState {

readonly playerTeam: MercenariesBattleTeam = new MercenariesBattleTeam();
readonly opponentTeam: MercenariesBattleTeam = new MercenariesBattleTeam();
readonly actionQueue: readonly MercenariesAction[] = [];
readonly actionQueue: MercenariesAction[] = [];

// A cache that is populated on game start
readonly mercenariesFromMemory: MemoryMercenariesInfo;
Expand Down Expand Up @@ -55,6 +55,27 @@ export class MercenariesBattleTeam {
);
return this.update({ mercenaries: newMercenaries });
}

public updateAbility(
entityId: number,
base: Partial<NonFunctionProperties<BattleAbility>>,
): MercenariesBattleTeam {
const newMercenaries: readonly BattleMercenary[] =
(this.mercenaries ?? []).map((merc) => (
merc.update(
BattleMercenary.create(
{
abilities: (merc.abilities ?? []).map((ability) => (
ability.entityId === entityId
? ability.update(BattleAbility.create(base))
: ability
))
}
)
)
))
return this.update({ mercenaries: newMercenaries });
}
}

export class BattleMercenary {
Expand Down
4 changes: 4 additions & 0 deletions core/src/js/services/game-events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,10 @@ export class GameEvents {
// Legacy, to avoid regenerating all the tests
turnNumber: gameEvent.Value.Turn || gameEvent.Value,
timestamp: gameEvent.Value.Timestamp,
playerBoard: gameEvent.Value?.GameState?.Player?.Board,
playerLettuceAbilities: gameEvent.Value?.GameState?.Player?.LettuceAbilities,
opponentBoard: gameEvent.Value?.GameState?.Opponent?.Board,
opponentLettuceAbilities: gameEvent.Value?.GameState?.Opponent?.LettuceAbilities,
},
} as GameEvent),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class MercenariesSpeedParser implements MercenariesParser {
entityId,
cardId,
BattleAbility.create({
cardId: cardId,
speed: event.additionalData.cost,
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
import { GameEvent } from '../../../models/game-event';
import { MercenariesBattleState } from '../../../models/mercenaries/mercenaries-battle-state';
import { MercenariesBattleState, MercenariesBattleTeam } from '../../../models/mercenaries/mercenaries-battle-state';
import { getHeroRole } from '../mercenaries-utils';
import { MercenariesParser } from './_mercenaries-parser';
import { GameTag, TagRole } from '@firestone-hs/reference-data';

export class MercenariesTurnStartParser implements MercenariesParser {
public eventType = () => GameEvent.TURN_START;

public applies = (battleState: MercenariesBattleState) => !!battleState;

public async parse(battleState: MercenariesBattleState, event: GameEvent): Promise<MercenariesBattleState> {
public async parse(
battleState: MercenariesBattleState,
event: GameEvent,
): Promise<MercenariesBattleState> {

if(event.additionalData?.playerBoard && event.additionalData?.playerLettuceAbilities){
var newPlayerTeam: MercenariesBattleTeam = battleState.playerTeam;

for (var merc of event.additionalData?.playerBoard) {
newPlayerTeam = newPlayerTeam.updateMercenary(merc.entityId,
{
cardId: merc.cardId,
role: getHeroRole(TagRole[merc?.tags?.find((tag) => tag.Name==GameTag.LETTUCE_ROLE)?.Value]),
});
}

for (var ability of event.additionalData?.playerLettuceAbilities) {
newPlayerTeam = newPlayerTeam.updateAbility(ability.entityId,
{
cardId: ability.cardId,
speed: ability.tags?.find((tag) => tag.Name==GameTag.COST)?.Value,
cooldownLeft: ability.tags?.find((tag) => tag.Name==GameTag.LETTUCE_CURRENT_COOLDOWN)?.Value,
}
)
}
}
else {
console.warn('[merc-game-state-update] player info not provided', event);
}

if(event.additionalData?.opponentBoard && event.additionalData?.opponentLettuceAbilities){
var newOpponentTeam: MercenariesBattleTeam = battleState.opponentTeam;

for (var merc of event.additionalData?.opponentBoard) {
newOpponentTeam = newOpponentTeam.updateMercenary(merc.entityId,
{
cardId: merc.cardId,
role: getHeroRole(TagRole[merc?.tags?.find((tag) => tag.Name==GameTag.LETTUCE_ROLE)?.Value]),
});
}
for (var ability of event.additionalData?.opponentLettuceAbilities) {
newOpponentTeam = newOpponentTeam.updateAbility(ability.entityId,
{
cardId: ability.cardId,
speed: ability.tags?.find((tag) => tag.Name==GameTag.COST)?.Value,
cooldownLeft: ability.tags?.find((tag) => tag.Name==GameTag.LETTUCE_CURRENT_COOLDOWN)?.Value,
}
)
}
}
else {
console.warn('[merc-game-state-update] opponent info not provided', event);
}

return battleState.update({
playerTeam: newPlayerTeam,
opponentTeam: newOpponentTeam,
currentTurn: event.additionalData.turnNumber,
actionQueue: [],
});
} as MercenariesBattleState);
}
}

0 comments on commit 7f8b3b9

Please sign in to comment.