Skip to content
This repository was archived by the owner on Oct 24, 2021. It is now read-only.

Commit aa0eb88

Browse files
authored
fix: prevent crash with Questing Adventurer
1 parent e1744b6 commit aa0eb88

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/GameState.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,21 @@ export class GameState {
222222
* @param entity
223223
*/
224224
resolveEntity(entity: Pick<CardEntity, 'cardName' | 'entityId' | 'cardId'> & Partial<CardEntity>) {
225-
this.#entities[entity.entityId] = merge(this.#entities[entity.entityId], entity);
225+
const existing = this.#entities[entity.entityId];
226+
this.#entities[entity.entityId] = merge({
227+
type: 'card',
228+
tags: {},
229+
player: 'bottom'
230+
}, existing, entity);
226231

227-
// A better algorithm requires caching to a private property
228232
const {cardName, entityId, cardId} = entity;
229233
const newProps = {entityId, cardName, cardId};
230234

231235
if (isEmpty(cardName) || !this.#missingEntityIds.has(entityId)) {
232236
return;
233237
}
234238

239+
// Update entities for each log entry
235240
for (const entry of this.matchLog) {
236241
if (isEmpty(entry.source.cardName) && entry.source.entityId === entityId) {
237242
entry.source = {...entry.source, ...newProps};

src/line-parsers/block-parser.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class EntityCollection {
1717
private readonly gameState: GameState,
1818
private readonly entities: {[key: number]: CardEntity}) {}
1919

20+
values() {
21+
return Object.values(this.entities);
22+
}
23+
2024
get(entityId: number) {
2125
return this.entities[entityId] ??
2226
this.gameState.getEntity(entityId) ??
@@ -148,7 +152,16 @@ export class BlockParser extends LineParser {
148152
// Read GameState blocks. These are used to build the Match Log.
149153
const block = this.reader.readLine(line, gameState);
150154
if (block) {
151-
this._handleMatchLog(emitter, gameState, block);
155+
try {
156+
this._handleMatchLog(emitter, gameState, block);
157+
} catch (err) {
158+
const source = isCard(block.entity) && block.entity.cardName;
159+
const target = isCard(block.target) && block.target.cardName;
160+
console.error(`\nFailed to process block: Type=${block.blockType} Source=${source} Target=${target}`);
161+
console.trace(err.toString());
162+
console.error();
163+
}
164+
152165
return true;
153166
}
154167

@@ -378,7 +391,7 @@ export class BlockParser extends LineParser {
378391
}
379392

380393
// Merge these entities into the match log (before, for perf)
381-
for (const entity of Object.values(entities)) {
394+
for (const entity of entities.values()) {
382395
gameState.resolveEntity(entity);
383396
}
384397

0 commit comments

Comments
 (0)