Sandbox.Services.Stats.Increment

Started by Bot 2 posts 363 views

Reply on sbox.game
  1. #1
    Bot
    Sandbox.Services.Stats.Increment : api/Sandbox.Services.Stats/Increment
  2. #2
    Droob 600
    When Increment() is called, the change is predicted on the client using the following method:

    internal void Predict( string name, double amount )
    {
    	PlayerStat playerStat = Get( name );
    	PlayerStat playerStat2 = default( PlayerStat );
    	playerStat2.Name = name;
    	playerStat2.Title = playerStat.Title;
    	playerStat2.Description = playerStat.Description;
    	playerStat2.Unit = playerStat.Unit;
    	playerStat2.Value = playerStat.Value;
    	playerStat2.ValueString = playerStat.ValueString;
    	playerStat2.Max = Math.Max( playerStat.Max, amount );
    	playerStat2.Min = Math.Min( playerStat.Min, amount );
    	playerStat2.Sum = playerStat.Sum + amount;
    	playerStat2.Last = DateTime.UtcNow;
    	playerStat2.LastValue = amount;
    	playerStat2.First = playerStat.First;
    	playerStat2.FirstValue = playerStat.FirstValue;
    	playerStat = playerStat2;
    	stats[name] = playerStat;
    }
    This allows the front-end (for UI, achievements, etc.) to update without waiting for the new stats to be fetched.

    It is worth noting that only the following fields use this prediction:

    Sum Max Min LastValue