Rules

IGorillaArenaRules is what a Match is played by. It is the one part the framework cannot supply: the framework knows that people take turns, your rules know what a turn does.

Nothing in the unit knows about tethering or FireMonkey, which is what makes a set of rules testable on its own — without a network and without a form.

Table of Contents

The Interface

IGorillaArenaRules = interface(IInterface)
  function  GetRulesInfo() : TGorillaArenaRulesInfo;

  procedure Reset(const AMembers : TGorillaArenaMembers);
  function  CurrentSeat() : Integer;
  procedure SubmitTurn(const ASeat : Integer; const AMove : TJSONObject);
  procedure MemberLeft(const ASeat : Integer);
  function  Finished() : Boolean;

  function  StateToJSON() : TJSONObject;
  procedure StateFromJSON(const AState : TJSONObject);

  function  Headline() : String;
  function  Scoreboard() : TGorillaArenaScoreboard;
end;

GetRulesInfo describes the rules without playing them — this is what a lobby lists:

Field
Id Stable key. It travels on the wire, so never translate it
Name What the lobby shows
Description One line under the name
MinMembers Fewer than this and the match will not start
MaxMembers More than this are not seated. 0 means no limit

Two Base Classes

Both implement the interface and bring the seating with them; pick by how the rules come into existence.

Class
TGorillaArenaRules A component — drop it on the form and point TGorillaArenaMatch.RulesComponent at it
TGorillaArenaCustomRules Reference counted, built on demand — for a registry of several games

They give you Reset and MemberLeft already done, plus two helpers:

NextPresentSeat(AFrom) The next seat that is still occupied, or -1 when nobody is left
PresentCount() How many are still here

Members is the seating as an array.

A Worked Example

A game in which everybody plays one card per round, highest wins:

type
  THighCardRules = class(TGorillaArenaRules)
    protected
      FSeat  : Integer;
      FScore : TArray<Integer>;
      FRound : Integer;

    public
      function  GetRulesInfo() : TGorillaArenaRulesInfo; override;
      procedure Reset(const AMembers : TGorillaArenaMembers); override;
      function  CurrentSeat() : Integer; override;
      procedure SubmitTurn(const ASeat : Integer;
        const AMove : TJSONObject); override;
      function  Finished() : Boolean; override;
      function  StateToJSON() : TJSONObject; override;
      procedure StateFromJSON(const AState : TJSONObject); override;
      function  Headline() : String; override;
      function  Scoreboard() : TGorillaArenaScoreboard; override;
  end;

function THighCardRules.GetRulesInfo() : TGorillaArenaRulesInfo;
begin
  Result.Id          := 'highcard';
  Result.Name        := 'High Card';
  Result.Description := 'One card each, highest takes the round';
  Result.MinMembers  := 2;
  Result.MaxMembers  := 6;
end;

procedure THighCardRules.Reset(const AMembers : TGorillaArenaMembers);
begin
  inherited;                       // fills the seating

  SetLength(FScore, Length(AMembers));
  FSeat  := 0;
  FRound := 1;
end;

function THighCardRules.CurrentSeat() : Integer;
begin
  if Finished() then
    Result := -1
  else
    Result := FSeat;
end;

procedure THighCardRules.SubmitTurn(const ASeat : Integer;
  const AMove : TJSONObject);
var LCard : Integer;
begin
  LCard := 0;
  if Assigned(AMove) and (AMove.GetValue('card') is TJSONNumber) then
    LCard := (AMove.GetValue('card') as TJSONNumber).AsInt;

  Inc(FScore[ASeat], LCard);

  FSeat := NextPresentSeat(ASeat);
  if FSeat <= 0 then                // back at the top: a round is done
    Inc(FRound);
end;

function THighCardRules.Finished() : Boolean;
begin
  Result := (FRound > 3) or (PresentCount() < 2);
end;

StateToJSON / StateFromJSON carry FScore, FSeat and FRound; Scoreboard turns them into rows.

Seating and Turn Order

Reset is handed the seating in the order the members are to play, and that order does not change again for the match. A member who leaves keeps their seat so the scoreboard can still show what they scored; MemberLeft marks them, and NextPresentSeat skips them from then on.

CurrentSeat must return -1 once the match is over, and Finished must agree with it. The host asks Finished first: when it says yes, an over message goes out and no turn is announced.

Watch for the case where everybody has left. NextPresentSeat returns -1 rather than spinning, and PresentCount() < 2 is usually the right thing to put into Finished.

Moves

A move is a TJSONObject your application builds and reads; the framework only carries it. It may be nil for rules that need no payload — check before reading.

The move reaches the rules only on the host. Whatever a client needs to see of it must travel in the state, or through OnRemoteMove if it is only decoration.

State

StateToJSON produces everything a client needs to render, and the caller owns the result. StateFromJSON applies what the host sent, and does not take ownership — copy out what you keep.

function THighCardRules.StateToJSON() : TJSONObject;
var LArr : TJSONArray;
    LVal : Integer;
begin
  Result := TJSONObject.Create();

  LArr := TJSONArray.Create();
  for LVal in FScore do
    LArr.Add(LVal);

  Result.AddPair('score', LArr);
  Result.AddPair('seat', TJSONNumber.Create(FSeat));
  Result.AddPair('round', TJSONNumber.Create(FRound));
end;

Keep it small: it goes out after every single move.

The Scoreboard

TGorillaArenaScoreRow = record
  Name    : String;
  Score   : Integer;   // the number that decides the ranking
  Detail  : String;    // free text next to it
  Current : Boolean;   // marks the member whose turn it is
  Gone    : Boolean;   // marks a member who left
end;

Headline() is the line above it. Both are pure presentation and are asked on every device, including the clients, which is why they have to work off the state alone.

Testing Rules Without a Network

Because the rules touch neither tethering nor FireMonkey, a console program is enough:

LRules := THighCardRules.Create(nil);
SetLength(LMembers, 2);
LMembers[0].Id := '{a}'; LMembers[0].Name := 'Anna'; LMembers[0].Here := true;
LMembers[1].Id := '{b}'; LMembers[1].Name := 'Ben';  LMembers[1].Here := true;

LRules.Reset(LMembers);
Assert(LRules.CurrentSeat() = 0);

LMove := TJSONObject.Create();
try
  LMove.AddPair('card', TJSONNumber.Create(10));
  LRules.SubmitTurn(0, LMove);
finally
  FreeAndNil(LMove);
end;

Assert(LRules.CurrentSeat() = 1);

The layer above can be exercised without a network too — see Session, where Transmit and RebuildMembers are virtual for exactly this purpose.

See Also