Skip to content

Commit

Permalink
Make creating games asynchronous
Browse files Browse the repository at this point in the history
Thus allowing for (later) adding HTTP requests when selecting start/goal articles
  • Loading branch information
Lichtschalter-5000 committed Jun 21, 2020
1 parent f2b1910 commit 1c47308
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/MatchMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = class MatchMaker {
* Create a new game hosted by the given player.
*/

createGame (player) {
const [origin, goal] = this.wikiPages.randomPair()
async createGame (player) {
const [origin, goal] = await this.wikiPages.randomPair()
const game = new WikiBattle(origin, goal)
game.connect(player)
return game
Expand All @@ -32,7 +32,7 @@ module.exports = class MatchMaker {
* either the previous, or the next player that is passed to this method.
*/

pair (player) {
async pair (player) {
if (this.waitingPair) {
debug('pairing with existing')
const game = this.waitingPair
Expand All @@ -49,7 +49,7 @@ module.exports = class MatchMaker {
}

debug('waiting for pairing')
const game = this.createGame(player)
const game = await this.createGame(player)
this.waitingPair = game

player.notifyJoinedGame(game)
Expand All @@ -63,10 +63,10 @@ module.exports = class MatchMaker {
* shareable URL.
*/

new (player) {
async new (player) {
debug('forcing new game')

const game = this.createGame(player)
const game = await this.createGame(player)
this.games[game.id] = game

player.notifyJoinedGame(game)
Expand Down
10 changes: 5 additions & 5 deletions src/SocketHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ module.exports = class SocketHandler {
const sock = new SocketEvents(raw)
const player = new Player(sock)

sock.on('gameType', (type, id) => {
sock.on('gameType', async (type, id) => {
switch (type) {
case 'pair':
game = this.matchMaker.pair(player)
game = await this.matchMaker.pair(player)
break
case 'new':
game = this.matchMaker.new(player)
game = await this.matchMaker.new(player)
break
case 'join':
try {
Expand All @@ -47,8 +47,8 @@ module.exports = class SocketHandler {
}
})

raw.on('close', () => {
if (game) {
raw.on('close', async () => {
if (await game) {
game.disconnect(player)

this.matchMaker.disconnected(game)
Expand Down
2 changes: 1 addition & 1 deletion src/WikiPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = class WikiPages extends EventEmitter {
* Get a pair of random article names, guaranteed to be two different pages.
*/

randomPair () {
async randomPair () {
const one = this.random()

let two = this.random()
Expand Down

0 comments on commit 1c47308

Please sign in to comment.