Exemple #1
0
 public void StartNextPack()
 {
     packNumber++;
     if (packNumber == 1) // Begin the draft.
     {
         draftStarted = true;
         string[] shuffledAliases = aliases.Values.ToArray().OrderBy(x => Util.random.Next()).ToArray();
         draftStates = new DraftState[aliases.Count];
         for (int i = 0; i < shuffledAliases.Length; i++)
         {
             string alias = shuffledAliases[i];
             draftStates[i] = new DraftState(alias);
         }
     }
     else if (packNumber == packs + 1) // End the draft.
     {
         serverWindow.PrintLine("The draft has ended.");
         TrySendMessage("DONE");
         return;
     }
     foreach (DraftState draftState in draftStates)
     {
         List <string> booster = GenerateBooster();
         draftState.AddBooster(booster);
         TrySendMessage(draftState.alias, "BOOSTER|" + string.Join("|", booster));
     }
     SendPackCounts();
     serverWindow.PrintLine("Passed out pack #" + packNumber + ".");
 }
        private void HandleMessage(TcpServerConnection connection, string msg)
        {
            string[] parts = msg.Split('|');
            if (parts[0] == "VERSION")
            {
                int version = int.Parse(parts[1]);
                if (version < Util.version)
                {
                    serverWindow.PrintLine("<" + GetAlias(connection) + "> attempted to connect with old client version " + version + ".");
                    TrySendMessage(connection, "ERROR|OLD_CLIENT_VERSION|" + Util.version);
                }
                else if (version > Util.version)
                {
                    serverWindow.PrintLine("<" + GetAlias(connection) + "> attempted to connect with newer client version " + version + ". Please update the server.");
                    TrySendMessage(connection, "ERROR|OLD_SERVER_VERSION|" + Util.version);
                }
                else
                {
                    TrySendMessage(connection, "OK|VERSION");
                }
            }
            else if (parts[0] == "ALIAS")
            {
                if (aliases.Values.Contains(parts[1]))
                {
                    serverWindow.PrintLine("<" + GetAlias(connection) + "> attempted to connect with an in-use alias.");
                    TrySendMessage(connection, "ERROR|ALIAS_IN_USE");
                }
                else if (draftStarted && FindIndexOfDraftState(parts[1]) == -1)
                {
                    serverWindow.PrintLine("<" + GetAlias(connection) + "> attempted to join an in-progress.");
                    TrySendMessage(connection, "ERROR|DRAFT_IN_PROGRESS");
                }
                else
                {
                    // Reconnect user to draft.
                    serverWindow.PrintLine("<" + GetAlias(connection) + "> has new alias " + parts[1] + ".");
                    aliases.TryAdd(connection, parts[1]);
                    TrySendMessage(connection, "OK|ALIAS");
                    TrySendMessage("USER_CONNECTED|" + parts[1]);
                    if (draftStarted)
                    {
                        DraftState draftState = draftStates[FindIndexOfDraftState(parts[1])];
                        if (draftState.CardPool.Count > 0)
                        {
                            TrySendMessage(connection, "CARD_POOL|" + string.Join("|", draftState.CardPool));
                        }
                        if (draftState.Boosters.Count > 0)
                        {
                            TrySendMessage(connection, "BOOSTER|" + string.Join("|", draftState.Boosters.Peek()));
                        }
                        SendPackCounts();
                    }
                    else
                    {
                        UpdateUserList();
                    }
                }
            }
            else if (parts[0] == "PICK")
            {
                // Remove pick from pack and add to card pool.
                var draftIndex     = FindIndexOfDraftState(aliases[connection]);
                var draftState     = draftStates[draftIndex];
                var nextDraftState = packNumber % 2 == 1
                    ? draftStates[(draftIndex + 1) % draftStates.Length]
                    : draftStates[(draftIndex + draftStates.Length - 1) % draftStates.Length];
                var pickIndex = int.Parse(parts[1]);

                var booster = draftState.MakePick(pickIndex);

                TrySendMessage(connection, "OK|PICK");
                serverWindow.PrintLine("<" + draftState.Alias + "> made a pick.");

                // Pass the pack to the next player, if not empty.
                if (booster.Count > 0)
                {
                    nextDraftState.AddBooster(booster);
                    serverWindow.PrintLine("<" + nextDraftState.Alias + "> got a new pack in their queue (now " + nextDraftState.Boosters.Count + ").");
                    if (nextDraftState.Boosters.Count == 1 && nextDraftState != draftState)
                    {
                        TrySendMessage(nextDraftState.Alias, "BOOSTER|" + string.Join("|", booster));
                    }
                }
                else
                {
                    // Check if no one has any boosters.
                    bool atLeastOnePackLeft = draftStates.Any(s => s.Boosters.Any());
                    if (!atLeastOnePackLeft)
                    {
                        StartNextPack();
                        return;
                    }
                }

                // Current player gets the next booster in their queue, if any.
                if (draftState.Boosters.Count > 0)
                {
                    TrySendMessage(connection, "BOOSTER|" + string.Join("|", draftState.Boosters.Peek()));
                }

                // Send message with pack count of each player.
                SendPackCounts();
            }
            else if (parts[0] == "CHAT")
            {
                if (aliases.ContainsKey(connection))
                {
                    TrySendMessage("CHAT|" + GetAlias(connection) + "|" + parts[1]);
                    serverWindow.PrintLine("<" + GetAlias(connection) + ">: " + parts[1]);
                }
            }
            else
            {
                serverWindow.PrintLine("<" + GetAlias(connection) + "> Unknown message: " + msg);
            }
        }
 public void StartNextPack()
 {
     packNumber++;
     if (packNumber == 1) // Begin the draft.
     {
         draftStarted = true;
         string[] shuffledAliases = aliases.Values.ToArray().OrderBy(x => Util.random.Next()).ToArray();
         draftStates = new DraftState[aliases.Count];
         for (int i = 0; i < shuffledAliases.Length; i++)
         {
             string alias = shuffledAliases[i];
             draftStates[i] = new DraftState(alias);
         }
     }
     else if (packNumber == packs + 1) // End the draft.
     {
         serverWindow.PrintLine("The draft has ended.");
         TrySendMessage("DONE");
         return;
     }
     foreach (DraftState draftState in draftStates)
     {
         List<string> booster = GenerateBooster();
         draftState.AddBooster(booster);
         TrySendMessage(draftState.alias, "BOOSTER|" + string.Join("|", booster));
     }
     SendPackCounts();
     serverWindow.PrintLine("Passed out pack #" + packNumber + ".");
 }