Пример #1
0
        public static async Task <GotchiTradeRequestResult> MakeTradeRequestAsync(ICommandContext context, SQLiteDatabase db, Gotchi offeredGotchi, Gotchi recievedGotchi)
        {
            // If either gotchi passed in is null, the request is invalid.

            if (offeredGotchi is null || recievedGotchi is null)
            {
                return(GotchiTradeRequestResult.Invalid);
            }

            // If the user has made previous trade requests, remove them.
            _trade_requests.RemoveAll(x => x.OfferedGotchi.OwnerId == offeredGotchi.OwnerId);

            // If their partner already has an open trade request that hasn't been accepted, don't allow a new trade request to be made.
            // This is so users cannot make a new trade request right before one is accepted and snipe the trade.

            GotchiTradeRequest request = GetTradeRequest(recievedGotchi);

            if (!(request is null))
            {
                if (request.IsExpired)
                {
                    _trade_requests.RemoveAll(x => x.ReceivedGotchi.OwnerId == recievedGotchi.OwnerId);
                }
                else
                {
                    return(GotchiTradeRequestResult.RequestPending);
                }
            }

            request = new GotchiTradeRequest {
                OfferedGotchi  = offeredGotchi,
                ReceivedGotchi = recievedGotchi
            };

            if (!await ValidateTradeRequestAsync(context, db, request))
            {
                return(GotchiTradeRequestResult.Invalid);
            }

            _trade_requests.Add(request);

            return(GotchiTradeRequestResult.Success);
        }
Пример #2
0
        public static async Task <bool> ValidateTradeRequestAsync(ICommandContext context, SQLiteDatabase db, GotchiTradeRequest tradeRequest)
        {
            // The request is invalid if:
            // - Either user involved in the trade has gotten a new gotchi since the trade was initiated
            // - Either gotchi has died since the trade was initiated
            // - The request has expired

            if (tradeRequest.IsExpired || tradeRequest.OfferedGotchi is null || tradeRequest.ReceivedGotchi is null)
            {
                return(false);
            }

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

            Gotchi gotchi1 = user1 is null ? null : await db.GetGotchiAsync(user1.ToCreator());

            if (gotchi1 is null || !gotchi1.IsAlive || gotchi1.Id != tradeRequest.OfferedGotchi.Id)
            {
                return(false);
            }

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

            Gotchi gotchi2 = user2 is null ? null : await db.GetGotchiAsync(user2.ToCreator());

            if (gotchi2 is null || !gotchi1.IsAlive || gotchi2.Id != tradeRequest.ReceivedGotchi.Id)
            {
                return(false);
            }

            return(true);
        }
Пример #3
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);
        }