initialize repo
This commit is contained in:
133
Lib/Civilization.Server/.gitignore
vendored
Normal file
133
Lib/Civilization.Server/.gitignore
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.sln.docstates
|
||||
|
||||
# Build results
|
||||
|
||||
[Dd]ebug/
|
||||
[Rr]elease/
|
||||
x64/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.log
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.Publish.xml
|
||||
*.pubxml
|
||||
*.azurePubxml
|
||||
|
||||
# NuGet Packages Directory
|
||||
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
|
||||
packages/
|
||||
## TODO: If the tool you use requires repositories.config, also uncomment the next line
|
||||
!packages/repositories.config
|
||||
|
||||
# Windows Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
|
||||
# Others
|
||||
sql/
|
||||
*.Cache
|
||||
ClientBin/
|
||||
[Ss]tyle[Cc]op.*
|
||||
![Ss]tyle[Cc]op.targets
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.[Pp]ublish.xml
|
||||
|
||||
*.publishsettings
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file to a newer
|
||||
# Visual Studio version. Backup files are not needed, because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
App_Data/*.mdf
|
||||
App_Data/*.ldf
|
||||
|
||||
# =========================
|
||||
# Windows detritus
|
||||
# =========================
|
||||
|
||||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Mac desktop service store files
|
||||
.DS_Store
|
||||
|
||||
_NCrunch*
|
15
Lib/Civilization.Server/Civilization.Server.csproj
Normal file
15
Lib/Civilization.Server/Civilization.Server.csproj
Normal file
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Civilization.Core\Civilization.Core.csproj" />
|
||||
<ProjectReference Include="..\Civilization.Shared\Civilization.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
68
Lib/Civilization.Server/Game/GameSession.cs
Normal file
68
Lib/Civilization.Server/Game/GameSession.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Civilization.Core;
|
||||
using Civilization.Core.Actions;
|
||||
using Civilization.Core.Game;
|
||||
using Civilization.Core.Grid;
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Core.Units;
|
||||
using Civilization.Shared.Commands;
|
||||
using Civilization.Shared.Packets;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Game;
|
||||
|
||||
public class GameSession
|
||||
{
|
||||
private const int DefaultSize = 10;
|
||||
private readonly GameState _state;
|
||||
private readonly GameSessionCoordinator _coordinator;
|
||||
|
||||
public GameState State => _state;
|
||||
|
||||
public GameSession(GameSessionCoordinator coordinator)
|
||||
{
|
||||
_coordinator = coordinator;
|
||||
var grid = new SquareGrid(DefaultSize, DefaultSize);
|
||||
var map = new GameMap(grid);
|
||||
|
||||
var players = new List<Player>
|
||||
{
|
||||
new(0, "Player 1", new ColorRGBA(255, 0, 0)),
|
||||
new(1, "Player 2", new ColorRGBA(0, 0, 255)),
|
||||
};
|
||||
|
||||
_state = new GameState(map, players);
|
||||
|
||||
_state.AddUnit(new Unit(0, UnitType.Settler, new Vec2I(2, 2)));
|
||||
}
|
||||
|
||||
public Task ProcessCommand(ClientMessage msg)
|
||||
{
|
||||
var context = new GameActionContext(_state);
|
||||
|
||||
switch (msg.Command)
|
||||
{
|
||||
case MoveUnitCommand move:
|
||||
EnqueueAndExecute(new MoveUnitAction(move.UnitId, move.TargetPosition), context);
|
||||
break;
|
||||
case SettleCityCommand settle:
|
||||
EnqueueAndExecute(new SettleCityAction(settle.UnitId), context);
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"Command type {msg.Command.GetType()} is not supported.");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void EnqueueAndExecute(IGameAction action, GameActionContext context)
|
||||
{
|
||||
if (!action.CanExecute(context))
|
||||
{
|
||||
Console.WriteLine($"Rejected invalid command from player {context.CurrentPlayer.Id}");
|
||||
return;
|
||||
}
|
||||
|
||||
_state.ActionQueue.Enqueue(action);
|
||||
_state.ActionQueue.ExecuteAll(context);
|
||||
}
|
||||
}
|
115
Lib/Civilization.Server/Game/GameSessionCoordinator.cs
Normal file
115
Lib/Civilization.Server/Game/GameSessionCoordinator.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using Civilization.Core.Interfaces;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared.Commands;
|
||||
using Civilization.Shared.Packets;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Game;
|
||||
|
||||
public class GameSessionCoordinator
|
||||
{
|
||||
private readonly GameSession _session;
|
||||
private readonly Dictionary<int, bool> _connectedPlayers = new();
|
||||
private readonly Dictionary<int, IClientConnection> _connections = new();
|
||||
|
||||
public GameSessionCoordinator()
|
||||
{
|
||||
_session = new GameSession(this);
|
||||
}
|
||||
|
||||
public Task RegisterPlayerAsync(int playerId, IClientConnection connection)
|
||||
{
|
||||
_connectedPlayers[playerId] = true;
|
||||
_connections[playerId] = connection;
|
||||
|
||||
Console.WriteLine($"Player {playerId} registered.");
|
||||
|
||||
// Send initial log message
|
||||
return connection.SendAsync(new LogMessage("Welcome to the game!"));
|
||||
}
|
||||
|
||||
public async Task ReceiveCommandAsync(ClientMessage message)
|
||||
{
|
||||
if (!_connectedPlayers.ContainsKey(message.PlayerId))
|
||||
{
|
||||
Console.WriteLine($"Rejected command from unregistered player {message.PlayerId}.");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (message.Command)
|
||||
{
|
||||
case EndTurnCommand:
|
||||
await HandleEndTurnAsync(message.PlayerId);
|
||||
break;
|
||||
|
||||
default:
|
||||
try
|
||||
{
|
||||
await _session.ProcessCommand(message);
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
await BroadcastAsync(new StateUpdateMessage(_session.State, currentPlayerInfo));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Player {message.PlayerId}] Error: {ex.Message}");
|
||||
await SendToPlayerAsync(message.PlayerId, new ErrorMessage(ex.Message));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendToPlayerAsync(int playerId, BaseServerMessage message)
|
||||
{
|
||||
if (_connections.TryGetValue(playerId, out var conn))
|
||||
{
|
||||
await conn.SendAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastAsync(BaseServerMessage message)
|
||||
{
|
||||
foreach (var conn in _connections.Values)
|
||||
{
|
||||
await conn.SendAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task BroadcastStateUpdateAsync()
|
||||
{
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
var msg = new StateUpdateMessage(state, currentPlayerInfo);
|
||||
await BroadcastAsync(msg);
|
||||
}
|
||||
|
||||
public async Task OnTurnAdvancedAsync()
|
||||
{
|
||||
var state = _session.State;
|
||||
var currentPlayerInfo = new PlayerInfo(state.CurrentPlayer.Id, state.CurrentPlayer.Name, state.CurrentPlayer.Color.ToHex());
|
||||
var message = new StateUpdateMessage(_session.State, currentPlayerInfo);
|
||||
await BroadcastAsync(message);
|
||||
}
|
||||
|
||||
public async Task HandleEndTurnAsync(int playerId)
|
||||
{
|
||||
if (playerId != _session.State.CurrentPlayer.Id)
|
||||
{
|
||||
Console.WriteLine($"[Turn] Player {playerId} attempted to end turn out of order.");
|
||||
await SendToPlayerAsync(playerId, new ErrorMessage("Not your turn."));
|
||||
return;
|
||||
}
|
||||
|
||||
_session.State.NextTurn();
|
||||
|
||||
Console.WriteLine($"[Turn] Player {playerId} ended their turn. Now it's Player {_session.State.CurrentPlayer.Id}'s turn.");
|
||||
|
||||
await BroadcastAsync(new LogMessage($"Player {playerId} ended their turn."));
|
||||
await OnTurnAdvancedAsync();
|
||||
}
|
||||
|
||||
// Later: Add methods like:
|
||||
// - BroadcastGameState()
|
||||
// - GetStateForPlayer(int playerId)
|
||||
// - NotifyTurnAdvance()
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Networking.Interfaces;
|
||||
|
||||
public interface IClientConnection
|
||||
{
|
||||
int PlayerId { get; }
|
||||
Task SendAsync(BaseServerMessage message);
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
using Civilization.Server.Game;
|
||||
|
||||
namespace Civilization.Server.Networking.Interfaces;
|
||||
|
||||
public interface ITransport
|
||||
{
|
||||
Task StartAsync(GameSessionCoordinator coordinator, CancellationToken cancellationToken = default);
|
||||
}
|
25
Lib/Civilization.Server/Networking/TcpClientConnection.cs
Normal file
25
Lib/Civilization.Server/Networking/TcpClientConnection.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Text.Json;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared;
|
||||
using Civilization.Shared.Packets.ServerMessages;
|
||||
|
||||
namespace Civilization.Server.Networking;
|
||||
|
||||
public class TcpClientConnection : IClientConnection
|
||||
{
|
||||
private readonly StreamWriter _writer;
|
||||
|
||||
public int PlayerId { get; }
|
||||
|
||||
public TcpClientConnection(int playerId, Stream stream)
|
||||
{
|
||||
PlayerId = playerId;
|
||||
_writer = new StreamWriter(stream) { AutoFlush = true };
|
||||
}
|
||||
|
||||
public Task SendAsync(BaseServerMessage message)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(message, SharedJson.Options);
|
||||
return _writer.WriteLineAsync(json);
|
||||
}
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
using Civilization.Server.Game;
|
||||
using Civilization.Server.Networking.Interfaces;
|
||||
using Civilization.Shared;
|
||||
using Civilization.Shared.Packets;
|
||||
|
||||
namespace Civilization.Server.Networking.Transports;
|
||||
|
||||
public class TcpTransport : ITransport
|
||||
{
|
||||
private readonly int _port;
|
||||
|
||||
public TcpTransport(int port = 9000)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
public async Task StartAsync(GameSessionCoordinator coordinator, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Any, _port);
|
||||
listener.Start();
|
||||
Console.WriteLine($"TCP transport listening on port {_port}");
|
||||
|
||||
var nextPlayerId = 0;
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var client = await listener.AcceptTcpClientAsync(cancellationToken);
|
||||
_ = HandleClientAsync(client, nextPlayerId++, coordinator, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleClientAsync(
|
||||
TcpClient client,
|
||||
int playerId,
|
||||
GameSessionCoordinator coordinator,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine($"Client {playerId} connected.");
|
||||
var stream = client.GetStream();
|
||||
var reader = new StreamReader(stream);
|
||||
var writer = new StreamWriter(stream) { AutoFlush = true };
|
||||
|
||||
await writer.WriteLineAsync($"{{\"info\": \"You are Player {playerId}\"}}");
|
||||
|
||||
var connection = new TcpClientConnection(playerId, stream);
|
||||
|
||||
await coordinator.RegisterPlayerAsync(playerId, connection);
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested && client.Connected)
|
||||
{
|
||||
var line = await reader.ReadLineAsync(cancellationToken);
|
||||
if (line is null)
|
||||
break;
|
||||
|
||||
try
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<ClientMessage>(line, SharedJson.Options);
|
||||
if (message is not null)
|
||||
{
|
||||
await coordinator.ReceiveCommandAsync(message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[Player {playerId}] Failed to process message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"Client {playerId} disconnected.");
|
||||
}
|
||||
}
|
7
Lib/Civilization.Server/Program.cs
Normal file
7
Lib/Civilization.Server/Program.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using Civilization.Server.Game;
|
||||
using Civilization.Server.Networking.Transports;
|
||||
|
||||
var transport = new TcpTransport();
|
||||
var coordinator = new GameSessionCoordinator();
|
||||
|
||||
await transport.StartAsync(coordinator);
|
Reference in New Issue
Block a user