示例#1
0
    public void TestDeleteInventory()
    {
        var                  blueprintApi   = BlueprintAPI.WithBaseUrl(baseUrl);
        UserCredentials      user           = null;
        ResponseGetInventory finalInventory = null;

        // Authenticate user to gain access token
        Task.Run(async() => {
            APIResult <UserCredentials, JsonError> response = await blueprintApi.AsyncAuthenticateUser(validUser);
            user = response.GetSuccess();
        }).GetAwaiter().GetResult();

        // Add item to test user inventory
        Task.Run(async() => {
            List <InventoryEntry> entries = new List <InventoryEntry>();
            entries.Add(new InventoryEntry(1, 1));

            ResponseGetInventory inventory          = new ResponseGetInventory(entries);
            APIResult <Boolean, JsonError> response = await blueprintApi.AsyncAddToInventory(user, inventory);
        }).GetAwaiter().GetResult();

        // Delete inventory and assert on response
        Task.Run(async() => {
            try {
                APIResult <Boolean, JsonError> response = await blueprintApi.AsyncDeleteInventory(user);

                // Success case
            } catch (WebException e) {
                // Failure case
                Assert.Fail();
            }
        }).GetAwaiter().GetResult();
    }
示例#2
0
    public void TestGetInventory()
    {
        var                  blueprintApi   = BlueprintAPI.WithBaseUrl(baseUrl);
        UserCredentials      user           = null;
        ResponseGetInventory finalInventory = null;

        // Authenticate user to gain access token
        Task.Run(async() => {
            APIResult <UserCredentials, JsonError> response = await blueprintApi.AsyncAuthenticateUser(validUser);
            user = response.GetSuccess();
        }).GetAwaiter().GetResult();


        // Add item to test user inventory
        Task.Run(async() => {
            List <InventoryEntry> entries = new List <InventoryEntry>();
            entries.Add(new InventoryEntry(1, 1));
            ResponseGetInventory inventory = new ResponseGetInventory(entries);

            APIResult <Boolean, JsonError> response = await blueprintApi.AsyncAddToInventory(user, inventory);
        }).GetAwaiter().GetResult();

        // Retrieve inventory of new user
        Task.Run(async() => {
            APIResult <ResponseGetInventory, JsonError> finalInventoryResponse = await blueprintApi.AsyncGetInventory(user);
            finalInventory = finalInventoryResponse.GetSuccess();
        }).GetAwaiter().GetResult();

        Assert.That(finalInventory.items[0].item_id, Is.EqualTo(1));
        Assert.That(finalInventory.items[0].item_id, Is.GreaterThanOrEqualTo(1));
    }
示例#3
0
        public void Start()
        {
            heldItem          = GameObject.Find("HeldItem");
            currentHeld       = 0;
            inventoryContents = new InventoryItem[size];
            itemSlots         = GameObject.Find("GridPanel").GetComponentsInChildren <InventorySlotController>().ToList();
            UserCredentials user = GameManager.Instance().GetUserCredentials();

            GameManager.Instance().store.Subscribe(this);
            var blueprintApi = BlueprintAPI.DefaultCredentials();

            Task.Run(async() => {
                APIResult <ResponseGetInventory, JsonError> finalInventoryResponse = await blueprintApi.AsyncGetInventory(user);
                if (finalInventoryResponse.isSuccess())
                {
                    remoteInv = finalInventoryResponse.GetSuccess();
                }
                else
                {
                    JsonError error = finalInventoryResponse.GetError();
                }
            }).GetAwaiter().GetResult();

            foreach (InventoryEntry entry in remoteInv.items)
            {
                GameManager.Instance().store.Dispatch(
                    new AddItemToInventory(entry.item_id, entry.quantity, GetItemName(entry.item_id)));
            }

            /*
             *          Task.Run(async () => {
             *              try {
             *                  APIResult<Boolean, JsonError> response = await blueprintApi.AsyncDeleteInventory(user);
             *
             *                  // Success case
             *              } catch (WebException e) {
             *                  // Failure case
             *                  throw new System.Exception("Did not delete inventory.");
             *              }
             *          }).GetAwaiter().GetResult();*/
            foreach (Transform child in heldItem.transform)
            {
                if (child.gameObject.CompareTag("Held"))
                {
                    child.gameObject.GetComponent <Text>().text = GetItemName(inventoryContents[currentHeld].GetId());
                }
            }
        }
示例#4
0
        public async Task <APIResult <ResponseGetInventory, JsonError> > AsyncGetInventory(UserCredentials user)
        {
            try {
                string response = await rs.PerformAsyncGet(inventoryEndpoint, user.GetAccessToken());

                ResponseGetInventory inventory = JsonUtility.FromJson <ResponseGetInventory>(response);

                // Return APIResult:ResponseGetInventory in success case
                return(new APIResult <ResponseGetInventory, JsonError>(inventory));
            } catch (WebException e) when(RetrieveHTTPCode(e) == (int)httpResponseCode.unauthorised)
            {
                // If access token doesn't match a user, refresh tokens and retry
                APIResult <ResponseAuthenticate, JsonError> refreshedTokens = await AsyncRefreshTokens(user);

                try {
                    string response = await rs.PerformAsyncGet(inventoryEndpoint, refreshedTokens.GetSuccess().access);

                    ResponseGetInventory inventory = JsonUtility.FromJson <ResponseGetInventory>(response);

                    // Return APIResult:ResponseGetInventory in success case
                    return(new APIResult <ResponseGetInventory, JsonError>(inventory));
                } catch (WebException f) {
                    // Retrieve error payload from WebException
                    JsonError error = JsonUtility.FromJson <JsonError>(retrieveErrorJson(f));

                    // Return APIResult:JsonError in error case
                    return(new APIResult <ResponseGetInventory, JsonError>(error));
                }
            } catch (WebException e) {
                // Retrieve error payload from WebException
                JsonError error = JsonUtility.FromJson <JsonError>(retrieveErrorJson(e));

                // Return APIResult:JsonError in error case
                return(new APIResult <ResponseGetInventory, JsonError>(error));
            }
        }