Example #1
0
        public static async Task <Tuple <bool, int> > CreateGameWithRetry(
            int gameCount,
            Client client,
            BotConfiguration botConfiguration,
            AccountCharacter account)
        {
            var newGameCount = gameCount;
            var result       = await GeneralHelpers.TryWithTimeout(async (retryCount) =>
            {
                bool createGame = false;
                try
                {
                    createGame = client.CreateGame(botConfiguration.Difficulty, $"{botConfiguration.GameNamePrefix}{newGameCount}", botConfiguration.GamePassword, botConfiguration.GameDescriptions[0]);
                }
                catch
                {
                }

                if (!createGame)
                {
                    newGameCount++;
                    var retryDuration = Math.Pow(1 + retryCount, 1.2) * TimeSpan.FromSeconds(3);
                    Log.Information($"Creating game failed for {client.LoggedInUserName()} retrying in {retryDuration.TotalSeconds} seconds");
                    await RealmConnectHelpers.ConnectToRealmWithRetry(client, botConfiguration, account, 10);
                    await Task.Delay(retryDuration);
                }

                return(createGame);
            }, TimeSpan.FromSeconds(15));

            return(Tuple.Create(result, newGameCount));
        }
Example #2
0
 public bool IsCharacterOwned(string user, string character)
 {
     using (var c = new GameDbContext())
     {
         AccountCharacter ac = c.AccountsEx.Find(user);
         if (ac.GameID1 != null && ac.GameID1.Equals(character))
         {
             return(true);
         }
         if (ac.GameID2 != null && ac.GameID2.Equals(character))
         {
             return(true);
         }
         if (ac.GameID3 != null && ac.GameID3.Equals(character))
         {
             return(true);
         }
         if (ac.GameID4 != null && ac.GameID4.Equals(character))
         {
             return(true);
         }
         if (ac.GameID5 != null && ac.GameID5.Equals(character))
         {
             return(true);
         }
     }
     return(false);
 }
Example #3
0
        public static bool ConnectToRealm(Client client,
                                          BotConfiguration botConfiguration,
                                          AccountCharacter accountCharacter)
        {
            var connect = client.Connect(
                botConfiguration.Realm,
                botConfiguration.KeyOwner,
                botConfiguration.GameFolder);

            if (!connect)
            {
                return(false);
            }
            var characters = client.Login(accountCharacter.Username, accountCharacter.Password);

            if (characters == null)
            {
                return(false);
            }

            var selectedCharacter = characters.FirstOrDefault(c =>
                                                              c.Name.Equals(accountCharacter.Character, StringComparison.CurrentCultureIgnoreCase));

            if (selectedCharacter == null)
            {
                throw new CharacterNotFoundException(accountCharacter.Character);
            }
            client.SelectCharacter(selectedCharacter);

            return(true);
        }
Example #4
0
        public static async Task <bool> ConnectToRealmWithRetry(
            Client client,
            BotConfiguration botConfiguration,
            AccountCharacter accountCharacter,
            int maxRetries)
        {
            var connectCount = 0;

            while (connectCount < maxRetries)
            {
                try
                {
                    client.Disconnect();
                    if (ConnectToRealm(client, botConfiguration, accountCharacter))
                    {
                        return(true);
                    }
                }
                catch
                {
                }

                connectCount++;
                Log.Warning($"Connecting to realm failed for {accountCharacter.Username}, doing re-attempt {connectCount} out of 10");
                await Task.Delay(Math.Pow(connectCount, 1.5) *TimeSpan.FromSeconds(5));
            }

            return(connectCount < maxRetries);
        }
 public SingleClientBotBase(
     BotConfiguration config,
     AccountCharacter accountCharacter,
     IExternalMessagingClient externalMessagingClient,
     IMuleService muleService)
 {
     _config                  = config;
     _accountCharacter        = accountCharacter;
     _externalMessagingClient = externalMessagingClient;
     _muleService             = muleService;
 }
Example #6
0
        private static ApiKeyInfo ParseApiKeyInfoXml(XElement result)
        {
            DateTimeOffset expiry                = default(DateTimeOffset);
            int            accessMask            = 0;
            var            type                  = ApiKeyType.Invalid;
            ApiKeyInfo     keyInfo               = null;
            IEnumerable <AccountCharacter> chars = new AccountCharacter[0];

            // get the key element in the result
            XElement keyElement = result.Element(ApiConstants.Key);

            if (keyElement != null)
            {
                // get details from the attributes
                foreach (XAttribute attribute in keyElement.Attributes())
                {
                    switch (attribute.Name.LocalName)
                    {
                    case "accessMask":
                        int.TryParse(attribute.Value, out accessMask);
                        break;

                    case "type":
                        Enum.TryParse(attribute.Value, out type);
                        break;

                    case "expires":
                        DateTime temp;
                        expiry = DateTime.TryParse(attribute.Value, out temp)
                                ? new DateTimeOffset(temp, TimeSpan.Zero)
                                : default(DateTimeOffset);
                        break;
                    }
                }

                // get the character rowset that might be present in the result
                XElement characterSet = keyElement.Element(ApiConstants.Rowset);
                if (characterSet != null)
                {
                    chars = GetCharactersFromRowSet(characterSet);
                }

                keyInfo = new ApiKeyInfo {
                    AccessMask = accessMask, ApiType = type, Expires = expiry, Characters = chars
                };
            }

            return(keyInfo);
        }
Example #7
0
        //
        // GET: /GamePanel/

        public ActionResult Index(ResetModel model)
        {
            ResetModel       mdl;
            AccountCharacter account = GetAccountCharacter();

            if (TempData["r_model"] != null)
            {
                mdl = (ResetModel)TempData["r_model"];
            }
            else
            {
                mdl = new ResetModel();
            }
            List <Character> chars = new List <Character>();

            if (account.GameID1 != null)
            {
                chars.Add(GetCharacter(account.GameID1));
            }
            if (account.GameID2 != null)
            {
                chars.Add(GetCharacter(account.GameID2));
            }
            if (account.GameID3 != null)
            {
                chars.Add(GetCharacter(account.GameID3));
            }
            if (account.GameID4 != null)
            {
                chars.Add(GetCharacter(account.GameID4));
            }
            if (account.GameID5 != null)
            {
                chars.Add(GetCharacter(account.GameID5));
            }
            ViewBag.Chars = chars;
            return(View(mdl));
        }
Example #8
0
        private async Task <bool> LeaveGameAndRejoinMCPWithRetry(Client client, AccountCharacter cowAccount)
        {
            if (!client.Chat.IsConnected())
            {
                if (!await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, cowAccount, 10))
                {
                    return(false);
                }
            }

            if (client.Game.IsInGame())
            {
                Log.Information($"Leaving game with {client.LoggedInUserName()}");
                client.Game.LeaveGame();
            }

            if (!client.RejoinMCP())
            {
                Log.Warning($"Disconnecting client {cowAccount.Username} since reconnecting to MCP failed, reconnecting to realm");
                return(await RealmConnectHelpers.ConnectToRealmWithRetry(client, _config, cowAccount, 10));
            }

            return(true);
        }
        public async Task <bool> MuleItemsForClient(Client client)
        {
            var muleGameName = $"{_botConfig.GameNamePrefix}m{GameCount++}";

            if (!client.CreateGame(Difficulty.Normal, muleGameName, _botConfig.GamePassword, _botConfig.GameDescriptions?.ElementAtOrDefault(0)))
            {
                await Task.Delay(TimeSpan.FromSeconds(10));

                return(false);
            }

            var failedToJoinCount = 0;

            foreach (var account in _muleConfig.Accounts)
            {
                List <Item> muleItems = GetMuleItems(client, account);
                if (!muleItems.Any())
                {
                    continue;
                }

                var accountCharacters = GetAccountCharactersForMule(account);

                foreach (var character in accountCharacters)
                {
                    InventoryHelpers.CleanupCursorItem(client.Game);

                    if (!HasAnyItemsToMule(client))
                    {
                        break;
                    }

                    var muleClient       = new Client();
                    var accountCharacter = new AccountCharacter()
                    {
                        Username  = account.Username,
                        Password  = account.Password,
                        Character = character
                    };

                    if (!RealmConnectHelpers.ConnectToRealm(
                            muleClient, _botConfig, accountCharacter))
                    {
                        Log.Error($"Fail to connect to realm with {account.Username} with character {character}");
                        return(false);
                    }

                    if (!muleClient.JoinGame(muleGameName, _botConfig.GamePassword))
                    {
                        Log.Error($"Fail to join game with {account.Username} with character {character}");
                        failedToJoinCount++;
                        await Task.Delay(TimeSpan.FromSeconds(5) *failedToJoinCount);

                        if (failedToJoinCount > 5)
                        {
                            client.Game.LeaveGame();
                            await Task.Delay(TimeSpan.FromSeconds(2));

                            client.RejoinMCP();
                            return(false);
                        }
                        continue;
                    }

                    await Task.Delay(TimeSpan.FromSeconds(2));

                    InventoryHelpers.CleanupCursorItem(muleClient.Game);

                    MoveItemResult moveItemResult = MoveItemResult.Succes;
                    do
                    {
                        var movableInventoryItems = muleClient.Game.Inventory.Items.Where(i => Pickit.Pickit.CanTouchInventoryItem(muleClient.Game, i)).ToList();
                        moveItemResult = InventoryHelpers.StashItemsAndGold(muleClient.Game, movableInventoryItems, 0);
                        if (moveItemResult == MoveItemResult.Failed)
                        {
                            break;
                        }

                        var itemsToTrade = GetItemsToTrade(muleClient.Game.Inventory, muleItems);
                        if (!itemsToTrade.Any())
                        {
                            break;
                        }

                        var stashItemsToTrade = itemsToTrade.Where(i => i.Container == ContainerType.Stash || i.Container == ContainerType.Stash2).ToList();
                        if (stashItemsToTrade.Count > 0)
                        {
                            moveItemResult = InventoryHelpers.MoveStashItemsToInventory(client.Game, stashItemsToTrade);
                            InventoryHelpers.CleanupCursorItem(client.Game);
                        }
                        else
                        {
                            moveItemResult = MoveItemResult.Succes;
                        }

                        var itemIdsToTrade = itemsToTrade.Select(i => i.Id).ToHashSet();
                        itemsToTrade = client.Game.Inventory.Items.Where(i => itemIdsToTrade.Contains(i.Id)).ToList();
                        if (!itemsToTrade.Any())
                        {
                            break;
                        }

                        if (moveItemResult != MoveItemResult.Failed)
                        {
                            moveItemResult = await TradeInventoryItems(client, muleClient, itemsToTrade);

                            await Task.Delay(TimeSpan.FromSeconds(5));
                        }
                        muleItems = GetMuleItems(client, account);
                        if (!muleItems.Any())
                        {
                            break;
                        }
                    } while (moveItemResult == MoveItemResult.Succes);

                    await Task.Delay(TimeSpan.FromSeconds(2));

                    muleClient.Game.LeaveGame();
                    await Task.Delay(TimeSpan.FromSeconds(1));

                    muleClient.Disconnect();
                    if (moveItemResult == MoveItemResult.Failed)
                    {
                        client.Game.LeaveGame();
                        await Task.Delay(TimeSpan.FromSeconds(2));

                        client.RejoinMCP();
                        return(false);
                    }
                }
            }

            var stashInventoryItems = client.Game.Inventory.Items.Where(i => i.IsIdentified && Pickit.Pickit.ShouldKeepItem(client.Game, i) && Pickit.Pickit.CanTouchInventoryItem(client.Game, i)).ToList();

            InventoryHelpers.StashItemsAndGold(client.Game, stashInventoryItems, 0);
            client.Game.LeaveGame();
            await Task.Delay(TimeSpan.FromSeconds(2));

            if (!client.RejoinMCP())
            {
                return(false);
            }

            return(true);
        }
Example #10
0
        public static async Task <bool> JoinGameWithRetry(int gameCount, Client client, BotConfiguration botConfiguration, AccountCharacter cowAccount)
        {
            return(await GeneralHelpers.TryWithTimeout(async (retryCount) =>
            {
                bool joinGame = false;
                try
                {
                    joinGame = client.JoinGame($"{botConfiguration.GameNamePrefix}{gameCount}", botConfiguration.GamePassword);
                }
                catch
                {
                }

                if (!joinGame)
                {
                    var retryDuration = Math.Pow(1 + retryCount, 1.2) * TimeSpan.FromSeconds(3);
                    Log.Information($"Joining game failed for {client.LoggedInUserName()} retrying in {retryDuration.TotalSeconds} seconds");
                    await RealmConnectHelpers.ConnectToRealmWithRetry(client, botConfiguration, cowAccount, 10);
                    await Task.Delay(retryDuration);
                }

                return joinGame;
            }, TimeSpan.FromSeconds(20)));
        }