move-by-move 1.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.
@@ -0,0 +1 @@
1
+ export declare const pipe: <T>(...fns: ((arg: T) => T)[]) => (value: T) => T;
package/lib/helpers.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pipe = void 0;
4
+ const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
5
+ exports.pipe = pipe;
package/lib/index.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ interface GameAction {
2
+ type: string;
3
+ }
4
+ declare type GameState = Object;
5
+ declare type RuleIO = [action: GameAction, state: GameState];
6
+ declare const createGame: (rules: ((input: RuleIO) => RuleIO)[], initialGameState: GameState) => {
7
+ subscribe: (listener: Function) => () => void;
8
+ getState: () => Object;
9
+ move: (action: GameAction) => void;
10
+ };
11
+ export default createGame;
package/lib/index.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const helpers_1 = require("./helpers");
4
+ const createGame = (rules, initialGameState) => {
5
+ let listeners = [];
6
+ let state = initialGameState;
7
+ const subscribe = (listener) => {
8
+ listeners = [...listeners, listener];
9
+ return () => {
10
+ listeners = listeners.filter(item => item !== listener);
11
+ };
12
+ };
13
+ const getState = () => {
14
+ return state;
15
+ };
16
+ const move = (action) => {
17
+ const result = (0, helpers_1.pipe)(...rules)([action, state]);
18
+ state = result[1];
19
+ for (const listener of listeners) {
20
+ listener();
21
+ }
22
+ };
23
+ return {
24
+ subscribe,
25
+ getState,
26
+ move,
27
+ };
28
+ };
29
+ exports.default = createGame;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "move-by-move",
3
+ "version": "1.0.0",
4
+ "description": "Simple game engine for turn based games",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib"
9
+ ],
10
+ "scripts": {
11
+ "prepare": "npm run build",
12
+ "build": "tsc",
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "keywords": [
16
+ "game",
17
+ "engine"
18
+ ],
19
+ "author": "Andreas Riedmüller",
20
+ "license": "MIT",
21
+ "devDependencies": {
22
+ "typescript": "^4.5.4"
23
+ }
24
+ }