Skip to content

Commit 18fd866

Browse files
committed
+ Add graceful stop to sinergy with process managers (i.e. PM2)
+ Add npm start script
1 parent 34db0b7 commit 18fd866

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

memoryStore.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
const MemoryStore = {
22
games: new Map(),
3-
set: async function (gameId, gameState) {
3+
async set(gameId, gameState) {
44
this.games.set(gameId, gameState);
55
return Promise.resolve(gameState.id);
66
},
7-
get: async function (gameId) {
7+
async get(gameId) {
88
return Promise.resolve(this.games.get(gameId));
99
},
10-
del: async function (gameId) {
10+
async del(gameId) {
1111
return Promise.resolve(this.games.delete(gameId));
12+
},
13+
async quit() {
14+
return Promise.resolve('OK');
1215
}
1316
};
1417

moonBot.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const { concatMap, map } = require('rxjs/operators');
88
const { partition } = require('rxjs');
99
const keyBoards = require('./telegramKeyboard');
1010
const { Rules, GameEventType } = require('./gameRules');
11-
const Store = require('./memoryStore');
11+
const Store = require('./memoryStore'); //memory store for testing and develop
12+
//const Store = require('./redisStore'); //redis store recommended for production
13+
1214
const showStateEvent = Symbol.for("SHOW_GAME_STATE");
1315

1416
const token = process.env.MOON_BOT_TOKEN;
@@ -368,4 +370,12 @@ Operation Target Cost
368370
369371
All 2 register operations store the result in the first register.
370372
"or A B" will modify register A.
371-
"mov A B" will copy register B value into register A.\`\`\``;
373+
"mov A B" will copy register B value into register A.\`\`\``;
374+
375+
process.on('SIGINT', function () {
376+
377+
eventStream.complete();
378+
eventStream.unsubscribe();
379+
Game.Quit();
380+
381+
});

moonGame.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ function Game(store) {
154154
gameState = StateManager.fixError({ gameState, playerId, error }).gameState;
155155

156156
return gameState;
157+
},
158+
159+
async Quit() {
160+
return store.quit();
157161
}
158162
};
159163

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "tabletop moon game on telegram",
55
"main": "moonBot.js",
66
"scripts": {
7+
"start": "node moonBot.js",
78
"test": "mocha"
89
},
910
"repository": {

0 commit comments

Comments
 (0)