Skip to content

Commit

Permalink
+ Add graceful stop to sinergy with process managers (i.e. PM2)
Browse files Browse the repository at this point in the history
+ Add npm start script
  • Loading branch information
jlvaquero committed Jan 27, 2020
1 parent 34db0b7 commit 18fd866
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
9 changes: 6 additions & 3 deletions memoryStore.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const MemoryStore = {
games: new Map(),
set: async function (gameId, gameState) {
async set(gameId, gameState) {
this.games.set(gameId, gameState);
return Promise.resolve(gameState.id);
},
get: async function (gameId) {
async get(gameId) {
return Promise.resolve(this.games.get(gameId));
},
del: async function (gameId) {
async del(gameId) {
return Promise.resolve(this.games.delete(gameId));
},
async quit() {
return Promise.resolve('OK');
}
};

Expand Down
14 changes: 12 additions & 2 deletions moonBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const { concatMap, map } = require('rxjs/operators');
const { partition } = require('rxjs');
const keyBoards = require('./telegramKeyboard');
const { Rules, GameEventType } = require('./gameRules');
const Store = require('./memoryStore');
const Store = require('./memoryStore'); //memory store for testing and develop
//const Store = require('./redisStore'); //redis store recommended for production

const showStateEvent = Symbol.for("SHOW_GAME_STATE");

const token = process.env.MOON_BOT_TOKEN;
Expand Down Expand Up @@ -368,4 +370,12 @@ Operation Target Cost
All 2 register operations store the result in the first register.
"or A B" will modify register A.
"mov A B" will copy register B value into register A.\`\`\``;
"mov A B" will copy register B value into register A.\`\`\``;

process.on('SIGINT', function () {

eventStream.complete();
eventStream.unsubscribe();
Game.Quit();

});
4 changes: 4 additions & 0 deletions moonGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ function Game(store) {
gameState = StateManager.fixError({ gameState, playerId, error }).gameState;

return gameState;
},

async Quit() {
return store.quit();
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "tabletop moon game on telegram",
"main": "moonBot.js",
"scripts": {
"start": "node moonBot.js",
"test": "mocha"
},
"repository": {
Expand Down

0 comments on commit 18fd866

Please sign in to comment.