private async void TimerCallback(object timerState) { var result = await PostMessage(); switch (result.Type) { case CheckSessionResultType.Error: if (Settings.CheckSessionStopOnError) { Stop(); } Logger.LogError(result.Message); OnSessionChanged?.Invoke(result); break; case CheckSessionResultType.Changed: Stop(); Logger.LogInformation(result.Message); OnSessionChanged?.Invoke(result); break; case CheckSessionResultType.Valid: Logger.LogDebug(result.Message); break; } }
/// <summary> /// Изменить сессию /// </summary> public void MakeChange(InterpretedSession changed) { if (changed.FrameNumber < FrameNumber) { return; } FrameNumber = changed.FrameNumber; Params = changed.Params; if (OnSessionChanged != null) { OnSessionChanged.Invoke(this); } }
internal void RaiseSessionChanged() { OnSessionChanged?.Invoke(this._session); }
/// <summary> /// Сделать ход /// </summary> public void MakeMove(SessionsController sender, int subjectID, string command, object[] param) { if (sender == null) { throw new ArgumentNullException(nameof(sender)); } if (command == null) { throw new ArgumentNullException(nameof(command)); } if (param == null) { throw new ArgumentNullException(nameof(param)); } var controller = Controllers.Find(q => q.Obj1 == sender.ControllerInfo); if (controller == null) { throw new ArgumentException("Controller is'nt player of this session"); } if (controller.Obj2 != TurnStatus.expected) { throw new ArgumentException("Controller already make his move"); } var subject = GetObject(subjectID); if (subject == null) { throw new ArgumentException("No subject with this ID in this session"); } var components = subject.GetComponentsOf <Component>(); foreach (var component in components) { List <Type> types = new List <Type>(); foreach (var f in param) { types.Add(f.GetType()); } var method = component.GetType().GetMethod(command, types.ToArray()); if (method == null) { throw new ArgumentException("Wrong method or params"); } var attr = method.GetCustomAttribute(typeof(ControllerCommand), true); if (attr == null) { throw new ArgumentException("Method is'nt controller command"); } if ((attr as ControllerCommand).OnMyTurn != (HisTurn == sender)) { throw new ArgumentException("Turn error, not your turn"); } method.Invoke(component, param); OnSessionChanged.Invoke(this, new SessionChange(sender.ControllerInfo.Name + " has his turn")); Controllers.Find(q => q.Obj1 == sender.ControllerInfo).SetObj2(TurnStatus.completed); return; } }
private void onMessage(object sender, MessageEventArgs e) { if (!e.IsText) { return; } JObject json = JObject.Parse(e.Data); if (json.SelectToken("type") != null) { switch (json.SelectToken("type").ToString()) { case "command": OnPing?.Invoke(client, new Events.Client.OnPingArgs { UUID = json.SelectToken("extra").SelectToken("uuid").ToString() }); break; case "follow": OnFollow?.Invoke(client, new Events.Client.OnFollowArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.Follow.Extra(json.SelectToken("extra")), Viewer = new Models.Client.Viewer(json.SelectToken("viewer")) }); break; case "subscribe": OnSubscriber?.Invoke(client, new Events.Client.OnSubscriberArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.Sub.Extra(json.SelectToken("extra")), Viewer = new Models.Client.Viewer(json.SelectToken("viewer")) }); break; case "points_given": OnBits?.Invoke(client, new Events.Client.OnBitsArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.Bits.Extra(json.SelectToken("extra")), Viewer = new Models.Client.Viewer(json.SelectToken("viewer")) }); break; case "session_change": OnSessionChanged?.Invoke(client, new Events.Client.OnSessionChangeArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.SessionChange.Extra(json.SelectToken("extra")) }); break; case "hosted": OnHosted?.Invoke(client, new Events.Client.OnHostedArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.Hosted.Extra(json.SelectToken("extra")), Viewer = new Models.Client.Viewer(json.SelectToken("viewer")) }); break; case "donate": OnDonation?.Invoke(client, new Events.Client.OnDonationArgs { Channel = json.SelectToken("channel").ToString(), Date = Internal.Common.DateTimeStringToObject(json.SelectToken("date").ToString()), Message = json.SelectToken("message")?.ToString(), UserMessage = json.SelectToken("user_message")?.ToString(), Extra = new Models.Client.Donation.Extra(json.SelectToken("extra")), Viewer = new Models.Client.Viewer(json.SelectToken("viewer")) }); break; } } }