Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
GameClock Class Reference

Single owner of game frequency and elapsed game time. More...

#include <GameClock.hpp>

+ Collaboration diagram for GameClock:

Classes

struct  Stamp
 A clock reading: wall time and ticks since boot, captured at one instant. More...
 

Public Member Functions

 GameClock (int freq)
 
int freq () const
 
std::chrono::microseconds elapsed () const
 Wall-clock time since construction.
 
double ticks () const
 Total ticks elapsed: integral of freq over time. Fractional by nature.
 
float setFreq (int newFreq)
 Change frequency, banking ticks accrued at the old rate first.
 
void recordJoin (const std::string &team)
 
std::optional< StampjoinOf (const std::string &team) const
 The join stamp for team, or nullopt if the team never joined.
 

Private Member Functions

double _ticksSince (std::chrono::steady_clock::time_point from) const
 

Private Attributes

int _freq
 
std::chrono::steady_clock::time_point _startTime
 
std::chrono::steady_clock::time_point _lastFreqChange
 
double _accumTicks = 0.0
 
std::unordered_map< std::string, Stamp_joins
 

Detailed Description

Single owner of game frequency and elapsed game time.

A tick is one time unit, lasting 1/freq seconds. The server is wall-clock driven, so ticks are not counted one by one: instead this integrates freq over time. Every freq change (GUI sst) banks the ticks accrued so far at the old rate, so ticks() stays correct across any number of changes.

Also stamps when each team first joined, so win timing can be measured from the team's entry rather than from server boot (which includes idle lobby time before any AI connected).

See also
server/doc.md - "core/GameClock"

Definition at line 22 of file GameClock.hpp.

Constructor & Destructor Documentation

◆ GameClock()

GameClock::GameClock ( int  freq)
explicit

Definition at line 3 of file GameClock.cpp.

Member Function Documentation

◆ _ticksSince()

double GameClock::_ticksSince ( std::chrono::steady_clock::time_point  from) const
private

Definition at line 14 of file GameClock.cpp.

References _freq.

Referenced by setFreq(), and ticks().

+ Here is the caller graph for this function:

◆ elapsed()

std::chrono::microseconds GameClock::elapsed ( ) const

Wall-clock time since construction.

Definition at line 8 of file GameClock.cpp.

References _startTime.

Referenced by CommandDispatcher::gameElapsed(), and recordJoin().

+ Here is the caller graph for this function:

◆ freq()

◆ joinOf()

std::optional< GameClock::Stamp > GameClock::joinOf ( const std::string &  team) const

The join stamp for team, or nullopt if the team never joined.

Definition at line 37 of file GameClock.cpp.

References _joins.

Referenced by CommandDispatcher::teamJoin().

+ Here is the caller graph for this function:

◆ recordJoin()

void GameClock::recordJoin ( const std::string &  team)

Stamp a team's join moment. Write-once: later calls for the same team are ignored, so it keeps the first player's entry.

Definition at line 31 of file GameClock.cpp.

References _joins, elapsed(), and ticks().

Referenced by CommandDispatcher::_onAiJoined().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFreq()

float GameClock::setFreq ( int  newFreq)

Change frequency, banking ticks accrued at the old rate first.

Returns
oldFreq / newFreq, the ratio to feed Scheduler::rescale().

Definition at line 22 of file GameClock.cpp.

References _accumTicks, _freq, _lastFreqChange, and _ticksSince().

Referenced by CommandDispatcher::_handleSst().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ticks()

double GameClock::ticks ( ) const

Total ticks elapsed: integral of freq over time. Fractional by nature.

Definition at line 20 of file GameClock.cpp.

References _accumTicks, _lastFreqChange, and _ticksSince().

Referenced by CommandDispatcher::_handleStu(), CommandDispatcher::gameTicks(), and recordJoin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ _accumTicks

double GameClock::_accumTicks = 0.0
private

Definition at line 59 of file GameClock.hpp.

Referenced by setFreq(), and ticks().

◆ _freq

int GameClock::_freq
private

Definition at line 56 of file GameClock.hpp.

Referenced by _ticksSince(), freq(), and setFreq().

◆ _joins

std::unordered_map<std::string, Stamp> GameClock::_joins
private

Definition at line 60 of file GameClock.hpp.

Referenced by joinOf(), and recordJoin().

◆ _lastFreqChange

std::chrono::steady_clock::time_point GameClock::_lastFreqChange
private

Definition at line 58 of file GameClock.hpp.

Referenced by setFreq(), and ticks().

◆ _startTime

std::chrono::steady_clock::time_point GameClock::_startTime
private

Definition at line 57 of file GameClock.hpp.

Referenced by elapsed().


The documentation for this class was generated from the following files: