public static async Task <Gotchi> GetGotchiAsync(IUser user)
        {
            GotchiUserInfo userData = await GetUserInfoAsync(user);

            // Get this user's primary Gotchi.

            Gotchi gotchi = await GetGotchiAsync(userData.PrimaryGotchiId);

            // If this user's primary gotchi doesn't exist (either it was never set or no longer exists), pick a primary gotchi from their current gotchis.

            if (gotchi is null)
            {
                Gotchi[] gotchis = await GetGotchisAsync(userData.UserId);

                if (gotchis.Count() > 0)
                {
                    gotchi = gotchis[0];

                    userData.PrimaryGotchiId = gotchi.Id;

                    await UpdateUserInfoAsync(userData);
                }
            }

            return(gotchi);
        }
        private async Task DoAutoFeederAsync(ulong userId, IEnumerable <Gotchi> userGotchis)
        {
            // Auto-feeder

            if ((await GotchiUtils.GetItemFromInventoryAsync(userId, GotchiItemId.AutoFeeder)).Count > 0)
            {
                GotchiUserInfo userInfo = await GotchiUtils.GetUserInfoAsync(userId);

                const int costPerFeeding = 5;

                if (userInfo != null && userInfo.G >= costPerFeeding)
                {
                    int gotchisFed = userGotchis.Where(g => g.IsHungry).Count();

                    if (gotchisFed > 0)
                    {
                        await GotchiUtils.FeedGotchisAsync(userId);

                        userInfo.G = Math.Max(0, userInfo.G - (costPerFeeding * gotchisFed));

                        await GotchiUtils.UpdateUserInfoAsync(userInfo);
                    }
                }
            }
        }
        public static async Task UpdateUserInfoAsync(GotchiUserInfo userInfo)
        {
            using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR REPLACE INTO GotchiUser(user_id, g, gotchi_limit, primary_gotchi_id) VALUES ($user_id, $g, $gotchi_limit, $primary_gotchi_id);")) {
                cmd.Parameters.AddWithValue("$user_id", userInfo.UserId);
                cmd.Parameters.AddWithValue("$g", userInfo.G);
                cmd.Parameters.AddWithValue("$gotchi_limit", userInfo.GotchiLimit);
                cmd.Parameters.AddWithValue("$primary_gotchi_id", userInfo.PrimaryGotchiId);

                await Database.ExecuteNonQuery(cmd);
            }
        }
        public static async Task <GotchiUserInfo> GetUserInfoAsync(ulong userId)
        {
            using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM GotchiUser WHERE user_id = $user_id;")) {
                cmd.Parameters.AddWithValue("$user_id", userId);

                DataRow row = await Database.GetRowAsync(cmd);

                if (!(row is null))
                {
                    return(GotchiUserInfo.FromDataRow(row));
                }
            }

            // If the user is not yet in the database, return a default user object.
            return(new GotchiUserInfo(userId));
        }
