1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Move Presences to their own class

This commit is contained in:
Daniel Barber 2018-09-09 15:48:03 -04:00
parent 8538aad2f6
commit ae3ecefcf7
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 33 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import _ from "lodash";
import socket from "./socket";
import { Presence } from "phoenix";
import Presences from "./presences";
import {
setUserId,
setPlayers,
@ -13,12 +13,16 @@ class Channel {
constructor(store, gameId) {
this.store = store;
this.channel = socket.channel(`game:${gameId}`, {});
this.presences = {};
this.presences = new Presences();
this.join();
this.subscribe();
}
get opponentId() {
return this.store.getState().opponentId;
}
join() {
this.channel.join()
.receive("error", resp => {
@ -34,12 +38,12 @@ class Channel {
this.channel.on("game:update", this.updateGame.bind(this));
this.channel.on("presence_state", data => {
this.presences = Presence.syncState(this.presences, data);
this.presences.syncState(data);
this.setOpponentStatus();
});
this.channel.on("presence_diff", data => {
this.presences = Presence.syncDiff(this.presences, data);
this.presences.syncDiff(data);
this.setOpponentStatus();
});
}
@ -54,16 +58,10 @@ class Channel {
setOpponentStatus() {
this.store.dispatch(setOpponentStatus(
this.opponentOnline() ? "viewing" : "offline"
this.presences.opponentOnline(this.opponentId) ? "viewing" : "offline"
));
}
opponentOnline() {
return _.find(this.presences, (value, id) => {
return parseInt(id) == this.store.getState().opponentId;
});
}
getAvailableMoves(square) {
this.channel.push("game:get_available_moves", { square })
.receive("ok", (data) => {

View File

@ -0,0 +1,24 @@
import _ from "lodash";
import { Presence } from "phoenix";
class Presences {
constructor() {
this.presences = {};
}
syncState(data) {
this.presences = Presence.syncState(this.presences, data);
}
syncDiff(data) {
this.presences = Presence.syncDiff(this.presences, data);
}
opponentOnline(opponentId) {
return _.find(this.presences, (value, id) => {
return parseInt(id) == opponentId;
});
}
}
export default Presences;