Exemplo n.º 1
0
    public XmlReader.Element readXml(XmlReader reader) {
      return reader.expect(
          "rules",
          new XmlReader.ElementClose() {
            public IXmlSerializable read(XmlReader reader) {

              reader.expect(
                  "battlefieldWidth",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      props.setBattlefieldWidth(Integer.parseInt(value));
                    }
                  });
              reader.expect(
                  "battlefieldHeight",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      props.setBattlefieldHeight(Integer.parseInt(value));
                    }
                  });

              reader.expect(
                  "numRounds",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      props.setNumRounds(Integer.parseInt(value));
                    }
                  });
              reader.expect(
                  "inactivityTime",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      props.setInactivityTime(Integer.parseInt(value));
                    }
                  });
              reader.expect(
                  "gunCoolingRate",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      props.setGunCoolingRate(Double.parseDouble(value));
                    }
                  });

              return BattleRulesWrapper.this;
            }

            public void close() {
              recinfo.battleRules =
                  HiddenAccess.createRules(
                      props.getBattlefieldWidth(),
                      props.getBattlefieldHeight(),
                      props.getNumRounds(),
                      props.getGunCoolingRate(),
                      props.getInactivityTime(),
                      props.getHideEnemyNames(),
                      props.getSentryBorderSize());
            }
          });
    }
Exemplo n.º 2
0
    public XmlReader.Element readXml(XmlReader reader) {
      return reader.expect(
          name,
          new XmlReader.Element() {
            public IXmlSerializable read(XmlReader reader) {
              final IntValue recordInfo = new IntValue(name);

              reader.expect(
                  "value",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      recordInfo.intValue = Integer.parseInt(value);
                    }
                  });
              return recordInfo;
            }
          });
    }
Exemplo n.º 3
0
    public XmlReader.Element readXml(XmlReader reader) {
      return reader.expect(
          "record",
          new XmlReader.Element() {
            public IXmlSerializable read(final XmlReader reader) {

              final XmlReader.Element element = (new BattleRecordInfo()).readXml(reader);

              reader.expect(
                  "recordInfo",
                  new XmlReader.ElementClose() {
                    public IXmlSerializable read(XmlReader reader) {
                      recordInfo = (BattleRecordInfo) element.read(reader);
                      return recordInfo;
                    }

                    public void close() {
                      reader.getContext().put("robots", recordInfo.robotCount);
                    }
                  });

              reader.expect(
                  "turns",
                  new XmlReader.ListElement() {
                    public IXmlSerializable read(XmlReader reader) {
                      // prototype
                      return new TurnSnapshot();
                    }

                    public void add(IXmlSerializable child) {
                      try {
                        me.oos.writeObject(child);
                      } catch (IOException e) {
                        me.lastException = e;
                      }
                    }

                    public void close() {}
                  });

              return me;
            }
          });
    }
Exemplo n.º 4
0
  public void loadRecord(String recordFilename, BattleRecordFormat format) {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    ZipInputStream zis = null;
    ObjectInputStream ois = null;
    InputStream xis = null;

    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    ObjectOutputStream oos = null;

    try {
      createTempFile();
      fis = new FileInputStream(recordFilename);
      bis = new BufferedInputStream(fis, 1024 * 1024);

      if (format == BattleRecordFormat.BINARY) {
        ois = new ObjectInputStream(bis);
      } else if (format == BattleRecordFormat.BINARY_ZIP) {
        zis = new ZipInputStream(bis);
        zis.getNextEntry();
        ois = new ObjectInputStream(zis);
      } else if (format == BattleRecordFormat.XML_ZIP) {
        zis = new ZipInputStream(bis);
        zis.getNextEntry();
        xis = zis;
      } else if (format == BattleRecordFormat.XML) {
        xis = bis;
      }
      if (format == BattleRecordFormat.BINARY || format == BattleRecordFormat.BINARY_ZIP) {
        recordInfo = (BattleRecordInfo) ois.readObject();
        if (recordInfo.turnsInRounds != null) {
          fos = new FileOutputStream(tempFile);
          bos = new BufferedOutputStream(fos, 1024 * 1024);
          oos = new ObjectOutputStream(bos);

          for (int i = 0; i < recordInfo.turnsInRounds.length; i++) {
            for (int j = recordInfo.turnsInRounds[i] - 1; j >= 0; j--) {
              try {
                ITurnSnapshot turn = (ITurnSnapshot) ois.readObject();

                oos.writeObject(turn);
              } catch (ClassNotFoundException e) {
                logError(e);
              }
            }
          }
        }
      } else {
        final RecordRoot root = new RecordRoot();

        fos = new FileOutputStream(tempFile);
        bos = new BufferedOutputStream(fos, 1024 * 1024);
        root.oos = new ObjectOutputStream(bos);
        XmlReader.deserialize(xis, root);
        if (root.lastException != null) {
          logError(root.lastException);
        }
        recordInfo = root.recordInfo;
      }
    } catch (IOException e) {
      logError(e);
      createTempFile();
      recordInfo = null;
    } catch (ClassNotFoundException e) {
      if (e.getMessage().contains("robocode.recording.BattleRecordInfo")) {
        Logger.logError(
            "Sorry, backward compatibility with record from version 1.6 is not provided.");
      } else {
        logError(e);
      }
      createTempFile();
      recordInfo = null;
    } finally {
      FileUtil.cleanupStream(oos);
      FileUtil.cleanupStream(bos);
      FileUtil.cleanupStream(fos);
      FileUtil.cleanupStream(ois);
      FileUtil.cleanupStream(zis);
      FileUtil.cleanupStream(bis);
      FileUtil.cleanupStream(fis);
    }
  }
