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

Update presence JS

This commit is contained in:
Daniel Barber 2018-11-09 13:30:46 -05:00
parent f9fec7f78f
commit 5e0308b53b
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 15 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import _ from "lodash"; import _ from "lodash";
import socket from "./socket"; import socket from "./socket";
import Presences from "./presences"; import { Presence } from "phoenix";
import { import {
setUserId, setUserId,
setPlayers, setPlayers,
@ -13,7 +13,7 @@ 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 = new Presences(); this.presence = new Presence(this.channel);
this.join(); this.join();
this.subscribe(); this.subscribe();
@ -38,15 +38,9 @@ class Channel {
subscribe() { subscribe() {
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.presence.onSync(() => {
this.presences.syncState(data);
this.setOpponentStatus(); this.setOpponentStatus();
}); })
this.channel.on("presence_diff", data => {
this.presences.syncDiff(data);
this.setOpponentStatus();
});
} }
updateGame(data) { updateGame(data) {
@ -60,11 +54,20 @@ class Channel {
setOpponentStatus() { setOpponentStatus() {
this.store.dispatch( this.store.dispatch(
setOpponentStatus( setOpponentStatus(
this.presences.opponentOnline(this.opponentId) ? "viewing" : "offline" this.opponentOnline(this.opponentId) ? "viewing" : "offline"
) )
); );
} }
opponentOnline(opponentId) {
return _.find(
this.presence.list(),
({ metas: [user, ...rest] }, id) => {
return parseInt(user.id) == opponentId;
}
);
}
getAvailableMoves(square) { getAvailableMoves(square) {
this.channel this.channel
.push("game:get_available_moves", { square }) .push("game:get_available_moves", { square })

View File

@ -106,7 +106,7 @@ defmodule ChessWeb.GameChannel do
def track_presence(socket) do def track_presence(socket) do
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{ {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
user_id: socket.assigns.user_id, id: socket.assigns.user_id,
online_at: inspect(System.system_time(:seconds)) online_at: inspect(System.system_time(:seconds))
}) })