Exemplo n.º 1
0
  /**
   * 根据方法的服务质量过滤,优先保留服务质量good的clients,数量低于least时加入服务质量normal的clients
   *
   * @param clientList
   * @param request
   * @param least 最少保留个数
   * @return
   */
  public List<Client> getQualityPreferClients(
      List<Client> clientList, InvocationRequest request, float least) {
    // 筛选good,normal,bad clients
    // 直接进行服务质量路由,先只保留服务质量good的,如果不够(比如少于1个),加入服务质量normal的
    if (!CollectionUtils.isEmpty(addrReqUrlQualities)) {
      String requestUrl = getRequestUrl(request);

      Map<RequrlQuality, List<Client>> filterQualityClientsMap =
          new HashMap<RequrlQuality, List<Client>>();
      for (RequrlQuality reqQuality : RequrlQuality.values()) {
        filterQualityClientsMap.put(reqQuality, new ArrayList<Client>());
      }

      for (Client client : clientList) {
        if (addrReqUrlQualities.containsKey(client.getAddress())) {
          ConcurrentHashMap<String, Quality> reqUrlQualities =
              addrReqUrlQualities.get(client.getAddress());
          if (reqUrlQualities.containsKey(requestUrl)) {
            Quality quality = reqUrlQualities.get(requestUrl);

            switch (quality.getQuality()) {
              case REQURL_QUALITY_GOOD:
                filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_GOOD).add(client);
                break;
              case REQURL_QUALITY_NORNAL:
                filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_NORNAL).add(client);
                break;
              case REQURL_QUALITY_BAD:
                filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_BAD).add(client);
                break;
              default:
                // never be here
                break;
            }
          }
        }
      }

      List<Client> filterQualityClients = new ArrayList<Client>();
      filterQualityClients.addAll(filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_GOOD));

      if (filterQualityClients.size() < least) {
        filterQualityClients.addAll(
            filterQualityClientsMap.get(RequrlQuality.REQURL_QUALITY_NORNAL));
      }

      return filterQualityClients;
    }

    return clientList;
  }
Exemplo n.º 2
0
  public static void main(String[] args) {
    Quality quality = new Quality(105, 1);
    System.out.println(quality.getQualityValue());
    final ConcurrentHashMap<String, ConcurrentHashMap<String, Quality>> addrReqUrlQualities =
        new ConcurrentHashMap<String, ConcurrentHashMap<String, Quality>>();
    new Thread() {
      @Override
      public void run() {
        RequestQualityManager.INSTANCE.setAddrReqUrlQualities(addrReqUrlQualities);
      }
    }.start();

    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      logger.warn("[main] thread interupt", e);
    }
    System.out.println(RequestQualityManager.INSTANCE.getAddrReqUrlQualities());
  }
Exemplo n.º 3
0
  public void save(ConfigurationSection s) {
    s.set("name", name);
    s.set("id", id);
    s.set("display", displayName.replaceAll("" + ChatColor.COLOR_CHAR, "&"));
    s.set("quality", quality.toString());
    s.set("damageMin", damageMin);
    s.set("damageMax", damageMax);
    s.set("armour", armour);
    s.set("type", type.replaceAll("" + ChatColor.COLOR_CHAR, "&"));
    s.set("hand", hand.replaceAll("" + ChatColor.COLOR_CHAR, "&"));
    s.set("lore", loreText.replaceAll("" + ChatColor.COLOR_CHAR, "&"));
    ArrayList<String> descriptionConv = new ArrayList<String>(description);
    for (int i = 0; i < descriptionConv.size(); i++) {
      descriptionConv.set(i, descriptionConv.get(i).replaceAll("" + ChatColor.COLOR_CHAR, "&"));
    }
    s.set("description", descriptionConv);
    s.set("item", item.getType().toString());
    s.set("ignoreWorldGuard", ignoreWorldGuard);

    ItemMeta meta = localeMeta.get("en_GB");
    if (meta instanceof LeatherArmorMeta) {
      s.set("item_colour", ((LeatherArmorMeta) meta).getColor().asRGB());
    } else {
      s.set("item_data", item.getDurability());
    }
    ConfigurationSection powerConfigs = s.createSection("powers");
    int i = 0;
    for (Power p : powers) {
      MemoryConfiguration pConfig = new MemoryConfiguration();
      pConfig.set("powerName", p.getName());
      p.save(pConfig);
      powerConfigs.set(Integer.toString(i), pConfig);
      i++;
    }

    // Recipes
    s.set("hasRecipe", hasRecipe);
    if (hasRecipe) {
      s.set("recipe", recipe);
    }

    ConfigurationSection drops = s.createSection("dropChances");
    for (String key : dropChances.keySet()) {
      drops.set(key, dropChances.get(key));
    }

    s.set("maxDurability", maxDurability);
    s.set("forceBar", forceBar);
  }
