Skip to content

Commit

Permalink
fix(events): Fix bug with logging property changes related to the Eve…
Browse files Browse the repository at this point in the history
…ntRecord PK refactor

re #104
  • Loading branch information
jcowman2 committed Jul 21, 2019
1 parent 22f9eae commit 1d312da
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/agents/impl/agent-manager-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class AgentManagerImpl implements AgentManager {
// A change should be replaced if it happened during the same event,
// or if the change happened after any potential recycling
const shouldReplaceSingle = (pc: PropertyChange) =>
pc.eventId === event.id ||
pc.eventId.index > getUntrackedEventPK().index;
pc.eventId.equals(event.id) ||
pc.eventId.index() > getUntrackedEventPK().index();

// If the property history has two changes, update the more recent one.
// If property history has only change, check when the change happened.
Expand Down
18 changes: 18 additions & 0 deletions src/events/impl/event-keys.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
/*
* Contains the `EventRecord` primary key system.
*
* Copyright (c) Joseph R Cowman
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { buildPKProvider } from "../../common";
import { EventRecord } from "../event-record";

/** The set of reserved `EventRecord` primary keys. */
export const EVENT_RESERVED_KEYS = {
/**
* The key for all changes made in the default, untracked `EventRecord`.
*
* **No reserved event keys should have a greater value than this!**
*/
UNTRACKED: 100
};

/**
* Reference this `PKProvider` for `EventRecord` PKs so we don't have
* to do additional instantiations.
*/
const INTERNAL_EVENT_PK_PROVIDER = buildPKProvider<EventRecord>(
EVENT_RESERVED_KEYS
);

/** Shorthand for the `EVENT_RESERVED_KEYS.UNTRACKED` primary key. */
export const getUntrackedEventPK = () =>
INTERNAL_EVENT_PK_PROVIDER.reserved(EVENT_RESERVED_KEYS.UNTRACKED);
4 changes: 2 additions & 2 deletions src/state/game-instance-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/

import { InstanceAgentsInternal } from "../agents";
import { FK } from "../common";
import { GameOptions, InstanceOptionsInternal } from "../config";
import { InstanceEventsInternal, EventRecord } from "../events";
import { EventRecord, InstanceEventsInternal } from "../events";
import { InstanceOutputInternal } from "../output";
import { InstanceRandomInternal } from "../random";
import { GameInstance } from "./game-instance";
import { FK } from "../common";

/**
* Internal interface for `GameInstance`.
Expand Down
8 changes: 4 additions & 4 deletions src/state/impl/game-instance-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../../agents";
import { isAgentArrayReference } from "../../agents/agent-array-reference";
import { isAgentReference } from "../../agents/agent-reference";
import { FK } from "../../common";
import {
buildInstanceOptions,
GameOptions,
Expand All @@ -24,17 +25,16 @@ import {
import { RegalError } from "../../error";
import {
buildInstanceEvents,
EventRecord,
getUntrackedEventPK,
InstanceEventsInternal,
on,
TrackedEvent,
getUntrackedEventPK,
EventRecord
TrackedEvent
} from "../../events";
import { buildInstanceOutput, InstanceOutputInternal } from "../../output";
import { buildInstanceRandom, InstanceRandomInternal } from "../../random";
import { ContextManager } from "../context-manager";
import { GameInstanceInternal } from "../game-instance-internal";
import { FK } from "../../common";

/**
* Constructs a new `GameInstance` with optional `GameOption` overrides.
Expand Down
21 changes: 11 additions & 10 deletions test/unit/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ describe("Agents", function() {
})(myGame);

const [pk0, pk1] = aPKs(1);
const epk0 = getUntrackedEventPK();

const game2 = myGame.recycle();

Expand All @@ -2181,8 +2182,8 @@ describe("Agents", function() {
game: game2,
dummy: [
{
eventId: 0,
eventName: "DEFAULT",
eventId: epk0,
eventName: DEFAULT_EVENT_NAME,
init: undefined,
final: {
refId: pk1
Expand All @@ -2192,8 +2193,8 @@ describe("Agents", function() {
],
foo: [
{
eventId: 0,
eventName: "DEFAULT",
eventId: epk0,
eventName: DEFAULT_EVENT_NAME,
init: undefined,
final: true,
op: PropertyOperation.ADDED
Expand All @@ -2205,8 +2206,8 @@ describe("Agents", function() {
game: game2,
name: [
{
eventId: 0,
eventName: "DEFAULT",
eventId: epk0,
eventName: DEFAULT_EVENT_NAME,
init: "D1",
final: "Jimmy",
op: PropertyOperation.MODIFIED
Expand Down Expand Up @@ -2295,8 +2296,8 @@ describe("Agents", function() {
game: game2,
dummy: [
{
eventId: 0,
eventName: "DEFAULT",
eventId: getUntrackedEventPK(),
eventName: DEFAULT_EVENT_NAME,
init: undefined,
final: {
refId: pk1
Expand All @@ -2310,8 +2311,8 @@ describe("Agents", function() {
game: game2,
name: [
{
eventId: 0,
eventName: "DEFAULT",
eventId: getUntrackedEventPK(),
eventName: DEFAULT_EVENT_NAME,
init: "D1",
final: undefined,
op: PropertyOperation.DELETED
Expand Down
14 changes: 8 additions & 6 deletions test/unit/api-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ describe("API Hooks", function() {
const myGame = buildGameInstance();
HookManager.playerCommandHook("Test Command")(myGame);

const [_epk0, epk1, epk2, epk3] = ePKs(3);

expect(myGame.events.history).to.deep.equal([
{
id: 3,
id: epk3,
name: "DOUBLE INPUT",
changes: [
{
Expand All @@ -166,10 +168,10 @@ describe("API Hooks", function() {
final: "Test Command Test Command"
}
],
causedBy: 1
causedBy: epk1
},
{
id: 2,
id: epk2,
name: "SET INPUT",
changes: [
{
Expand All @@ -180,12 +182,12 @@ describe("API Hooks", function() {
final: "Test Command"
}
],
causedBy: 1
causedBy: epk1
},
{
id: 1,
id: epk1,
name: "INPUT",
caused: [2, 3]
caused: [epk2, epk3]
}
]);
});
Expand Down
Loading

0 comments on commit 1d312da

Please sign in to comment.