Exemplo n.º 1
0
        void OnRecieveMessage(string json)
        {
            Message.IsType <ClientMessage.LeaveRoom>(json, (data) =>
            {
                if (OnLeaveRoom != null)
                {
                    OnLeaveRoom(this, null);
                }
            });

            Message.IsType <ClientMessage.RequestAdmin>(json, (data) =>
            {
                if (data.Password == Settings.Config.AdminPassword)
                {
                    Logger.Log("Admin granted to {0}", Data.Name);
                    MakeAdmin();
                }
            });

            Message.IsType <ClientMessage.Game.SendAction>(json, (data) =>
            {
                if (OnGameAction != null)
                {
                    OnGameAction(this, data);
                }
            });

            Message.IsType <ClientMessage.NotifyAwayStatus>(json, (data) =>
            {
                Away = data.Away;

                if (Away)
                {
                    Logger.Log("{0} is now AFK", Data.Name);
                }
                else
                {
                    Logger.Log("{0} is no longer AFK", Data.Name);
                }

                if (OnAwayStatusChanged != null)
                {
                    OnAwayStatusChanged(this, Away);
                }
            });

            var obj = JsonHelper.FromJson <Message>(json);

            if (OnMessage != null)
            {
                OnMessage(this, json);
            }
        }
Exemplo n.º 2
0
        void IConnectionMessageHandler.HandleMessage(Player player, string json)
        {
            Message.IsType <ClientMessage.Game.SendAction>(json, (data) =>
            {
                if (player == Owner)
                {
                    switch (data.Action)
                    {
                    case GameAction.Start:
                        Logger.Log(this, "{0} has started the game", Owner.Data.Name);
                        StartCountdown();
                        break;

                    case GameAction.CancelStart:
                        Logger.Log(this, "{0} has cancelled the game from starting", Owner.Data.Name);
                        CancelCountdown();
                        break;

                    case GameAction.Restart:
                        Logger.Log(this, "{0} has restarted the game", Owner.Data.Name);
                        Game.Restart();
                        break;

                    case GameAction.StartNewRound:
                        Logger.Log(this, "{0} has started a new round", Owner.Data.Name);
                        Game.StartNewRound();
                        break;

                    case GameAction.ForceStart:
                        if (player.IsAdmin)
                        {
                            StartCountdown();
                        }
                        break;

                    default:
                        break;
                    }

                    SendUpdateToAll();
                }
            });

            Message.IsType <ClientMessage.SendChat>(json, (data) =>
            {
                Logger.Log(this, "{0} said something", player.Data.Name);
                // Echo chat to all clients
                Players.ForEach(x => x.SendChat(player.Data, data.Message));
            });
        }
Exemplo n.º 3
0
        public void OnOpen(IWebSocketConnection socket)
        {
            Logger.Log(this, "A connection was opened. (Socket ID: {0})", socket.ConnectionInfo.Id);

            // Respond to a join request by assigning a unique ID to the connection and sending it back to the client
            var player = AddPlayer(socket);

            player.RequestClientInfo();

            // Wait for player reponse with their name
            player.Socket.OnBinary += (binary) =>
            {
                // Messages are sent as binary from Unity (the WebGL wrapper only sends binary for some reason)
                var message = Encoding.UTF8.GetString(binary);

                Message.IsType <ClientMessage.GiveClientInfo>(message, (data) =>
                {
                    if (data.ProtocolVersion != ProtocolInfo.Version)
                    {
                        // Notify protocol mismatch. Client and Server must match!
                        player.SendConnectionError(ConnectionError.ProtocolMismatch);
                        return;
                    }

                    // Check if name is within character range
                    bool nameWithinCharLimit = data.Name.Length >= SettingsLoader.Values.Server.NameMinChars && data.Name.Length <= SettingsLoader.Values.Server.NameMaxChars;

                    // Check if an existing connection already has that name
                    bool nameisUnique = !ConnectedPlayers.Any(x => x.Data.Name.ToLower() == data.Name.ToLower());

                    if (nameWithinCharLimit && nameisUnique)
                    {
                        OnConnectionSuccessful(player, data);
                    }
                    else if (!nameWithinCharLimit)
                    {
                        player.SendConnectionError(ConnectionError.InvalidNameLength);
                    }
                    else
                    {
                        player.SendConnectionError(ConnectionError.MatchesExistingName);
                    }
                });
            };
        }
Exemplo n.º 4
0
        void IConnectionMessageHandler.HandleMessage(Player player, string json)
        {
            Message.IsType <ClientMessage.RequestRoomList>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} for a list a rooms.", player);
                var rooms = Rooms.Select(x => x.RoomData).ToList();
                player.SendMessage(new ServerMessage.RoomList(rooms));
            });

            Message.IsType <ClientMessage.JoinRoom>(json, (data) =>
            {
                Logger.Log(this, "Recieved a request from {0} to join room {1}.", player.Data, data.RoomId);

                var roomHasPlayer = Rooms.Find(x => x.HasPlayer(player.Data));
                if (roomHasPlayer != null)
                {
                    roomHasPlayer.Leave(player.Data);
                }

                var targetRoom = Rooms.Find(x => x.RoomData.ID == data.RoomId);

                if (targetRoom != null)
                {
                    targetRoom.Join(player, data.Password);
                }
                else
                {
                    player.SendRoomJoinNotice(RoomNotice.RoomDoesNotExist);
                }
            });

            Message.IsType <ClientMessage.LeaveRoom>(json, (data) =>
            {
                var containingRoom = Rooms.Find(x => x.HasPlayer(player.Data));

                if (containingRoom != null)
                {
                    Logger.Log(this, "Remove {0} from room", player);
                    containingRoom.Leave(player.Data);
                }
                else
                {
                    Logger.Warn(this, "Cannot remove {0} from room as they are not in that room", player);
                }
            });

            Message.IsType <ClientMessage.CreateRoom>(json, (data) =>
            {
                if (Rooms.Count != SettingsLoader.Values.Server.MaxRooms)
                {
                    Logger.Log(this, "Create room for {0} with password {1}", player, data.Password);

                    var playerCurrentRoom = FindRoomContainingPlayer(player.Data);
                    if (playerCurrentRoom != null)
                    {
                        playerCurrentRoom.Leave(player.Data);
                    }

                    var room = new Room(ConnectionsHandler, player, SettingsLoader, data.Password);

                    room.OnEmpty += OnRoomEmpty;

                    Rooms.Add(room);
                }
                else
                {
                    Logger.Log(this, "Cannot create room for {0} as the maximum room limit has been reached", player);
                    player.SendRoomJoinNotice(RoomNotice.MaxRoomsLimitReached);
                }
            });
        }