コード例 #1
0
        static string parseCommand(string command)
        {
            if (command.StartsWith("!player.join"))
            {
                string [] playerinfo = parseFields(command);

                foreach (Player p in gameManager.players)
                {
                    if (playerinfo[0] == p.Name)
                    {
                        return "nameTaken";
                    }
                }

                gameManager.players.Add(new Player(playerinfo[0], playerinfo[1], playerinfo[2], playerinfo[3]));
                return "Added player: " + playerinfo[0];
            }
            else if (command.StartsWith("!player.draw"))
            {
                int numCards;
                if(command.Substring(command.IndexOf('|')+1) == "max")
                {
                    numCards = gameManager.maxCards;
                }
                else
                {
                    numCards = int.Parse(command.Substring(command.IndexOf('|') + 1));
                }
                string hand = "";

                for (int i = 0; i < numCards; i++)
                {
                    hand += gameManager.whiteDeck.Pop().toHand();
                    hand += "`";
                }
                hand = hand.Substring(0, hand.Length - 1);
                return hand;
            }
            else if (command.StartsWith("!player.isCzar"))
            {
                return gameManager.players[gameManager.CzarCounter].Name;
            }
            else if (command.StartsWith("!player.leave"))
            {
                string[] playerinfo = parseFields(command);

                for (int i = 0; i < gameManager.players.Count; i++)
                {
                    if (gameManager.players[i].Name == playerinfo[0])
                    {
                        gameManager.players.RemoveAt(i);
                    }
                }

                for (int i = 0; i < gameManager.afkPlayers.Count; i++)
                {
                    if (gameManager.afkPlayers[i].Name == playerinfo[0])
                    {
                        gameManager.afkPlayers.RemoveAt(i);
                    }
                }
                return "0";
            }
            else if (command.StartsWith("!player.rejoin"))
            {
                string[] playerinfo = parseFields(command);

                for (int i = 0; i < gameManager.afkPlayers.Count; i++)
                {
                    if (gameManager.afkPlayers[i].Name == playerinfo[0])
                    {
                        gameManager.players.Add(gameManager.afkPlayers[i]);
                        gameManager.afkPlayers.RemoveAt(i);
                    }
                }
                return "0";
            }
            else if (command.StartsWith("!player.playCard"))
            {
                int entries =  gameManager.numFields(gameManager.currentBlackCard.text);

                string[] playerinfo = parseFields(command);

                if (entries == 1)
                {
                    gameManager.currentPlayerCards.Add(new PlayInfo(playerinfo[0], playerinfo[1]));
                }
                else
                {
                    string temp = "";

                    for (int i = 0; i < entries; i++)
                    {
                        temp += playerinfo[i] + "`";
                    }

                    temp = temp.Substring(0, temp.Length - 1);

                    gameManager.currentPlayerCards.Add(new PlayInfo(temp, playerinfo[playerinfo.Length - 1]));
                }

                for (int i = 0; i < gameManager.waitingFor.Count; i++ )
                {
                    if (gameManager.waitingFor[i].Name == playerinfo[1])
                    {
                        gameManager.waitingFor.RemoveAt(i);
                    }
                }

                return "0";
            }
            else if (command.StartsWith("!game.blackcard"))
            {
                if (!AIstarted)
                {
                    Thread AiLoop = new Thread(AIloop);
                    AiLoop.Start();
                    AIstarted = true;
                }
                return gameManager.currentBlackCard.text;
            }
            else if (command.StartsWith("!player.hasWon"))
            {
                foreach (Player p in gameManager.players)
                {
                    if (p.hasWon())
                    {
                        return p.Name;
                    }
                }
                foreach (AI ai in gameManager.AIs)
                {
                    if (ai.hasWon())
                    {
                        return ai.Name;
                    }
                }

                return "no";
            }
            else if (command.StartsWith("!game.start"))
            {
                gameManager.gameStarted = true;

                gameManager.players = gameManager.players.OrderBy(o => o.TimeSinceDump()).ToList();
                gameManager.players[gameManager.CzarCounter].IsCzar = true;

                return gameManager.players[0].Name + " is the Card Czar";

            }
            else if (command.StartsWith("!game.hasStarted"))
            {
                return gameManager.gameStarted.ToString();
            }
            else if (command.StartsWith("!game.roundPlayed"))
            {
                return gameManager.played().ToString();
            }
            else if (command.StartsWith("!game.roundWinner"))
            {
                return gameManager.roundWinner;
            }
            else if (command.StartsWith("!game.newRound"))
            {
                gameManager.NewRound();
                return "0";
            }
            else if (command.StartsWith("!game.setNextCzar"))
            {
                gameManager.incrementCzarCounter();
                return "0";
            }
            else if (command.StartsWith("!game.numPlayers"))
            {
                return (gameManager.numAIs + gameManager.players.Count).ToString();
            }
            else if (command.StartsWith("!game.addAI"))
            {
                Random rand = new Random();

                AI newAi = new AI();

                int nameIndex = rand.Next(0,AInames.Count-1);
                if (AInames.Count > 0)
                {
                    newAi.Name = AInames[nameIndex];
                    AInames.RemoveAt(nameIndex);
                }
                else
                {
                    newAi.Name = "AI-" + rand.Next(0, int.MaxValue);
                }
                newAi.IpAddress = ipaddr.ToString();
                for (int i = 0; i < gameManager.maxCards; i++)
                {
                    newAi.hand.Add(gameManager.whiteDeck.Pop().toHand());
                }

                gameManager.numAIs++;
                gameManager.AIs.Add(newAi);

                return "Added AI:" + newAi.Name;

            }
            else if (command.StartsWith("!game.setWinCards|"))
            {
                command = command.Substring(command.IndexOf('|') + 1);

                gameManager.winCards = command;

                return "0";
            }
            else if (command.StartsWith("!game.getWinCards|"))
            {
                return gameManager.winCards;
            }
            else if (command.StartsWith("!game.roundEntries"))
            {
                string temp = "";

                Shuffler shuffler = new Shuffler();
                shuffler.Shuffle(gameManager.currentPlayerCards);

                foreach (PlayInfo p in gameManager.currentPlayerCards)
                {
                    temp += p.card + "`";
                }

                if (temp == "")
                {
                    return "0";
                }

                temp = temp.Substring(0, temp.Length - 1);

                return temp;
            }
            else if (command.StartsWith("!game.setWinner"))
            {
                command = command.Substring(command.IndexOf('|') + 1);
                gameManager.roundWinner = gameManager.currentPlayerCards[int.Parse(command)].cardPlayer;
                foreach (Player p in gameManager.players)
                {
                    if (p.Name == gameManager.roundWinner)
                    {
                        p.Points++;
                    }
                }
                foreach (AI ai in gameManager.AIs)
                {
                    if (ai.Name == gameManager.roundWinner)
                    {
                        ai.Points++;
                    }
                }
                return "0";
            }
            else if (command.StartsWith("!game.numPlayers"))
            {
                return (gameManager.AIs.Count + gameManager.players.Count).ToString();
            }
            else if (command.StartsWith("!game.viewPoints"))
            {
                string tally = "";

                foreach (Player p in gameManager.players)
                {
                    tally += p.Name + "\t\t" + p.Points + "\n";
                }
                foreach (AI ai in gameManager.AIs)
                {
                    tally += ai.Name + "\t\t" + ai.Points + "\n";
                }
                return tally;
            }
            else if (command.StartsWith("!game.playerTimeout"))
            {
                foreach(Player p in gameManager.waitingFor)
                {
                    gameManager.afkPlayers.Add(p);
                    gameManager.players.Remove(p);
                }

                return "0";
            }
            else if (command.StartsWith("!game.quit"))
            {
                Environment.Exit(0);
                return "0";
            }
            else if (command.StartsWith("!help"))
            {
                return "|command| \t\t |function| \n" +
                       "!game.start \t\t starts the game (do so only after all players have joined) \n" +
                       "!game.addAI \t\t adds a rando cardrisian (a player bot that picks cards randomly) \n" +
                       "!game.quit \t\t ends the game (not gracefully for clients midgame) \n";
            }
            else if (command.StartsWith("!chat.sendMessage"))
            {
                foreach (Player p in gameManager.players)
                {
                    p.messages.Push(command.Substring(command.IndexOf('|') + 1));
                }

                return "0";
            }
            else if (command.StartsWith("!chat.getMessages"))
            {
                string name = command.Substring(command.IndexOf('|') + 1);
                string messages = "";
                foreach (Player p in gameManager.players)
                {
                    if (name == p.Name)
                    {
                        while(p.messages.Count != 0)
                        {
                            messages += p.messages.Pop() + "|";
                        }
                    }
                }
                if (messages != "")
                {
                    messages = messages.Substring(0, messages.LastIndexOf('|'));
                    return messages;
                }
                else
                {
                    return "0";
                }

            }
            else if (command == "\n")
            {
                return "0";
            }
            else
            {
                return "Unknown Command";
            }
        }
        static string parseCommand(string command)
        {
            if (command.StartsWith("!player.join"))
            {
                string [] playerinfo = parseFields(command);
                gameManager.players.Add(new Player(playerinfo[0], playerinfo[1], playerinfo[2], playerinfo[3]));
                return "Added player: " + playerinfo[0];
            }
            else if (command.StartsWith("!player.draw"))
            {
                int numCards;
                if(command.Substring(command.IndexOf('|')+1) == "max")
                {
                    numCards = gameManager.maxCards;
                }
                else
                {
                    numCards = int.Parse(command.Substring(command.IndexOf('|') + 1));
                }
                string hand = "";

                for (int i = 0; i < numCards; i++)
                {
                    hand += gameManager.whiteDeck.pop().toHand();
                    hand += "-";
                }
                return hand;
            }
            else if (command.StartsWith("!player.isCzar"))
            {
                return gameManager.players[gameManager.CzarCounter].IpAddress.ToString();
            }
            else if (command.StartsWith("!player.playCard"))
            {
                string[] playerinfo = parseFields(command);

                gameManager.currentPlayerCards.Add(new PlayInfo(playerinfo[0],playerinfo[1]));

                return "";
            }
            else if (command.StartsWith("!game.blackcard"))
            {
                return gameManager.currentBlackCard.text;
            }
            else if (command.StartsWith("!player.hasWon"))
            {
                bool temp;

                foreach (Player p in gameManager.players)
                {
                    temp = p.hasWon();
                    if (temp)
                    {
                        return p.Name;
                    }
                }

                return "no";
            }
            else if (command.StartsWith("!game.start"))
            {
                gameManager.gameStarted = true;

                gameManager.players = gameManager.players.OrderBy(o => o.TimeSinceDump()).ToList();
                gameManager.players[0].IsCzar = true;

                return gameManager.players[0].Name + " is the Card Czar";

            }
            else if (command.StartsWith("!game.hasStarted"))
            {
                return gameManager.gameStarted.ToString();
            }
            else if (command.StartsWith("!game.roundPlayed"))
            {
                return gameManager.played().ToString();
            }
            else if (command.StartsWith("!game.roundWinner"))
            {
                return gameManager.roundWinner;
            }
            else if (command.StartsWith("!game.newRound"))
            {
                gameManager.NewRound();
                return "0";
            }
            else if (command.StartsWith("!game.addAI"))
            {
                Random rand = new Random();

                AI newAi = new AI();
                newAi.Name = "AI" + rand.Next(0,1000).ToString();
                newAi.IpAddress = ipaddr.ToString();
                for (int i = 0; i < gameManager.maxCards; i++)
                {
                    newAi.hand.Add(gameManager.whiteDeck.pop().toHand());
                }

                gameManager.numAIs++;
                gameManager.players.Add(newAi);

                return "Added AI:" + newAi.Name;

            }
            else if (command.StartsWith("!game.roundEntries"))
            {
                string temp = "";
                foreach (PlayInfo p in gameManager.currentPlayerCards)
                {
                    temp += p.card + "-";
                }

                temp = temp.Substring(0, temp.Length - 1);

                return temp;
            }
            else if (command.StartsWith("!game.setWinner"))
            {
                command = command.Substring(command.IndexOf('|') + 1);
                gameManager.roundWinner = gameManager.currentPlayerCards[int.Parse(command)].cardPlayer;
                foreach (Player p in gameManager.players)
                {
                    if (p.Name == gameManager.roundWinner)
                    {
                        p.Points++;
                    }
                }
                return "";
            }
            else if (command == "\n")
            {
                return "";
            }
            else
            {
                return "Unknown Command";
            }
        }