-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
420 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const dayjs = require('dayjs'); | ||
|
||
module.exports = class GameList { | ||
static games = {}; | ||
|
||
static add(id, game) { | ||
this.games[id] = game; | ||
} | ||
|
||
static get(id) { | ||
return this.games[id]; | ||
} | ||
|
||
static flushExpired() { | ||
const now = dayjs(); | ||
const expiredIds = []; | ||
|
||
for(let id in this.games) { | ||
if(this.games.hasOwnProperty(id) && now.isAfter(this.games[id].getExpiration())) { | ||
delete this.games[id]; | ||
|
||
expiredIds.push(id); | ||
} | ||
} | ||
|
||
return expiredIds; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
module.exports = function createServer(createGame, onJoinGame, onGameAction) { | ||
const express = require('express'); | ||
const app = express(); | ||
const compression = require('compression'); | ||
const bodyParser = require('body-parser'); | ||
const http = require('http').Server(app); | ||
const io = require('socket.io')(http); | ||
const HTTP_PORT = 80; | ||
const SOCKET_PORT = 3000; | ||
|
||
app.use(compression()); | ||
app.use(express.static('public')); | ||
app.use(bodyParser.json()); | ||
|
||
app.post('/game', createGame); | ||
io.on('connection', function (socket) { | ||
onJoinGame(io, socket); | ||
onJoinGame(socket); | ||
|
||
socket.on('game-action', function (data) { | ||
onGameAction(io, socket, data); | ||
onGameAction(socket, data); | ||
}); | ||
}); | ||
|
||
http.listen(HTTP_PORT); | ||
io.listen(SOCKET_PORT); | ||
|
||
return io; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.