Skip to content

Commit

Permalink
Notify UI of snake movements.
Browse files Browse the repository at this point in the history
  • Loading branch information
robholland committed Aug 21, 2024
1 parent 06d7859 commit a22a88f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
85 changes: 85 additions & 0 deletions game/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions game/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@temporalio/client": "^1.10.2",
"@temporalio/worker": "^1.10.2",
"@temporalio/workflow": "^1.10.2",
"node-fetch": "^3.3.2",
"yargs": "^17.7.2"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions game/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { Snake } from './workflows';
import fetch from 'node-fetch';

export async function snakeWork(durationMs: number) {
// sleep for duration
await new Promise((resolve) => setTimeout(resolve, durationMs));
}

export async function snakeMovedNotification(snake: Snake) {
await fetch(`http://localhost:1234/snake/${snake.id}/moved`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(snake.segments),
});
}
3 changes: 2 additions & 1 deletion game/src/starter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Client } from '@temporalio/client';
import { WorkflowIdReusePolicy } from '@temporalio/workflow';
import { GameWorkflow, PlayerWorkflow, roundStartUpdate, snakeChangeDirectionSignal, playerWaitForRoundUpdate, playerJoinTeamUpdate } from './workflows';

export async function runWorkflows(client: Client, taskQueue: string, numWorkflows: number): Promise<void> {
// Players can be created at any time. They will wait for a game to start if one is not active.
const playerHandles = await Promise.all(
Array.from({ length: 4 }).map(
(_, i) => client.workflow.start(PlayerWorkflow, { taskQueue, workflowId: `player-${i}`})
(_, i) => client.workflow.start(PlayerWorkflow, { taskQueue, workflowId: `player-${i}` })
)
);

Expand Down
10 changes: 8 additions & 2 deletions game/src/workflows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
proxyActivities, defineSignal, setHandler, condition, defineUpdate,
proxyActivities, proxyLocalActivities, defineSignal, setHandler, condition, defineUpdate,
log, startChild, workflowInfo, sleep, getExternalWorkflowHandle
} from '@temporalio/workflow';

Expand All @@ -13,6 +13,10 @@ const { snakeWork } = proxyActivities<typeof activities>({
startToCloseTimeout: '1 seconds',
});

const { snakeMovedNotification } = proxyLocalActivities<typeof activities>({
startToCloseTimeout: '1 seconds',
});

type GameConfig = {
width: number;
height: number;
Expand Down Expand Up @@ -56,7 +60,7 @@ type Segment = {
length: number;
}

type Snake = {
export type Snake = {
team: Team;
id: string;
segments: Segment[];
Expand Down Expand Up @@ -271,6 +275,8 @@ export async function GameWorkflow(config: GameConfig): Promise<void> {
const snake = moveSnake(game, id, direction);

log.info('Snake moved', { snake: id, direction: direction });

await snakeMovedNotification(snake);
});

let gameFinished = false;
Expand Down

0 comments on commit a22a88f

Please sign in to comment.