private static String replaceHeadFootVariables(
      String pBlock, String pPlanName, List<Attack> pAttacks) {
    String result = pBlock;
    // set creator
    Tribe user = GlobalOptions.getSelectedProfile().getTribe();
    if (user != null) {
      result = result.replaceAll(CREATOR, Matcher.quoteReplacement(user.getName()));
    } else {
      result = result.replaceAll(CREATOR, "-");
    }
    // set planname
    if (pPlanName != null) {
      result = result.replaceAll(PLANNAME, EscapeChars.forHTML(pPlanName));
    } else {
      result = result.replaceAll(PLANNAME, "-");
    }
    // set attack count
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumFractionDigits(0);
    nf.setMinimumFractionDigits(0);
    result = result.replaceAll(ATTACK_COUNT, nf.format(pAttacks.size()));
    // set attack count
    String server = GlobalOptions.getSelectedServer();
    if (server != null) {
      result = result.replaceAll(SERVER, server);
    } else {
      result = result.replaceAll(SERVER, "-");
    }
    // replace version
    result =
        result.replaceAll(VERSION, Double.toString(Constants.VERSION) + Constants.VERSION_ADDITION);
    // replace creation date
    SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy 'um' HH:mm:ss 'Uhr'");
    result = result.replaceAll(CREATION_DATE, f.format(new Date(System.currentTimeMillis())));

    return result;
  }
  private void buildTree() {
    mRoot = new SelectionTreeRootNode("Auswahl");
    if (treeMode) {
      // tree view
      // add all villages
      Hashtable<Ally, AllyNode> allyNodes = new Hashtable<Ally, AllyNode>();
      Hashtable<Tribe, TribeNode> tribeNodes = new Hashtable<Tribe, TribeNode>();
      Hashtable<Tribe, Hashtable<Tag, TagNode>> tagNodes =
          new Hashtable<Tribe, Hashtable<Tag, TagNode>>();

      List<Village> used = new LinkedList<Village>();

      for (Village v : treeData) {
        Tribe t = v.getTribe();
        if (t == null) {
          t = Barbarians.getSingleton();
        }
        Ally a = t.getAlly();
        if (a == null) {
          a = NoAlly.getSingleton();
        }

        AllyNode aNode = allyNodes.get(a);
        if (aNode == null) {
          // new ally
          aNode = new AllyNode(a);
          allyNodes.put(a, aNode);
          mRoot.add(aNode);
        }
        TribeNode tNode = tribeNodes.get(t);
        if (tNode == null) {
          // new tribe
          tNode = new TribeNode(t);
          tribeNodes.put(t, tNode);
          aNode.add(tNode);
        }
        boolean hasTag = false;
        for (Tag tag : TagManager.getSingleton().getTags(v)) {
          hasTag = true;

          Hashtable<Tag, TagNode> nodes = tagNodes.get(t);
          if (nodes == null) {
            nodes = new Hashtable<Tag, TagNode>();
            tagNodes.put(t, nodes);
          }
          TagNode tagNode = nodes.get(tag);
          if (tagNode == null) {
            // new tribe
            tagNode = new TagNode(tag);
            nodes.put(tag, tagNode);
            tNode.add(tagNode);
          }
          tagNode.add(new VillageNode(v));
        }

        if (!hasTag) {
          // only add directly if not added to any tag node
          tNode.add(new VillageNode(v));
        }
        used.add(v);
      }
    } else {
      // simple view
      for (Village v : treeData) {
        mRoot.add(new VillageNode(v));
      }
    }

    jSelectionTree.setModel(new DefaultTreeModel(mRoot));
  }
 public List<Village> getSelectedElements() {
   TreePath[] paths = jSelectionTree.getSelectionModel().getSelectionPaths();
   List<Village> result = new LinkedList<Village>();
   if (paths == null) {
     return result;
   }
   for (TreePath p : paths) {
     Object o = p.getLastPathComponent();
     if (o instanceof AllyNode) {
       Ally a = ((AllyNode) o).getUserObject();
       Village[] copy = treeData.toArray(new Village[] {});
       for (Village v : copy) {
         if (v.getTribe() == Barbarians.getSingleton() && a.equals(BarbarianAlly.getSingleton())) {
           // remove barbarian ally member
           if (!result.contains(v)) {
             result.add(v);
           }
         } else if (v.getTribe() != Barbarians.getSingleton()
             && v.getTribe().getAlly() == null
             && a.equals(NoAlly.getSingleton())) {
           // remove no-ally member
           if (!result.contains(v)) {
             result.add(v);
           }
         } else if (v.getTribe() != Barbarians.getSingleton()
             && v.getTribe().getAlly() != null
             && a.equals(v.getTribe().getAlly())) {
           // remove if ally is equal
           if (!result.contains(v)) {
             result.add(v);
           }
         }
       }
     } else if (o instanceof TribeNode) {
       Tribe t = ((TribeNode) o).getUserObject();
       Village[] copy = treeData.toArray(new Village[] {});
       for (Village v : copy) {
         if (v.getTribe() == Barbarians.getSingleton() && t.equals(Barbarians.getSingleton())) {
           // if village is barbarian village and selected tribe are barbs, remove village
           if (!result.contains(v)) {
             result.add(v);
           }
         } else if (v.getTribe() != null
             && !v.getTribe().equals(Barbarians.getSingleton())
             && t != null
             && v.getTribe().getId() == t.getId()) {
           // selected tribe are no barbs, so check tribes to be equal
           if (!result.contains(v)) {
             result.add(v);
           }
         }
       }
     } else if (o instanceof TagNode) {
       Tag t = ((TagNode) o).getUserObject();
       Village[] copy = treeData.toArray(new Village[] {});
       for (Village v : copy) {
         if (v != null && t != null && t.tagsVillage(v.getId())) {
           if (!result.contains(v)) {
             result.add(v);
           }
         }
       }
     } else if (o instanceof VillageNode) {
       Village v = ((VillageNode) o).getUserObject();
       if (!result.contains(v)) {
         result.add(v);
       }
     } else if (o != null && o.equals(mRoot)) {
       // remove all
       result = new LinkedList<Village>(treeData);
       // nothing more than everything can be removed
       return result;
     } else {
       // remove nothing
     }
   }
   return result;
 }
