-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from linomp/refactor/break-ui-into-components
Refactor/break UI into components
- Loading branch information
Showing
46 changed files
with
357 additions
and
973 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script lang="ts"> | ||
import { gameOverReason, gameSession } from "src/stores/stores"; | ||
</script> | ||
|
||
<h3>Game Over</h3> | ||
<pre>{JSON.stringify($gameSession, null, 2)}</pre> | ||
<p>{$gameOverReason}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<script lang="ts"> | ||
import { isUndefinedOrNull } from "src/shared/utils"; | ||
import { PlayerActionsService } from "src/api/generated"; | ||
import { | ||
dayInProgress, | ||
gameOver, | ||
gameSession, | ||
globalSettings, | ||
predictionPurchaseButtonDisabled, | ||
sensorPurchaseButtonDisabled, | ||
} from "src/stores/stores"; | ||
import Sensor from "src/components/Sensor.svelte"; | ||
$: { | ||
sensorPurchaseButtonDisabled.set( | ||
$dayInProgress || | ||
($gameSession?.available_funds ?? 0) < | ||
($globalSettings?.sensor_cost ?? Infinity), | ||
); | ||
predictionPurchaseButtonDisabled.set( | ||
$dayInProgress || | ||
($gameSession?.available_funds ?? 0) < | ||
($globalSettings?.prediction_model_cost ?? Infinity), | ||
); | ||
} | ||
const purchaseSensor = async (sensorName: string) => { | ||
if ($gameOver) { | ||
return; | ||
} | ||
try { | ||
const result = | ||
await PlayerActionsService.purchaseSensorPlayerActionsPurchasesSensorsPost( | ||
sensorName, | ||
$gameSession?.id!, | ||
); | ||
gameSession.set(result); | ||
} catch (error: any) { | ||
if (error.status === 400) { | ||
alert("Not enough funds to buy the sensor!"); | ||
} else { | ||
console.error("Error buying sensor:", error); | ||
} | ||
} | ||
}; | ||
const purchaseRulPredictionModel = async () => { | ||
if ($gameOver) { | ||
return; | ||
} | ||
try { | ||
const result = | ||
await PlayerActionsService.purchasePredictionPlayerActionsPurchasesPredictionModelsPost( | ||
"predicted_rul", | ||
$gameSession?.id!, | ||
); | ||
gameSession.set(result); | ||
} catch (error: any) { | ||
if (error.status === 400) { | ||
alert("Not enough funds to buy the prediction model!"); | ||
} else { | ||
console.error("Error buying prediction model:", error); | ||
} | ||
} | ||
}; | ||
</script> | ||
|
||
{#if !isUndefinedOrNull($gameSession)} | ||
<div class="machine-data"> | ||
<h3>Operational Parameters</h3> | ||
{#each Object.entries($gameSession?.machine_state?.operational_parameters ?? {}) as [parameter, value]} | ||
<Sensor | ||
sensorCost={$globalSettings?.sensor_cost ?? 0} | ||
sensorPurchaseButtonDisabled={$sensorPurchaseButtonDisabled} | ||
{parameter} | ||
{value} | ||
{purchaseSensor} | ||
/> | ||
{/each} | ||
<p> | ||
{"Remaining Useful Life"}: {$gameSession?.machine_state?.predicted_rul | ||
? `${$gameSession.machine_state?.predicted_rul} steps` | ||
: "???"} | ||
<span | ||
hidden={!isUndefinedOrNull($gameSession?.machine_state?.predicted_rul)} | ||
> | ||
<button | ||
disabled={$predictionPurchaseButtonDisabled} | ||
on:click={() => purchaseRulPredictionModel()} | ||
> | ||
Buy (${$globalSettings?.prediction_model_cost}) | ||
</button> | ||
</span> | ||
</p> | ||
</div> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script lang="ts"> | ||
import { dayInProgress, gameOver, gameSession } from "src/stores/stores"; | ||
import { isUndefinedOrNull } from "src/shared/utils"; | ||
import runningMachineSrc from "/img/healthy.gif"; | ||
const stoppedMachineSrc = new URL("/img/stopped.PNG", import.meta.url).href; | ||
let stopAnimation = false; | ||
$: { | ||
stopAnimation = $gameOver || !$dayInProgress; | ||
} | ||
</script> | ||
|
||
<img | ||
class="machine-view" | ||
src={stopAnimation ? stoppedMachineSrc : runningMachineSrc} | ||
alt="..." | ||
width="369" | ||
height="276" | ||
hidden={isUndefinedOrNull($gameSession)} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script lang="ts"> | ||
export let parameter: string; | ||
export let value: number | null; | ||
export let sensorPurchaseButtonDisabled: boolean; | ||
export let purchaseSensor: (parameter: string) => void; | ||
export let sensorCost: number; | ||
const formatParameterName = (parameter: string) => { | ||
return parameter | ||
.split("_") | ||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) | ||
.join(" "); | ||
}; | ||
</script> | ||
|
||
<p> | ||
{formatParameterName(parameter)}: {value ?? "???"} | ||
<span hidden={value != null}> | ||
<button | ||
disabled={sensorPurchaseButtonDisabled} | ||
on:click={() => purchaseSensor(parameter)} | ||
> | ||
Buy (${sensorCost}) | ||
</button> | ||
</span> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<script lang="ts"> | ||
import { PlayerActionsService, SessionsService } from "src/api/generated"; | ||
import { isUndefinedOrNull } from "src/shared/utils"; | ||
import { | ||
dayInProgress, | ||
gameOver, | ||
gameSession, | ||
globalSettings, | ||
maintenanceButtonDisabled, | ||
performedMaintenanceInThisTurn, | ||
} from "src/stores/stores"; | ||
export let maintenanceCost: number; | ||
export let fetchExistingSession: () => Promise<void>; | ||
$: { | ||
maintenanceButtonDisabled.set( | ||
$performedMaintenanceInThisTurn || | ||
$dayInProgress || | ||
($gameSession?.available_funds ?? 0) < | ||
($globalSettings?.maintenance_cost ?? Infinity), | ||
); | ||
} | ||
const advanceToNextDay = async () => { | ||
if (isUndefinedOrNull($gameSession) || $gameOver) { | ||
return; | ||
} | ||
// TODO: migrate this polling strategy to a websocket connection | ||
// start fetching machine health every second while the day is advancing | ||
const intervalId = setInterval(fetchExistingSession, 500); | ||
dayInProgress.set(true); | ||
try { | ||
let result = await SessionsService.advanceSessionsTurnsPut( | ||
$gameSession?.id!, | ||
); | ||
gameSession.set(result); | ||
} catch (error) { | ||
console.error("Error advancing day:", error); | ||
} finally { | ||
await fetchExistingSession(); | ||
// stop fetching machine health until the player advances to next day again | ||
clearInterval(intervalId); | ||
dayInProgress.set(false); | ||
performedMaintenanceInThisTurn.set(false); | ||
} | ||
}; | ||
const doMaintenance = async () => { | ||
if ($gameOver || isUndefinedOrNull($gameSession)) { | ||
return; | ||
} | ||
try { | ||
const result = | ||
await PlayerActionsService.doMaintenancePlayerActionsMaintenanceInterventionsPost( | ||
$gameSession!.id, | ||
); | ||
gameSession.set(result); | ||
performedMaintenanceInThisTurn.set(true); | ||
} catch (error: any) { | ||
if (error.status === 400) { | ||
alert("Not enough funds to perform maintenance!"); | ||
} else { | ||
console.error("Error performing maintenance:", error); | ||
} | ||
} | ||
}; | ||
</script> | ||
|
||
{#if !isUndefinedOrNull($gameSession)} | ||
<div class="session-data"> | ||
<h3>Game Session Details</h3> | ||
<p>Current Step: {$gameSession?.current_step}</p> | ||
<p>Available Funds: {$gameSession?.available_funds}</p> | ||
<div class="session-commands"> | ||
<button on:click={advanceToNextDay} disabled={$dayInProgress}> | ||
Advance to next day | ||
</button> | ||
<button on:click={doMaintenance} disabled={$maintenanceButtonDisabled}> | ||
Perform Maintenance (${maintenanceCost}) | ||
</button> | ||
</div> | ||
</div> | ||
{/if} | ||
|
||
<style> | ||
.session-commands { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.75rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<div class="spinner w-full text-center {$$props.class ?? 'h-11'}"> | ||
<svg | ||
class="animate-spin" | ||
viewBox="0 0 24 24" | ||
stroke-width="1.5" | ||
stroke="#9ca3af" | ||
fill="none" | ||
stroke-linecap="round" | ||
stroke-linejoin="round" | ||
> | ||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path> | ||
<line x1="12" y1="6" x2="12" y2="3"></line> | ||
<line x1="6" y1="12" x2="3" y2="12"></line> | ||
<line x1="7.75" y1="7.75" x2="5.6" y2="5.6"></line> | ||
</svg> | ||
</div> | ||
|
||
<style> | ||
.animate-spin { | ||
@apply h-full; | ||
animation: spin 1s infinite linear; | ||
} | ||
@keyframes spin { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
100% { | ||
transform: rotate(359deg); | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.