Exemplo n.º 5
0
  public XmlReader.Element readXml(XmlReader reader) {
    return reader.expect(
        "recordInfo",
        new XmlReader.Element() {
          public IXmlSerializable read(XmlReader reader) {
            final BattleRecordInfo recordInfo = new BattleRecordInfo();

            reader.expect(
                "robotCount",
                new XmlReader.Attribute() {
                  public void read(String value) {
                    recordInfo.robotCount = Integer.parseInt(value);
                  }
                });
            reader.expect(
                "roundsCount",
                new XmlReader.Attribute() {
                  public void read(String value) {
                    recordInfo.roundsCount = Integer.parseInt(value);
                  }
                });

            new BattleRulesWrapper(recordInfo).readXml(reader);

            reader.expect(
                "rounds",
                new XmlReader.ListElement() {
                  final ArrayList<Integer> ints = new ArrayList<Integer>();

                  public IXmlSerializable read(XmlReader reader) {
                    // prototype
                    return new IntValue("turns");
                  }

                  public void add(IXmlSerializable child) {
                    ints.add(((IntValue) child).intValue);
                  }

                  public void close() {
                    recordInfo.turnsInRounds = new Integer[ints.size()];
                    ints.toArray(recordInfo.turnsInRounds);
                  }
                });

            reader.expect(
                "results",
                new XmlReader.ListElement() {

                  public IXmlSerializable read(XmlReader reader) {
                    recordInfo.results = new ArrayList<BattleResults>();
                    // prototype
                    return new BattleResultsWrapper();
                  }

                  public void add(IXmlSerializable child) {
                    recordInfo.results.add((BattleResults) child);
                  }

                  public void close() {}
                });
            return recordInfo;
          }
        });
  }
Exemplo n.º 6
0
    public XmlReader.Element readXml(XmlReader reader) {
      return reader.expect(
          "result",
          new XmlReader.Element() {
            public IXmlSerializable read(XmlReader reader) {
              final BattleResultsWrapper rules = new BattleResultsWrapper();

              reader.expect(
                  "teamLeaderName",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.teamLeaderName = value;
                    }
                  });

              reader.expect(
                  "rank",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.rank = Integer.parseInt(value);
                    }
                  });
              reader.expect(
                  "score",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.score = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "survival",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.survival = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "lastSurvivorBonus",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.lastSurvivorBonus = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "bulletDamage",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.bulletDamage = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "bulletDamageBonus",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.bulletDamageBonus = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "ramDamage",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.ramDamage = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "ramDamageBonus",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.ramDamageBonus = Double.parseDouble(value);
                    }
                  });
              reader.expect(
                  "firsts",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.firsts = Integer.parseInt(value);
                    }
                  });
              reader.expect(
                  "seconds",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.seconds = Integer.parseInt(value);
                    }
                  });
              reader.expect(
                  "thirds",
                  new XmlReader.Attribute() {
                    public void read(String value) {
                      rules.thirds = Integer.parseInt(value);
                    }
                  });

              return rules;
            }
          });
    }