diff --git a/src/components/NetworkStatus/NetworkStatus.tsx b/src/components/NetworkStatus/NetworkStatus.tsx index 653a35c7d9..e868ba54f4 100644 --- a/src/components/NetworkStatus/NetworkStatus.tsx +++ b/src/components/NetworkStatus/NetworkStatus.tsx @@ -19,6 +19,7 @@ import * as React from "react"; import * as preferences from "preferences"; import { _ } from "translate"; import { socket } from "sockets"; +import { lookingAtOurLiveGame } from "TimeControl/util"; //interface NetworkStatusProps {} @@ -37,14 +38,7 @@ export function NetworkStatus(): JSX.Element { // Otherwise it's a discreet little icon // (note: we don't check if it's their turn because it might become their turn at any time, they // could do with knowing their internet is bad anyhow.) - const in_live_game = - typeof window !== "undefined" && - window["global_goban"] && - window["global_goban"].engine && - window["global_goban"].engine.phase === "play" && - window["global_goban"].engine.time_control !== "correspondence" && - window["user"] && - window["user"].id in window["global_goban"].engine.player_pool; + const in_live_game = lookingAtOurLiveGame(); React.useEffect(() => { stateRef.current = state; // needed so we can refer to the current value in the async timer below @@ -84,7 +78,14 @@ export function NetworkStatus(): JSX.Element { }, []); // let's leave this here - it might be handy if someone is having problems - console.log("Network status: ", state); + console.log( + "Network status: ", + state, + show_slow_internet_warning ? ": warning toggle on," : ": warning toggle off,", + in_live_game ? "in live game," : "not in live game,", + "time control:", + window["global_goban"]?.engine?.time_control, + ); if (state === "connected" || state === "went-away") { return null; diff --git a/src/lib/sockets.ts b/src/lib/sockets.ts index 92946877e7..d0f9978d18 100644 --- a/src/lib/sockets.ts +++ b/src/lib/sockets.ts @@ -17,6 +17,7 @@ import Debug from "debug"; import { GobanSocket, protocol, Goban, JGOFTimeControl } from "goban"; +import { lookingAtOurLiveGame } from "TimeControl/util"; const debug = new Debug("sockets"); @@ -96,42 +97,30 @@ socket.on("latency", (latency, drift) => { // If they are playing a live game at the moment, work out what timing they would like // us to make sure that they have... - if ( - typeof window !== "undefined" && - window["global_goban"] && - window["global_goban"].engine && - window["global_goban"].engine.phase === "play" && - window["user"] - ) { + if (lookingAtOurLiveGame()) { const goban = window["global_goban"] as Goban; const time_control = goban.engine.time_control as JGOFTimeControl; - - if ( - time_control.speed !== "correspondence" && - window["user"].id in goban.engine.player_pool - ) { - switch (time_control.system) { - case "fischer": - timing_needed = time_control.time_increment || time_control.initial_time; - break; - - case "byoyomi": - timing_needed = time_control.period_time || time_control.main_time; - break; - - case "canadian": - timing_needed = time_control.period_time || time_control.main_time; - break; - - case "simple": - timing_needed = time_control.per_move; - break; - - case "absolute": - // actually, they'd like it as fast as possible, but this probably suffices - timing_needed = time_control.total_time; - break; - } + switch (time_control.system) { + case "fischer": + timing_needed = time_control.time_increment || time_control.initial_time; + break; + + case "byoyomi": + timing_needed = time_control.period_time || time_control.main_time; + break; + + case "canadian": + timing_needed = time_control.period_time || time_control.main_time; + break; + + case "simple": + timing_needed = time_control.per_move; + break; + + case "absolute": + // actually, they'd like it as fast as possible, but this probably suffices + timing_needed = time_control.total_time; + break; } } else { timing_needed = 0;