示例#1
0
        public async void OnMessageReceived(object sender, object message)
        {
            Debug.WriteLine("Calling message: " + message.GetType());

            #region Before Start
            if (message is ClientBeforeStart)
            {
                if (Packets.ReconnectInfo != null && Packets.ReconnectInfo.Game != null)
                {
                    OnMessageReceived(sender, (object)Packets.ReconnectInfo.PlayerCredentials);
                    return;
                }

                Client.Status("Successfully connected!", AccountName);
                Client.Accounts.Add(Packets);

                var playerCount         = Client.Accounts.Count();
                var lastConnectedPlayer = Client.Accounts.LastOrDefault();
                if (Account.Autoboost)
                {
                    BuyBoost();
                }

                if (lastConnectedPlayer == null)
                {
                    Console.WriteLine("Critical error!");
                    Controller.Restart();
                    return;
                }

                if (playerCount == _setting.MaxBots && lastConnectedPlayer.AllSummonerData.Summoner.SumId.Equals(SummonerId))
                {
                    Client.Status("Players connected! Creating lobby...", AccountName);
                    Timer createPremade = new Timer {
                        Interval = 3000, AutoReset = false
                    };
                    createPremade.Elapsed += (ek, eo) =>
                    {
                        createPremade.Stop();
                        OnMessageReceived(sender, (object)new CreateLobby());
                    };
                    createPremade.Start();
                    return;
                }
            }
            #endregion

            #region Creating lobby...
            if (message is CreateLobby)
            {
                if (!Controller.IsAvailable(SummonerQueue))
                {
                    Client.Status("QueueType is invalid or it is not supported!", AccountName);
                    return;
                }

                GameQueueConfig Game = new GameQueueConfig();
                Game.Id = SummonerQueue;

                if (_setting.Difficulty == "EASY" || _setting.Difficulty == "MEDIUM")
                {
                    Client.Lobby = await Connections.createArrangedBotTeamLobby(Game.Id, _setting.Difficulty);
                }
                else
                {
                    Client.Lobby = await Connections.createArrangedTeamLobby(Game.Id);
                }

                PlayerAcceptedInvite = true;
                Client.Status("Lobby created. Inviting players...", AccountName);

                if (Client.Accounts.Count == 1)
                {
                    OnMessageReceived(sender, Client.Lobby);
                    return;
                }

                foreach (var bot in Client.Accounts)
                {
                    if ((int)bot.AllSummonerData.Summoner.SumId != (int)SummonerId)
                    {
                        await Connections.Invite(bot.AllSummonerData.Summoner.SumId);
                    }
                }
            }
            #endregion

            #region Invite requested
            if (message is InvitationRequest)
            {
                var invitation = message as InvitationRequest;

                if (invitation.InvitationId == Client.Lobby.InvitationID && PlayerAcceptedInvite == false)
                {
                    Client.Lobby = await Connections.AcceptLobby(invitation.InvitationId);

                    PlayerAcceptedInvite = true;
                    Client.Status("Invitation accepted.", AccountName);
                    return;
                }
            }
            #endregion

            #region Lobby status
            if (message is LobbyStatus)
            {
                #region Ignore pls
                List <string> errors = new List <string>();
                if (Client.Lobby == null)
                {
                    errors.Add("NO!");
                }
                if (SummonerName != Client.Lobby.Owner.SummonerName)
                {
                    errors.Add("Trying to access LobbyStatus not as owner.");
                }
                if (Client.LobbyStatusWaiting)
                {
                    errors.Add("Currently waiting for all players.");
                }

                if (errors.Count > 0)
                {
                    Debug.WriteLine("-----------------------------");
                    Debug.WriteLine("LobbyStatus was terminated due following errors:");
                    foreach (var msg in errors)
                    {
                        Debug.WriteLine("        " + msg);
                    }
                    Debug.WriteLine("-----------------------------");
                    return;
                }
                #endregion

                if (Client.Lobby.Members.Count < _setting.MaxBots && !Client.LobbyStatusWaiting)
                {
                    Client.LobbyStatusWaiting = true;
                    while (Client.Lobby.Members.Count < _setting.MaxBots)
                    {
                        Thread.Sleep(100);
                    }
                }

                var lobbyInfo = Client.Lobby;
                Client.Status("Players are ready to start the game!", AccountName);

                #region Queue
                Client.LobbyGame.QueueIds = new Int32[1] {
                    (int)SummonerQueue
                };
                Client.LobbyGame.InvitationId  = lobbyInfo.InvitationID;
                Client.LobbyGame.Team          = lobbyInfo.Members.Select(stats => Convert.ToInt32(stats.SummonerId)).ToList();
                Client.LobbyGame.BotDifficulty = _setting.Difficulty;
                #endregion

                OnMessageReceived(sender, await Connections.AttachTeamToQueue(Client.LobbyGame));
                Client.Status("Game search initialized!", AccountName);
                return;
            }
            #endregion

            #region Game state
            if (message is GameDTO)
            {
                GameDTO game = message as GameDTO;
                switch (game.GameState)
                {
                case "CHAMP_SELECT":
                    if (FirstSelection)
                    {
                        break;
                    }

                    FirstSelection = true;
                    Client.Status("Champion select in.", AccountName);
                    await Connections.SetClientReceivedGameMessage(game.Id, "CHAMP_SELECT_CLIENT");

                    if (SummonerQueue != 65)
                    {
                        var hArray = HeroesArray.Shuffle();
                        await Connections.SelectChampion(hArray.First(hr => hr.FreeToPlay || hr.Owned || !hr.OwnedByYourTeam).ChampionId);

                        await Connections.ChampionSelectCompleted();
                    }

                    break;

                case "POST_CHAMP_SELECT":
                    FirstQueue = true;
                    //Client.Status("Post champion select.", AccountName);
                    break;

                case "PRE_CHAMP_SELECT":
                    break;

                case "GAME_START_CLIENT":
                    Client.Status("Lauching League of Legends.", AccountName);
                    break;

                case "GameClientConnectedToServer":
                    break;

                case "IN_QUEUE":
                    Client.Status("Waiting for game.", AccountName);
                    break;

                case "TERMINATED":
                    FirstQueue           = true;
                    PlayerAcceptedInvite = false;
                    Client.Status("Re-entering queue.", AccountName);
                    break;

                case "JOINING_CHAMP_SELECT":
                    if (FirstQueue)
                    {
                        Client.Status("Game accepted!", AccountName);
                        FirstQueue     = false;
                        FirstSelection = false;
                        try
                        {
                            await Connections.AcceptPoppedGame(true);
                        }
                        catch
                        {
                            RestartQueue(sender);
                        }
                        break;
                    }
                    break;

                case "LEAVER_BUSTED":
                    Client.Status("Leave Busted!", AccountName);
                    break;
                }
            }
            #endregion

            #region Starting game...
            if (message is PlayerCredentialsDto)
            {
                string gameLocation = Controller.GameClientLocation(_setting.GamePath);
                PlayerCredentialsDto credentials = message as PlayerCredentialsDto;
                ProcessStartInfo     startInfo   = new ProcessStartInfo();

                startInfo.CreateNoWindow   = false;
                startInfo.WorkingDirectory = gameLocation;
                startInfo.FileName         = "League of Legends.exe";
                startInfo.Arguments        = "\"8394\" \"LoLLauncher.exe\" \"\" \"" + credentials.ServerIp + " " +
                                             credentials.ServerPort + " " + credentials.EncryptionKey + " " + credentials.SummonerId + "\"";
                Client.Status("Launching League of Legends", AccountName);



                new Thread((ThreadStart)(() =>
                {
                    while (Client.ClientDelay)
                    {
                        Thread.Sleep(100);
                    }

                    Client.ClientDelay = true;
                    LeagueProcess = Process.Start(startInfo);
                    LeagueProcess.Exited += LeagueProcess_Exited;
                    while (LeagueProcess.MainWindowHandle == IntPtr.Zero)
                    {
                        ;
                    }
                    LeagueProcess.PriorityClass = ProcessPriorityClass.Idle;
                    LeagueProcess.EnableRaisingEvents = true;
                    Timer clientDelay = new Timer {
                        AutoReset = false, Interval = Client.Delay
                    };
                    clientDelay.Elapsed += (o, args) =>
                    {
                        Client.ClientDelay = false;
                    };
                    clientDelay.Start();
                })).Start();
            }

            if (message is EndOfGameStats)
            {
                Client.Accounts.Clear();
                Client.LobbyStatusWaiting = false;

                // Process kill
                LeagueProcess.Exited -= LeagueProcess_Exited;

                while (LeagueProcess.Responding)
                {
                    LeagueProcess.Kill();
                    Thread.Sleep(500);
                }


                var msg = message as EndOfGameStats;
                Packets = await Connections.GetLoginDataPacketForUser();

                WebService.SetLevel(Account.Id, (int)Packets.AllSummonerData.SummonerLevel.Level);
                WebService.SetMoney(Account.Id, (int)Packets.IpBalance);


                if (SummonerLevel < Packets.AllSummonerData.SummonerLevel.Level)
                {
                    Client.Status("Level up! " + Packets.AllSummonerData.SummonerLevel.Level, AccountName);
                }

                // Player level limit
                if (MaxLevelReached((int)Packets.AllSummonerData.SummonerLevel.Level))
                {
                    // This player will not be added to lobby!
                    Client.Status("Maximum level reached!", AccountName);
                    return;
                }
                else if (Account.Autoboost)
                {
                    BuyBoost();
                }

                OnMessageReceived(sender, new ClientBeforeStart());
                return;
            }
            #endregion

            #region Searching for match
            if (message is SearchingForMatchNotification)
            {
                var result = message as SearchingForMatchNotification;

                if (result.PlayerJoinFailures != null)
                {
                    List <Tuple <string, int> > summoners = new List <Tuple <string, int> >();
                    string accessToken = null;
                    bool   penalty     = false;


                    foreach (var item in result.PlayerJoinFailures)
                    {
                        var x = new QueueDodger(item as TypedObject);
                        if (x.ReasonFailed == "LEAVER_BUSTED")
                        {
                            accessToken = x.AccessToken;
                            summoners.Add(new Tuple <string, int>(x.Summoner.Name, x.LeaverPenaltyMillisRemaining));
                            penalty = true;
                        }
                        else
                        {
                            Client.Status("Reason: " + x.ReasonFailed, AccountName);

                            return;
                        }
                    }

                    if (penalty)
                    {
                        Debug.WriteLine("Penalty timer.");
                        var timeWait = summoners.OrderByDescending(s => s.Item2).FirstOrDefault().Item2;
                        var time     = TimeSpan.FromMilliseconds(timeWait);
                        var players  = string.Join(",", summoners.Select(s => s.Item1).ToArray());
                        Debug.WriteLine("Time wait" + timeWait + "ms." + "Counted summoners: " + summoners.Count + "; Summoners: " + players);
                        Client.Status("Waiting " + time.Minutes + " mins to be able to join queue", AccountName);
                        Thread.Sleep(timeWait + 2999);

                        if (SummonerName == Client.Lobby.Owner.SummonerName)
                        {
                            OnMessageReceived(sender, await Connections.AttachToQueue(Client.LobbyGame, accessToken));
                        }
                    }
                }
            }
            #endregion
        }