Esempio n. 5
0
        public static async Task ExecuteTradeRequestAsync(ICommandContext context, SQLiteDatabase db, GotchiTradeRequest tradeRequest)
        {
            // Get both users and their gotchis.

            IUser user1 = await context.Guild.GetUserAsync(tradeRequest.OfferedGotchi.OwnerId);

            Gotchi gotchi1 = await db.GetGotchiAsync(user1.ToCreator());

            GotchiUserInfo userInfo1 = await db.GetUserInfoAsync(user1.ToCreator());

            IUser user2 = await context.Guild.GetUserAsync(tradeRequest.ReceivedGotchi.OwnerId);

            Gotchi gotchi2 = await db.GetGotchiAsync(user2.ToCreator());

            GotchiUserInfo userInfo2 = await db.GetUserInfoAsync(user2.ToCreator());

            // Swap the owners of the gotchis.

            using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Gotchi SET owner_id = $owner_id WHERE id = $id")) {
                cmd.Parameters.AddWithValue("$owner_id", user1.Id);
                cmd.Parameters.AddWithValue("$id", gotchi2.Id);

                await db.ExecuteNonQueryAsync(cmd);
            }

            userInfo1.PrimaryGotchiId = gotchi2.Id;

            await db.UpdateUserInfoAsync(userInfo1);

            using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Gotchi SET owner_id = $owner_id WHERE id = $id")) {
                cmd.Parameters.AddWithValue("$owner_id", user2.Id);
                cmd.Parameters.AddWithValue("$id", gotchi1.Id);

                await db.ExecuteNonQueryAsync(cmd);
            }

            userInfo2.PrimaryGotchiId = gotchi1.Id;

            await db.UpdateUserInfoAsync(userInfo2);

            // Remove all existing trade requests involving either user.
            _trade_requests.RemoveAll(x => x.OfferedGotchi.OwnerId == user1.Id || x.ReceivedGotchi.OwnerId == user2.Id);
        }
        public static async Task AddGotchiAsync(IUser user, Species species)
        {
            // We need to generate a name for this Gotchi that doesn't already exist for this user.

            string name = GenerateGotchiName(user, species);

            while (!(await GetGotchiAsync(user.Id, name) is null))
            {
                name = GenerateGotchiName(user, species);
            }

            // Add the Gotchi to the database.

            long ts = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

            using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO Gotchi(species_id, name, owner_id, fed_ts, born_ts, died_ts, evolved_ts) VALUES($species_id, $name, $owner_id, $fed_ts, $born_ts, $died_ts, $evolved_ts);")) {
                cmd.Parameters.AddWithValue("$species_id", species.Id);
                cmd.Parameters.AddWithValue("$owner_id", user.Id);
                cmd.Parameters.AddWithValue("$name", name.ToLower());
                cmd.Parameters.AddWithValue("$fed_ts", ts - 60 * 60); // subtract an hour to keep it from eating immediately after creation
                cmd.Parameters.AddWithValue("$born_ts", ts);
                cmd.Parameters.AddWithValue("$died_ts", 0);
                cmd.Parameters.AddWithValue("$evolved_ts", ts);

                await Database.ExecuteNonQuery(cmd);
            }

            // Set this gotchi as the user's primary Gotchi.

            GotchiUserInfo user_data = await GetUserInfoAsync(user);

            using (SQLiteCommand cmd = new SQLiteCommand("SELECT id FROM Gotchi WHERE owner_id = $owner_id AND born_ts = $born_ts")) {
                cmd.Parameters.AddWithValue("$owner_id", user.Id);
                cmd.Parameters.AddWithValue("$born_ts", ts);

                user_data.PrimaryGotchiId = await Database.GetScalar <long>(cmd);
            }

            await UpdateUserInfoAsync(user_data);
        }
