Sends an ActionMessage between client and server
This commit is contained in:
parent
9df04610ef
commit
d8e74dc6fd
@ -33,14 +33,14 @@ export default class WebSocketService {
|
||||
if (this._onError) this.ws.onerror = this._onError;
|
||||
}
|
||||
|
||||
public send(data: ActionRequest | string): void {
|
||||
public send(data: ActionMessage | string): void {
|
||||
if (typeof data !== "string") {
|
||||
data = JSON.stringify(data);
|
||||
}
|
||||
this.ws?.send(data);
|
||||
}
|
||||
|
||||
public async sendAndReceive<R>(data: ActionRequest): Promise<R> {
|
||||
public async sendAndReceive<R>(data: ActionMessage): Promise<R> {
|
||||
if (!this.isOpen()) return Promise.reject("WebSocket is not open");
|
||||
|
||||
let result: R | undefined;
|
||||
|
@ -2,6 +2,7 @@ import React from "react";
|
||||
import GameCanvas from "../components/gameCanvas";
|
||||
import Game from "../game/game";
|
||||
import {AllDice} from "./dice";
|
||||
import {Action} from "../classes/actions";
|
||||
|
||||
let game: Game;
|
||||
export const GameComponent: Component = () => {
|
||||
@ -18,9 +19,12 @@ export const GameComponent: Component = () => {
|
||||
|
||||
function updateState() {
|
||||
game.wsService.onReceive = (message) => {
|
||||
const parsed = JSON.parse(message.data);
|
||||
if (parsed instanceof Array) {
|
||||
setDice(parsed);
|
||||
const parsed: ActionMessage = JSON.parse(message.data);
|
||||
console.log(parsed);
|
||||
switch (parsed.Action) {
|
||||
case Action.rollDice:
|
||||
setDice(parsed.Data as number[]);
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ export default class Game {
|
||||
public async gameLoop(setDice: Setter<number[] | undefined>): Promise<void> {
|
||||
// Throw the dices
|
||||
const result = await this.rollDice();
|
||||
setDice(result);
|
||||
setDice(result.Data);
|
||||
|
||||
// Choose a dice and move pac-man or a ghost
|
||||
|
||||
@ -55,9 +55,9 @@ export default class Game {
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
private async rollDice(): Promise<number[]> {
|
||||
let result: number[];
|
||||
result = await this._wsService.sendAndReceive<number[]>({action: Action.rollDice});
|
||||
private async rollDice(): Promise<ActionMessage<number[]>> {
|
||||
let result: ActionMessage<number[]>;
|
||||
result = await this._wsService.sendAndReceive<ActionMessage<number[]>>({Action: Action.rollDice});
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ type Setter<T> = React.Dispatch<React.SetStateAction<T>>;
|
||||
|
||||
type WebSocketData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
||||
|
||||
type ActionRequest = {
|
||||
action: import("../classes/actions").Action,
|
||||
data?: object
|
||||
type ActionMessage<T = object> = {
|
||||
Action: import("../classes/actions").Action,
|
||||
Data?: T
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class GameController : GenericController
|
||||
stringResult = Regex.Replace(stringResult, @"\p{C}+", "");
|
||||
|
||||
Logger.Log(LogLevel.Information, "Received: {}", stringResult);
|
||||
var action = JsonSerializer.Deserialize<ActionRequest>(stringResult);
|
||||
var action = JsonSerializer.Deserialize<ActionMessage>(stringResult);
|
||||
|
||||
switch (action?.Action)
|
||||
{
|
||||
@ -39,7 +39,8 @@ public class GameController : GenericController
|
||||
var rolls = _diceCup.Roll();
|
||||
Logger.Log(LogLevel.Information, "Rolled {}", string.Join(", ", rolls));
|
||||
|
||||
return rolls.ToArraySegment();
|
||||
action.Data = rolls;
|
||||
return action.ToArraySegment();
|
||||
default:
|
||||
return new ArraySegment<byte>("Invalid action"u8.ToArray());
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
namespace pacMan.Game;
|
||||
|
||||
public enum GameAction
|
||||
{
|
||||
RollDice
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
namespace pacMan.Game;
|
||||
|
||||
public class ActionRequest
|
||||
{
|
||||
public GameAction Action { get; set; }
|
||||
public object? Data { get; set; }
|
||||
}
|
16
pac-man-board-game/Game/Actions.cs
Normal file
16
pac-man-board-game/Game/Actions.cs
Normal file
@ -0,0 +1,16 @@
|
||||
namespace pacMan.Game;
|
||||
|
||||
public enum GameAction
|
||||
{
|
||||
RollDice
|
||||
}
|
||||
|
||||
public class ActionMessage<T>
|
||||
{
|
||||
public GameAction Action { get; set; }
|
||||
public T? Data { get; set; }
|
||||
}
|
||||
|
||||
public class ActionMessage : ActionMessage<dynamic>
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user