Refactored ActionService, players state in frontend
This commit is contained in:
parent
eed4f50313
commit
45131efb37
@ -8,6 +8,7 @@ import {testMap} from "../game/map";
|
|||||||
import {TileType} from "../game/tileType";
|
import {TileType} from "../game/tileType";
|
||||||
import Player, {State} from "../game/player";
|
import Player, {State} from "../game/player";
|
||||||
import {Colour} from "../game/colour";
|
import {Colour} from "../game/colour";
|
||||||
|
import PlayerStats from "../game/playerStats";
|
||||||
|
|
||||||
const wsService = new WebSocketService(import.meta.env.VITE_API);
|
const wsService = new WebSocketService(import.meta.env.VITE_API);
|
||||||
|
|
||||||
@ -16,12 +17,10 @@ const ghosts = [
|
|||||||
new Ghost({Colour: Colour.Purple}),
|
new Ghost({Colour: Colour.Purple}),
|
||||||
];
|
];
|
||||||
|
|
||||||
export const GameComponent: Component<{ player: Player }> = (
|
export const GameComponent: Component<{ player: Player }> = ({player}) => {
|
||||||
{
|
|
||||||
player
|
|
||||||
}) => {
|
|
||||||
// TODO find spawn points
|
// TODO find spawn points
|
||||||
const [characters, setCharacters] = useState<Character[]>();
|
const [characters, setCharacters] = useState<Character[]>();
|
||||||
|
const [players, setPlayers] = useState<Player[]>([player]);
|
||||||
|
|
||||||
const [dice, setDice] = useState<number[]>();
|
const [dice, setDice] = useState<number[]>();
|
||||||
const [selectedDice, setSelectedDice] = useState<SelectedDice>();
|
const [selectedDice, setSelectedDice] = useState<SelectedDice>();
|
||||||
@ -46,7 +45,7 @@ export const GameComponent: Component<{ player: Player }> = (
|
|||||||
rollDice();
|
rollDice();
|
||||||
}
|
}
|
||||||
|
|
||||||
function doAction(message: MessageEvent<string>): void {
|
function doAction(message: MessageEvent<string>): void { // TODO move to Service
|
||||||
const parsed: ActionMessage = JSON.parse(message.data);
|
const parsed: ActionMessage = JSON.parse(message.data);
|
||||||
|
|
||||||
switch (parsed.Action) {
|
switch (parsed.Action) {
|
||||||
@ -59,9 +58,10 @@ export const GameComponent: Component<{ player: Player }> = (
|
|||||||
removeEatenPellets(parsed);
|
removeEatenPellets(parsed);
|
||||||
break;
|
break;
|
||||||
case GameAction.playerInfo:
|
case GameAction.playerInfo:
|
||||||
const players = parsed.Data as PlayerProps[];
|
const playerProps = parsed.Data as PlayerProps[];
|
||||||
console.log(players);
|
console.log(playerProps);
|
||||||
const pacMen = players.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!));
|
setPlayers(playerProps.map(p => new Player(p)));
|
||||||
|
const pacMen = playerProps.filter(p => p.PacMan).map(p => new PacMan(p.PacMan!));
|
||||||
console.log(pacMen);
|
console.log(pacMen);
|
||||||
// TODO find spawn points
|
// TODO find spawn points
|
||||||
setCharacters([...pacMen, ...ghosts]);
|
setCharacters([...pacMen, ...ghosts]);
|
||||||
@ -69,10 +69,9 @@ export const GameComponent: Component<{ player: Player }> = (
|
|||||||
case GameAction.ready:
|
case GameAction.ready:
|
||||||
const isReady = parsed.Data.AllReady as boolean;
|
const isReady = parsed.Data.AllReady as boolean;
|
||||||
if (isReady) {
|
if (isReady) {
|
||||||
setCurrentPlayer(parsed.Data.Starter as Player);
|
setCurrentPlayer(new Player(parsed.Data.Starter as PlayerProps));
|
||||||
} else {
|
|
||||||
// TODO update player states
|
|
||||||
}
|
}
|
||||||
|
setPlayers((parsed.Data.Players as PlayerProps[]).map(p => new Player(p)));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,21 +135,13 @@ export const GameComponent: Component<{ player: Player }> = (
|
|||||||
<>
|
<>
|
||||||
<div className={"flex-center"}>
|
<div className={"flex-center"}>
|
||||||
{
|
{
|
||||||
player.State === State.waitingForPlayers ?
|
currentPlayer === undefined || currentPlayer.State === State.waitingForPlayers ?
|
||||||
<button onClick={sendReady}>Ready</button> :
|
<button onClick={sendReady}>Ready</button> :
|
||||||
<button onClick={startGameLoop}>Roll dice</button>
|
<button onClick={startGameLoop}>Roll dice</button>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
<AllDice values={dice} onclick={handleDiceClick} selectedDiceIndex={selectedDice?.index}/>
|
<AllDice values={dice} onclick={handleDiceClick} selectedDiceIndex={selectedDice?.index}/>
|
||||||
{
|
{players?.map(p => <PlayerStats key={p.Name} player={p} isCurrentPlayer={currentPlayer === p}/>)}
|
||||||
(characters?.filter(c => c.isPacMan()) as PacMan[] | undefined)?.map(c =>
|
|
||||||
/*TODO use PlayerStats instead*/
|
|
||||||
<div key={c.Colour} className={"mx-auto w-fit m-2"}>
|
|
||||||
<p className={currentPlayer === player ? "underline" : ""}>Player: {player.Colour}</p>
|
|
||||||
<p>Pellets: {player.Box.count}</p>
|
|
||||||
<p>PowerPellets: {player.Box.countPowerPellets}</p>
|
|
||||||
</div>)
|
|
||||||
}
|
|
||||||
{characters &&
|
{characters &&
|
||||||
<GameBoard className={"mx-auto my-2"}
|
<GameBoard className={"mx-auto my-2"}
|
||||||
characters={characters}
|
characters={characters}
|
||||||
|
@ -15,6 +15,7 @@ const PlayerStats: Component<PlayerStatsProps> = (
|
|||||||
}) => (
|
}) => (
|
||||||
<div key={player.Colour} className={`mx-auto w-fit m-2 ${className}`} id={id}>
|
<div key={player.Colour} className={`mx-auto w-fit m-2 ${className}`} id={id}>
|
||||||
<p className={isCurrentPlayer ? "underline" : ""}>Player: {player.Name}</p>
|
<p className={isCurrentPlayer ? "underline" : ""}>Player: {player.Name}</p>
|
||||||
|
<p>Colour: {player.Colour}</p>
|
||||||
{player.State === State.inGame ?
|
{player.State === State.inGame ?
|
||||||
<>
|
<>
|
||||||
<p>Pellets: {player.Box.count}</p>
|
<p>Pellets: {player.Box.count}</p>
|
||||||
|
@ -9,9 +9,9 @@ namespace pacMan.Services;
|
|||||||
public interface IActionService
|
public interface IActionService
|
||||||
{
|
{
|
||||||
void DoAction(ActionMessage message);
|
void DoAction(ActionMessage message);
|
||||||
void RollDice(ActionMessage message);
|
List<int> RollDice(ActionMessage message);
|
||||||
void PlayerInfo(ActionMessage message);
|
List<IPlayer> PlayerInfo(ActionMessage message);
|
||||||
void Ready(ActionMessage message);
|
object Ready(ActionMessage message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ActionService : IActionService // TODO tests
|
public class ActionService : IActionService // TODO tests
|
||||||
@ -32,54 +32,54 @@ public class ActionService : IActionService // TODO tests
|
|||||||
|
|
||||||
public void DoAction(ActionMessage message)
|
public void DoAction(ActionMessage message)
|
||||||
{
|
{
|
||||||
switch (message.Action)
|
message.Data = message.Action switch
|
||||||
{
|
{
|
||||||
case GameAction.RollDice:
|
GameAction.RollDice => RollDice(message),
|
||||||
RollDice(message);
|
|
||||||
break;
|
GameAction.PlayerInfo => PlayerInfo(message),
|
||||||
case GameAction.PlayerInfo:
|
GameAction.Ready => Ready(message),
|
||||||
PlayerInfo(message);
|
_ => message.Data
|
||||||
break;
|
};
|
||||||
case GameAction.Ready:
|
|
||||||
Ready(message);
|
|
||||||
break;
|
|
||||||
case GameAction.MoveCharacter:
|
|
||||||
default:
|
|
||||||
_logger.Log(LogLevel.Information, "Forwarding message to all clients");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RollDice(ActionMessage message)
|
public List<int> RollDice(ActionMessage message)
|
||||||
{
|
{
|
||||||
var rolls = _diceCup.Roll();
|
var rolls = _diceCup.Roll();
|
||||||
_logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
|
_logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
|
||||||
|
|
||||||
message.Data = rolls;
|
return rolls;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayerInfo(ActionMessage message)
|
public List<IPlayer> PlayerInfo(ActionMessage message)
|
||||||
{
|
{
|
||||||
_player = JsonSerializer.Deserialize<Player>(message.Data);
|
_player = JsonSerializer.Deserialize<Player>(message.Data);
|
||||||
_group = _wsService.AddPlayer(_player); // TODO missing some data?
|
_group = _wsService.AddPlayer(_player); // TODO missing some data?
|
||||||
|
|
||||||
message.Data = _group.Players;
|
return _group.Players;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Ready(ActionMessage message)
|
public object Ready(ActionMessage message)
|
||||||
{
|
{
|
||||||
|
object data;
|
||||||
if (_player != null)
|
if (_player != null)
|
||||||
{
|
{
|
||||||
var players = _group.SetReady(_player).ToArray();
|
var players = _group.SetReady(_player).ToArray();
|
||||||
if (players.All(p => p.State == State.Ready))
|
if (players.All(p => p.State == State.Ready))
|
||||||
|
{
|
||||||
// TODO roll to start
|
// TODO roll to start
|
||||||
message.Data = new { AllReady = true, Starter = _group.RandomPlayer };
|
_group.SetAllInGame();
|
||||||
|
data = new { AllReady = true, Players = players, Starter = _group.RandomPlayer };
|
||||||
|
}
|
||||||
else
|
else
|
||||||
message.Data = new { AllReady = false, players };
|
{
|
||||||
|
data = new { AllReady = false, Players = players };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
message.Data = "Player not found, please create a new player";
|
data = "Player not found, please create a new player";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,4 +28,9 @@ public class GameGroup // TODO tests
|
|||||||
player.State = State.Ready;
|
player.State = State.Ready;
|
||||||
return Players;
|
return Players;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetAllInGame()
|
||||||
|
{
|
||||||
|
foreach (var player in Players) player.State = State.InGame;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user