Esempio n. 7
0
        private async Task _endBattle(ICommandContext context)
        {
            PlayerState winner = player1.Gotchi.Stats.Hp <= 0.0 ? player2 : player1;
            PlayerState loser  = player1.Gotchi.Stats.Hp <= 0.0 ? player1 : player2;

            // Calculate the amount of EXP awarded to the winner.
            // The loser will get 50% of the winner's EXP.

            double exp = _getExpEarned(winner.Gotchi.Gotchi, loser.Gotchi.Gotchi, won: true);

            double exp1 = player2.Gotchi.Stats.Hp <= 0.0 ? exp : exp * .5;
            double exp2 = player1.Gotchi.Stats.Hp <= 0.0 ? exp : exp * .5;

            long levels1 = player1.Gotchi.Stats.AddExperience((int)exp1);
            long levels2 = player2.Gotchi.Stats.AddExperience((int)exp2);

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(battleText);

            // Show the winner's accomplishments, then the loser's.

            if (!IsCpuGotchi(winner.Gotchi.Gotchi))
            {
                double winner_exp    = winner.Gotchi.Gotchi.Id == player1.Gotchi.Gotchi.Id ? exp1 : exp2;
                long   winner_levels = winner.Gotchi.Gotchi.Id == player1.Gotchi.Gotchi.Id ? levels1 : levels2;
                long   winner_g      = (long)Math.Round(loser.Gotchi.Stats.Level * (BotUtils.RandomInteger(150, 200) / 100.0));

                sb.AppendLine(string.Format("🏆 **{0}** won the battle! Earned **{1} EXP** and **{2}G**.",
                                            winner.Gotchi.Gotchi.Name,
                                            winner_exp,
                                            winner_g));

                if (winner_levels > 0)
                {
                    sb.AppendLine(string.Format("🆙 **{0}** leveled up to level **{1}**!", winner.Gotchi.Gotchi.Name, winner.Gotchi.Stats.Level));
                }

                if (((winner.Gotchi.Stats.Level - winner_levels) / 10) < (winner.Gotchi.Stats.Level / 10))
                {
                    if (await GotchiUtils.EvolveAndUpdateGotchiAsync(winner.Gotchi.Gotchi))
                    {
                        Species sp = await BotUtils.GetSpeciesFromDb(winner.Gotchi.Gotchi.SpeciesId);

                        sb.AppendLine(string.Format("🚩 Congratulations, **{0}** evolved into **{1}**!", winner.Gotchi.Gotchi.Name, sp.ShortName));
                    }
                }

                // Update the winner's G.

                GotchiUserInfo user_data = await GotchiUtils.GetUserInfoAsync(winner.Gotchi.Gotchi.OwnerId);

                user_data.G += winner_g;

                await GotchiUtils.UpdateUserInfoAsync(user_data);

                sb.AppendLine();
            }

            if (!IsCpuGotchi(loser.Gotchi.Gotchi))
            {
                double loser_exp    = loser.Gotchi.Gotchi.Id == player1.Gotchi.Gotchi.Id ? exp1 : exp2;
                long   loser_levels = loser.Gotchi.Gotchi.Id == player1.Gotchi.Gotchi.Id ? levels1 : levels2;

                sb.AppendLine(string.Format("💀 **{0}** lost the battle... Earned **{1} EXP**.", loser.Gotchi.Gotchi.Name, loser_exp));

                if (loser_levels > 0)
                {
                    sb.AppendLine(string.Format("🆙 **{0}** leveled up to level **{1}**!", loser.Gotchi.Gotchi.Name, loser.Gotchi.Stats.Level));
                }

                if (((loser.Gotchi.Stats.Level - loser_levels) / 10) < (loser.Gotchi.Stats.Level / 10))
                {
                    if (await GotchiUtils.EvolveAndUpdateGotchiAsync(loser.Gotchi.Gotchi))
                    {
                        Species sp = await BotUtils.GetSpeciesFromDb(loser.Gotchi.Gotchi.SpeciesId);

                        sb.AppendLine(string.Format("🚩 Congratulations, **{0}** evolved into **{1}**!", loser.Gotchi.Gotchi.Name, sp.ShortName));
                    }
                }
            }

            // Update exp in the database.

            using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Gotchi SET level=$level, exp=$exp WHERE id=$id;")) {
                cmd.Parameters.AddWithValue("$id", player1.Gotchi.Gotchi.Id);
                cmd.Parameters.AddWithValue("$level", DBNull.Value);
                cmd.Parameters.AddWithValue("$exp", player1.Gotchi.Stats.Experience);

                await Database.ExecuteNonQuery(cmd);
            }

            if (!IsCpuGotchi(player2.Gotchi.Gotchi))
            {
                using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Gotchi SET level=$level, exp=$exp WHERE id=$id;")) {
                    cmd.Parameters.AddWithValue("$id", player2.Gotchi.Gotchi.Id);
                    cmd.Parameters.AddWithValue("$level", DBNull.Value);
                    cmd.Parameters.AddWithValue("$exp", player2.Gotchi.Stats.Experience);

                    await Database.ExecuteNonQuery(cmd);
                }
            }

            // Deregister the battle state.

            DeregisterBattle(context.User.Id);

            battleText = sb.ToString();
        }