/** * Generates a match from the given match parameters. * * @param params match parameters object * @return generated match object * @throws IOException if map download fails */ public static AutoRefMatch createMatch(AutoRefMatch.MatchParams params) throws IOException { AutoRefMatch m = AutoRefMap.createMatch(params.getMap(), null); Iterator<AutoRefTeam> teamiter = m.getTeams().iterator(); for (AutoRefMatch.MatchParams.TeamInfo teaminfo : params.getTeams()) { if (!teamiter.hasNext()) break; AutoRefTeam team = teamiter.next(); team.setName(teaminfo.getName()); for (String name : teaminfo.getPlayers()) team.addExpectedPlayer(name); } return m; }
// register that we just died public void registerDeath(PlayerDeathEvent e) { // sanity check... if (e.getEntity() != getPlayer()) return; // get the last damage cause, and mark that as the cause of one death AutoRefPlayer.DamageCause dc = AutoRefPlayer.DamageCause.fromDamageEvent(e.getEntity().getLastDamageCause()); deaths.put(dc, 1 + deaths.get(dc)); ++totalDeaths; AutoRefMatch match = getTeam().getMatch(); Location loc = e.getEntity().getLocation(); match.addEvent( new TranscriptEvent( match, TranscriptEvent.EventType.PLAYER_DEATH, e.getDeathMessage(), loc, this, dc.p)); }
/** * Get map info object associated with a zip * * @param zip zip file containing a configuration file * @return map info object if zip contains a map, otherwise null */ public static AutoRefMap getMapInfo(File zip) { // skip non-directories if (zip.isDirectory()) return null; Element worldConfig; try { worldConfig = getConfigFileData(zip); } catch (IOException e) { e.printStackTrace(); return null; } catch (JDOMException e) { e.printStackTrace(); return null; } String mapName = "??", version = "1.0"; Element meta = worldConfig.getChild("meta"); if (meta != null) { mapName = AutoRefMatch.normalizeMapName(meta.getChildText("name")); version = meta.getChildText("version"); } try { return new AutoRefMap(mapName, version, zip); } catch (IOException e) { e.printStackTrace(); return null; } }
/** * Gets maps that are not installed, but may be downloaded. * * @return set of all maps available for download */ public static Set<AutoRefMap> getRemoteMaps() { // check the cache first, we might want to just use the cached value long time = ManagementFactory.getRuntimeMXBean().getUptime() - _cachedRemoteMapsTime; if (_cachedRemoteMaps != null && time < AutoRefMap.REMOTE_MAP_CACHE_LENGTH) return _cachedRemoteMaps; Set<AutoRefMap> maps = Sets.newHashSet(); String repo = AutoRefMatch.getMapRepo(); try { Map<String, String> params = Maps.newHashMap(); params.put("prefix", "maps/"); for (; ; ) { String url = String.format("%s?%s", repo, QueryUtil.prepareParams(params)); Element listing = new SAXBuilder().build(new URL(url)).getRootElement(); assert "ListBucketResult".equals(listing.getName()) : "Unexpected response"; Namespace ns = listing.getNamespace(); String lastkey = null; for (Element entry : listing.getChildren("Contents", ns)) { lastkey = entry.getChildTextTrim("Key", ns); if (!lastkey.endsWith(".zip")) continue; String[] keyparts = lastkey.split("/"); String mapfile = keyparts[keyparts.length - 1]; String mapslug = mapfile.substring(0, mapfile.length() - 4); String slugparts[] = mapslug.split("-v"); if (slugparts.length < 2) { AutoReferee.log("Invalid map filename: " + mapfile, Level.WARNING); AutoReferee.log("Map files should be of the form \"MapName-vX.X.zip\"", Level.WARNING); } else { String etag = entry.getChildTextTrim("ETag", ns); maps.add( new AutoRefMap( slugparts[0], slugparts[1], lastkey, etag.substring(1, etag.length() - 1))); } } // stop looping if the result says that it hasn't been truncated (no more results) if (!Boolean.parseBoolean(listing.getChildText("IsTruncated", ns))) break; // use the last key as a marker for the next pass if (lastkey != null) params.put("marker", lastkey); } } catch (IOException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } _cachedRemoteMapsTime = ManagementFactory.getRuntimeMXBean().getUptime(); return _cachedRemoteMaps = maps; }
/** * Gets map object associated with given map name. * * @param name name of map * @return map object associated with the name */ public static AutoRefMap getMap(String name) { // assume worldName exists if (name == null) return null; name = AutoRefMatch.normalizeMapName(name); // if there is no map library, quit File mapLibrary = AutoRefMap.getMapLibrary(); if (!mapLibrary.exists()) return null; AutoRefMap bmap = null; int ldist = MAX_NAME_DISTANCE; for (AutoRefMap map : getAvailableMaps()) { String mapName = AutoRefMatch.normalizeMapName(map.name); int namedist = StringUtils.getLevenshteinDistance(name, mapName); if (namedist <= ldist) { bmap = map; ldist = namedist; } } // get best match return bmap; }
private void download() throws IOException { // mark the file as downloading this.zip = AutoRefMap.DOWNLOADING; String bparts[] = filename.split("/"), basename = bparts[bparts.length - 1]; File zip = new File(AutoRefMap.getMapLibrary(), basename); FileUtils.copyURLToFile(new URL(AutoRefMatch.getMapRepo() + filename), zip); // if the md5s match, return the zip String md5comp = DigestUtils.md5Hex(new FileInputStream(zip)); if (md5comp.equalsIgnoreCase(md5sum)) { this.zip = zip; return; } // if the md5sum did not match, quit here zip.delete(); throw new IOException("MD5 Mismatch: " + md5comp + " != " + md5sum); }
@Override public void run() { AutoRefMatch match; try { match = AutoRefMap.createMatch(this.map, this.custom); } catch (IOException e) { e.printStackTrace(); return; } MatchLoadEvent event = new MatchLoadEvent(match); AutoReferee.callEvent(event); AutoReferee plugin = AutoReferee.getInstance(); AutoReferee.log( String.format( "%s loaded %s (%s)", sender.getName(), match.getVersionString(), match.getWorld().getName())); sender.sendMessage(ChatColor.DARK_GRAY + match.getVersionString() + " setup!"); if (sender instanceof Player) match.joinMatch((Player) sender); if (sender == Bukkit.getConsoleSender()) plugin.setConsoleWorld(match.getWorld()); }
@Override public void run() { // get remote map directory Map<String, AutoRefMap> remote = Maps.newHashMap(); for (AutoRefMap map : getRemoteMaps()) remote.put(map.name, map); for (File folder : AutoRefMap.getMapLibrary().listFiles()) if (folder.isDirectory()) try { File arxml = new File(folder, AutoReferee.CFG_FILENAME); if (!arxml.exists()) continue; Element arcfg = new SAXBuilder().build(arxml).getRootElement(); Element meta = arcfg.getChild("meta"); if (meta != null) { AutoRefMap rmap = remote.get(AutoRefMatch.normalizeMapName(meta.getChildText("name"))); if (rmap != null && rmap.getZip() != null) { FileUtils.deleteQuietly(folder); AutoReferee.getInstance() .sendMessageSync( sender, String.format( "Updated %s to new format (%s)", rmap.name, rmap.getVersionString())); } } } catch (IOException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } // check for updates on installed maps for (AutoRefMap map : getInstalledMaps()) try { // get the remote version and check if there is an update AutoRefMap rmap; if ((rmap = remote.get(map.name)) != null) { boolean needsUpdate = map.md5sum != null && !map.md5sum.equals(rmap.md5sum); if (force || needsUpdate) { AutoReferee.getInstance() .sendMessageSync( sender, String.format( "UPDATING %s (%s -> %s)...", rmap.name, map.version, rmap.version)); if (rmap.getZip() == null) AutoReferee.getInstance() .sendMessageSync(sender, "Update " + ChatColor.RED + "FAILED"); else { AutoReferee.getInstance() .sendMessageSync( sender, "Update " + ChatColor.GREEN + "SUCCESS: " + ChatColor.RESET + rmap.getVersionString()); FileUtils.deleteQuietly(map.getZip()); } } } } catch (IOException e) { e.printStackTrace(); } }