Skip to main content

FriendRequestToken

A class in Root.Township, the global namespace.

A player's friend token: hand it to another player and, if both sides report the handover within 2 seconds, the server sends a friend request through the API. Not saved.

Constructors​

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

Fields​

16 private and internal fields
MemberWhat it is
private PositionEvent alreadyFriendsEffectEffect played when the players were already friends.
private IAnalyticsService analyticsServiceAnalytics service used to log friend requests.
private PositionEvent deniedFriendshipEffectEffect played when the request did not result in a friendship.
private HoverPickup hoverPickupHover component the token returns to when let go.
private static readonly Logger loggerNLog logger for this class.
private int maxNameLengthSerialized name length limit. Not used in this code.
private TextRenderer[] nameRenderersText renderers that show the owner's name.
private PositionEvent newFriendshipEffectEffect played when a new friendship is made.
private int otherTokenIdUser ID of the other player in the current handover, or -1.
private Pickup pickupThe token's pickup.
private bool receivedOwnerTokenServer: true once the owner's side of the handover has arrived.
private float releaseTimeoutOwner client: time until which a grab by someone else counts as a handover, or -1.
private MethodSyncStruct<int> syncFriendshipRequestMethod sync for handover reports. Note it uses the syncFriendshipRequestEffect message type.
private MethodSyncStruct<int> syncFriendshipRequestEffectMethod sync for the result effect. Note it uses the syncFriendshipRequest message type.
private Timeline.IEntry timelineEntryPending timeout call.
private const float TimeoutTimeHandover window in seconds (2).

Methods​

MemberWhat it is
override void Initialize()Sets up the request and effect sync messages.
void Inject(IAnalyticsService analyticsService)VContainer injection of the analytics service.
14 private and internal methods
MemberWhat it is
private async void AddFriend()Server: checks the API for an existing friendship, sends a friend request if needed, updates both players' friend lists and syncs the result effect.
private void Awake()Stops the pickup saving and hooks its filter, grab and release events; clients also update the name on owner change.
private void CallFriendshipEvent(Player player, Player friend, FriendshipInfo friendship)Adds the other player to a player's FriendshipManager from the API's friendship info.
private void Filter(Interactor interactor, Interactable interactable, ref bool result)Lets the local player grab the token only if they own it or the owner is holding it.
private void Grabbed(Interactable interactable, Interactor interactor)When a non-owner takes it, both the taker's client and the owner's client (if released in the last 2 seconds) send the handover to the server.
private void Hover(Interactable interactable, Interactor interactor)On release, puts the token back on its hover point; for the owner, starts the 2 second handover window.
private void OnDestroy()Cancels the pending timeout.
private void RunEffects(FriendRequestToken.FriendshipSyncResult friendshipSyncResult)Client: plays the new friend, already friends or denied effect at the token.
private void SyncFriendRequestForPlayer(int friendID)Server: handles the owner's report; adds the friend if the other side already reported, else remembers it.
private void SyncFriendRequestForTokenOwner(IPlayer player)Server: handles the taker's report; adds the friend if the owner already reported, else remembers the taker.
private void SyncFriendshipRequest(IPlayer player, Stream stream, int friendID)Server: reads a handover report, starts the timeout on the first one and routes it by sender.
private void SyncFriendshipRequestEffect(IPlayer player, Stream stream, int friendshipResultIndex)Client: reads the result, plays its effect and marks the player as a friend on success.
private void TimeoutFade()Server: clears a half-finished handover after 2 seconds and fades the token out.
private void UpdateName(IPlayer owner)Client: writes the owner's username on the name renderers and adds them to materialize.