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();
}, []);
function sendReady() {
function sendReady(): void {
wsService.send({Action: GameAction.ready});
}

View File

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

View File

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

View File

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

View File

@ -20,10 +20,10 @@ const ghosts = [
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);
switch (parsed.Action) {
switch (parsed.Action as GameAction) {
case GameAction.rollDice:
store.set(diceAtom, parsed.Data as number[]);
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)));
break;
}
}
};
function removeEatenPellets(parsed: ActionMessage): void {
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 {atom} from "jotai";
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 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 selectedDiceAtom = atom<SelectedDice | undefined>(undefined);
export const currentPlayerAtom = atom<Player | undefined>(undefined);

View File

@ -59,7 +59,7 @@ public abstract class GenericController : ControllerBase
WsService.SendToAll(segment);
} 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)
{
@ -70,4 +70,4 @@ public abstract class GenericController : ControllerBase
}
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);
void SendToAll(ArraySegment<byte> segment);
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();
GameGroup AddPlayer(IPlayer player);
}
}

View File

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

View File

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