5dplomacy/MultiversalDiplomacy/GameController.cs

50 lines
1.8 KiB
C#
Raw Permalink Normal View History

using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacy;
/// <summary>
/// The game controller is a stateless class that defines the basic, high-level operations that can be taken to modify
/// the game state.
/// </summary>
public static class GameController
{
public static World InitializeWorld()
{
return World.Standard;
}
public static World SubmitOrderSet(World world, string text)
{
var orderSet = new OrderSet(text);
return world.Update(orderSets: world.OrderSets.Append(orderSet));
}
public static World AdjudicateOrders(World world)
{
// Determine which phase the game is in, which determines how orders should be interpreted and adjudicated.
PhaseType phaseType = world.GetNextPhaseType();
IPhaseAdjudicator adjudicator = phaseType switch {
PhaseType.Movement => MovementPhaseAdjudicator.Instance,
PhaseType.Retreat => throw new NotImplementedException(),
PhaseType.Build => throw new NotImplementedException(),
PhaseType.Sustain => throw new NotImplementedException(),
_ => throw new InvalidOperationException(phaseType.ToString()),
};
// Parse the order sets into actual orders.
List<Order> parsedOrders = adjudicator.ParseOrderSets(world, world.OrderSets.ToList());
// Validate the orders.
var orderValidations = adjudicator.ValidateOrders(world, parsedOrders);
// Adjudicate the orders.
var validOrders = orderValidations.Where(v => v.Valid).Select(v => v.Order).ToList();
var results = adjudicator.AdjudicateOrders(world, validOrders);
// Update the world.
return adjudicator.UpdateWorld(world, results);
}
}