Skip to content

Commit

Permalink
feat(common): Adds the peek() method to PKProvider
Browse files Browse the repository at this point in the history
Also finishes fixing unit tests for the Agent PK refactor and adds documentation

re #104
  • Loading branch information
jcowman2 committed Jun 28, 2019
1 parent bba1ea0 commit 5c00d18
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 112 deletions.
13 changes: 12 additions & 1 deletion src/agents/impl/agent-keys.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
/*
* Contains the `Agent` primary key system.
*
* Copyright (c) Joseph R Cowman
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { buildPKProvider } from "../../common";

/** The set of reserved `Agent` primary keys. */
export const AGENT_RESERVED_KEYS = {
/** Every `GameInstance` has this key. */
GAME_INSTANCE: 100,
/** Before an `Agent` is activated, it has this key. */
INACTIVE: 99
};

export let STATIC_AGENT_PK_PROVIDER = buildPKProvider(AGENT_RESERVED_KEYS);
/** The `Agent` `PKProvider` for static agents. */
export const STATIC_AGENT_PK_PROVIDER = buildPKProvider(AGENT_RESERVED_KEYS);
16 changes: 13 additions & 3 deletions src/agents/impl/agent-utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
/*
* Contains various utilities for the Agents component.
*
* Copyright (c) Joseph R Cowman
* Licensed under MIT License (see https://github.com/regal/regal)
*/

import { PK } from "../../common";
import { Agent } from "../agent";
import { AGENT_RESERVED_KEYS, STATIC_AGENT_PK_PROVIDER } from "./agent-keys";

/** Shorthand for the `AGENT_RESERVED_KEYS.INACTIVE` primary key. */
export const getInactiveAgentPK = () =>
STATIC_AGENT_PK_PROVIDER.reserved(AGENT_RESERVED_KEYS.INACTIVE);

export const isAgentActive = (id: PK<Agent>) =>
!getInactiveAgentPK().equals(id);

/** Shorthand for the `AGENT_RESERVED_KEYS.GAME_INSTANCE` primary key. */
export const getGameInstancePK = () =>
STATIC_AGENT_PK_PROVIDER.reserved(AGENT_RESERVED_KEYS.GAME_INSTANCE);

/** Determines whether an `Agent` has been activated, given its primary key. */
export const isAgentActive = (id: PK<Agent>) =>
!getInactiveAgentPK().equals(id);
1 change: 1 addition & 0 deletions src/agents/impl/instance-agents-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const buildInstanceAgents = (

/** Implementation of `InstanceAgentsInternal`. */
class InstanceAgentsImpl implements InstanceAgentsInternal {
/** The internal `Agent` `PKProvider`. */
private _pkProvider: PKProvider<Agent>;

constructor(
Expand Down
4 changes: 4 additions & 0 deletions src/common/impl/pk-provider-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,8 @@ export class PKProviderImpl<PKClass> implements PKProvider<PKClass> {

this.lastPK = sortedPKs[sortedPKs.length - 1];
}

public peek(): PK<PKClass> {
return this.lastPK.plus(1);
}
}
18 changes: 18 additions & 0 deletions src/common/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export interface PK<T> {
*/
value(): string;

/**
* Returns the placement of this key in the list of all keys.
* For example, an index of 2 means this was the second key generated.
*
* This includes reserved keys; if a set of reserved keys was used
* to generate this key's `PKProvider`, the entry with the lowest value
* will have an index of 0.
*/
index(): number;
}

Expand Down Expand Up @@ -82,7 +90,17 @@ export interface PKProvider<T> {
*/
reserved(key: number): PK<T>;

/**
* Resets the `PKProvider`'s internal `PK` generator back to its start value.
* Any reserved keys used to generate this `PKProvider` will remain.
*/
reset(): void;

/**
* Provides the `PK` that would be generated by `PKProvider.next()` without
* incrementing the internal key generator.
*/
peek(): PK<T>;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/common/type-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Custom TypeScript utilities.
*
* Copyright (c) Joseph R Cowman
* Licensed under MIT License (see https://github.com/regal/regal)
*/

/**
* Makes readonly properties in an object mutable and requried.
* Only use this when you need it; it's like admin access.
Expand Down
20 changes: 11 additions & 9 deletions test/unit/agents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2636,18 +2636,20 @@ describe("Agents", function() {
});

describe("StaticAgentRegistry", function() {
// it("StaticAgentRegistry.getNextAvailableId increments by 1 for each static agent", function() {
// expect(StaticAgentRegistry.getNextAvailableId()).to.equal(1);
it("STATIC_AGENT_PK_PROVIDER increments by 1 for each static agent added to the static registry", function() {
const [_pk0, pk1, pk2, pk3, _pk4, pk5] = pks(5);

// const D = new Dummy("D1", 10);
// expect(StaticAgentRegistry.getNextAvailableId()).to.equal(2);
expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk1)).to.be.true;

// const P = new Parent(D);
// expect(StaticAgentRegistry.getNextAvailableId()).to.equal(3);
const D = new Dummy("D1", 10);
expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk2)).to.be.true;

const P = new Parent(D);
expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk3)).to.be.true;

// const P2 = new Parent(new Dummy("D2", 5));
// expect(StaticAgentRegistry.getNextAvailableId()).to.equal(5);
// });
const P2 = new Parent(new Dummy("D2", 5));
expect(STATIC_AGENT_PK_PROVIDER.peek().equals(pk5)).to.be.true;
});

it("StaticAgentRegistry.hasAgentProperty works properly", function() {
const D = new Dummy("D1", 10);
Expand Down
20 changes: 10 additions & 10 deletions test/unit/api-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "mocha";

import { on, noop } from "../../src/events";
import { metadataWithOptions, getDemoMetadata } from "../test-utils";
import { PropertyOperation } from "../../src/agents";
import { PropertyOperation, getGameInstancePK } from "../../src/agents";
import { RegalError } from "../../src/error";
import {
Game,
Expand Down Expand Up @@ -59,7 +59,7 @@ describe("API Hooks", function() {
name: "INPUT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "input",
init: undefined,
Expand Down Expand Up @@ -101,7 +101,7 @@ describe("API Hooks", function() {
name: "SET INPUT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "input",
init: undefined,
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("API Hooks", function() {
name: "DOUBLE INPUT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.MODIFIED,
property: "input",
init: "Test Command",
Expand All @@ -171,7 +171,7 @@ describe("API Hooks", function() {
name: "SET INPUT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "input",
init: undefined,
Expand Down Expand Up @@ -230,7 +230,7 @@ describe("API Hooks", function() {
name: "START",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "init",
init: undefined,
Expand Down Expand Up @@ -270,7 +270,7 @@ describe("API Hooks", function() {
name: "SET INIT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "init",
init: undefined,
Expand Down Expand Up @@ -339,7 +339,7 @@ describe("API Hooks", function() {
name: "APPEND FOO",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.MODIFIED,
property: "foo",
init: "One",
Expand All @@ -353,7 +353,7 @@ describe("API Hooks", function() {
name: "SET FOO",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "foo",
init: undefined,
Expand All @@ -367,7 +367,7 @@ describe("API Hooks", function() {
name: "SET INIT",
changes: [
{
agentId: 0,
agentId: getGameInstancePK(),
op: PropertyOperation.ADDED,
property: "init",
init: undefined,
Expand Down

0 comments on commit 5c00d18

Please sign in to comment.