Пример #1
0
 public void ValidateAndPerformInventoryRequest(InventoryOperationRequest request, Player p)
 {
     if (InventoryOperationRequest.validateRequest(request, p.info))
     {
         InventoryOperationRequest.performInventoryRequest(request, p);
     }
 }
Пример #2
0
    private void sendInventoryUpdateToServer(InventoryOperationRequest request)
    {
        Debug.Log("sending inventory request\nop:" + request.op + "\nquant: " + request.quantity + "\nswapinv: " + request.swapInv + "\nswapslot: " + request.swapInvSlot);
        NetworkWriter writer = new NetworkWriter();

        request.Serialize(writer);
        localPlayer.GetComponent <Player> ().CmdRequestInventoryActions(writer.AsArray());
    }
Пример #3
0
    public void CmdRequestInventoryActions(byte[] operationInfo)
    {
        NetworkReader             reader  = new NetworkReader(operationInfo);
        InventoryOperationRequest request = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, InventoryOperationRequest.InventoryType.Ground, 0);

        request.Deserialize(reader);

        Debug.Log("recieved inventory request\nop:" + request.op + "\nquant: " + request.quantity + "\nswapinv: " + request.swapInv + "\nswapslot" + request.swapInvSlot);

        if (InventoryOperationRequest.validateRequest(request, info))
        {
            ItemInventorySlot dropped      = InventoryOperationRequest.performInventoryRequest(request, this);
            SpawnManager      spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent <SpawnManager> ();
            Debug.Log("player dropping " + dropped.getQuantity() + " " + dropped.getItem() + " items");
            spawnManager.SpawnPlayerDroppedItem(dropped.getItem(), dropped.getQuantity(), controller);
        }
        else
        {
            Debug.Log("recieved bad inventory request! client may be out of sync");
        }
    }
Пример #4
0
    void handleSlotSelect(ItemInventorySlotUI slotUI, string menuIdentifier, string slotIdentifier, int quantity)
    {
        InventoryOperationRequest.InventoryType menuType = InventoryOperationRequest.InventoryType.Ground;
        InventoryOperationRequest req = null;

        if (menuIdentifier.Equals("BeltSlot"))
        {
            menuType = InventoryOperationRequest.InventoryType.Belt;
        }
        else if (menuIdentifier.Equals("BackpackSlot"))
        {
            menuType = InventoryOperationRequest.InventoryType.Backpack;
        }
        else if (menuIdentifier.Equals("EquipSlot"))
        {
            menuType = InventoryOperationRequest.InventoryType.Equip;
        }


        if (slotUI == null)           //THREW IT ON THE GROUND
        {
            req = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, InventoryOperationRequest.InventoryType.Ground, System.Int32.Parse(slotIdentifier), quantity);
        }
        else if (cursorSlot.getSlotBackingInfo().isEmpty())
        {
            req = new InventoryOperationRequest(InventoryOperationRequest.Operation.ToCursor, menuType, System.Int32.Parse(slotIdentifier), quantity);
        }
        else
        {
            req = new InventoryOperationRequest(InventoryOperationRequest.Operation.FromCursor, menuType, System.Int32.Parse(slotIdentifier), quantity);
        }

        //perform local changes before sending info to server. Annoying to wait on server for ui change. If sync error, server should let us know eventually and fix
        if (InventoryOperationRequest.validateRequest(req, localPlayer.info))            //perform check server will do to verify before performing locally
        {
            ItemInventorySlot dropped = InventoryOperationRequest.performInventoryRequest(req, localPlayer);

            if (!localPlayer.isServer)
            {
                sendInventoryUpdateToServer(req);
            }
            else
            {
                //host drops items
                if (!dropped.isEmpty())
                {
                    SpawnManager spawnManager = GameObject.FindGameObjectWithTag("SpawnManager").GetComponent <SpawnManager>();
                    spawnManager.SpawnPlayerDroppedItem(dropped.getItem(), dropped.getQuantity(), localPlayer.controller);
                }
            }

            cursorSlot.updateUI();

            switch (menuType)
            {
            case InventoryOperationRequest.InventoryType.Belt:
                beltUI.updateUI();
                break;

            case InventoryOperationRequest.InventoryType.Backpack:
                backpackUI.updateUI();
                break;

            case InventoryOperationRequest.InventoryType.Equip:
                if (slotIdentifier.Equals("6"))
                {
                    if (localPlayer.info.backpack == null)
                    {
                        backpackUI.loadInventory(null);
                    }
                    else
                    {
                        backpackUI.loadInventory(localPlayer.info.backpack.inventory);
                    }
                }
                equipUI.updateUI(localPlayer.info);
                break;
            }
        }
    }
