Пример #1
0
  private static Promise moveFromVault(
      final ClientWebView webView,
      final String subject,
      final Item item,
      final int stackSize,
      final Promise p) {
    JSONObject obj = generateMoveJson(subject, item, false, stackSize);
    final Promise p_inner = new Promise();
    webView
        .call("destinyService.TransferItem", obj)
        .then(
            new ClientWebView.Callback() {
              @Override
              public void onAccept(String result) {
                webView.queueRunnable(
                    new Runnable() {
                      @Override
                      public void run() {
                        item.moveTo(subject, stackSize);
                        p.onSuccess();
                        p_inner.onSuccess();
                      }
                    });
              }

              @Override
              public void onError(String result) {
                p.onError(result);
                p_inner.onError(result);
              }
            });
    return p_inner;
  }
Пример #2
0
  public static Promise equip(
      final ClientWebView webView, final String owner, final Item item, final Promise p) {
    final Promise p_inner = new Promise();

    JSONObject obj = new JSONObject();
    try {
      obj.put("characterId", owner);
      obj.put("itemId", item.getItemId());
      obj.put("membershipType", Data.getInstance().getMembership().getType());
    } catch (JSONException e) {
      Log.e(LOG_TAG, "exception", e);
      e.printStackTrace();
    }

    webView
        .call("destinyService.EquipItem", obj)
        .then(
            new ClientWebView.Callback() {
              @Override
              public void onAccept(String result) {
                for (Item tested_item : Items.getInstance().all()) {
                  if (tested_item.getBucketTypeHash() == item.getBucketTypeHash()
                      && item.getItemHash() != tested_item.getItemHash()
                      && Items.getInstance().getItemOwner(tested_item).equals(owner)
                      && tested_item.getCanEquip()) {
                    tested_item.setIsEquipped(false);
                    item.setIsEquipped(true);
                    break;
                  }
                }
                p_inner.onSuccess();
              }

              @Override
              public void onError(String result) {
                p_inner.onError(result);
              }
            });
    return p_inner;
  }
Пример #3
0
  private static Promise moveToVault(
      final ClientWebView webView,
      final String owner,
      final Item item,
      final int stackSize,
      final Promise p) {
    JSONObject obj = generateMoveJson(owner, item, true, stackSize);
    final Promise p_inner = new Promise();
    if (item.isEquipped()) {
      /* find items from the same hash */
      List<Item> proposed = new ArrayList<>();
      String item_owner = Items.getInstance().getItemOwner(item);
      for (Item tested_item : Items.getInstance().all()) {
        if (tested_item.getBucketTypeHash() == item.getBucketTypeHash()
            && item.getItemHash() != tested_item.getItemHash()
            && Items.getInstance().getItemOwner(tested_item).equals(item_owner)
            && tested_item.getCanEquip()) {
          proposed.add(tested_item);
        }
      }
      Collections.sort(proposed);

      if (proposed.size() > 0) {
        final Item to_equip = proposed.get(proposed.size() - 1);
        equip(webView, owner, to_equip, p)
            .then(
                new Result() {
                  @Override
                  public void onSuccess() {
                    item.setIsEquipped(false);
                    to_equip.setIsEquipped(true);
                    moveToVault(webView, owner, item, stackSize, p)
                        .then(
                            new Result() {
                              @Override
                              public void onSuccess() {
                                p_inner.onSuccess();
                              }

                              @Override
                              public void onError(String e) {
                                p_inner.onError(e);
                              }
                            });
                  }

                  @Override
                  public void onError(String e) {
                    p.onError(e);
                    p_inner.onError(e);
                  }
                });
      } else {
        webView.postDelayed(
            new Runnable() {
              @Override
              public void run() {
                String message =
                    webView.getContext().getString(R.string.cannot_move_error, item.toString());
                p_inner.onError(message);
                if (p != null) {
                  p.onError(message);
                } else {
                  Log.e(LOG_TAG, message);
                }
              }
            },
            1);
      }
      return p_inner;
    }
    webView
        .call("destinyService.TransferItem", obj)
        .then(
            new ClientWebView.Callback() {
              @Override
              public void onAccept(String result) {
                webView.queueRunnable(
                    new Runnable() {
                      @Override
                      public void run() {
                        item.moveTo(Items.VAULT_ID, stackSize);
                        if (p != null) p.onSuccess();
                        p_inner.onSuccess();
                      }
                    });
              }

              @Override
              public void onError(String result) {
                try {
                  if (new JSONObject(result).optInt("errorCode") == 1656) {
                    // do reload database
                  }
                } catch (JSONException e) {
                  e.printStackTrace();
                }
                if (p != null) p.onError(result);
                p_inner.onError(result);
              }
            });
    return p_inner;
  }