Skip to content

Instantly share code, notes, and snippets.

@buesing
Created June 28, 2019 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buesing/2fc0984f488ac18980de168888aeadba to your computer and use it in GitHub Desktop.
Save buesing/2fc0984f488ac18980de168888aeadba to your computer and use it in GitHub Desktop.
startListening() {
this.statusRecord.subscribe(`player-${this.isHost ? 2 : 1}`, value => {
switch (value.action) {
case ACTION.CONNECT:
setTimeout(this.sendPings.bind(this), 1000);
this.statusRecord.set('room-is-open', false);
this.isOpponentConnected = true;
this.emitter.emit(EVENT.OPPONENT_CONNECTED);
break;
case ACTION.DISCONNECT:
this.isOpponentConnected = false;
this.emitter.emit(EVENT.OPPONENT_DISCONNECTED);
break;
case ACTION.PAUSE:
if (this.isOpponentConnected) {
this.emitter.emit(EVENT.OPPONENT_PAUSED);
}
break;
case ACTION.UNPAUSE:
if (this.isOpponentConnected) {
this.emitter.emit(EVENT.OPPONENT_UNPAUSED);
}
break;
case ACTION.REQUEST_COUNTDOWN:
this.callbacks.receivedRequestCountdown();
break;
case ACTION.RESTART_GAME:
this.callbacks.receivedRestartGame();
break;
default:
console.warn('unknown action');
}
});
if (this.isHost) {
this.paddle2Record.subscribe('position', value => {
this.callbacks.receivedMove(value);
});
} else {
this.paddle1Record.subscribe('position', value => {
this.callbacks.receivedMove(value);
});
}
this.hitRecord.subscribe(`player-${this.isHost ? 2 : 1}`, value => {
this.callbacks.receivedHit(value);
});
this.missRecord.subscribe(`player-${this.isHost ? 2 : 1}`, value => {
this.callbacks.receivedMiss(value);
});
for (let i = 0; i < 20; i += 1) {
// make 20 ping records so the pings don't get mixed up
this.pingRecord.subscribe(`player-${this.isHost ? 2 : 1}-ping-${i}`, value => {
if (value.ping) {
this.pingRecord.set(`player-${this.isHost ? 1 : 2}-ping-${value.index}`, {
index: value.index,
pong: true,
});
} else {
this.receivedPong(value);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment