pub struct Replica { /* private fields */ }Expand description
A client’s local copy of a combat, for optimistic UIs.
Holds the state the server has confirmed plus this client’s commands
that are still in flight. Replica::state is the confirmed state with
the pending commands applied on top, so a user sees the result of their
action immediately. When the server’s events arrive (in its order, which
may put other players’ actions first) the pending commands are replayed
on top of the new confirmed state; if the server rejects one, it simply
disappears from the prediction.
Contains no IO, so any frontend (native or WASM) can drive it from whatever transport it has.
Implementations§
Source§impl Replica
impl Replica
Sourcepub fn new(snapshot: CombatSnapshot) -> Self
pub fn new(snapshot: CombatSnapshot) -> Self
A replica starting from snapshot, with nothing pending.
Sourcepub fn state(&self) -> &CombatState
pub fn state(&self) -> &CombatState
What to show: the confirmed state plus this client’s pending commands.
Sourcepub fn confirmed(&self) -> &CombatState
pub fn confirmed(&self) -> &CombatState
What the server has confirmed.
Sourcepub fn pending(&self) -> &[CommandEnvelope]
pub fn pending(&self) -> &[CommandEnvelope]
Commands sent but not yet confirmed or rejected.
Sourcepub fn submit(&mut self, envelope: CommandEnvelope) -> Result<(), CombatError>
pub fn submit(&mut self, envelope: CommandEnvelope) -> Result<(), CombatError>
Records a command about to be sent and applies its predicted outcome. Commands that are certainly invalid fail here, instantly, and should not be sent. Commands whose outcome needs a server dice roll, and undos, are accepted but only show up once the server answers.
Sourcepub fn receive(&mut self, message: ServerMessage) -> Received
pub fn receive(&mut self, message: ServerMessage) -> Received
Takes in a message from the server, replaying pending commands on top of what it confirms.