Skip to content

Commit

Permalink
Limit the number of test players, add the Rail Gun
Browse files Browse the repository at this point in the history
  • Loading branch information
snow-jallen committed Feb 16, 2024
1 parent 4736c10 commit f6b9973
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 32 deletions.
28 changes: 28 additions & 0 deletions src/SpaceWars.Logic/Exceptions/TooManyPlayersException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SpaceWars.Logic.Exceptions;

[Serializable]
public class TooManyPlayersException : Exception
{
public TooManyPlayersException()
{
}

public TooManyPlayersException(string? message) : base(message)
{
}

public TooManyPlayersException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected TooManyPlayersException(SerializationInfo info, StreamingContext context) : base(info, context)

Check warning on line 25 in src/SpaceWars.Logic/Exceptions/TooManyPlayersException.cs

View workflow job for this annotation

GitHub Actions / build

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)
{
}
}
15 changes: 14 additions & 1 deletion src/SpaceWars.Logic/Game.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using SpaceWars.Logic.Actions;
using SpaceWars.Logic.Exceptions;
using SpaceWars.Logic.Weapons;

namespace SpaceWars.Logic;

public class Game
{
private readonly List<IPurchasable> Shop = [new BasicCannon(), new PowerFist()];
public const int MaxPlayerCount = 100;

private readonly List<IPurchasable> Shop = [
new BasicCannon(),
new PowerFist(),
new RailGun()
];

private readonly Dictionary<PlayerToken, Player> players;
private readonly IInitialLocationProvider locationProvider;
private ITimer? timer;
Expand All @@ -29,6 +37,11 @@ public Game(IInitialLocationProvider locationProvider, ITimer gameTimer = null,

public GameJoinResult Join(string playerName)
{
if(players.Count > MaxPlayerCount)
{
throw new TooManyPlayersException();
}

var newPlayer = new Player(playerName, new Ship(locationProvider.GetNewInitialLocation(BoardWidth, BoardHeight)));
players.Add(newPlayer.Token, newPlayer);

Expand Down
1 change: 0 additions & 1 deletion src/SpaceWars.Logic/Weapons/PowerFist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ public PowerFist() : base("Power Fist", "Close-range heavy-hitter.")
PurchaseCost = 500;
}
}

15 changes: 15 additions & 0 deletions src/SpaceWars.Logic/Weapons/RailGun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SpaceWars.Logic.Weapons;

public class RailGun : Weapon
{
public RailGun() : base("Rail Gun", "Long-range weapon with high damage.")
{
Ranges = [
new WeaponRange(500, 100),
new WeaponRange(600, 50),
new WeaponRange(700, 25)
];
MaxDamage = 10;
PurchaseCost = 1_050;
}
}
16 changes: 11 additions & 5 deletions src/SpaceWars.Web/Components/Pages/Hud.razor
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ else
<div class="mb-2">
<PlayerActionQueue Player="Player" />
</div>
<button class="btn btn-primary" @onclick="JoinTestPlayers">Join Test Players</button>
@if(showJoinTestPlayersButton)
{
<button class="btn btn-primary" @onclick="JoinTestPlayers">Join Test Players</button>
}
</div>
<div class="col-12 col-xl-6">
<NearbyMap Game="@Game" CurrentPlayer="@Player" />
Expand All @@ -56,11 +59,14 @@ else
public PlayerTokenModel TokenModel { get; set; } = new PlayerTokenModel();
public PlayerToken? PlayerToken = null;
public bool IsValidToken = false;
bool showJoinTestPlayersButton = true;
const int MaxTestPlayers = 20;

protected override void OnInitialized()
{
GetPlayer();
Game.Ticked += GameTicked;
showJoinTestPlayersButton = Game.PlayerLocations.Count() < MaxTestPlayers;
}

public void GameTicked(object? sender, EventArgs e)
Expand Down Expand Up @@ -115,7 +121,6 @@ else
var query = QueryHelpers.ParseQuery(uri.Query);
PlayerToken = null;


if (query.TryGetValue("token", out var token))
{
if (string.IsNullOrWhiteSpace(token))
Expand All @@ -142,15 +147,16 @@ else

public void JoinTestPlayers()
{
for (int i = 0; i < 10; i++)
showJoinTestPlayersButton = Game.PlayerLocations.Count() < MaxTestPlayers;
var playersToJoin = MaxTestPlayers - Game.PlayerLocations.Count();
for (int i = 0; i < playersToJoin; i++)
{
Game.Join($"Test Player {i}");
}
}
}

public class PlayerTokenModel
{
public string TokenValue { get; set; }
}

}
2 changes: 1 addition & 1 deletion src/SpaceWars.Web/Controllers/GameController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SpaceWars.Logic;
using SpaceWars.Logic.Exceptions;
using SpaceWars.Logic.Actions;
using SpaceWars.Web.Exceptions;
using SpaceWars.Web.Types;

namespace SpaceWars.Web.Controllers;
Expand Down
24 changes: 0 additions & 24 deletions src/SpaceWars.Web/Exceptions/TooManyPlayersException.cs

This file was deleted.

4 changes: 4 additions & 0 deletions src/SpaceWars.Web/SpaceWars.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<ProjectReference Include="..\SpaceWars.Web.Types\SpaceWars.Web.Types.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Exceptions\" />
</ItemGroup>

</Project>

0 comments on commit f6b9973

Please sign in to comment.