public static UserEntered AddUserEnteredEventHandler <T>( this RoomWatcher <T> rw, Action <int> callback) where T : IWebSocket { callback.ThrowIfNull(nameof(callback)); var eventProcessor = new UserEntered(); eventProcessor.OnEvent += callback; rw.EventRouter.AddProcessor(eventProcessor); return(eventProcessor); }
public static UserEntered AddUserEnteredEventHandler <T>( this RoomWatcher <T> rw, Action <Chat.User> callback) where T : IWebSocket { callback.ThrowIfNull(nameof(callback)); var eventProcessor = new UserEntered(); eventProcessor.OnEvent += id => { var user = new Chat.User(rw.Host, id, rw.Auth); callback(user); }; rw.EventRouter.AddProcessor(eventProcessor); return(eventProcessor); }
public override void Update(IEnumerable <Body> bodies) { base.Update(bodies); var trackingIds = new HashSet <ulong>(); foreach (var body in bodies) { if (body.IsTracked) { trackingIds.Add(body.TrackingId); } } var currentCount = trackingIds.Count; var previousCount = _trackingIds.Count; if (currentCount != previousCount) { _consecutiveFrames++; if (_consecutiveFrames >= WindowSize) { // The users that entered or left. var users = new HashSet <ulong>(); if (currentCount > previousCount) { // ONE OR MORE USERS ENTERED foreach (var id in trackingIds) { if (!_trackingIds.Contains(id)) { users.Add(id); } } _args.Users = users; UserEntered?.Invoke(this, _args); } else { // ONE OR MORE USERS LEFT foreach (var id in _trackingIds) { if (!trackingIds.Contains(id)) { users.Add(id); } } _args.Users = users; UserLeft?.Invoke(this, _args); } _trackingIds = trackingIds; _consecutiveFrames = 0; } } }
public static void ProcessResponse(string Json) { if (!active) { return; } RequestObject req = JsonConvert.DeserializeObject <RequestObject>(Json); switch (req.Module) { case "admin": switch (req.Cmd) { case "ban": string msg = "You got banned "; if (req.Args != null) { msg += "till " + req.Args.ToString(); } else { msg += "permanently"; } Banned?.Invoke(msg); break; case "unban": msg = "You got Unbanned"; Unbanned?.Invoke(msg); break; } break; case "info": if (req.Cmd == "all") { RoomObj[] rooms = JsonConvert.DeserializeObject <RoomObj[]>(req.Args.ToString()); if (rooms.Length > 0) { roomDataReceived(rooms); } } break; case "login": switch (req.Cmd) { case "user": loginSuccessfull?.Invoke((string)req.Args); break; case "admin": loggedAsAdmin?.Invoke((string)req.Args); break; case "banned": loggedBanned?.Invoke((string)req.Args); break; default: loginFail?.Invoke((string)req.Args); break; } break; case "msg": switch (req.Cmd) { case "msg": object[] args = JsonConvert.DeserializeObject <object[]>(req.Args.ToString()); ChatMessage m = JsonConvert.DeserializeObject <ChatMessage>(args[1].ToString()); m.TimeStamp = m.TimeStamp.ToLocalTime(); messageRecieived?.Invoke((string)args[0], m); break; case "active": args = JsonConvert.DeserializeObject <object[]>(req.Args.ToString()); RoomHistoryReceived?.Invoke((string)args[0], JsonConvert.DeserializeObject <ChatMessage[]>(args[1].ToString())); break; case "notify": notificationReceived?.Invoke((string)req.Args); break; case "entered": args = JsonConvert.DeserializeObject <string[]>(req.Args.ToString()); UserEntered?.Invoke((string)args[0], (string)args[1]); break; case "left": args = JsonConvert.DeserializeObject <string[]>(req.Args.ToString()); UserLeft?.Invoke((string)args[0], (string)args[1]); break; } break; case "private": privateMessageReceived?.Invoke(JsonConvert.DeserializeObject <ChatMessage>(req.Args.ToString())); break; case "room": switch (req.Cmd) { case "created": Dictionary <string, string> kv_args = JsonConvert.DeserializeObject <Dictionary <string, string> >(req.Args.ToString()); string roomname = kv_args["Name"]; string creator = kv_args["creator"]; roomCreated?.Invoke(roomname, creator); break; case "removed": roomRemoved?.Invoke((string)req.Args); break; default: roomError?.Invoke((string)req.Args); break; } break; case "history": switch (req.Cmd) { case "room": object[] args = JsonConvert.DeserializeObject <object[]>(req.Args.ToString()); ChatMessage[] history = JsonConvert.DeserializeObject <ChatMessage[]>(args[1].ToString()); foreach (ChatMessage msg in history) { msg.TimeStamp = msg.TimeStamp.ToLocalTime(); } RoomHistoryReceived?.Invoke((string)args[0], history); break; case "private": args = JsonConvert.DeserializeObject <object[]>(req.Args.ToString()); string user = args[0].ToString(); history = JsonConvert.DeserializeObject <ChatMessage[]>(args[1].ToString()); foreach (ChatMessage msg in history) { msg.TimeStamp = msg.TimeStamp.ToLocalTime(); } PrivateHistoryReceived?.Invoke(user, history); break; default: break; } break; } }