Пример #5
0
    //returns items to be dropped/spawned
    public static ItemInventorySlot performInventoryRequest(InventoryOperationRequest request, Player p)
    {
        switch (request.op)
        {
        case InventoryOperationRequest.Operation.ToCursor:
            switch (request.swapInv)
            {
            case InventoryOperationRequest.InventoryType.Belt:
                ItemInventorySlot beltslot = p.info.belt.getSlots() [request.swapInvSlot];
                ItemInventorySlot tempSlot = new ItemInventorySlot();
                if (request.quantity <= 0)
                {
                    tempSlot.setSlot(beltslot.getItem(), beltslot.getQuantity());
                    beltslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity());
                    p.info.cursorSlot.setSlot(tempSlot.getItem(), tempSlot.getQuantity());
                }
                else
                {
                    p.info.cursorSlot.addItemMany(beltslot.getItem(), request.quantity);
                    beltslot.removeMany(request.quantity);
                }
                break;

            case InventoryOperationRequest.InventoryType.Backpack:
                ItemInventorySlot backpackslot = p.info.backpack.inventory.getSlots() [request.swapInvSlot];
                ItemInventorySlot tempSlot2    = new ItemInventorySlot();
                if (request.quantity <= 0)
                {
                    tempSlot2.setSlot(backpackslot.getItem(), backpackslot.getQuantity());
                    backpackslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity());
                    p.info.cursorSlot.setSlot(tempSlot2.getItem(), tempSlot2.getQuantity());
                }
                else
                {
                    p.info.cursorSlot.addItemMany(backpackslot.getItem(), request.quantity);
                    backpackslot.removeMany(request.quantity);
                }
                break;

            case InventoryOperationRequest.InventoryType.Equip:
                switch ((InventoryOperationRequest.EquipSlots)request.swapInvSlot)
                {
                case InventoryOperationRequest.EquipSlots.Helmet:
                    p.info.cursorSlot.setSlot(p.info.helmet, 1);
                    p.info.helmet = null;
                    break;

                case InventoryOperationRequest.EquipSlots.UpperBody:
                    p.info.cursorSlot.setSlot(p.info.upperBody, 1);
                    p.info.upperBody = null;
                    break;

                case InventoryOperationRequest.EquipSlots.LowerBody:
                    p.info.cursorSlot.setSlot(p.info.lowerBody, 1);
                    p.info.lowerBody = null;
                    break;

                case InventoryOperationRequest.EquipSlots.Boots:
                    p.info.cursorSlot.setSlot(p.info.boots, 1);
                    p.info.boots = null;
                    break;

                case InventoryOperationRequest.EquipSlots.RightClaw:
                    p.info.cursorSlot.setSlot(p.info.rightClaw, 1);
                    p.info.rightClaw = null;
                    break;

                case InventoryOperationRequest.EquipSlots.LeftClaw:
                    p.info.cursorSlot.setSlot(p.info.leftClaw, 1);
                    p.info.leftClaw = null;
                    break;

                case InventoryOperationRequest.EquipSlots.Backpack:
                    p.info.cursorSlot.setSlot(p.info.backpack, 1);
                    p.info.backpack = null;
                    break;
                }
                break;
            }
            break;

        case InventoryOperationRequest.Operation.FromCursor:

            Item cursorItem = p.info.cursorSlot.getItem();

            switch (request.swapInv)
            {
            //if moving to an inventory slot, slot is either empty or has the item in it already.
            case InventoryOperationRequest.InventoryType.Belt:
                ItemInventorySlot beltslot = p.info.belt.getSlots() [request.swapInvSlot];

                if (request.quantity == 0)
                {
                    if (!p.info.cursorSlot.getItem().IsSameType(beltslot.getItem()))
                    {
                        ItemInventorySlot tempSlot = new ItemInventorySlot();
                        tempSlot.setSlot(beltslot.getItem(), beltslot.getQuantity());
                        beltslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity());
                        p.info.cursorSlot.setSlot(tempSlot.getItem(), tempSlot.getQuantity());
                    }
                    else
                    {
                        int remainder = beltslot.addItemMany(cursorItem, p.info.cursorSlot.getQuantity());
                        p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity() - remainder);
                    }
                }
                else
                {
                    beltslot.addItemMany(cursorItem, request.quantity);
                    p.info.cursorSlot.removeMany(request.quantity);
                }
                break;

            case InventoryOperationRequest.InventoryType.Backpack:
                ItemInventorySlot backpackslot = p.info.backpack.inventory.getSlots() [request.swapInvSlot];


                if (request.quantity == 0)
                {
                    if (!p.info.cursorSlot.getItem().IsSameType(backpackslot.getItem()))
                    {
                        ItemInventorySlot tempSlot2 = new ItemInventorySlot();
                        tempSlot2.setSlot(backpackslot.getItem(), backpackslot.getQuantity());
                        backpackslot.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity());
                        p.info.cursorSlot.setSlot(tempSlot2.getItem(), tempSlot2.getQuantity());
                    }
                    else
                    {
                        int remainder = backpackslot.addItemMany(cursorItem, p.info.cursorSlot.getQuantity());
                        p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity() - remainder);
                    }
                }
                else
                {
                    backpackslot.addItemMany(cursorItem, request.quantity);
                    p.info.cursorSlot.removeMany(request.quantity);
                }
                break;

            case InventoryOperationRequest.InventoryType.Equip:
                switch ((InventoryOperationRequest.EquipSlots)request.swapInvSlot)
                {
                case InventoryOperationRequest.EquipSlots.Helmet:
                    Item tempHelmet = p.info.helmet;
                    p.info.helmet = p.info.cursorSlot.getItem() as HelmetItem;
                    p.info.cursorSlot.setSlot(tempHelmet, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.UpperBody:
                    Item tempUpperBody = p.info.upperBody;
                    p.info.upperBody = p.info.cursorSlot.getItem() as UpperBodyItem;
                    p.info.cursorSlot.setSlot(tempUpperBody, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.LowerBody:
                    Item tempLowerBody = p.info.lowerBody;
                    p.info.lowerBody = p.info.cursorSlot.getItem() as LowerBodyItem;
                    p.info.cursorSlot.setSlot(tempLowerBody, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.Boots:
                    Item tempBoots = p.info.boots;
                    p.info.boots = p.info.cursorSlot.getItem() as BootsItem;
                    p.info.cursorSlot.setSlot(tempBoots, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.RightClaw:
                    Item tempRightClaw = p.info.rightClaw;
                    p.info.rightClaw = p.info.cursorSlot.getItem() as ClawItem;
                    p.info.cursorSlot.setSlot(tempRightClaw, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.LeftClaw:
                    Item tempLeftClaw = p.info.leftClaw;
                    p.info.leftClaw = p.info.cursorSlot.getItem() as ClawItem;
                    p.info.cursorSlot.setSlot(tempLeftClaw, 1);
                    break;

                case InventoryOperationRequest.EquipSlots.Backpack:
                    Item tempBackpack = p.info.backpack;
                    p.info.backpack = p.info.cursorSlot.getItem() as BackpackItem;
                    p.info.cursorSlot.setSlot(tempBackpack, 1);
                    break;
                }
                break;

            case InventoryOperationRequest.InventoryType.Ground:
                ItemInventorySlot dropped = new ItemInventorySlot();
                if (request.quantity == 0)
                {
                    dropped.setSlot(p.info.cursorSlot.getItem(), p.info.cursorSlot.getQuantity());
                    p.info.cursorSlot.removeMany(p.info.cursorSlot.getQuantity());
                }
                else
                {
                    dropped.setSlot(p.info.cursorSlot.getItem(), request.quantity);
                    p.info.cursorSlot.removeMany(request.quantity);
                }
                return(dropped);
            }
            break;
        }
        return(new ItemInventorySlot());
    }
Пример #6
0
    public static bool validateRequest(InventoryOperationRequest request, PlayerInfo info)
    {
        BackpackInventory backpack = null;

        if (info.backpack != null)
        {
            backpack = info.backpack.inventory;
        }
        BeltInventory     belt   = info.belt;
        ItemInventorySlot cursor = info.cursorSlot;

        ItemInventorySlot swapSlot      = null;
        EquipmentItem     swapEquipItem = null;
        bool drop = false;

        switch (request.swapInv)
        {
        case InventoryType.Ground:
            drop = true;
            break;

        case InventoryType.Backpack:
            swapSlot = assertSlotExists(backpack.getSlots(), request.swapInvSlot);
            if (swapSlot == null)
            {
                Debug.Log("InventoryOperation validation error: trying to swap to bad backpack slot");
                return(false);
            }
            break;

        case InventoryType.Belt:
            swapSlot = assertSlotExists(belt.getSlots(), request.swapInvSlot);
            if (swapSlot == null)
            {
                Debug.Log("InventoryOperation validation error: trying to swap to bad belt slot");
                return(false);
            }
            break;

        case InventoryType.Equip:
            swapEquipItem = getEquipItem(info, request.swapInvSlot);
            break;

        default:
            Debug.Log("InventoryOperation validation error: unsupported inventory detected");
            return(false);
        }

        switch (request.op)
        {
        case Operation.ToCursor:
            return(validateToCursorOperation(swapSlot, swapEquipItem, request.quantity, info));

        case Operation.FromCursor:
            return(validateFromCursorOperation(swapSlot, swapEquipItem, request.swapInvSlot, drop, request.quantity, info));

        default:
            Debug.Log("InventoryOperation validation error: unsupported operation detected");
            return(false);
        }
    }