Exemplo n.º 4
0
  public RPGItem(ConfigurationSection s) {

    name = s.getString("name");
    id = s.getInt("id");
    setDisplay(s.getString("display"), false);
    setType(
        s.getString("type", Plugin.plugin.getConfig().getString("defaults.sword", "Sword")), false);
    setHand(
        s.getString("hand", Plugin.plugin.getConfig().getString("defaults.hand", "One handed")),
        false);
    setLore(s.getString("lore"), false);
    description = (List<String>) s.getList("description", new ArrayList<String>());
    for (int i = 0; i < description.size(); i++) {
      description.set(i, ChatColor.translateAlternateColorCodes('&', description.get(i)));
    }
    quality = Quality.valueOf(s.getString("quality"));
    damageMin = s.getInt("damageMin");
    damageMax = s.getInt("damageMax");
    armour = s.getInt("armour", 0);
    item = new ItemStack(Material.valueOf(s.getString("item")));
    ItemMeta meta = item.getItemMeta();
    if (meta instanceof LeatherArmorMeta) {
      ((LeatherArmorMeta) meta).setColor(Color.fromRGB(s.getInt("item_colour", 0)));
    } else {
      item.setDurability((short) s.getInt("item_data", 0));
    }
    for (String locale : Locale.getLocales()) {
      localeMeta.put(locale, meta.clone());
    }
    ignoreWorldGuard = s.getBoolean("ignoreWorldGuard", false);

    // Powers
    ConfigurationSection powerList = s.getConfigurationSection("powers");
    if (powerList != null) {
      for (String sectionKey : powerList.getKeys(false)) {
        ConfigurationSection section = powerList.getConfigurationSection(sectionKey);
        try {
          if (!Power.powers.containsKey(section.getString("powerName"))) {
            // Invalid power
            continue;
          }
          Power pow = Power.powers.get(section.getString("powerName")).newInstance();
          pow.init(section);
          pow.item = this;
          addPower(pow, false);
        } catch (InstantiationException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
      }
    }
    encodedID = getMCEncodedID(id);

    // Recipes
    hasRecipe = s.getBoolean("hasRecipe", false);
    if (hasRecipe) {
      recipe = (List<ItemStack>) s.getList("recipe");
    }

    ConfigurationSection drops = s.getConfigurationSection("dropChances");
    if (drops != null) {
      for (String key : drops.getKeys(false)) {
        double chance = drops.getDouble(key, 0.0);
        chance = Math.min(chance, 100.0);
        if (chance > 0) {
          dropChances.put(key, chance);
          if (!Events.drops.containsKey(key)) {
            Events.drops.put(key, new HashSet<Integer>());
          }
          Set<Integer> set = Events.drops.get(key);
          set.add(getID());
        } else {
          dropChances.remove(key);
          if (Events.drops.containsKey(key)) {
            Set<Integer> set = Events.drops.get(key);
            set.remove(getID());
          }
        }
        dropChances.put(key, chance);
      }
    }
    if (item.getType().getMaxDurability() != 0) {
      hasBar = true;
    }
    maxDurability = s.getInt("maxDurability", item.getType().getMaxDurability());
    forceBar = s.getBoolean("forceBar", false);

    if (maxDurability == 0) {
      maxDurability = -1;
    }

    rebuild();
  }