From 8818bcbbea0b80a44bde41cd7a42a6beb4f3e3f2 Mon Sep 17 00:00:00 2001 From: jajakob Date: Tue, 23 Jul 2024 11:33:07 +0200 Subject: [PATCH] demo-game: initial display player --- apps/demo-game/src/pages/admin/games/[id].tsx | 199 +++++++++--------- apps/demo-game/src/pages/play/cockpit.tsx | 162 +++++++++----- apps/demo-game/src/pages/play/welcome.tsx | 2 +- packages/platform/src/lib/util.ts | 6 +- 4 files changed, 211 insertions(+), 158 deletions(-) diff --git a/apps/demo-game/src/pages/admin/games/[id].tsx b/apps/demo-game/src/pages/admin/games/[id].tsx index d48c36f7..f6b2804c 100644 --- a/apps/demo-game/src/pages/admin/games/[id].tsx +++ b/apps/demo-game/src/pages/admin/games/[id].tsx @@ -73,19 +73,6 @@ function ManageGame() { } ) - if (loading || !data?.game) { - return
loading...
- } - - if (error) { - return
{error.message}
- } - - const game = data.game - const activePeriod = game?.activePeriod - const segments = activePeriod?.segments - const activeSegmentIx = activePeriod?.activeSegmentIx - const nextPeriod = () => activateNextPeriod({ variables: { @@ -103,6 +90,11 @@ function ManageGame() { }) const getButton = useCallback(() => { + const game = data.game + const activePeriod = game?.activePeriod + const segments = activePeriod?.segments + const activeSegmentIx = activePeriod?.activeSegmentIx + const isScheduled = game.status === GameStatus.Scheduled const isResultState = game.status === GameStatus.Results if (!activePeriod || isScheduled || isResultState) { @@ -144,7 +136,17 @@ function ManageGame() { Period Results ) - }, [game.status]) + }, [data?.game]) + + if (loading || !data?.game) { + return
loading...
+ } + + if (error) { + return
{error.message}
+ } + + const game: Game = data.game // TODO(JJ): Since it is not refreshing nicely after a new status update of // period or one of the segments, I am wondering if we should precompute @@ -156,8 +158,8 @@ function ManageGame() {
- {data.game.periods.map((period, ix) => { - const periodStatus = computePeriodStatus(data.game as Game, ix) + {game.periods.map((period, ix) => { + const periodStatus = computePeriodStatus(game, ix) const labels = [ period.facts.spotTradingEnabled && 'S', @@ -216,7 +218,7 @@ function ManageGame() {
{period.segments.map((segment, ix) => { const segmentStatus = computeSegmentStatus( - data.game as Game, + game, period as Period, ix ) @@ -263,86 +265,85 @@ function ManageGame() {
) })} - {!isPeriodCompleted && - (data.game as Game).periods.length - 1 === ix && ( - { - await addPeriodSegment({ - variables: { - gameId: Number(router.query.id), - periodIx: variables.periodIx, - facts: {}, - storyElements: variables.storyElements - ? variables.storyElements.split(',') - : [], - learningElements: variables.learningElements - ? variables.learningElements.split(',') - : [], - }, - }) - resetForm() - }} - > - {(newSegmentForm) => ( - setIsSegmentModalOpen(false)} - trigger={ - - } - title="Add Segment" - onSecondaryAction={ - - } - onPrimaryAction={ - - } - > - - - - )} - - )} + {!isPeriodCompleted && game.periods.length - 1 === ix && ( + { + await addPeriodSegment({ + variables: { + gameId: Number(router.query.id), + periodIx: variables.periodIx, + facts: {}, + storyElements: variables.storyElements + ? variables.storyElements.split(',') + : [], + learningElements: variables.learningElements + ? variables.learningElements.split(',') + : [], + }, + }) + resetForm() + }} + > + {(newSegmentForm) => ( + setIsSegmentModalOpen(false)} + trigger={ + + } + title="Add Segment" + onSecondaryAction={ + + } + onPrimaryAction={ + + } + > + + + + )} + + )}
{ await newPeriodForm.setFieldValue( 'newPeriodIx', - (data.game as Game).periods.length + game.periods.length ) newPeriodForm.handleSubmit() setIsPeriodModalOpen(false) @@ -430,7 +431,7 @@ function ManageGame() {
Players
- {data.game.players.map((player, ix) => ( + {game.players.map((player, ix) => (
diff --git a/apps/demo-game/src/pages/play/cockpit.tsx b/apps/demo-game/src/pages/play/cockpit.tsx index 509d25e3..004e58d4 100644 --- a/apps/demo-game/src/pages/play/cockpit.tsx +++ b/apps/demo-game/src/pages/play/cockpit.tsx @@ -1,4 +1,5 @@ import { useMutation, useQuery } from '@apollo/client' +import { PlayerDisplay } from '@gbl-uzh/ui' import { Switch, Table } from '@uzh-bf/design-system' import { useEffect, useState } from 'react' import { CartesianGrid, Line, LineChart, Tooltip, XAxis, YAxis } from 'recharts' @@ -177,6 +178,28 @@ function Cockpit() { }, ] + // TODO(JJ): + // - Level is not consistent with component + // Component wants a number, level is an object, though. + // console.log(self) + // - Do onclick logic + // - PlayerDisplay is not ideal/nice yet + const playerDisplay = ( +
+ {}} + /> +
+ ) + const header = (
@@ -189,38 +212,56 @@ function Cockpit() { switch (currentGame?.status) { case 'PREPARATION': - return
{header} Game is begin prepared.
+ return ( +
+
{header} Game is begin prepared.
+ {/*
{playerDisplay}
*/} + {playerDisplay} +
+ ) case 'COMPLETED': return
Game is completed.
case 'CONSOLIDATION': - return
Game is being consolidated.
- - case 'PREPARATION': - return
Game is being prepared.
+ return ( +
+
Game is being consolidated.
+ {playerDisplay} +
+ ) case 'RESULTS': - return
RESULTS
+ return ( +
+
RESULTS
+ {playerDisplay} +
+ ) case 'SCHEDULED': - return
Game is scheduled.
+ return ( +
+
Game is scheduled.
+ {playerDisplay} +
+ ) case 'PAUSED': const totalAssetsPerMonth = getTotalAssetsOfPreviousResults( previousResults ).map((s, i) => ({ total: s, month: 'month_' + String(i) })) return ( -
- {header} -
- - -
+
+
+ {header} +
+
+
Total over time chart
@@ -230,55 +271,62 @@ function Cockpit() { + + {playerDisplay} ) case 'RUNNING': return ( -
- {header} -
-
- +
+
+ {header} +
+
+ -
- {decisions.map(function (decision, i) { - return ( -
- { - decision.effect(checked) - await performAction({ - variables: { - type: decision.action, - payload: JSON.stringify({ - decision: checked, - }), - }, - }) - }} - /> -
- ) - })} +
+ {decisions.map(function (decision, i) { + return ( +
+ { + decision.effect(checked) + await performAction({ + variables: { + type: decision.action, + payload: JSON.stringify({ + decision: checked, + }), + }, + }) + }} + /> +
+ ) + })} +
+ {playerDisplay} ) + default: + return
Game has not been created yet.
} } diff --git a/apps/demo-game/src/pages/play/welcome.tsx b/apps/demo-game/src/pages/play/welcome.tsx index 6fc9f865..52c27abe 100644 --- a/apps/demo-game/src/pages/play/welcome.tsx +++ b/apps/demo-game/src/pages/play/welcome.tsx @@ -60,7 +60,7 @@ function Welcome() { />
- Welcome, {values.name}! Your company has its headquarters in{' '} + Welcome, {values.name}! Your bank has its headquarters in{' '} {values.location}.
diff --git a/packages/platform/src/lib/util.ts b/packages/platform/src/lib/util.ts index b9819e9f..79150e4b 100644 --- a/packages/platform/src/lib/util.ts +++ b/packages/platform/src/lib/util.ts @@ -26,7 +26,11 @@ export function diceRoll(seeds: number[] = []) { return integer(1, 6)(rng) } -export function computeScenarioOutcome(trend: number, gap: number, diceRoll: number) { +export function computeScenarioOutcome( + trend: number, + gap: number, + diceRoll: number +) { return trend + (diceRoll - 7) * gap }