コード例 #1
0
        public async Task Run()
        {
            var client1 = new Client();

            if (!client1.Connect(
                    _config.Realm,
                    _config.KeyOwner,
                    _config.GameFolder))
            {
                return;
            }
            var selectedCharacter1 = client1.Login("test", "1234")?.Single(c =>
                                                                           c.Name.Equals("testcharacter", StringComparison.CurrentCultureIgnoreCase));

            if (selectedCharacter1 == null)
            {
                throw new CharacterNotFoundException("testcharacter");
            }
            client1.SelectCharacter(selectedCharacter1);
            client1.Chat.EnterChat();

            await _muleService.MuleItemsForClient(client1);
        }
コード例 #2
0
        protected async Task CreateGameLoop(Client client)
        {
            _accountCharacter.Validate();
            try
            {
                if (!RealmConnectHelpers.ConnectToRealm(client, _config, _accountCharacter))
                {
                    throw new Exception("Could not connect to realm");
                }

                int totalCount           = 0;
                int gameCount            = 0;
                int successiveFailures   = 0;
                int gameDescriptionIndex = 0;
                while (true)
                {
                    if (successiveFailures > 0 && successiveFailures % 10 == 0)
                    {
                        gameDescriptionIndex++;
                        if (gameDescriptionIndex == _config.GameDescriptions?.Count)
                        {
                            gameDescriptionIndex = 0;
                        }
                        var reconnectMessage = $"Many successive failures, swithing GS to {_config.GameDescriptions?.ElementAtOrDefault(gameDescriptionIndex)}";
                        Log.Warning(reconnectMessage);
                        bool reconnectResult = await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, _accountCharacter, 10);

                        if (!reconnectResult)
                        {
                            await _externalMessagingClient.SendMessage($"Reconnect tries of 10 reached, restarting bot");

                            return;
                        }
                    }

                    if (gameCount >= 100)
                    {
                        gameCount = 1;
                    }

                    if (NeedsMule)
                    {
                        await _externalMessagingClient.SendMessage($"{client.LoggedInUserName()}: needs mule, starting mule");

                        if (await _muleService.MuleItemsForClient(client))
                        {
                            NeedsMule = false;
                            await _externalMessagingClient.SendMessage($"{client.LoggedInUserName()}: finished mule");
                        }
                        else
                        {
                            await Task.Delay(Math.Pow(successiveFailures, 1.3) *TimeSpan.FromSeconds(5));

                            await _externalMessagingClient.SendMessage($"{client.LoggedInUserName()}: failed to mule all items, trying again");

                            if (!await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, _accountCharacter, 10))
                            {
                                throw new Exception("Could not connect to realm");
                            }
                            successiveFailures++;
                            continue;
                        }
                    }

                    try
                    {
                        gameCount++;
                        totalCount++;
                        if (client.CreateGame(_config.Difficulty, $"{_config.GameNamePrefix}{gameCount}", _config.GamePassword, _config.GameDescriptions?.ElementAtOrDefault(gameDescriptionIndex)))
                        {
                            if (!await RunSingleGame(client))
                            {
                                successiveFailures += 1;
                            }
                            else
                            {
                                successiveFailures = 0;
                            }
                        }
                        else
                        {
                            successiveFailures += 1;
                            await Task.Delay(Math.Pow(successiveFailures, 1.3) *TimeSpan.FromSeconds(5));
                        }

                        if (client.Game.IsInGame())
                        {
                            client.Game.LeaveGame();
                            await Task.Delay(TimeSpan.FromSeconds(3));
                        }

                        if (!client.RejoinMCP())
                        {
                            var reconnectMessage = $"Reconnecting to MCP failed, reconnecting to realm instead";
                            Log.Warning(reconnectMessage);
                            if (!await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, _accountCharacter, 10))
                            {
                                throw new Exception("Could not connect to realm");
                            }
                        }
                    }
                    catch (HttpRequestException)
                    {
                        await _externalMessagingClient.SendMessage($"{client.LoggedInUserName() } Received http exception, map server is probably down, restarting bot");

                        return;
                    }
                    catch (Exception e)
                    {
                        gameDescriptionIndex++;
                        if (gameDescriptionIndex == _config.GameDescriptions?.Count)
                        {
                            gameDescriptionIndex = 0;
                        }

                        successiveFailures += 1;
                        Log.Warning($"Disconnecting client due to exception {e}, reconnecting to realm, game description is now: {_config.GameDescriptions?.ElementAtOrDefault(gameDescriptionIndex)}");
                        bool reconnectResult = await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, _accountCharacter, 10);

                        if (!reconnectResult)
                        {
                            await _externalMessagingClient.SendMessage($"Reconnect tries of 10 reached, restarting bot");

                            return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e, $"Unhandled Exception: {e}");
                await _externalMessagingClient.SendMessage($"bot crashed with exception: {e}");

                throw e;
            }
            finally
            {
                if (client.Game.IsInGame())
                {
                    client.Game.LeaveGame();
                }

                client.Disconnect();
            }
        }