Skip to content

Commit

Permalink
Additional Map Info for Villages
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett92 committed Aug 12, 2017
1 parent 03178a8 commit 1704de0
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 38 deletions.
75 changes: 37 additions & 38 deletions src/com/imraginbro/wurm/mapgen/filegen/FileGeneration.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,76 +134,75 @@ 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;
}

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<Village> villages = new ArrayList<Village>();

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(\"<div align='center'><b>"+vi.getVillageName()+"</b><br>"
+ "<i>" + vi.getMotto() + "</i></div><br>"
+ "<b>Mayor:</b> " + vi.getMayorName() + "<br>"
+ "<b>Citizens:</b> " + vi.getCitizenCount() + "\"));" + newLine);

count++;
}

resultSet.close();
statement.close();

mainDeedString.append("}" + newLine + newLine);
deedBordersString.append("\treturn deedBorders;" + newLine + "}" + newLine + newLine);
deedMarkersString.append("\treturn deedMarkers;" + newLine + "}");
Expand Down
246 changes: 246 additions & 0 deletions src/com/imraginbro/wurm/mapgen/filegen/Village.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 1704de0

Please sign in to comment.