From fa04aab6c654821e1b734cbc011ca2d258cc6062 Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Thu, 6 Apr 2023 19:15:28 -0700 Subject: [PATCH] Add adjudicate command and implement AdjudicateOrders --- .../Adjudicate/IPhaseAdjudicator.cs | 12 ++++++++ .../Adjudicate/MovementPhaseAdjudicator.cs | 5 ++++ MultiversalDiplomacy/GameController.cs | 29 ++++++++++++++++--- MultiversalDiplomacy/Model/Phase.cs | 9 ++++++ MultiversalDiplomacy/Model/World.cs | 11 +++++++ .../Script/GameScriptHandler.cs | 5 ++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 MultiversalDiplomacy/Model/Phase.cs diff --git a/MultiversalDiplomacy/Adjudicate/IPhaseAdjudicator.cs b/MultiversalDiplomacy/Adjudicate/IPhaseAdjudicator.cs index 5ab155a..6a1c5cf 100644 --- a/MultiversalDiplomacy/Adjudicate/IPhaseAdjudicator.cs +++ b/MultiversalDiplomacy/Adjudicate/IPhaseAdjudicator.cs @@ -8,6 +8,18 @@ namespace MultiversalDiplomacy.Adjudicate; /// public interface IPhaseAdjudicator { + /// + /// Given a list of order sets, determine which entries are comprehensible as orders. + /// An order set entry may comprehensible as an order but not valid for the current + /// phase; these orders will be rejected by . + /// + /// The global game state. + /// The order sets to adjudicate. + /// + /// A list of objects representing the orders parsed. + /// + public List ParseOrderSets(World world, List orderSets); + /// /// Given a list of orders, determine which orders are valid for this adjudicator and /// which should be rejected before adjudication. Adjudication should be performed on diff --git a/MultiversalDiplomacy/Adjudicate/MovementPhaseAdjudicator.cs b/MultiversalDiplomacy/Adjudicate/MovementPhaseAdjudicator.cs index bc675c7..34bec81 100644 --- a/MultiversalDiplomacy/Adjudicate/MovementPhaseAdjudicator.cs +++ b/MultiversalDiplomacy/Adjudicate/MovementPhaseAdjudicator.cs @@ -19,6 +19,11 @@ public class MovementPhaseAdjudicator : IPhaseAdjudicator this.logger = logger; } + public List ParseOrderSets(World world, List orderSets) + { + throw new NotImplementedException(); + } + public List ValidateOrders(World world, List orders) { // The basic workflow of this function will be to look for invalid orders, remove these diff --git a/MultiversalDiplomacy/GameController.cs b/MultiversalDiplomacy/GameController.cs index 21864b8..c557c68 100644 --- a/MultiversalDiplomacy/GameController.cs +++ b/MultiversalDiplomacy/GameController.cs @@ -1,4 +1,6 @@ +using MultiversalDiplomacy.Adjudicate; using MultiversalDiplomacy.Model; +using MultiversalDiplomacy.Orders; namespace MultiversalDiplomacy; @@ -21,9 +23,28 @@ public static class GameController public static World AdjudicateOrders(World world) { - // TODO: Parse the order sets into orders - // TODO: Execute the correct adjudicator for the current world state - // TODO: Update the world - return 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 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); } } \ No newline at end of file diff --git a/MultiversalDiplomacy/Model/Phase.cs b/MultiversalDiplomacy/Model/Phase.cs new file mode 100644 index 0000000..3e95617 --- /dev/null +++ b/MultiversalDiplomacy/Model/Phase.cs @@ -0,0 +1,9 @@ +namespace MultiversalDiplomacy.Model; + +public enum PhaseType +{ + Movement = 1, + Retreat = 2, + Build = 3, + Sustain = 4, +} diff --git a/MultiversalDiplomacy/Model/World.cs b/MultiversalDiplomacy/Model/World.cs index 0d0907d..ab9803f 100644 --- a/MultiversalDiplomacy/Model/World.cs +++ b/MultiversalDiplomacy/Model/World.cs @@ -309,6 +309,17 @@ public class World return foundUnit; } + public PhaseType GetNextPhaseType() + { + // TODO: Figure how to order build and sustain phases in a staggered multiverse + if (RetreatingUnits.Any()) + { + return PhaseType.Retreat; + } + + return PhaseType.Movement; + } + /// /// The standard Diplomacy provinces. /// diff --git a/MultiversalDiplomacy/Script/GameScriptHandler.cs b/MultiversalDiplomacy/Script/GameScriptHandler.cs index 1ef0659..01a2b8a 100644 --- a/MultiversalDiplomacy/Script/GameScriptHandler.cs +++ b/MultiversalDiplomacy/Script/GameScriptHandler.cs @@ -30,11 +30,16 @@ public class GameScriptHandler : IScriptHandler case "help": case "?": Console.WriteLine("commands:"); + Console.WriteLine(" adjudicate: adjudicate the current orders"); Console.WriteLine(" list: list things in a game category"); Console.WriteLine(" orders: submit order sets"); Console.WriteLine(" status: overview of the state of the game"); break; + case "adjudicate": + World = GameController.AdjudicateOrders(World); + break; + case "list" when args.Length == 1: Console.WriteLine("usage:"); Console.WriteLine(" list ordersets: unadjudicated order sets");