TableturfBattleApp/TableturfBattleServer/Card.cs
Andrio Celos d366d955af Add upcoming cards
Upcoming cards are identified by a negative number. The pre-release number will be preserved as an alt number after release to avoid breaking saved deck and replay data that references it.
2023-08-18 21:36:19 +10:00

70 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
namespace TableturfBattleServer;
public class Card {
[JsonProperty("number")]
public int Number { get; }
[JsonProperty("altNumber")]
public int? AltNumber { get; }
[JsonProperty("name")]
public string Name { get; }
[JsonProperty("rarity")]
public Rarity Rarity { get; }
[JsonProperty("specialCost")]
public int SpecialCost { get; }
[JsonIgnore]
public int Size { get; }
[JsonProperty("grid")]
private readonly Space[,] grid;
internal Card(int number, string name, Rarity rarity, Space[,] grid) : this(number, null, name, rarity, null, grid) { }
internal Card(int number, int? altNumber, string name, Rarity rarity, Space[,] grid) : this(number, altNumber, name, rarity, null, grid) { }
internal Card(int number, string name, Rarity rarity, int? specialCost, Space[,] grid) : this(number, null, name, rarity, specialCost, grid) { }
internal Card(int number, int? altNumber, string name, Rarity rarity, int? specialCost, Space[,] grid) {
this.Number = number;
this.AltNumber = altNumber;
this.Name = name ?? throw new ArgumentNullException(nameof(name));
this.Rarity = rarity;
this.grid = grid ?? throw new ArgumentNullException(nameof(grid));
var size = 0;
if (grid.GetUpperBound(0) != 7 || grid.GetUpperBound(1) != 7)
throw new ArgumentException("Grid must be 8 × 8.", nameof(grid));
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
switch (grid[x, y]) {
case Space.Empty:
break;
case Space.Ink1:
case Space.SpecialInactive1:
size++;
break;
default:
throw new ArgumentException("Grid contains invalid values.", nameof(grid));
}
}
}
this.Size = size;
this.SpecialCost = specialCost ?? size switch { <= 3 => 1, <= 5 => 2, <= 8 => 3, <= 11 => 4, <= 15 => 5, _ => 6 };
}
/// <summary>Returns the space in the specified position on the card grid when rotated in the specified manner.</summary>
/// <param name="x">The number of spaces right from the top left corner.</param>
/// <param name="y">The number of spaces down from the top left corner.</param>
/// <param name="rotation">The number of clockwise rotations.</param>
public Space GetSpace(int x, int y, int rotation) {
if (x is < 0 or >= 8)
throw new ArgumentOutOfRangeException(nameof(x));
if (y is < 0 or >= 8)
throw new ArgumentOutOfRangeException(nameof(y));
return (rotation & 3) switch {
0 => this.grid[x, y],
1 => this.grid[y, 7 - x],
2 => this.grid[7 - x, 7 - y],
_ => this.grid[7 - y, x],
};
}
}