def-game 1.0.7 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,35 +1,3 @@
1
- export interface DefaultWsEventMap {
2
- }
3
- export interface DefaultGameLogicState {
4
- }
5
- export type WsEvent<T extends keyof M, M extends Record<string, any>> = {
6
- type: T;
7
- payload: M[T];
8
- };
9
- export type Task<L extends DefaultGameLogicState> = {
10
- type: string;
11
- execute: (gs: L) => TaskResult<L>;
12
- };
13
- export interface TaskResult<L extends DefaultGameLogicState> {
14
- type: string;
15
- state: L;
16
- }
17
- export type GameState<L extends DefaultGameLogicState> = {
18
- gameLogicState: L;
19
- taskQueue: Task<L>[];
20
- };
21
- export interface GameRule<L extends DefaultGameLogicState, A extends Record<string, any>> {
22
- initialGameLogicState: () => L;
23
- divider<T extends keyof A>(event: WsEvent<T, A>): Task<L>[];
24
- prioritizeTasks(newTasks: Task<L>[], gameState: GameState<L>): GameState<L>;
25
- doTask(state: GameState<L>): GameState<L>;
26
- }
27
- export declare class GameEngine<S extends DefaultGameLogicState, A extends Record<string, any>> {
28
- private rules;
29
- private storage;
30
- constructor(rules: GameRule<S, A>);
31
- executeAction<T extends keyof A>(action: WsEvent<T, A>): GameState<S>;
32
- getState(): GameState<S>;
33
- setState(state: GameState<S>): void;
34
- }
1
+ export * from './types';
2
+ export * from './simulator';
35
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,iBAAiB;CACjC;AAED,MAAM,WAAW,qBAAqB;CACrC;AAED,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI;IACpE,IAAI,EAAE,CAAC,CAAC;IACR,OAAO,EAAE,CAAC,CAAC,CAAC,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,SAAS,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC;IAerE,QAAQ,IAAI,SAAS,CAAC,CAAC,CAAC;IAIxB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;CAGtC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -1,48 +1,18 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GameEngine = void 0;
4
- ;
5
- // This is a simulator
6
- class GameEngine {
7
- constructor(rules) {
8
- this.rules = rules;
9
- this.storage = new InMemoryGameStateStorage();
10
- // Initialize the game state
11
- const gameState = {
12
- gameLogicState: this.rules.initialGameLogicState(),
13
- taskQueue: []
14
- };
15
- this.storage.saveGameState(gameState);
16
- }
17
- executeAction(action) {
18
- const tasks = this.rules.divider(action);
19
- const state = this.storage.loadGameState();
20
- // ------ Game Logic ------
21
- const updatedQueueState = this.rules.prioritizeTasks(tasks, state);
22
- const newState = this.rules.doTask(updatedQueueState);
23
- // ------------------------
24
- this.storage.saveGameState(newState);
25
- return newState; // this means that this game state is broadcasted to all players
26
- }
27
- getState() {
28
- return this.storage.loadGameState();
29
- }
30
- setState(state) {
31
- this.storage.saveGameState(state);
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
32
7
  }
33
- }
34
- exports.GameEngine = GameEngine;
35
- class InMemoryGameStateStorage {
36
- constructor() {
37
- this.state = null;
38
- }
39
- loadGameState() {
40
- if (this.state === null) {
41
- throw new Error('Game state is not initialized');
42
- }
43
- return this.state;
44
- }
45
- saveGameState(state) {
46
- this.state = state;
47
- }
48
- }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./simulator"), exports);
@@ -0,0 +1,10 @@
1
+ import { DefaultGameLogicState, GameRule, GameState, ToServer } from "./types";
2
+ export declare class GameEngine<S extends DefaultGameLogicState> {
3
+ private rules;
4
+ private storage;
5
+ constructor(rules: GameRule<S>);
6
+ executeAction(action: ToServer): GameState<S>;
7
+ getState(): GameState<S>;
8
+ setState(state: GameState<S>): void;
9
+ }
10
+ //# sourceMappingURL=simulator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simulator.d.ts","sourceRoot":"","sources":["../src/simulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAG/E,qBAAa,UAAU,CAAC,CAAC,SAAS,qBAAqB;IACnD,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,OAAO,CAAsB;gBAEzB,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;IAY9B,aAAa,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC;IAe7C,QAAQ,IAAI,SAAS,CAAC,CAAC,CAAC;IAIxB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;CAGtC"}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GameEngine = void 0;
4
+ // This is a simulator
5
+ class GameEngine {
6
+ constructor(rules) {
7
+ this.rules = rules;
8
+ this.storage = new InMemoryGameStateStorage();
9
+ // Initialize the game state
10
+ const gameState = {
11
+ gameLogicState: this.rules.initialGameLogicState(),
12
+ taskQueue: []
13
+ };
14
+ this.storage.saveGameState(gameState);
15
+ }
16
+ executeAction(action) {
17
+ const tasks = this.rules.divider(action);
18
+ const state = this.storage.loadGameState();
19
+ // ------ Game Logic ------
20
+ const updatedQueueState = this.rules.prioritizeTasks(tasks, state);
21
+ const newState = this.rules.doTask(updatedQueueState);
22
+ // ------------------------
23
+ this.storage.saveGameState(newState);
24
+ return newState; // this means that this game state is broadcasted to all players
25
+ }
26
+ getState() {
27
+ return this.storage.loadGameState();
28
+ }
29
+ setState(state) {
30
+ this.storage.saveGameState(state);
31
+ }
32
+ }
33
+ exports.GameEngine = GameEngine;
34
+ class InMemoryGameStateStorage {
35
+ constructor() {
36
+ this.state = null;
37
+ }
38
+ loadGameState() {
39
+ if (this.state === null) {
40
+ throw new Error('Game state is not initialized');
41
+ }
42
+ return this.state;
43
+ }
44
+ saveGameState(state) {
45
+ this.state = state;
46
+ }
47
+ }
@@ -0,0 +1,55 @@
1
+ export type FromServerMap = Record<string, any>;
2
+ export type ToServerMap = Record<string, any>;
3
+ export type InteractionMapType = {
4
+ [key in InteractionKey]: {
5
+ fromServer: FromServerMap[key];
6
+ toServer: ToServerMap[key];
7
+ };
8
+ };
9
+ export interface DefaultGameLogicState {
10
+ forClient: ForClient;
11
+ }
12
+ export type FromServerDataType = keyof FromServerMap;
13
+ export type ToServerDataType = keyof ToServerMap;
14
+ export type FromServerData = {
15
+ type: FromServerDataType;
16
+ payload: FromServerMap[FromServerDataType];
17
+ };
18
+ export type ToServerData = {
19
+ type: ToServerDataType;
20
+ payload: ToServerMap[ToServerDataType];
21
+ };
22
+ export type InteractionKey = Extract<keyof FromServerMap, keyof ToServerMap>;
23
+ export type InteractionFromServerData = {
24
+ type: InteractionKey;
25
+ payload: InteractionMapType[InteractionKey]["fromServer"];
26
+ };
27
+ export type InteractionToServerData = {
28
+ type: InteractionKey;
29
+ payload: InteractionMapType[InteractionKey]["toServer"];
30
+ };
31
+ export type FromServer = FromServerData | InteractionFromServerData;
32
+ export type ToServer = ToServerData | InteractionToServerData;
33
+ export type PlayerId = string;
34
+ export type ForClient = {
35
+ [key: PlayerId]: FromServer;
36
+ };
37
+ export type Task<L extends DefaultGameLogicState> = {
38
+ type: string;
39
+ execute: (gs: L) => TaskResult<L>;
40
+ };
41
+ export interface TaskResult<L extends DefaultGameLogicState> {
42
+ type: string;
43
+ state: L;
44
+ }
45
+ export type GameState<L extends DefaultGameLogicState> = {
46
+ gameLogicState: L;
47
+ taskQueue: Task<L>[];
48
+ };
49
+ export interface GameRule<L extends DefaultGameLogicState> {
50
+ initialGameLogicState: () => L;
51
+ divider(event: ToServer): Task<L>[];
52
+ prioritizeTasks(newTasks: Task<L>[], gameState: GameState<L>): GameState<L>;
53
+ doTask(state: GameState<L>): GameState<L>;
54
+ }
55
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAChD,MAAM,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC9C,MAAM,MAAM,kBAAkB,GAAG;KAC5B,GAAG,IAAI,cAAc,GAAG;QACrB,UAAU,EAAE,aAAa,CAAC,GAAG,CAAC,CAAC;QAC/B,QAAQ,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;KAC9B;CACJ,CAAA;AAED,MAAM,WAAW,qBAAqB;IAClC,SAAS,EAAE,SAAS,CAAC;CACxB;AAED,MAAM,MAAM,kBAAkB,GAAG,MAAM,aAAa,CAAC;AACrD,MAAM,MA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CAAC,SAAS,qBAAqB,IAAI;IACrD,cAAc,EAAE,CAAC,CAAC;IAClB,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;CACxB,CAAC;AACF,MAAM,WAAW,QAAQ,CAAC,CAAC,SAAS,qBAAqB;IACrD,qBAAqB,EAAE,MAAM,CAAC,CAAC;IAC/B,OAAO,CAAC,KAAK,EAAE,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IACpC,eAAe,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;IAC5E,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;CAC7C"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "def-game",
3
- "version": "1.0.7",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",