Moved actions to a Service
This commit is contained in:
parent
ead816849f
commit
eed4f50313
@ -1,9 +1,6 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using pacMan.Game;
|
||||
using pacMan.Game.Interfaces;
|
||||
using pacMan.Game.Items;
|
||||
using pacMan.Interfaces;
|
||||
using pacMan.Services;
|
||||
using pacMan.Utils;
|
||||
@ -14,12 +11,11 @@ namespace pacMan.Controllers;
|
||||
[Route("api/[controller]")]
|
||||
public class GameController : GenericController
|
||||
{
|
||||
private readonly IDiceCup _diceCup;
|
||||
private GameGroup _group = new();
|
||||
private IPlayer? _player;
|
||||
private readonly IActionService _actionService;
|
||||
|
||||
public GameController(ILogger<GameController> logger, IWebSocketService wsService) : base(logger, wsService) =>
|
||||
_diceCup = new DiceCup();
|
||||
public GameController(ILogger<GameController> logger, IWebSocketService wsService, IActionService actionService) :
|
||||
base(logger, wsService) =>
|
||||
_actionService = actionService;
|
||||
|
||||
[HttpGet]
|
||||
public override async Task Accept() => await base.Accept();
|
||||
@ -31,45 +27,7 @@ public class GameController : GenericController
|
||||
Logger.Log(LogLevel.Information, "Received: {}", stringResult);
|
||||
var action = ActionMessage.FromJson(stringResult);
|
||||
|
||||
DoAction(action);
|
||||
_actionService.DoAction(action);
|
||||
return action.ToArraySegment();
|
||||
}
|
||||
|
||||
private void DoAction(ActionMessage message)
|
||||
{
|
||||
switch (message.Action)
|
||||
{
|
||||
case GameAction.RollDice:
|
||||
var rolls = _diceCup.Roll();
|
||||
Logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
|
||||
|
||||
message.Data = rolls;
|
||||
break;
|
||||
case GameAction.PlayerInfo:
|
||||
_player = JsonSerializer.Deserialize<Player>(message.Data);
|
||||
_group = WsService.AddPlayer(_player); // TODO missing some data?
|
||||
|
||||
message.Data = _group.Players;
|
||||
break;
|
||||
case GameAction.Ready:
|
||||
if (_player != null)
|
||||
{
|
||||
var players = _group.SetReady(_player).ToArray();
|
||||
if (players.All(p => p.State == State.Ready))
|
||||
// TODO roll to start
|
||||
message.Data = new { AllReady = true, Starter = _group.RandomPlayer };
|
||||
else
|
||||
message.Data = new { AllReady = false, players };
|
||||
}
|
||||
else
|
||||
{
|
||||
message.Data = "Player not found, please create a new player";
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
Logger.Log(LogLevel.Information, "Forwarding message to all clients");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,16 +6,15 @@ var builder = WebApplication.CreateBuilder(args);
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllersWithViews();
|
||||
builder.Services.AddSingleton<IWebSocketService, WebSocketService>();
|
||||
builder.Services.AddSingleton<IWebSocketService, WebSocketService>()
|
||||
.AddTransient<IActionService, ActionService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
@ -23,9 +22,9 @@ app.UseRouting();
|
||||
app.UseWebSockets();
|
||||
|
||||
app.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller}/{action=Index}/{id?}");
|
||||
"default",
|
||||
"{controller}/{action=Index}/{id?}");
|
||||
|
||||
app.MapFallbackToFile("index.html");
|
||||
|
||||
app.Run();
|
||||
app.Run();
|
||||
|
85
pac-man-board-game/Services/ActionService.cs
Normal file
85
pac-man-board-game/Services/ActionService.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System.Text.Json;
|
||||
using pacMan.Game;
|
||||
using pacMan.Game.Interfaces;
|
||||
using pacMan.Game.Items;
|
||||
using pacMan.Interfaces;
|
||||
|
||||
namespace pacMan.Services;
|
||||
|
||||
public interface IActionService
|
||||
{
|
||||
void DoAction(ActionMessage message);
|
||||
void RollDice(ActionMessage message);
|
||||
void PlayerInfo(ActionMessage message);
|
||||
void Ready(ActionMessage message);
|
||||
}
|
||||
|
||||
public class ActionService : IActionService // TODO tests
|
||||
{
|
||||
private readonly IDiceCup _diceCup;
|
||||
private readonly ILogger<ActionService> _logger;
|
||||
private readonly IWebSocketService _wsService;
|
||||
|
||||
private GameGroup _group = new();
|
||||
private IPlayer? _player;
|
||||
|
||||
public ActionService(ILogger<ActionService> logger, IWebSocketService wsService)
|
||||
{
|
||||
_logger = logger;
|
||||
_diceCup = new DiceCup();
|
||||
_wsService = wsService;
|
||||
}
|
||||
|
||||
public void DoAction(ActionMessage message)
|
||||
{
|
||||
switch (message.Action)
|
||||
{
|
||||
case GameAction.RollDice:
|
||||
RollDice(message);
|
||||
break;
|
||||
case GameAction.PlayerInfo:
|
||||
PlayerInfo(message);
|
||||
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)
|
||||
{
|
||||
var rolls = _diceCup.Roll();
|
||||
_logger.Log(LogLevel.Information, "Rolled [{}]", string.Join(", ", rolls));
|
||||
|
||||
message.Data = rolls;
|
||||
}
|
||||
|
||||
public void PlayerInfo(ActionMessage message)
|
||||
{
|
||||
_player = JsonSerializer.Deserialize<Player>(message.Data);
|
||||
_group = _wsService.AddPlayer(_player); // TODO missing some data?
|
||||
|
||||
message.Data = _group.Players;
|
||||
}
|
||||
|
||||
public void Ready(ActionMessage message)
|
||||
{
|
||||
if (_player != null)
|
||||
{
|
||||
var players = _group.SetReady(_player).ToArray();
|
||||
if (players.All(p => p.State == State.Ready))
|
||||
// TODO roll to start
|
||||
message.Data = new { AllReady = true, Starter = _group.RandomPlayer };
|
||||
else
|
||||
message.Data = new { AllReady = false, players };
|
||||
}
|
||||
else
|
||||
{
|
||||
message.Data = "Player not found, please create a new player";
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ using pacMan.Game.Interfaces;
|
||||
|
||||
namespace pacMan.Services;
|
||||
|
||||
public class GameGroup
|
||||
public class GameGroup // TODO tests
|
||||
{
|
||||
private readonly Random _random = new();
|
||||
public List<IPlayer> Players { get; } = new();
|
||||
|
@ -5,7 +5,7 @@ using pacMan.Utils;
|
||||
|
||||
namespace pacMan.Services;
|
||||
|
||||
public class WebSocketService : IWebSocketService
|
||||
public class WebSocketService : IWebSocketService // TODO add tests
|
||||
{
|
||||
private readonly ILogger<WebSocketService> _logger;
|
||||
|
||||
@ -54,10 +54,7 @@ public class WebSocketService : IWebSocketService
|
||||
_logger.Log(LogLevel.Information, "WebSocket connection closed");
|
||||
}
|
||||
|
||||
public int CountConnected()
|
||||
{
|
||||
return Connections?.GetInvocationList().Length ?? 0;
|
||||
}
|
||||
public int CountConnected() => Connections?.GetInvocationList().Length ?? 0;
|
||||
|
||||
public GameGroup AddPlayer(IPlayer player)
|
||||
{
|
||||
@ -75,4 +72,4 @@ public class WebSocketService : IWebSocketService
|
||||
|
||||
return Games[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user