Skip to main content

PlayerInteractionManager

A class in Root.Township, the global namespace.

Server-owned, saved per-player charge counters for limited interactions ("you can take 3 of these"), reset daily at a set time of day. Get it with the static instance.

Constructors​

MemberWhat it is
PlayerInteractionManager()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

MemberWhat it is
static PlayerInteractionManager instanceThe last created manager, set in Awake.
const int maxIDs2048, the ID range the sync messages can carry.
8 private and internal fields
MemberWhat it is
private Dictionary<int, int> chargesPerIdStarting charges for each registered id.
private static Logger loggerNLog logger for this class.
private int nextIDNext id GetId will hand out. Saved.
private float oldInteractionMultiplierLast seen community storage multiplier, to spot changes.
private Dictionary<int, Dictionary<int, PlayerInteractionManager.Tracker>> playersTrackersTrackers by user id, then by interaction id. Saved, and cleared once per game day.
private MethodSyncStruct<bool> syncFullMethod sync for a full or clear update.
private MethodSyncClass<PlayerInteractionManager.Tracker> syncMarkerMethod sync for one tracker.
private float timeOfDayToRefreshTime of day (0 to 1) when all trackers reset.

Events​

MemberWhat it is
event Action UpdatedAllTrackersRaised when all counters are cleared.
event Action<int> UpdatedTrackerRaised with the ID when one counter changes for the local player.

Methods​

MemberWhat it is
int CheckAvailableInteractions(IPlayer player, int id)Charges the player has left for an ID, or the full charge count if they have not used it yet.
void ClearTrackers()Resets every player's counters and tells clients. Runs daily, when the community storage multiplier setting changes, and from a progress console command.
int GetId(int charges)Registers a new counter ID with its charge count and returns the ID.
override void Initialize()Sets up the full sync and single counter sync messages.
override void InitializeAsServer(SpawnData spawnData)Server: schedules the daily reset and watches the server's community storage multiplier.
int Interact(Player player, int id, int interactions = 1)Server: uses up to interactions charges and returns how many were used. On a client it only returns the charges left.
override void OnConnected(Player player)Server: sends the joining player their counters.
void Refund(Player player, int id, int refund)Server: gives charges back to a player for an ID.
void SetCharges(int id, int charges)Changes the charge count of a registered ID. Players who already have a counter keep theirs.
5 private and internal methods
MemberWhat it is
private void Awake()Sets the static instance.
private bool GetTracker(Player player, int id, out PlayerInteractionManager.Tracker tracker)Server: gets or creates a player's tracker for an id, starting with that id's registered charges. False if the id isn't registered.
private void SettingsChanged(Settings obj)Server: clears all trackers when the community storage multiplier changes.
private void SyncFull(IPlayer player, Stream stream, bool isClear)Sends a clear to everyone, or a player's whole tracker list on connect. Client: applies it.
private void SyncMarker(IPlayer player, Stream stream, PlayerInteractionManager.Tracker arguments)Sends one tracker's id and charges left. Client: stores it under the logged-in user and raises UpdatedTracker.