diff --git a/src/com/imraginbro/wurm/mapgen/filegen/FileGeneration.java b/src/com/imraginbro/wurm/mapgen/filegen/FileGeneration.java index 143b1c0..60ec4ec 100644 --- a/src/com/imraginbro/wurm/mapgen/filegen/FileGeneration.java +++ b/src/com/imraginbro/wurm/mapgen/filegen/FileGeneration.java @@ -134,7 +134,7 @@ public void generateGuardTowersFile() throws IOException, SQLException { } public void generateDeedsFile() throws IOException, SQLException { - if (!MapBuilder.propertiesManager.showDeeds || !MapBuilder.dbhandler.checkZonesConnection()) { + if (!MapBuilder.propertiesManager.showDeeds || !MapBuilder.dbhandler.checkZonesConnection() || !MapBuilder.dbhandler.checkItemsConnection() || !MapBuilder.dbhandler.checkPlayersConnection()) { System.out.println("Skipping deeds.js generation."); return; } @@ -142,68 +142,67 @@ public void generateDeedsFile() throws IOException, SQLException { System.out.println("Writing deeds.js file..."); BufferedWriter bw = new BufferedWriter(new FileWriter(MapBuilder.propertiesManager.saveLocation.getAbsolutePath() + separator + "includes" + separator + "deeds.js", false)); - StringBuilder mainDeedString = new StringBuilder(); - StringBuilder deedBordersString = new StringBuilder(); - StringBuilder deedMarkersString = new StringBuilder(); - System.out.println("Loading deeds from wurmzones.db..."); Statement statement = MapBuilder.dbhandler.getZonesConnection().createStatement(); - ResultSet resultSet = statement.executeQuery("SELECT * FROM VILLAGES WHERE DISBANDED=0;"); - + ResultSet resultSet = statement.executeQuery("SELECT ID FROM VILLAGES WHERE DISBANDED=0;"); + + ArrayList villages = new ArrayList(); + + while (resultSet.next()) { + villages.add(new Village(resultSet.getInt("ID"))); + } + + resultSet.close(); + statement.close(); + + StringBuilder mainDeedString = new StringBuilder(); + StringBuilder deedBordersString = new StringBuilder(); + StringBuilder deedMarkersString = new StringBuilder(); + mainDeedString.append("function setViewOnMainDeed(map) {" + newLine); deedBordersString.append("function deedBorders() {" + newLine + "\tvar deedBorders = [];" + newLine); deedMarkersString.append("function deedMarkers() {" + newLine + "\tvar deedMarkers = [];" + newLine); - - double mainX = 0; - double mainY = 0; - + int count = 0; - while (resultSet.next()) { - int sx = resultSet.getInt("STARTX"); - int sy = resultSet.getInt("STARTY"); - int ex = resultSet.getInt("ENDX"); - int ey = resultSet.getInt("ENDY"); - double x = (sx + ex + 1) / 2; - double y = (sy + ey + 1) / 2; + boolean setMainDeed = false; + for (int i = 0; i < villages.size(); i++) { + final Village vi = villages.get(i); deedBordersString.append("\tdeedBorders.push(L.polygon([" - + "xy("+sx+","+sy+")," - + "xy("+(ex+1)+","+sy+")," - + "xy("+(ex+1)+","+(ey+1)+")," - + "xy("+sx+","+(ey+1)+")]"); - - boolean perm = resultSet.getBoolean("PERMANENT"); - - if (perm) { + + "xy("+vi.getStartX()+","+vi.getStartY()+")," + + "xy("+(vi.getEndX()+1)+","+vi.getStartY()+")," + + "xy("+(vi.getEndX()+1)+","+(vi.getEndY()+1)+")," + + "xy("+vi.getStartX()+","+(vi.getEndY()+1)+")]"); + if (vi.isPermanent()) { deedBordersString.append(", {color:'orange',fillOpacity:0,weight:1})"); - if (mainX == 0 && mainY == 0) { - mainX = x; - mainY = y; - mainDeedString.append("\tmap.setView(xy("+mainX+","+mainY+"), config.mapMaxZoom-1)" + newLine); + if (!setMainDeed) { + mainDeedString.append("\tmap.setView(xy("+vi.getTokenX()+","+vi.getTokenY()+"), config.mapMaxZoom-1)" + newLine); + setMainDeed = true; } } else { deedBordersString.append(", {color:'white',fillOpacity:0,weight:1})"); } - deedBordersString.append(".bindPopup(\"" + resultSet.getString("NAME") + "\"));" + newLine); + deedBordersString.append(".bindPopup(\"" + vi.getVillageName() + "\"));" + newLine); - String firstLetter = resultSet.getString("NAME").substring(0, 1).toLowerCase(); + String firstLetter = vi.getVillageName().substring(0, 1).toLowerCase(); deedMarkersString.append("\tdeedMarkers.push(L.marker(" - + "xy("+(x+0.5)+","+(y+0.5)+"),"); - if (perm) { + + "xy("+(vi.getTokenX()+0.5)+","+(vi.getTokenY()+0.5)+"),"); + if (vi.isPermanent()) { deedMarkersString.append("{icon: mainIcon})"); } else { deedMarkersString.append("{icon: letter_"+firstLetter+"Icon})"); } - deedMarkersString.append(".bindPopup(\""+resultSet.getString("NAME")+"\"));" + newLine); + deedMarkersString.append(".bindPopup(\"
"+vi.getVillageName()+"
" + + "" + vi.getMotto() + "

" + + "Mayor: " + vi.getMayorName() + "
" + + "Citizens: " + vi.getCitizenCount() + "\"));" + newLine); + count++; } - resultSet.close(); - statement.close(); - mainDeedString.append("}" + newLine + newLine); deedBordersString.append("\treturn deedBorders;" + newLine + "}" + newLine + newLine); deedMarkersString.append("\treturn deedMarkers;" + newLine + "}"); diff --git a/src/com/imraginbro/wurm/mapgen/filegen/Village.java b/src/com/imraginbro/wurm/mapgen/filegen/Village.java new file mode 100644 index 0000000..65a4fb2 --- /dev/null +++ b/src/com/imraginbro/wurm/mapgen/filegen/Village.java @@ -0,0 +1,246 @@ +package com.imraginbro.wurm.mapgen.filegen; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import com.imraginbro.wurm.mapgen.MapBuilder; + +public class Village { + + private final int villageID; + private String villageName; + private String mayorName; + private String motto; + private int startX; + private int startY; + private int endX; + private int endY; + private boolean permanent; + private long deedID; + private long tokenID; + private int tokenX = 0; + private int tokenY = 0; + private int citizenCount = 0; + + public Village(int villageID) { + this.villageID = villageID; + populateVillage(); + populateTokenLocation(); + populateCitizenCount(); + } + + private void populateVillage() { + Statement statement = null; + ResultSet result = null; + try { + statement = MapBuilder.dbhandler.getZonesConnection().createStatement(); + result = statement.executeQuery("SELECT * FROM VILLAGES WHERE ID='"+villageID+"';"); + if (result.next()) { + setVillageName(result.getString("NAME")); + setMayorName(result.getString("MAYOR")); + setMotto(result.getString("DEVISE")); + setStartX(result.getInt("STARTX")); + setStartY(result.getInt("STARTY")); + setEndX(result.getInt("ENDX")); + setEndY(result.getInt("ENDY")); + setDeedID(result.getLong("DEEDID")); + setTokenID(result.getLong("TOKEN")); + setPermanent(result.getBoolean("PERMANENT")); + } + } catch(SQLException e) { + System.out.println("[ERROR] " + e.getMessage()); + e.printStackTrace(); + } finally { + if (result != null) { + try { + result.close(); + } catch (SQLException e) { } + } + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { } + } + } + } + + private void populateTokenLocation() { + Statement statement = null; + ResultSet result = null; + try { + statement = MapBuilder.dbhandler.getItemsConnection().createStatement(); + result = statement.executeQuery("SELECT POSX, POSY FROM ITEMS WHERE WURMID='"+tokenID+"';"); + if (result.next()) { + setTokenX((int) Math.floor(result.getInt("POSX")/4)); + setTokenY((int) Math.floor(result.getInt("POSY")/4)); + } + } catch(SQLException e) { + System.out.println("[ERROR] " + e.getMessage()); + e.printStackTrace(); + } finally { + if (result != null) { + try { + result.close(); + } catch (SQLException e) { } + } + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { } + } + } + if (getTokenX() < getStartX() || getTokenY() < getStartY() || getTokenX() > getEndX() || getTokenY() > getEndY()) { + setTokenX((getStartX() + getEndX())/2); + setTokenY((getStartY() + getEndY())/2); + } + } + + private void populateCitizenCount() { + Statement statement = null; + Statement statement_players = null; + ResultSet result = null; + ResultSet result_players = null; + try { + statement = MapBuilder.dbhandler.getZonesConnection().createStatement(); + result = statement.executeQuery("SELECT WURMID FROM CITIZENS WHERE VILLAGEID='"+villageID+"';"); + while (result.next()) { + final long tempID = result.getLong("WURMID"); + statement_players = MapBuilder.dbhandler.getPlayersConnection().createStatement(); + result_players = statement_players.executeQuery("Select WURMID FROM PLAYERS WHERE WURMID='"+tempID+"';"); + if (result_players.next()) { + this.citizenCount++; + } + } + } catch(SQLException e) { + System.out.println("[ERROR] " + e.getMessage()); + e.printStackTrace(); + } finally { + if (result != null) { + try { + result.close(); + } catch (SQLException e) { } + } + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { } + } + if (result_players != null) { + try { + result_players.close(); + } catch (SQLException e) { } + } + if (statement_players != null) { + try { + statement_players.close(); + } catch (SQLException e) { } + } + } + if (getTokenX() < getStartX() || getTokenY() < getStartY() || getTokenX() > getEndX() || getTokenY() > getEndY()) { + setTokenX((getStartX() + getEndX())/2); + setTokenY((getStartY() + getEndY())/2); + } + } + + public String getVillageName() { + return villageName; + } + + private void setVillageName(String villageName) { + this.villageName = villageName; + } + + public String getMayorName() { + return mayorName; + } + + private void setMayorName(String mayorName) { + this.mayorName = mayorName; + } + + public String getMotto() { + return motto; + } + + private void setMotto(String motto) { + this.motto = motto; + } + + public int getStartX() { + return startX; + } + + private void setStartX(int startX) { + this.startX = startX; + } + + public int getStartY() { + return startY; + } + + private void setStartY(int startY) { + this.startY = startY; + } + + public int getEndX() { + return endX; + } + + private void setEndX(int endX) { + this.endX = endX; + } + + public int getEndY() { + return endY; + } + + private void setEndY(int endY) { + this.endY = endY; + } + + public boolean isPermanent() { + return permanent; + } + + private void setPermanent(boolean permanent) { + this.permanent = permanent; + } + + public long getDeedID() { + return deedID; + } + + private void setDeedID(long deedID) { + this.deedID = deedID; + } + + public long getTokenID() { + return tokenID; + } + + private void setTokenID(long tokenID) { + this.tokenID = tokenID; + } + + public int getTokenX() { + return tokenX; + } + + private void setTokenX(int tokenX) { + this.tokenX = tokenX; + } + + public int getTokenY() { + return tokenY; + } + + private void setTokenY(int tokenY) { + this.tokenY = tokenY; + } + + public int getCitizenCount() { + return citizenCount; + } + +}