Updated jotai states and some backend cleanup

This commit is contained in:
martin 2023-07-08 11:51:26 +02:00
parent 5be200cc56
commit 1116942031
10 changed files with 24 additions and 23 deletions

View File

@ -69,7 +69,7 @@ export const GameComponent: Component<{ player: Player }> = ({player}) => {
return () => wsService.close(); return () => wsService.close();
}, []); }, []);
function sendReady() { function sendReady(): void {
wsService.send({Action: GameAction.ready}); wsService.send({Action: GameAction.ready});
} }

View File

@ -4,7 +4,7 @@ import {useAtom} from "jotai";
import {thisPlayerAtom} from "../utils/state"; import {thisPlayerAtom} from "../utils/state";
const Game: Component = () => { const Game: Component = () => {
const [player] = useAtom(thisPlayerAtom); const [player] = useAtom(thisPlayerAtom); // TODO get player from session storage
useEffect(() => { useEffect(() => {
if (!player) { if (!player) {

View File

@ -4,14 +4,14 @@ import Input from "../components/input";
import Dropdown from "../components/dropdown"; import Dropdown from "../components/dropdown";
import {Colour, getColours} from "../game/colour"; import {Colour, getColours} from "../game/colour";
import {useNavigate} from "react-router-dom"; import {useNavigate} from "react-router-dom";
import {useAtom} from "jotai"; import {useSetAtom} from "jotai";
import {thisPlayerAtom} from "../utils/state"; import {thisPlayerAtom} from "../utils/state";
const Home: Component = () => { const Home: Component = () => {
const input = useRef<HTMLInputElement>(null); const input = useRef<HTMLInputElement>(null);
const dropdown = useRef<HTMLSelectElement>(null); const dropdown = useRef<HTMLSelectElement>(null);
const [, setPlayer] = useAtom(thisPlayerAtom); const setPlayer = useSetAtom(thisPlayerAtom);
const navigate = useNavigate(); const navigate = useNavigate();
function formHandler(): void { function formHandler(): void {

View File

@ -5,7 +5,7 @@ type Setter<T> = React.Dispatch<React.SetStateAction<T>>;
type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView; type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView;
type ActionMessage<T = any> = { type ActionMessage<T = any> = {
Action: import("../websockets/actions").GameAction, Action: import("../utils/actions").GameAction,
Data?: T Data?: T
} }

View File

@ -20,10 +20,10 @@ const ghosts = [
const store = getDefaultStore(); const store = getDefaultStore();
export function doAction(message: MessageEvent<string>): void { // TODO divide into smaller functions export const doAction: MessageEventFunction<string> = (message): void => { // TODO divide into smaller functions
const parsed: ActionMessage = JSON.parse(message.data); const parsed: ActionMessage = JSON.parse(message.data);
switch (parsed.Action) { switch (parsed.Action as GameAction) {
case GameAction.rollDice: case GameAction.rollDice:
store.set(diceAtom, parsed.Data as number[]); store.set(diceAtom, parsed.Data as number[]);
break; break;
@ -49,7 +49,7 @@ export function doAction(message: MessageEvent<string>): void { // TODO divide i
store.set(playersAtom, (parsed.Data.Players as PlayerProps[]).map(p => new Player(p))); store.set(playersAtom, (parsed.Data.Players as PlayerProps[]).map(p => new Player(p)));
break; break;
} }
} };
function removeEatenPellets(parsed: ActionMessage): void { function removeEatenPellets(parsed: ActionMessage): void {
const pellets = parsed.Data?.eatenPellets as Position[]; const pellets = parsed.Data?.eatenPellets as Position[];

View File

@ -1,11 +1,14 @@
// TODO merge character and player atoms, since the player is the owner of the character
import Player from "../game/player"; import Player from "../game/player";
import {atom} from "jotai"; import {atom} from "jotai";
import {Character} from "../game/character"; import {Character} from "../game/character";
import {atomWithStorage, createJSONStorage} from "jotai/utils";
const playerStorage = createJSONStorage<Player | undefined>(() => sessionStorage);
// TODO merge character and player atoms, since the player is the owner of the character
export const charactersAtom = atom<Character[] | undefined>(undefined); export const charactersAtom = atom<Character[] | undefined>(undefined);
export const playersAtom = atom<Player[]>([]); export const playersAtom = atom<Player[]>([]);
export const thisPlayerAtom = atom<Player | undefined>(undefined); export const thisPlayerAtom = atomWithStorage<Player | undefined>("player", undefined, playerStorage);
export const diceAtom = atom<number[] | undefined>(undefined); export const diceAtom = atom<number[] | undefined>(undefined);
export const selectedDiceAtom = atom<SelectedDice | undefined>(undefined); export const selectedDiceAtom = atom<SelectedDice | undefined>(undefined);
export const currentPlayerAtom = atom<Player | undefined>(undefined); export const currentPlayerAtom = atom<Player | undefined>(undefined);

View File

@ -59,7 +59,7 @@ public abstract class GenericController : ControllerBase
WsService.SendToAll(segment); WsService.SendToAll(segment);
} while (true); } while (true);
await WsService.Close(_webSocket, result.CloseStatus.Value, result.CloseStatusDescription ?? "No reason"); await WsService.Close(_webSocket, result.CloseStatus.Value, result.CloseStatusDescription);
} }
catch (WebSocketException e) catch (WebSocketException e)
{ {
@ -70,4 +70,4 @@ public abstract class GenericController : ControllerBase
} }
protected abstract ArraySegment<byte> Run(WebSocketReceiveResult result, byte[] data); protected abstract ArraySegment<byte> Run(WebSocketReceiveResult result, byte[] data);
} }

View File

@ -10,7 +10,7 @@ public interface IWebSocketService
Task Send(WebSocket webSocket, ArraySegment<byte> segment); Task Send(WebSocket webSocket, ArraySegment<byte> segment);
void SendToAll(ArraySegment<byte> segment); void SendToAll(ArraySegment<byte> segment);
Task<WebSocketReceiveResult> Receive(WebSocket webSocket, byte[] buffer); Task<WebSocketReceiveResult> Receive(WebSocket webSocket, byte[] buffer);
Task Close(WebSocket webSocket, WebSocketCloseStatus closeStatus, string closeStatusDescription); Task Close(WebSocket webSocket, WebSocketCloseStatus closeStatus, string? closeStatusDescription);
int CountConnected(); int CountConnected();
GameGroup AddPlayer(IPlayer player); GameGroup AddPlayer(IPlayer player);
} }

View File

@ -9,9 +9,9 @@ namespace pacMan.Services;
public interface IActionService public interface IActionService
{ {
void DoAction(ActionMessage message); void DoAction(ActionMessage message);
List<int> RollDice(ActionMessage message); List<int> RollDice();
List<IPlayer> PlayerInfo(ActionMessage message); List<IPlayer> PlayerInfo(ActionMessage message);
object Ready(ActionMessage message); object Ready();
} }
public class ActionService : IActionService // TODO tests public class ActionService : IActionService // TODO tests
@ -34,15 +34,14 @@ public class ActionService : IActionService // TODO tests
{ {
message.Data = message.Action switch message.Data = message.Action switch
{ {
GameAction.RollDice => RollDice(message), GameAction.RollDice => RollDice(),
GameAction.PlayerInfo => PlayerInfo(message), GameAction.PlayerInfo => PlayerInfo(message),
GameAction.Ready => Ready(message), GameAction.Ready => Ready(),
_ => message.Data _ => message.Data
}; };
} }
public List<int> RollDice(ActionMessage message) public List<int> RollDice()
{ {
var rolls = _diceCup.Roll(); var rolls = _diceCup.Roll();
_logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls)); _logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
@ -58,7 +57,7 @@ public class ActionService : IActionService // TODO tests
return _group.Players; return _group.Players;
} }
public object Ready(ActionMessage message) public object Ready()
{ {
object data; object data;
if (_player != null) if (_player != null)

View File

@ -44,8 +44,7 @@ public class WebSocketService : IWebSocketService // TODO add tests
return result; return result;
} }
public async Task Close(WebSocket webSocket, WebSocketCloseStatus closeStatus, public async Task Close(WebSocket webSocket, WebSocketCloseStatus closeStatus, string? closeStatusDescription)
string closeStatusDescription = "No reason")
{ {
await webSocket.CloseAsync( await webSocket.CloseAsync(
closeStatus, closeStatus,