pong-game/PongGame/Hubs/PongHub.cs

77 lines
2.4 KiB
C#
Raw Normal View History

2022-11-03 12:29:35 +01:00
using Microsoft.AspNetCore.SignalR;
namespace PongGame.Hubs;
public class PongHub : Hub<IPongClient> {
private const string PLAYER_KEY = "PLAYER";
private readonly PongLobby Lobby;
private readonly ILogger<PongHub> Logger;
public PongHub(PongLobby lobby, ILogger<PongHub> logger) : base() {
Lobby = lobby;
Logger = logger;
}
private PongPlayer Player {
get => (Context.Items.TryGetValue(PLAYER_KEY, out var player) ? player as PongPlayer : null)
?? throw new InvalidProgramException("Player was not assigned at connection start!");
set => Context.Items[PLAYER_KEY] = value;
}
public override Task OnConnectedAsync() {
Player = Lobby.CreatePlayer();
Player.Client = Clients.Client(Context.ConnectionId);
Player.Username = "Anon";
return Task.CompletedTask;
}
private void AssertNotInRoom() {
if (Player.ConnectedRoom is PongRoom currentRoom)
throw new HubException($"User is already connected to room [{currentRoom}]");
}
private PongRoom AssertInRoom() {
if (Player.ConnectedRoom is not PongRoom currentRoom)
throw new HubException($"User is not in any room!");
return currentRoom;
}
2022-11-03 12:29:35 +01:00
public Task<string> CreateRoom() {
AssertNotInRoom();
var room = Lobby.CreateRoom(Player);
return Task.FromResult(room.ID);
}
public Task<string> JoinRoom(string roomId) {
AssertNotInRoom();
var room = Lobby.JoinRoom(Player, roomId);
return Task.FromResult(room.ID);
}
public Task MovePaddle(int dir) {
var room = AssertInRoom();
var direction = (PongPaddleDirection)dir;
if (!Enum.IsDefined(direction))
throw new HubException($"Invalid direction: {dir}!");
room.MovePaddle(Player, direction);
return Task.CompletedTask;
}
2022-11-03 12:29:35 +01:00
public Task LeaveRoom() {
Lobby.LeaveRoom(Player);
return Task.CompletedTask;
}
public Task RequestUsernameChange(string username) {
// TOOD: check this
2022-11-04 13:04:32 +01:00
Logger.LogInformation("Player {Player} requested username change to [{username}]", Player, username);
2022-11-03 12:29:35 +01:00
Player.Username = username;
return Task.CompletedTask;
}
public override Task OnDisconnectedAsync(Exception? exception) {
Lobby.RemovePlayer(Player);
return Task.CompletedTask;
}
}