예제 #1
0
  /** Method load. */
  public static void load() {
    VoteList.clear();

    try {
      File file = new File(Config.DATAPACK_ROOT, "data/xml/other/vote.xml");
      DocumentBuilderFactory factory2 = DocumentBuilderFactory.newInstance();
      factory2.setValidating(false);
      factory2.setIgnoringComments(true);
      Document doc2 = factory2.newDocumentBuilder().parse(file);

      for (Node n2 = doc2.getFirstChild(); n2 != null; n2 = n2.getNextSibling()) {
        if ("list".equals(n2.getNodeName())) {
          for (Node d2 = n2.getFirstChild(); d2 != null; d2 = d2.getNextSibling()) {
            if ("vote".equals(d2.getNodeName())) {
              Vote v = new Vote();
              v.id = Integer.parseInt(d2.getAttributes().getNamedItem("id").getNodeValue());
              v.maxPerAccount =
                  Integer.parseInt(d2.getAttributes().getNamedItem("maxPerAccount").getNodeValue());
              v.name = d2.getAttributes().getNamedItem("name").getNodeValue();
              v.active =
                  Boolean.parseBoolean(d2.getAttributes().getNamedItem("active").getNodeValue());

              for (Node i = d2.getFirstChild(); i != null; i = i.getNextSibling()) {
                if ("variant".equals(i.getNodeName())) {
                  v.variants.put(
                      Integer.parseInt(i.getAttributes().getNamedItem("id").getNodeValue()),
                      i.getAttributes().getNamedItem("desc").getNodeValue());
                }
              }

              VoteList.put(v.id, v);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    try (Connection con = DatabaseFactory.getInstance().getConnection(); ) {

      PreparedStatement st = con.prepareStatement("SELECT * FROM vote");
      ResultSet rs = st.executeQuery();

      while (rs.next()) {
        Vote v = VoteList.get(rs.getInt("id"));

        if (v != null) {
          String HWID = rs.getString("HWID");
          Integer[] rez = v.results.get(HWID);
          v.results.put(HWID, ArrayUtils.add(rez, rs.getInt("vote")));
        }
      }
      rs.close();
      st.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #2
0
  /**
   * Method vote.
   *
   * @param command String
   * @param activeChar Player
   * @param args String
   * @return boolean
   */
  @SuppressWarnings("unchecked")
  private boolean vote(String command, Player activeChar, String args) {
    if ((args != null) && !args.isEmpty()) {
      String[] param = args.split(" ");

      if ((param.length >= 2) && Util.isNumber(param[0]) && Util.isNumber(param[1])) {
        String playerId = activeChar.getAccountName();
        Vote v = VoteList.get(Integer.parseInt(param[0]));

        if ((v == null) || !v.active) {
          return false;
        }

        int var = Integer.parseInt(param[1]);
        Integer[] alreadyResults = v.results.get(playerId);

        if (alreadyResults == null) {
          v.results.put(playerId, new Integer[] {var});
          mysql.set(
              "INSERT IGNORE INTO vote (`id`, `HWID`, `vote`) VALUES (?,?,?)",
              param[0],
              playerId,
              param[1]);
        } else if (alreadyResults.length < v.maxPerAccount) {
          for (int id : alreadyResults) {
            if (id == var) {
              show("Error: you have already voted for this entry.", activeChar);
              return false;
            }
          }

          v.results.put(playerId, ArrayUtils.add(alreadyResults, var));
          mysql.set(
              "INSERT IGNORE INTO vote (`id`, `HWID`, `vote`) VALUES (?,?,?)",
              param[0],
              playerId,
              param[1]);
        } else {
          show("Error: you have reached votes limit.", activeChar);
          return false;
        }
      }
    }

    int count = 0;
    StringBuilder html = new StringBuilder("!Vote Manager:\n<br>");
    String playerId = activeChar.getAccountName();

    for (Entry<Integer, Vote> e : VoteList.entrySet()) {
      if (e.getValue().active) {
        count++;
        html.append(e.getValue().name).append(":<br>");
        Integer[] already = e.getValue().results.get(playerId);

        if ((already != null) && (already.length >= e.getValue().maxPerAccount)) {
          html.append("You have already voted.<br>");
        } else {
          Entry<Integer, String>[] variants = new Entry[e.getValue().variants.size()];
          int i = 0;

          for (Entry<Integer, String> variant : e.getValue().variants.entrySet()) {
            variants[i] = variant;
            i++;
          }

          shuffle(variants);
          variants:
          for (Entry<Integer, String> variant : variants) {
            if (already != null) {
              for (Integer et : already) {
                if (et.equals(variant.getKey())) {
                  continue variants;
                }
              }
            }

            html.append(
                "[user_vote "
                    + e.getValue().id
                    + " "
                    + variant.getKey()
                    + "|"
                    + variant.getValue()
                    + "]<br1>");
          }

          html.append("<br>");
        }
      }
    }

    if (count == 0) {
      html.append("No active votes now.");
    }

    show(html.toString(), activeChar);
    return true;
  }