Skip to content

Commit

Permalink
Merge pull request online-go#2399 from GreenAsJade/fix_network_status…
Browse files Browse the repository at this point in the history
…_live_game_check

Fix bugged check for live game
  • Loading branch information
anoek authored Oct 19, 2023
2 parents 9e79564 + 931a15a commit 13b1f90
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
19 changes: 10 additions & 9 deletions src/components/NetworkStatus/NetworkStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
57 changes: 23 additions & 34 deletions src/lib/sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 13b1f90

Please sign in to comment.