Skip to content

Commit

Permalink
⏪ Fix prettier mishap
Browse files Browse the repository at this point in the history
  • Loading branch information
coyotte508 committed Jul 4, 2023
1 parent e8f1576 commit eef0ce9
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions apps/web/src/components/Game/GameSidebar.svelte
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
<script lang="ts">
import { browser } from "$app/env";
import { keyBy } from "lodash";
import { elapsedSeconds } from "@bgs/utils";
import { timerTime, oneLineMarked, handleError, confirm, duration, shortDuration } from "@/utils";
import type { PlayerInfo } from "@bgs/types";
import Portal from "@/modules/portal";
import clockHistory from "@iconify/icons-bi/clock-history.js";
import { Button, Icon, Badge } from "@/modules/cdk";
import { getContext, onDestroy } from "svelte";
import { GameLog, ReplayControls, GameNotes, GamePreferences, GameSettings } from "./GameSidebar";
import type { GameContext } from "@/routes/game/[gameId].svelte";
import PlayerGameAvatar from "./PlayerGameAvatar.svelte";
import { useRest } from "@/composition/useRest";
import { useAccount } from "@/composition/useAccount";
import { useActiveGames } from "@/composition/useActiveGames";
import { useCurrentGame } from "@/composition/useCurrentGame";
import { useActiveGames } from "@/composition/useActiveGames";
import { useDeveloperSettings } from "@/composition/useDeveloperSettings";
import { useRest } from "@/composition/useRest";
import type { GameContext } from "@/pages/Game.svelte";
import { confirm, handleError } from "@/utils";
import type { PlayerInfo } from "@bgs/types";
import { elapsedSeconds } from "@bgs/utils";
import { keyBy } from "lodash";
import { getContext, onDestroy } from "svelte";
const { game, players, gameInfo }: GameContext = getContext("game");
const { post } = useRest();
const { account } = useAccount();
const { playerStatus } = useCurrentGame();
const { addActiveGame, removeActiveGame } = useActiveGames();
const { devGameSettings } = useDeveloperSettings();
let secondsCounter = 0;
const interval = setInterval(() => {
if (browser && !document.hidden) {
secondsCounter += 1;
}
}, 1000);
onDestroy(() => clearInterval(interval));
let requestedDrop: Record<string, boolean> = {};
$: userId = $account?._id;
$: playerUser = $game?.players.find((pl) => pl._id === userId);
$: gameId = $game?._id;
function status(playerId: string) {
return $playerStatus?.find((pl) => pl._id === playerId)?.status ?? "offline";
}
function playerElo(playerId: string) {
return $players.find((pl) => pl._id === playerId)?.elo ?? 0;
}
$: alwaysActive = $game?.options.timing.timer?.start === $game?.options.timing.timer?.end;
$: currentPlayersById = keyBy($game?.currentPlayers ?? [], "_id");
function isCurrentPlayer(id: string) {
return $game?.status !== "ended" && !!currentPlayersById[id];
}
const onGameChanged = () => {
if (userId) {
if (isCurrentPlayer(userId)) {
Expand All @@ -48,36 +65,44 @@
}
}
};
$: onGameChanged(), [userId, $game];
let remainingTimes: Record<string, number> = {};
function updateRemainingTimes() {
const ret: Record<string, number> = {};
for (const player of $game.players) {
ret[player._id] = remainingTime(player);
}
remainingTimes = ret;
}
$: updateRemainingTimes(), [secondsCounter];
function remainingTime(player: PlayerInfo) {
const currentPlayer = currentPlayersById[player._id];
if (currentPlayer) {
const spent = elapsedSeconds(new Date(currentPlayer.timerStart as any), $game.options.timing.timer);
of $game.players) {
ret[
// Trick to update every second
return Math.max(player.remainingTime - spent, 0) + (secondsCounter % 1);
}
return Math.max(player.remainingTime, 0);
}
async function voteCancel() {
if (
await confirm("This vote cannot be taken back. If all active players vote to cancel, the game will be cancelled.")
) {
await post(`/game/${gameId}/cancel`).catch(handleError);
}
}
async function quit() {
await post(`/game/${gameId}/quit`).catch(handleError);
}
async function requestDrop(playerId: string) {
await post(`/game/${gameId}/drop/${playerId}`).then(
() => (requestedDrop = { ...requestedDrop, [playerId]: true }),
Expand All @@ -86,7 +111,7 @@
}
</script>

<div id="floating-controls" />
<div id="floating-controls"></div>
<Portal target="#sidebar">
<h3 class="mt-75">Players</h3>
{#each $game.players as player}
Expand Down

0 comments on commit eef0ce9

Please sign in to comment.