using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; namespace Mogafa.App.Behaviours { public delegate void UserBehaviourChangedHandler(string code, long newValue, long oldValue); public class UserBehaviour { public event UserBehaviourChangedHandler BehaviourChanged; public UserBehaviour() { Behaviours = new List(); } private string userId; [JsonProperty("userId")] public string UserId { get { return userId; } set { userId = value; } } [JsonProperty("behaviours")] public List Behaviours { get; set; } public long GetBehaviour(string code) { var behaviour = Behaviours.FirstOrDefault(b => b.Code == code); if(behaviour == null) { return 0L; } return behaviour.Value; } public long Add(string code, long value) { var behaviour = Behaviours.FirstOrDefault(b => b.Code == code); if (behaviour == null) { behaviour = new Behaviour { Code = code, Value = 0L }; Behaviours.Add(behaviour); } var oldValue = behaviour.Value; behaviour.Value += value; if(value > 0) { BehaviourChanged?.Invoke(code, oldValue, behaviour.Value); } return behaviour.Value; } } }