Created
June 28, 2019 11:16
-
-
Save buesing/4c318327ff16aad88c42e398fea10f29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
tryConnecting(id) { | |
// try to connect to a given room id. the first character is a code for | |
// which server the opponent is connected to. in case of 2 servers, the | |
// first half of the available characters is reserved for the first server, | |
// the second half is reserved for the second server. this way we can still | |
// have as many random combinations with 4 letters. | |
this.isHost = false; | |
return new Promise((resolve, reject) => { | |
let serverIndex = -1; | |
this.availablePrefixes.forEach((prefixes, index) => { | |
if (prefixes.indexOf(id[0]) !== -1) { | |
serverIndex = index; | |
} | |
}); | |
if (serverIndex === -1) { | |
// impossible room code, there is no prefix like that | |
reject('no room found'); | |
} | |
this.connectToServer(this.availableServers[serverIndex]).then(() => { | |
this.GAME_ID = id; | |
this.isHost = false; | |
this.setRecords(); | |
this.statusRecord.subscribe('room-is-open', value => { | |
if (value) { | |
this.startListening(); | |
this.statusRecord.set('player-2', {action: ACTION.CONNECT}); | |
this.isOpponentConnected = true; | |
setTimeout(this.sendPings.bind(this), 1000); | |
resolve(); | |
} else { | |
reject('room already full'); | |
} | |
}); | |
setTimeout(() => { | |
reject('no room found'); | |
}, 2000); | |
}).catch(e => { | |
reject(e); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment