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:
parent
8538aad2f6
commit
ae3ecefcf7
@ -1,6 +1,6 @@
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import socket from "./socket";
|
import socket from "./socket";
|
||||||
import { Presence } from "phoenix";
|
import Presences from "./presences";
|
||||||
import {
|
import {
|
||||||
setUserId,
|
setUserId,
|
||||||
setPlayers,
|
setPlayers,
|
||||||
@ -13,12 +13,16 @@ class Channel {
|
|||||||
constructor(store, gameId) {
|
constructor(store, gameId) {
|
||||||
this.store = store;
|
this.store = store;
|
||||||
this.channel = socket.channel(`game:${gameId}`, {});
|
this.channel = socket.channel(`game:${gameId}`, {});
|
||||||
this.presences = {};
|
this.presences = new Presences();
|
||||||
|
|
||||||
this.join();
|
this.join();
|
||||||
this.subscribe();
|
this.subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get opponentId() {
|
||||||
|
return this.store.getState().opponentId;
|
||||||
|
}
|
||||||
|
|
||||||
join() {
|
join() {
|
||||||
this.channel.join()
|
this.channel.join()
|
||||||
.receive("error", resp => {
|
.receive("error", resp => {
|
||||||
@ -34,12 +38,12 @@ class Channel {
|
|||||||
this.channel.on("game:update", this.updateGame.bind(this));
|
this.channel.on("game:update", this.updateGame.bind(this));
|
||||||
|
|
||||||
this.channel.on("presence_state", data => {
|
this.channel.on("presence_state", data => {
|
||||||
this.presences = Presence.syncState(this.presences, data);
|
this.presences.syncState(data);
|
||||||
this.setOpponentStatus();
|
this.setOpponentStatus();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.channel.on("presence_diff", data => {
|
this.channel.on("presence_diff", data => {
|
||||||
this.presences = Presence.syncDiff(this.presences, data);
|
this.presences.syncDiff(data);
|
||||||
this.setOpponentStatus();
|
this.setOpponentStatus();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -54,16 +58,10 @@ class Channel {
|
|||||||
|
|
||||||
setOpponentStatus() {
|
setOpponentStatus() {
|
||||||
this.store.dispatch(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) {
|
getAvailableMoves(square) {
|
||||||
this.channel.push("game:get_available_moves", { square })
|
this.channel.push("game:get_available_moves", { square })
|
||||||
.receive("ok", (data) => {
|
.receive("ok", (data) => {
|
||||||
|
|||||||
24
assets/js/services/presences.js
Normal file
24
assets/js/services/presences.js
Normal 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;
|
||||||
Loading…
Reference in New Issue
Block a user