jdoleary / Spellmasons

https://store.steampowered.com/app/1618380/Spellmasons/
Other
0 stars 1 forks source link

Refactor Events footgun #826

Closed jdoleary closed 1 week ago

jdoleary commented 1 week ago

Events must be deep cloned when a unit is serializes so that the serialized unit doesn't share the same object.

I could do something like:


export function cloneEvents(unit: IUnit): IUnit {
  const {
    onDealDamageEvents,
    onTakeDamageEvents,
    onDeathEvents, onAgroEvents, onTurnStartEvents, onTurnEndEvents, onDrawSelectedEvents, onTeleportEvents, ...rest } = unit;
  return {
    ...rest,
    // Deep copy array so that serialized units don't share the object
    onDealDamageEvents: [...onDealDamageEvents],
    onTakeDamageEvents: [...onTakeDamageEvents],
    onDeathEvents: [...onDeathEvents],
    onAgroEvents: [...onAgroEvents],
    onTurnStartEvents: [...onTurnStartEvents],
    onTurnEndEvents: [...onTurnEndEvents],
    onDrawSelectedEvents: [...onDrawSelectedEvents],
    onTeleportEvents: [...onTeleportEvents],
  }

}

and then write a test but maybe I ought to just JSON.stringify the whole ...rest object?

jdoleary commented 1 week ago

Maybe refactor events arrays to just be one array?

jdoleary commented 1 week ago

Done in #828