Beispiel #1
0
        /// <summary>
        /// Handles when the host requests for a bot to be added
        /// </summary>
        /// <param name="msg">The message to handle</param>
        private void HandleHostReqBot(NetIncomingMessage msg)
        {
            // Read the bot data
            byte   difficulty = msg.ReadByte();
            string botName    = msg.ReadString();

            if (string.IsNullOrWhiteSpace(botName))
            {
                Random r = new Random();
                int    randomLineNumber = r.Next(0, BOT_NAMES.Length - 1);
                botName = BOT_NAMES[randomLineNumber];
                botName = botName.Trim();
                botName = char.ToUpper(botName[0]) + botName.Substring(1);
            }

            // We can only add bots in lobby
            if (myState == ServerState.InLobby)
            {
                // Try to get an open Player ID
                int playerID = myPlayers.GetNextAvailableId();

                Log("Attempting to add bot \"{0}\" with difficulty {1}", botName, difficulty / 255.0f);

                // If there is a slot open, add bot
                if (playerID != -1)
                {
                    // Make the player instance
                    Player botPlayer = new Player(new ClientTag(botName), (byte)playerID)
                    {
                        IsBot = true
                    };
                    // Make the bot instance
                    BotPlayer bot = new BotPlayer(this, botPlayer, (difficulty / 256.0f));

                    botPlayer.OnCardAddedToHand     += PlayerGainedCard;
                    botPlayer.OnCardRemovedFromHand += PlayerRemovedCard;

                    // Add the bot player
                    myPlayers[(byte)playerID] = botPlayer;

                    // Ad the bot instance
                    myBots.Add(bot);

                    // Notify all players that a bot has joined
                    NotifyPlayerJoined(botPlayer);

                    Log("Bot added");
                }
                else
                {
                    Log("Failed to add bot, lobby full");
                }
            }
            else
            {
                NotifyBadState(msg.SenderConnection, "Cannot add bot outside of lobby");
                Log("Failed to add bot during game");
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handles when a player has left
        /// </summary>
        /// <param name="player">The player that left</param>
        /// <param name="reason">The reason that the player has left</param>
        private void PlayerLeft(Player player, string reason)
        {
            // Remove that player!
            myPlayers.Remove(player);


            // Create the outgioing message
            NetOutgoingMessage outMsg = myServer.CreateMessage();

            // Write the header and player ID
            outMsg.Write((byte)MessageType.PlayerLeft);
            outMsg.Write(player.PlayerId);
            outMsg.Write(reason);

            // Update the tag
            myTag.PlayerCount = myPlayers.PlayerCount;

            // Send to all clients
            SendToAll(outMsg);

            // If this guy was the game host
            if (player == myGameHost)
            {
                // Work up the chain, this will naturally pick the second person to join
                for (byte index = 0; index < myPlayers.Count; index++)
                {
                    // We only want the host to be not null
                    if (myPlayers[index] != null)
                    {
                        // Update the local variable
                        myGameHost = myPlayers[index];

                        // Notify clients
                        NotifyHostChanged(index);
                        break;
                    }
                }
            }

            // Get the player count
            int playersLeft = myServer.Connections.Count;

            // If there's no-one left, we return to lobby
            if (playersLeft == 0)
            {
                Log("All players left, returning to lobby");
                SetServerState(ServerState.InLobby, "Game empty");
            }
            else
            {
                if (State != ServerState.InLobby && !player.IsBot)
                {
                    // Add a bot
                    BotPlayer bot = BotPlayer.CreateBot(player, this);

                    bot.Player.OnCardAddedToHand     += PlayerGainedCard;
                    bot.Player.OnCardRemovedFromHand += PlayerRemovedCard;

                    myBots.Add(bot);
                    myPlayers[bot.Player.PlayerId] = bot.Player;
                    NotifyPlayerJoined(bot.Player);
                }
            }
        }