วันเสาร์ที่ 25 มีนาคม พ.ศ. 2560

กันปั้ม api ที่ปั้มผ่าน charles

เอาไปทับใส่ function sql ที่ชื่อว่า WZ_BackpackFromInv


ALTER PROCEDURE [dbo].[WZ_BackpackFromInv]
    @in_CustomerID int,
    @in_CharID int,
    @in_InventoryID bigint,
    @in_Slot int,
    @in_Amount int,
    @in_ServerCall int = 0
AS
BEGIN
    SET NOCOUNT ON;
 
    if(@in_Amount <= 0) begin
        select 6 as ResultCode, 'bad amount' as ResultMsg
        exec DBG_StoreApiCall 'FromInv#1', 1, @in_CustomerID, @in_CharID, @in_Slot, -1, @in_Amount
        return
    end

    -- check if CustomerID/CharID pair is valid
    declare @CustomerID int
    declare @BackpackSize int
    declare @GameFlags int
    select @CustomerID=CustomerID, @BackpackSize=BackpackSize, @GameFlags=GameFlags FROM UsersChars WHERE CharID=@in_CharID
    if(@@ROWCOUNT = 0 or @CustomerID <> @in_CustomerID) begin
        select 6 as ResultCode, 'bad charid' as ResultMsg
        return
    end
 
    -- must be in safe zone
    if(@in_ServerCall = 0 and (@GameFlags & 1) = 0) begin
        select 6 as ResultCode, 'not in safezone' as ResultMsg
        return
    end
 
    -- check if game is still active or 90sec passed from last update (COPYPASTE_GAMECHECK, search for others)
    declare @lastgamedate datetime
    declare @GameServerId int
    select @GameServerId=GameServerId, @lastgamedate=lastgamedate from UsersData where CustomerID=@in_CustomerID
    if(@in_ServerCall = 0 and @GameServerId > 0 and DATEDIFF(second, @lastgamedate, GETDATE()) < 360) begin
        select 7 as ResultCode, 'game still active' as ResultMsg
        exec DBG_StoreApiCall 'FromInv#InGame', 2, @in_CustomerID, @in_CharID, @in_Slot, -1, @in_Amount, @GameServerId
        return
    end
 
    -- check if we have that item in inventory
    declare @InvCustomerID int
    declare @InvInventoryID bigint
    declare @InvItemID int
    declare @InvLeasedUntil datetime
    declare @InvQuantity int
    declare @InvVar1 int
    declare @InvVar2 int
    declare @InvVar3 int
    select
        @InvCustomerID=CustomerID,
        @InvInventoryID=InventoryID,
        @InvItemID=ItemID,
        @InvQuantity=Quantity,
        @InvLeasedUntil=LeasedUntil,
        @InvVar1=Var1,
        @InvVar2=Var2,
        @InvVar3=Var3
    from UsersInventory where InventoryID=@in_InventoryID
    if(@@ROWCOUNT = 0 or @InvCustomerID <> @in_CustomerID) begin
        select 6 as ResultCode, 'bad inventoryid' as ResultMsg
        return
    end
 
    if(@in_Amount > @InvQuantity) begin
        select 6 as ResultCode, 'bad quantity' as ResultMsg
        return
    end
 
    -- validate backpack slot number
    if(@in_Slot < 0 or @in_Slot >= @BackpackSize) begin
        select 6 as ResultCode, 'bad slot' as ResultMsg
        return
    end
    -- validate if we can move to that slot
    declare @BackpackInventoryID bigint = 0
    declare @BackpackItemID int = 0
    declare @BackpackVar1 int = 0
    declare @BackpackVar2 int = 0
    declare @BackpackVar3 int = 0
    select
        @BackpackInventoryID=InventoryID,
        @BackpackItemID=ItemID,
        @BackpackVar1=Var1,
        @BackpackVar2=Var2,
        @BackpackVar3=Var3
    from UsersInventory where CharID=@in_CharID and BackpackSlot=@in_Slot
    if(@@ROWCOUNT > 0) begin
        if(@BackpackItemID <> @InvItemID) begin
            select 6 as ResultCode, 'bad inventory id 2' as ResultMsg
            return
        end
        if(@BackpackVar1 <> @InvVar1 or @BackpackVar2 <> @InvVar2 or @BackpackVar3 <> @InvVar3) begin
            exec DBG_StoreApiCall 'FromInv_BadStack', 3, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            select 6 as ResultCode, 'bad stacking' as ResultMsg
            return
        end
        declare @CanStackWith int = 0
        exec @CanStackWith = FN_IsItemStackable @BackpackItemID, 0
        if(@CanStackWith = 0) begin
            exec DBG_StoreApiCall 'FromInv_NoStack', 3, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            select 6 as ResultCode, 'not stackable' as ResultMsg
            return
        end
    end

    -- clear attachments if moved item was in weapon slot
    if(@in_Slot = 0) update UsersChars set Attachment1='' where CharID=@in_CharID
    if(@in_Slot = 1) update UsersChars set Attachment2='' where CharID=@in_CharID
    -- check for easy case, unmodified item, no such item in backpack
    if(@BackpackInventoryID = 0 and @InvQuantity = @in_Amount)
    begin
        update UsersInventory set BackpackSlot=@in_Slot, CharID=@in_CharID where InventoryID=@InvInventoryID      
        select 0 as ResultCode
        select @InvInventoryID as 'InventoryID'

        if(@in_ServerCall > 0)  exec DBG_StoreApiCall 'FromInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
        else                    exec DBG_StoreApiCall 'FromInv', 0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
        return
    end

    if exists(SELECT * FROM UsersInventory where InventoryID=@InvInventoryID AND Quantity = @in_Amount) and ((SELECT Quantity FROM UsersInventory where InventoryID=@InvInventoryID) = @in_Amount)
    begin
        delete from UsersInventory where InventoryID=@InvInventoryID      
        if(@@ROWCOUNT = 0) begin
            select 6 as ResultCode, 'bad inventoryid' as ResultMsg
            return
        end
        ELSE
        BEGIN
            if not exists(select InventoryID from UsersInventory where InventoryID=@BackpackInventoryID)
            begin
                -- modified (won't stack) or new backpack item
                INSERT INTO UsersInventory (
                    CustomerID,
                    CharID,
                    ItemID,
                    BackpackSlot,
                    LeasedUntil,
                    Quantity,
                    Var1,
                    Var2,
                    Var3
                )
                VALUES (
                    @in_CustomerID,
                    @in_CharID,
                    @InvItemID,
                    @in_Slot,
                    @InvLeasedUntil,
                    @in_Amount,
                    @InvVar1,
                    @InvVar2,
                    @InvVar3
                )
                set @BackpackInventoryID = SCOPE_IDENTITY()
            end
            else
            begin
                update UsersInventory set Quantity=(Quantity+@in_Amount) where InventoryID=@BackpackInventoryID
            end
             
            select 0 as ResultCode;
            select @BackpackInventoryID as 'InventoryID'

            if(@in_ServerCall > 0) exec DBG_StoreApiCall 'FromInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            else                   exec DBG_StoreApiCall 'FromInv',     0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            return
        end
    end

    if exists(SELECT * FROM UsersInventory where InventoryID=@InvInventoryID AND Quantity > @in_Amount) and ((SELECT Quantity FROM UsersInventory where InventoryID=@InvInventoryID) > @in_Amount)
    begin
 
        update UsersInventory set Quantity=(Quantity-@in_Amount) where InventoryID=@InvInventoryID
        if(@@ROWCOUNT = 0) begin
            select 6 as ResultCode, 'bad inventoryid' as ResultMsg
            return
        end
        ELSE
        BEGIN
            if not exists(select InventoryID from UsersInventory where InventoryID=@BackpackInventoryID)
            begin
                -- modified (won't stack) or new backpack item
                INSERT INTO UsersInventory (
                    CustomerID,
                    CharID,
                    ItemID,
                    BackpackSlot,
                    LeasedUntil,
                    Quantity,
                    Var1,
                    Var2,
                    Var3
                )
                VALUES (
                    @in_CustomerID,
                    @in_CharID,
                    @InvItemID,
                    @in_Slot,
                    @InvLeasedUntil,
                    @in_Amount,
                    @InvVar1,
                    @InvVar2,
                    @InvVar3
                )
                set @BackpackInventoryID = SCOPE_IDENTITY()
            end
            else
            begin
                update UsersInventory set Quantity=(Quantity+@in_Amount) where InventoryID=@BackpackInventoryID
            end  
         
            select 0 as ResultCode;
            select @BackpackInventoryID as 'InventoryID'

            if(@in_ServerCall > 0) exec DBG_StoreApiCall 'FromInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            else                   exec DBG_StoreApiCall 'FromInv',     0, @in_CustomerID, @in_CharID, @in_Slot, @InvItemID, @in_Amount, @InvInventoryID, @BackpackInventoryID
            return    
        end
    end
END




เอาไปทับใส่ function sql ที่ชื่อว่า WZ_BackpackToInv



ALTER PROCEDURE [dbo].[WZ_BackpackToInv]
    @in_CustomerID int,
    @in_CharID int,
    @in_InventoryID bigint, -- target inventory id where to put that item
    @in_Slot int,
    @in_Amount int,
    @in_ServerCall int = 0
AS
BEGIN
    SET NOCOUNT ON;

    if(@in_Amount <= 0) begin
        select 6 as ResultCode, 'bad amount' as ResultMsg
        exec DBG_StoreApiCall 'ToInv#1', 1, @in_CustomerID, @in_CharID, @in_Slot, -1, @in_Amount
        return
    end

    -- check if CustomerID/CharID pair is valid
    declare @CustomerID int
    declare @GameFlags int
    select @CustomerID=CustomerID, @GameFlags=GameFlags FROM UsersChars WHERE CharID=@in_CharID
    if(@@ROWCOUNT = 0 or @CustomerID <> @in_CustomerID) begin
        select 6 as ResultCode, 'bad charid' as ResultMsg
        return
    end

    -- must be in safe zone
    if(@in_ServerCall = 0 and (@GameFlags & 1) = 0) begin
        select 6 as ResultCode, 'not in safezone' as ResultMsg
        return
    end
 
    -- check if game is still active or 90sec passed from last update (COPYPASTE_GAMECHECK, search for others)
    declare @lastgamedate datetime
    declare @GameServerId int
    select @GameServerId=GameServerId, @lastgamedate=lastgamedate from UsersData where CustomerID=@in_CustomerID
    if(@in_ServerCall = 0 and @GameServerId > 0 and DATEDIFF(second, @lastgamedate, GETDATE()) < 360) begin
        select 7 as ResultCode, 'game still active' as ResultMsg
        exec DBG_StoreApiCall 'ToInv#InGame', 2, @in_CustomerID, @in_CharID, @in_Slot, -1, @in_Amount, @GameServerId
        return
    end
 
    declare @BackpackInventoryID bigint
    declare @BackpackItemID int
    declare @BackpackLeasedUntil datetime
    declare @BackpackQuantity int
    declare @BackpackVar1 int
    declare @BackpackVar2 int
    declare @BackpackVar3 int
    select
        @BackpackInventoryID=InventoryID,
        @BackpackItemID=ItemID,
        @BackpackQuantity=Quantity,
        @BackpackLeasedUntil=LeasedUntil,
        @BackpackVar1=Var1,
        @BackpackVar2=Var2,
        @BackpackVar3=Var3
    from UsersInventory where CharID=@in_CharID and BackpackSlot=@in_Slot
    if(@@ROWCOUNT = 0) begin
        select 6 as ResultCode, 'bad slot' as ResultMsg
        return
    end
 
    if(@in_Amount > @BackpackQuantity) begin
        select 6 as ResultCode, 'bad quantity' as ResultMsg
        return
    end
 
    -- check for easy case, just switching to inventory

    if(@in_InventoryID = 0 and @BackpackQuantity = @in_Amount)
    begin
        update UsersInventory set BackpackSlot=-1, CharID=0 where InventoryID=@BackpackInventoryID

        select 0 as ResultCode
        select @BackpackInventoryID as 'InventoryID'

        if(@in_ServerCall > 0) exec DBG_StoreApiCall 'ToInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID
        else                   exec DBG_StoreApiCall 'ToInv', 0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID
        return
    end
    -- validate that we own that inventory slot and item can be moved there
    if(@in_InventoryID > 0)
    begin
        declare @InvCustomerID int
        declare @InvCharID int
        declare @InvItemID int
        declare @InvVar1 int
        declare @InvVar2 int
        declare @InvVar3 int
        select
            @InvCustomerID=CustomerID,
            @InvCharID=CharID,
            @InvItemID=ItemID,
            @InvVar1=Var1,
            @InvVar2=Var2,
            @InvVar3=Var3
            from UsersInventory where InventoryID=@in_InventoryID
        if(@@ROWCOUNT = 0 or @InvCustomerID <> @in_CustomerID or @InvCharID <> 0 or @InvItemID <> @BackpackItemID) begin
            select 6 as ResultCode, 'bad inventoryid' as ResultMsg
            return
        end
        if(@InvVar1 <> @BackpackVar1 or @InvVar2 <> @BackpackVar2 or @InvVar3 <> @BackpackVar3) begin
            exec DBG_StoreApiCall 'ToInv_BadStack', 3, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, 0
            select 6 as ResultCode, 'bad stacking' as ResultMsg
            return
        end
        declare @CanStackWith int = 0
        exec @CanStackWith = FN_IsItemStackable @InvItemID, 1
        if(@CanStackWith = 0) begin
            exec DBG_StoreApiCall 'ToInv_NoStack', 3, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, 0
            select 6 as ResultCode, 'not stackable' as ResultMsg
            return
        end
    end

    declare @InvInventoryID bigint = @in_InventoryID
    if exists(SELECT * FROM UsersInventory where InventoryID=@BackpackInventoryID AND Quantity = @in_Amount) and ((SELECT Quantity FROM UsersInventory where InventoryID=@BackpackInventoryID) = @in_Amount)
    BEGIN    
        delete from UsersInventory where InventoryID=@BackpackInventoryID
        if(@@ROWCOUNT = 0) begin
            select 6 as ResultCode, 'bad inventoryid' as ResultMsg
            return
        end
        ELSE
        BEGIN
            if not exists(select InventoryID from UsersInventory where InventoryID=@InvInventoryID) begin
            -- modified (won't stack) or new inventory item
                INSERT INTO UsersInventory (
                    CustomerID,
                    CharID,
                    BackpackSlot,
                    ItemID,
                    LeasedUntil,
                    Quantity,
                    Var1,
                    Var2,
                    Var3
                )
                VALUES (
                    @in_CustomerID,
                    0,
                    -1,
                    @BackpackItemID,
                    @BackpackLeasedUntil,
                    @in_Amount,
                    @BackpackVar1,
                    @BackpackVar2,
                    @BackpackVar3
                )
                set @InvInventoryID = SCOPE_IDENTITY()
            end
            else begin
                update UsersInventory set Quantity=(Quantity+@in_Amount) where InventoryID=@InvInventoryID
            end
            select 0 as ResultCode
            select @InvInventoryID as 'InventoryID'

            if(@in_ServerCall > 0) exec DBG_StoreApiCall 'ToInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, @InvInventoryID
            else                   exec DBG_StoreApiCall 'ToInv',     0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, @InvInventoryID
            return
        end
    END

    if exists(SELECT * FROM UsersInventory where InventoryID=@BackpackInventoryID AND Quantity > @in_Amount) and ((SELECT Quantity FROM UsersInventory where InventoryID=@BackpackInventoryID) > @in_Amount)
    BEGIN
        update UsersInventory set Quantity=(Quantity-@in_Amount) where InventoryID=@BackpackInventoryID
        if(@@ROWCOUNT = 0) begin
            select 6 as ResultCode, 'bad inventoryid' as ResultMsg
            return
        end
        ELSE
        BEGIN
            if not exists(select InventoryID from UsersInventory where InventoryID=@InvInventoryID) begin
            -- modified (won't stack) or new inventory item
                INSERT INTO UsersInventory (
                    CustomerID,
                    CharID,
                    ItemID,
                    LeasedUntil,
                    Quantity,
                    Var1,
                    Var2,
                    Var3
                )
                VALUES (
                    @in_CustomerID,
                    0,
                    @BackpackItemID,
                    @BackpackLeasedUntil,
                    @in_Amount,
                    @BackpackVar1,
                    @BackpackVar2,
                    @BackpackVar3
                )
                set @InvInventoryID = SCOPE_IDENTITY()
            end
            else begin
                update UsersInventory set Quantity=(Quantity+@in_Amount) where InventoryID=@InvInventoryID
            end
            select 0 as ResultCode
            select @InvInventoryID as 'InventoryID'

            if(@in_ServerCall > 0) exec DBG_StoreApiCall 'ToInv_Srv', 0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, @InvInventoryID
            else                   exec DBG_StoreApiCall 'ToInv',     0, @in_CustomerID, @in_CharID, @in_Slot, @BackpackItemID, @in_Amount, @BackpackInventoryID, @InvInventoryID
            return
            END
        END
END

ไม่มีความคิดเห็น:

แสดงความคิดเห็น