Class VoteBox

Namespace
Unity.Multiplayer.Core

The server's vote rules, engine-free: one vote at a time over a roster of client ids, worded and checked by the game's registered VoteKinds and decided by VoteRules. The roster when a vote starts is its voter list (a later joiner does not vote), the starter counts as Yes, and it passes as soon as Yes reaches the quorum: the kind's Passed effect runs, then Ended is raised. It is rejected as soon as the quorum can no longer be reached, and lapses after its time on the ITickClock. A voter who leaves (RemoveVoter(ulong)) is dropped and the quorum recomputed from who is left, which can pass or reject it at once.

public sealed class VoteBox : IDisposable
Inheritance
VoteBox
Implements
Inherited Members

Remarks

The networked VoteBoard runs one on the server; a game can also drive one directly (a bot, a test, a non-networked mode). Not thread-safe: call it from the clock's thread (Unity's main thread).

Constructors

VoteBox(ITickClock, VoteRules)

A box timed by clock (it subscribes to Ticked until disposed).

public VoteBox(ITickClock clock, VoteRules rules = null)

Parameters

clock ITickClock
rules VoteRules

Exceptions

ArgumentNullException

clock is null.

ArgumentOutOfRangeException

rules has a value out of range.

Properties

Clock

The clock votes are timed on.

public ITickClock Clock { get; }

Property Value

ITickClock

Current

The running vote, or null when there is none.

public Vote Current { get; }

Property Value

Vote

IsActive

True while a vote is running.

public bool IsActive { get; }

Property Value

bool

Kinds

Every registered kind.

public IReadOnlyCollection<VoteKind> Kinds { get; }

Property Value

IReadOnlyCollection<VoteKind>

Rules

The rules every vote follows (a kind may override the quorum and time). Changes apply to the next vote.

public VoteRules Rules { get; }

Property Value

VoteRules

Sequence

The running vote's number (1 for the first, counting up); the last vote's once it has ended; 0 before any.

public int Sequence { get; }

Property Value

int

Status

The running vote's status now, or null when there is none.

public VoteStatus Status { get; }

Property Value

VoteStatus

Methods

Cancel()

Calls off the running vote with no effect (Cancelled).

public bool Cancel()

Returns

bool

False when none was running.

Cast(ulong, bool)

Records voter's Yes or No. Voting again replaces the earlier choice; a player who is not a voter on the running vote (or no vote running) changes nothing.

public bool Cast(ulong voter, bool yes)

Parameters

voter ulong
yes bool

Returns

bool

True when the ballot counted.

ChoiceOf(ulong)

Where player stands on the running vote; NotAVoter when none runs.

public VoteChoice ChoiceOf(ulong player)

Parameters

player ulong

Returns

VoteChoice

CooldownSecondsLeft(ulong)

Whole seconds starter must still wait before starting a vote; 0 when they may.

public int CooldownSecondsLeft(ulong starter)

Parameters

starter ulong

Returns

int

Dispose()

Stops listening to the clock. A running vote is dropped without being reported.

public void Dispose()

Register(VoteKind)

Adds a kind, or replaces the one with the same id.

public VoteBox Register(VoteKind kind)

Parameters

kind VoteKind

Returns

VoteBox

Exceptions

ArgumentNullException

kind is null.

ArgumentOutOfRangeException

The kind's own quorum fraction or time is out of range.

RemoveVoter(ulong)

Drops player (who left) from the running vote and recomputes the quorum from the voters left, which can pass or reject it at once. A vote whose last voter leaves is Abandoned.

public void RemoveVoter(ulong player)

Parameters

player ulong

Start(VoteRequest, IEnumerable<ulong>)

Starts the vote request asks for, with roster (the connected players' client ids) as its voters. In order it is refused when a vote is running, the kind is unknown, the starter is not in the roster, MayStart says no, the starter is cooling down, the kind's Check refuses it, or nobody is left to vote. A one-voter room passes at once.

public VoteStartResult Start(VoteRequest request, IEnumerable<ulong> roster)

Parameters

request VoteRequest
roster IEnumerable<ulong>

Returns

VoteStartResult

Exceptions

ObjectDisposedException

The box was disposed.

TryGetKind(string, out VoteKind)

The kind registered as id.

public bool TryGetKind(string id, out VoteKind kind)

Parameters

id string
kind VoteKind

Returns

bool

Unregister(string)

Removes the kind with id. A running vote of that kind carries on.

public bool Unregister(string id)

Parameters

id string

Returns

bool

Events

Changed

Raised after every change to the running vote (start, ballot, departure, each second of the countdown) with its status, and with null once it ends.

public event Action<VoteStatus> Changed

Event Type

Action<VoteStatus>

Ended

Raised when a vote ends, after a passing vote's effect has run.

public event Action<VoteResult> Ended

Event Type

Action<VoteResult>