-
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 #2 from nukeop/integrate-gamestate
Integrate GameState into GameStateContext
- Loading branch information
Showing
7 changed files
with
138 additions
and
216 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
import React from 'react'; | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { GameStateProvider, useGameState } from './GameStateProvider'; | ||
import { Player, Team } from '../Player'; | ||
import { GameLog, ActionType } from '../GameLog'; | ||
import { GameStage } from '../GameStage'; | ||
|
||
describe('GameStateProvider', () => { | ||
it('provides game state context with initial values', () => { | ||
const wrapper = ({ children }: { children: React.ReactNode }) => <GameStateProvider>{children}</GameStateProvider>; | ||
const { result } = renderHook(() => useGameState(), { wrapper }); | ||
|
||
expect(result.current.machinePlayers).toEqual([]); | ||
expect(result.current.humanPlayer).toEqual(new Player('', Team.Humans)); | ||
expect(result.current.log).toBeInstanceOf(GameLog); | ||
expect(result.current.stage).toBeInstanceOf(GameStage); | ||
}); | ||
|
||
it('allows advancing game state', async () => { | ||
const wrapper = ({ children }: { children: React.ReactNode }) => <GameStateProvider>{children}</GameStateProvider>; | ||
const { result } = renderHook(() => useGameState(), { wrapper }); | ||
|
||
await act(async () => { | ||
await result.current.advance(); | ||
}); | ||
|
||
expect(result.current.stage.actingPlayer).not.toEqual(new Player('', Team.Machines)); | ||
}); | ||
|
||
it('processes player actions', async () => { | ||
const wrapper = ({ children }: { children: React.ReactNode }) => <GameStateProvider>{children}</GameStateProvider>; | ||
const { result } = renderHook(() => useGameState(), { wrapper }); | ||
|
||
await act(async () => { | ||
await result.current.processPlayerAction(); | ||
}); | ||
|
||
expect(result.current.log.messages.length).toBeGreaterThan(0); | ||
expect(result.current.log.messages[0].actionType).toBe(ActionType.Speech); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,22 +1,102 @@ | ||
import React, { ReactElement, createContext, useContext } from 'react'; | ||
import { GameState } from '../GameState'; | ||
import React, { createContext, useContext, useState } from 'react'; | ||
import { Player, Team } from '../Player'; | ||
import { GameLog } from '../GameLog'; | ||
import { GameStage } from '../GameStage'; | ||
import { sample } from 'lodash'; | ||
import { names, personalities } from '../../prompts'; | ||
import { OpenAiApiService } from '../../services/OpenAiService'; | ||
import { ActionType } from '../GameLog'; | ||
import Logger from '../../logger'; | ||
|
||
let Context = createContext<GameState | null>(null); | ||
Context.displayName = 'GameStateContext'; | ||
interface GameStateContextType { | ||
machinePlayers: Player[]; | ||
humanPlayer: Player; | ||
log: GameLog; | ||
stage: GameStage; | ||
advance: () => Promise<void>; | ||
processPlayerAction: () => Promise<void>; | ||
} | ||
|
||
const GameStateContext = createContext<GameStateContextType | undefined>(undefined); | ||
|
||
export function useGameState() { | ||
let context = useContext(Context); | ||
if (context === null) { | ||
export const useGameState = () => { | ||
const context = useContext(GameStateContext); | ||
if (context === undefined) { | ||
throw new Error('useGameState must be used within a GameStateProvider'); | ||
} | ||
return context; | ||
} | ||
}; | ||
|
||
interface Props { | ||
value: GameState; | ||
interface GameStateProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export function GameStateProvider({ value, children }: Props): ReactElement { | ||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
} | ||
export const GameStateProvider: React.FC<GameStateProviderProps> = ({ children }) => { | ||
const [machinePlayers, setMachinePlayers] = useState<Player[]>([]); | ||
const [humanPlayer, setHumanPlayer] = useState<Player>(new Player('', Team.Humans)); | ||
const [log, setLog] = useState<GameLog>(new GameLog()); | ||
const [stage, setStage] = useState<GameStage>(new GameStage(new Player('', Team.Machines))); | ||
|
||
const initGameState = (numberOfPlayers: number) => { | ||
let availableNames = names; | ||
const machinePlayersInit = Array.from({ length: numberOfPlayers }, (_) => { | ||
const name = sample(availableNames) ?? ''; | ||
availableNames = availableNames.filter((n) => n !== name); | ||
return new Player(name!, Team.Machines, sample(personalities)?.name); | ||
}); | ||
|
||
const humanName = sample(names) ?? ''; | ||
const humanPlayerInit = new Player(humanName!, Team.Humans); | ||
const stageInit = new GameStage(machinePlayersInit[0]); | ||
|
||
setMachinePlayers(machinePlayersInit); | ||
setHumanPlayer(humanPlayerInit); | ||
setStage(stageInit); | ||
}; | ||
|
||
const advance = async () => { | ||
if (stage.actingPlayer === humanPlayer) { | ||
stage.nextPlayer(); | ||
} else { | ||
try { | ||
Logger.debug(`Processing machine turn for: ${stage.actingPlayer.name}`); | ||
await processPlayerAction(); | ||
stage.nextPlayer(); | ||
Logger.debug(`Progressing to next player: ${stage.actingPlayer.name}`); | ||
} catch (error) { | ||
log.addErrorMessage('An error occurred while processing the machine turn.'); | ||
log.addErrorMessage((error as Error).message); | ||
stage.nextPlayer(); | ||
} | ||
} | ||
}; | ||
|
||
const processPlayerAction = async () => { | ||
const service = new OpenAiApiService(); | ||
const response = await service.createChatCompletion({ | ||
max_tokens: 512, | ||
model: 'gpt-3.5-turbo', | ||
tools: [], | ||
parallel_tool_calls: false, | ||
messages: [{ role: 'system', content: 'Your prompt here' }], | ||
}); | ||
|
||
const choice = response.choices[0]; | ||
Logger.debug(JSON.stringify(choice, null, 2)); | ||
const toolCall = choice.message?.tool_calls?.[0]; | ||
const message = choice.message?.content; | ||
|
||
if (toolCall) { | ||
const actionType: ActionType = toolCall?.function.name as ActionType; | ||
log.addPlayerAction(stage.actingPlayer, toolCall?.function.arguments!, actionType, toolCall.id); | ||
} else if (message) { | ||
log.addPlayerAction(stage.actingPlayer, message, ActionType.Speech); | ||
} | ||
}; | ||
|
||
return ( | ||
<GameStateContext.Provider value={{ machinePlayers, humanPlayer, log, stage, advance, processPlayerAction }}> | ||
{children} | ||
</GameStateContext.Provider> | ||
); | ||
}; |
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