예제 #4
0
  private void generateSOS() {
    String plan = (String) jAttackPlan.getSelectedItem();
    int amount = Math.min(UIHelper.parseIntFromField(jAmount, 1), jAttack.getItemCount());
    SOSRequest sos = null;
    Tribe t = null;
    for (int i = 0; i < amount; i++) {
      Attack a = AttackManager.getSingleton().getManagedElement(plan, i);
      if (sos == null) {
        sos = new SOSRequest(a.getTarget().getTribe());
        t = a.getTarget().getTribe();
      }

      if (t != null && a.getTarget().getTribe().getId() == t.getId()) {
        TargetInformation info = sos.addTarget(a.getTarget());
        if (info.getTroops().isEmpty()) {
          Hashtable<UnitHolder, Integer> troops = getDefendingTroops();

          Enumeration<UnitHolder> keys = troops.keys();
          while (keys.hasMoreElements()) {
            UnitHolder key = keys.nextElement();
            info.addTroopInformation(key, troops.get(key));
          }
          info.setWallLevel(20);
        }
        if (jIncludeTypes.isSelected()) {
          info.addAttack(
              a.getSource(),
              a.getArriveTime(),
              a.getUnit(),
              a.getType() == Attack.FAKE_TYPE,
              a.getUnit().getPlainName().equals("snob"));
        } else {
          info.addAttack(a.getSource(), a.getArriveTime());
        }
      } else {
        System.err.println(
            "Tribe "
                + a.getTarget().getTribe()
                + "does not fit request tribe "
                + sos.getDefender());
      }
    }

    /**
     * [b]Dorf:[/b] [coord]112|87[/coord] [b]Wallstufe:[/b] 20 [b]Verteidiger:[/b] 23011 22928 0 266
     * 0 814 0 0 0
     *
     * <p>bäääng! [coord]282|306[/coord] --> Ankunftszeit: 11.10.11 14:37:57
     * [player]MrBlue76[/player]
     *
     * <p>[b]Dorf:[/b] [coord]114|84[/coord] [b]Wallstufe:[/b] 20 [b]Verteidiger:[/b] 9079 9080 0
     * 100 0 300 0 0 0
     *
     * <p>bäääng! [coord]318|272[/coord] --> Ankunftszeit: 11.10.11 14:42:49
     * [player]MrBlue76[/player] bäääng! [coord]211|345[/coord] --> Ankunftszeit: 11.10.11 16:45:37
     * [player]MrBlue76[/player]
     */
    Enumeration<Village> targets = sos.getTargets();
    StringBuilder b = new StringBuilder();
    SimpleDateFormat df;
    if (de.tor.tribes.util.ServerSettings.getSingleton().isMillisArrival()) {
      df = new SimpleDateFormat("dd.MM.yy HH:mm:ss:SSS");
    } else {
      df = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
    }
    while (targets.hasMoreElements()) {
      Village target = targets.nextElement();
      b.append("[b]Dorf:[/b] ").append(target.toBBCode()).append("\n");
      TargetInformation ti = sos.getTargetInformation(target);
      b.append("[b]Wallstufe:[/b] ").append(ti.getWallLevel()).append("\n");
      b.append("[b]Verteidiger:[/b] ");
      for (UnitHolder unit : DataHolder.getSingleton().getUnits()) {
        b.append(ti.getTroops().get(unit)).append(" ");
      }
      b.append("\n\n");
      for (TimedAttack a : ti.getAttacks()) {
        if (jIncludeTypes.isSelected()) {
          if (a.isPossibleFake()) {
            b.append("Fake, ");
          } else {
            if (a.getUnit() != null) {
              if (a.getUnit().getPlainName().equals("axe")) {
                b.append("Axt, ");
              } else if (a.getUnit().getPlainName().equals("light")) {
                b.append("LKAV, ");
              } else if (a.getUnit().getPlainName().equals("snob")) {
                b.append("AG, ");
              } else if (a.getUnit().getPlainName().equals("heavy")) {
                b.append("SKAV, ");
              } else if (a.getUnit().getPlainName().equals("sword")) {
                b.append("Schwert, ");
              } else if (a.getUnit().getPlainName().equals("catapult")) {
                b.append("Kata, ");
              }
            }
          }
        }
        b.append(a.getSource().getName())
            .append(" ")
            .append(a.getSource().toBBCode())
            .append(" --> Ankunftszeit: ")
            .append(df.format(new Date(a.getlArriveTime())))
            .append(" ")
            .append(a.getSource().getTribe().toBBCode())
            .append("\n");
      }
      b.append("\n");
    }
    sendToClipboard(b.toString());
  }
  public static void doExport(File pHtmlFile, String pPlanName, List<Attack> pAttacks) {
    if (TEMPLATE_ERROR) {
      logger.warn("Skip writing HTML file due to TEMPLATE_ERROR flag");
      return;
    }
    SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    StringBuffer result = new StringBuffer();
    // append header
    result.append(replaceHeadFootVariables(HEADER, pPlanName, pAttacks));

    int cnt = 0;
    for (Attack a : pAttacks) {
      String b = BLOCK;
      // <editor-fold defaultstate="collapsed" desc="Replace DIV-IDs">
      if (cnt % 2 == 0) {
        b = b.replaceAll(DIV_CLASS, "odd_div");
      } else {
        b = b.replaceAll(DIV_CLASS, "even_div");
      }
      b = b.replaceAll(ID, Integer.toString(cnt));
      // </editor-fold>

      // <editor-fold defaultstate="collapsed" desc="Replace Unit Icons">
      UnitHolder unit = a.getUnit();
      b =
          b.replaceAll(
              UNIT,
              "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/"
                  + unit.getPlainName()
                  + ".png\">");

      switch (a.getType()) {
        case Attack.CLEAN_TYPE:
          {
            b =
                b.replaceAll(
                    TYPE, "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/att.png\">");
            break;
          }
        case Attack.SNOB_TYPE:
          {
            b =
                b.replaceAll(
                    TYPE, "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/snob.png\">");
            break;
          }
        case Attack.FAKE_TYPE:
          {
            b =
                b.replaceAll(
                    TYPE, "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/fake.png\">");
            break;
          }
        case Attack.FAKE_DEFF_TYPE:
          {
            b =
                b.replaceAll(
                    TYPE,
                    "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/def_fake.png\">");
            break;
          }
        case Attack.SUPPORT_TYPE:
          {
            b =
                b.replaceAll(
                    TYPE, "<img src=\"http://www.dsworkbench.de/DSWorkbench/export/ally.png\">");
            break;
          }
        default:
          {
            b = b.replaceAll(TYPE, "-");
            break;
          }
      }
      // </editor-fold>

      String baseURL = ServerManager.getServerURL(GlobalOptions.getSelectedServer()) + "/";

      // <editor-fold defaultstate="collapsed" desc=" replace source tribe and ally">
      Tribe sourceTribe = a.getSource().getTribe();
      String sourceTribeName = "";
      String sourceTribeLink = "";
      String sourceAllyName = "";
      String sourceAllyTag = "";
      String sourceAllyLink = "";
      String sourceVillageName = "";
      String sourceVillageCoord = "";
      String sourceVillageLink = "";

      if (sourceTribe == null) {
        // tribe is null, so it is a barbarian village
        sourceTribeName = "Barbaren";
        sourceAllyName = "Barbaren";
      } else {
        sourceTribeLink = baseURL;
        sourceTribeLink += "guest.php?screen=info_player&id=" + sourceTribe.getId();
        sourceTribeName = sourceTribe.getName();

        // replace source tribe
        Ally sourceAlly = sourceTribe.getAlly();
        if (sourceAlly == null) {
          // tribe has no ally
          sourceAllyName = "Kein Stamm";
        } else {
          // ally valid
          sourceAllyName = sourceAlly.getName();
          sourceAllyTag = sourceAlly.getTag();
          sourceAllyLink = baseURL;
          sourceAllyLink += "guest.php?screen=info_ally&id=" + sourceAlly.getId();
        }
      }
      // replace source village
      sourceVillageLink = baseURL;
      sourceVillageLink += "guest.php?screen=info_village&id=" + a.getSource().getId();
      sourceVillageName = a.getSource().getFullName();
      sourceVillageCoord = a.getSource().getCoordAsString();

      // replace values
      b = b.replaceAll(SOURCE_PLAYER_NAME, Matcher.quoteReplacement(sourceTribeName));
      b = b.replaceAll(SOURCE_PLAYER_LINK, sourceTribeLink);
      b = b.replaceAll(SOURCE_ALLY_NAME, Matcher.quoteReplacement(sourceAllyName));
      b = b.replaceAll(SOURCE_ALLY_TAG, Matcher.quoteReplacement(sourceAllyTag));
      b = b.replaceAll(SOURCE_ALLY_LINK, sourceAllyLink);
      b = b.replaceAll(SOURCE_VILLAGE_NAME, Matcher.quoteReplacement(sourceVillageName));
      b = b.replaceAll(SOURCE_VILLAGE_COORD, sourceVillageCoord);
      b = b.replaceAll(SOURCE_VILLAGE_LINK, sourceVillageLink);

      // </editor-fold>

      // <editor-fold defaultstate="collapsed" desc=" replace target tribe and ally">
      Tribe targetTribe = a.getTarget().getTribe();
      String targetTribeName = "";
      String targetTribeLink = "";
      String targetAllyName = "";
      String targetAllyTag = "";
      String targetAllyLink = "";
      String targetVillageName = "";
      String targetVillageCoord = "";
      String targetVillageLink = "";

      if (targetTribe == null) {
        // tribe is null, so it is a barbarian village
        targetTribeName = "Barbaren";
        targetAllyName = "Barbaren";
      } else {
        targetTribeLink = baseURL;
        targetTribeLink += "guest.php?screen=info_player&id=" + targetTribe.getId();
        targetTribeName = targetTribe.getName();

        // replace source tribe
        Ally targetAlly = targetTribe.getAlly();
        if (targetAlly == null) {
          // tribe has no ally
          targetAllyName = "Kein Stamm";
        } else {
          // ally valid
          targetAllyName = targetAlly.getName();
          targetAllyTag = targetAlly.getTag();
          targetAllyLink = baseURL;
          targetAllyLink += "guest.php?screen=info_ally&id=" + targetAlly.getId();
        }
      }
      // replace source village
      targetVillageLink = baseURL;
      targetVillageLink += "guest.php?screen=info_village&id=" + a.getTarget().getId();
      targetVillageName = a.getTarget().getFullName();
      targetVillageCoord = a.getTarget().getCoordAsString();

      // replace values
      b = b.replaceAll(TARGET_PLAYER_NAME, Matcher.quoteReplacement(targetTribeName));
      b = b.replaceAll(TARGET_PLAYER_LINK, targetTribeLink);
      b = b.replaceAll(TARGET_ALLY_NAME, Matcher.quoteReplacement(targetAllyName));
      b = b.replaceAll(TARGET_ALLY_TAG, Matcher.quoteReplacement(targetAllyTag));
      b = b.replaceAll(TARGET_ALLY_LINK, targetAllyLink);
      b = b.replaceAll(TARGET_VILLAGE_NAME, Matcher.quoteReplacement(targetVillageName));
      b = b.replaceAll(TARGET_VILLAGE_COORD, targetVillageCoord);
      b = b.replaceAll(TARGET_VILLAGE_LINK, targetVillageLink);

      // </editor-fold>

      // <editor-fold defaultstate="collapsed" desc="Replace times and place URL">
      // replace arrive time
      String arrive = f.format(a.getArriveTime());
      b = b.replaceAll(ARRIVE_TIME, arrive);
      // replace send time
      long send =
          a.getArriveTime().getTime()
              - ((long)
                      DSCalculator.calculateMoveTimeInSeconds(
                          a.getSource(), a.getTarget(), a.getUnit().getSpeed())
                  * 1000);
      b = b.replaceAll(SEND_TIME, f.format(new Date(send)));
      // replace place link
      String placeURL = baseURL + "game.php?village=";
      int uvID = GlobalOptions.getSelectedProfile().getUVId();
      if (uvID >= 0) {
        placeURL = baseURL + "game.php?t=" + uvID + "&village=";
      }
      placeURL +=
          a.getSource().getId() + "&screen=place&mode=command&target=" + a.getTarget().getId();

      placeURL += "&type=0";

      StandardAttack stdAttack = StandardAttackManager.getSingleton().getElementByIcon(a.getType());
      for (UnitHolder u : DataHolder.getSingleton().getUnits()) {
        int amount = 0;
        if (stdAttack != null) {
          amount = stdAttack.getAmountForUnit(u, a.getSource());
        }
        placeURL += "&" + u.getPlainName() + "=" + amount;
      }

      b = b.replaceAll(PLACE, placeURL);
      // </editor-fold>

      result.append(b);
      cnt++;
    }

    // append footer
    result.append(replaceHeadFootVariables(FOOTER, pPlanName, pAttacks));
    try {
      FileWriter w = new FileWriter(pHtmlFile);
      w.write(result.toString());
      w.flush();
      w.close();
    } catch (Exception e) {
      logger.error("Failed writing HTML file", e);
    }
  }