2011-04-08 15:51:07 +02:00
|
|
|
package org.mcteam.factions;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-26 15:01:48 +01:00
|
|
|
import java.io.*;
|
2011-03-18 17:33:23 +01:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2011-03-26 15:01:48 +01:00
|
|
|
import java.util.logging.Level;
|
2011-03-18 17:33:23 +01:00
|
|
|
import java.util.Map;
|
2011-02-06 13:36:11 +01:00
|
|
|
import java.util.Map.Entry;
|
2011-03-23 12:45:21 +01:00
|
|
|
import java.util.TreeMap;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
import org.bukkit.ChatColor;
|
2011-04-08 15:51:07 +02:00
|
|
|
import org.mcteam.factions.gson.JsonArray;
|
|
|
|
import org.mcteam.factions.gson.JsonElement;
|
|
|
|
import org.mcteam.factions.gson.JsonObject;
|
|
|
|
import org.mcteam.factions.gson.JsonParser;
|
|
|
|
import org.mcteam.factions.gson.reflect.TypeToken;
|
|
|
|
import org.mcteam.factions.util.AsciiCompass;
|
|
|
|
import org.mcteam.factions.util.DiscUtil;
|
|
|
|
import org.mcteam.factions.util.TextUtil;
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
public class Board {
|
2011-03-23 12:45:21 +01:00
|
|
|
private static transient File file = new File(Factions.instance.getDataFolder(), "board.json");
|
|
|
|
private static transient HashMap<FLocation, Integer> flocationIds = new HashMap<FLocation, Integer>();
|
2011-03-18 17:33:23 +01:00
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Get and Set
|
|
|
|
//----------------------------------------------//
|
|
|
|
public static int getIdAt(FLocation flocation) {
|
2011-03-23 12:45:21 +01:00
|
|
|
if ( ! flocationIds.containsKey(flocation)) {
|
2011-03-19 13:00:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:45:21 +01:00
|
|
|
return flocationIds.get(flocation);
|
2011-03-18 17:33:23 +01:00
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static Faction getFactionAt(FLocation flocation) {
|
|
|
|
return Faction.get(getIdAt(flocation));
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static void setIdAt(int id, FLocation flocation) {
|
|
|
|
if (id == 0) {
|
|
|
|
removeAt(flocation);
|
|
|
|
}
|
|
|
|
|
2011-03-23 12:45:21 +01:00
|
|
|
flocationIds.put(flocation, id);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static void setFactionAt(Faction faction, FLocation flocation) {
|
2011-03-22 17:20:21 +01:00
|
|
|
setIdAt(faction.getId(), flocation);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static void removeAt(FLocation flocation) {
|
2011-03-23 12:45:21 +01:00
|
|
|
flocationIds.remove(flocation);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
2011-04-08 16:22:00 +02:00
|
|
|
|
|
|
|
public static void unclaimAll(int factionId) {
|
|
|
|
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Entry<FLocation, Integer> entry = iter.next();
|
|
|
|
if (entry.getValue().equals(factionId)) {
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 13:00:03 +01:00
|
|
|
|
2011-02-12 18:05:05 +01:00
|
|
|
// Is this coord NOT completely surrounded by coords claimed by the same faction?
|
|
|
|
// Simpler: Is there any nearby coord with a faction other than the faction here?
|
2011-03-19 13:00:03 +01:00
|
|
|
public static boolean isBorderLocation(FLocation flocation) {
|
|
|
|
Faction faction = getFactionAt(flocation);
|
|
|
|
FLocation a = flocation.getRelative(1, 0);
|
|
|
|
FLocation b = flocation.getRelative(-1, 0);
|
|
|
|
FLocation c = flocation.getRelative(0, 1);
|
|
|
|
FLocation d = flocation.getRelative(0, -1);
|
|
|
|
return faction != getFactionAt(a) || faction != getFactionAt(b) || faction != getFactionAt(c) || faction != getFactionAt(d);
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
2011-06-21 04:15:41 +02:00
|
|
|
|
|
|
|
// Is this coord connected to any coord claimed by the specified faction?
|
|
|
|
public static boolean isConnectedLocation(FLocation flocation, Faction faction) {
|
|
|
|
FLocation a = flocation.getRelative(1, 0);
|
|
|
|
FLocation b = flocation.getRelative(-1, 0);
|
|
|
|
FLocation c = flocation.getRelative(0, 1);
|
|
|
|
FLocation d = flocation.getRelative(0, -1);
|
|
|
|
return faction == getFactionAt(a) || faction == getFactionAt(b) || faction == getFactionAt(c) || faction == getFactionAt(d);
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
//----------------------------------------------//
|
2011-03-19 13:00:03 +01:00
|
|
|
// Cleaner. Remove orphaned foreign keys
|
2011-02-13 11:18:08 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static void clean() {
|
2011-03-23 12:45:21 +01:00
|
|
|
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Entry<FLocation, Integer> entry = iter.next();
|
|
|
|
if ( ! Faction.exists(entry.getValue())) {
|
|
|
|
Factions.log("Board cleaner removed "+entry.getValue()+" from "+entry.getKey());
|
|
|
|
iter.remove();
|
2011-02-13 15:18:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 11:18:08 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Coord count
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static int getFactionCoordCount(int factionId) {
|
2011-02-06 13:36:11 +01:00
|
|
|
int ret = 0;
|
2011-03-23 12:45:21 +01:00
|
|
|
for (int thatFactionId : flocationIds.values()) {
|
|
|
|
if(thatFactionId == factionId) {
|
|
|
|
ret += 1;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-13 11:18:08 +01:00
|
|
|
|
2011-03-19 13:00:03 +01:00
|
|
|
public static int getFactionCoordCount(Faction faction) {
|
2011-03-22 17:20:21 +01:00
|
|
|
return getFactionCoordCount(faction.getId());
|
2011-02-13 11:18:08 +01:00
|
|
|
}
|
|
|
|
|
2011-06-30 13:13:47 +02:00
|
|
|
public static int getFactionCoordCountInWorld(Faction faction, String worldName) {
|
|
|
|
int factionId = faction.getId();
|
|
|
|
int ret = 0;
|
|
|
|
Iterator<Entry<FLocation, Integer>> iter = flocationIds.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Entry<FLocation, Integer> entry = iter.next();
|
|
|
|
if (entry.getValue() == factionId && entry.getKey().getWorldName().equals(worldName)) {
|
|
|
|
ret += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Map generation
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The map is relative to a coord and a faction
|
|
|
|
* north is in the direction of decreasing x
|
|
|
|
* east is in the direction of decreasing z
|
|
|
|
*/
|
2011-03-22 15:45:41 +01:00
|
|
|
public static ArrayList<String> getMap(Faction faction, FLocation flocation, double inDegrees) {
|
2011-02-06 13:36:11 +01:00
|
|
|
ArrayList<String> ret = new ArrayList<String>();
|
2011-03-22 19:25:11 +01:00
|
|
|
ret.add(TextUtil.titleize("("+flocation.getCoordString()+") "+getFactionAt(flocation).getTag(faction)));
|
2011-02-06 13:36:11 +01:00
|
|
|
|
|
|
|
int halfWidth = Conf.mapWidth / 2;
|
|
|
|
int halfHeight = Conf.mapHeight / 2;
|
2011-03-19 13:00:03 +01:00
|
|
|
FLocation topLeft = flocation.getRelative(-halfHeight, halfWidth);
|
2011-02-06 13:36:11 +01:00
|
|
|
int width = halfWidth * 2 + 1;
|
|
|
|
int height = halfHeight * 2 + 1;
|
|
|
|
|
2011-05-29 23:28:29 +02:00
|
|
|
if (Conf.showMapFactionKey) {
|
|
|
|
height--;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Character> fList = new HashMap<String, Character>();
|
|
|
|
int chrIdx = 0;
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
// For each row
|
|
|
|
for (int dx = 0; dx < height; dx++) {
|
|
|
|
// Draw and add that row
|
|
|
|
String row = "";
|
|
|
|
for (int dz = 0; dz > -width; dz--) {
|
|
|
|
if(dz == -(halfWidth) && dx == halfHeight) {
|
|
|
|
row += ChatColor.AQUA+"+";
|
|
|
|
} else {
|
2011-03-19 13:00:03 +01:00
|
|
|
FLocation flocationHere = topLeft.getRelative(dx, dz);
|
|
|
|
Faction factionHere = getFactionAt(flocationHere);
|
2011-03-23 17:39:56 +01:00
|
|
|
if (factionHere.isNone()) {
|
2011-02-06 13:36:11 +01:00
|
|
|
row += ChatColor.GRAY+"-";
|
2011-03-23 17:39:56 +01:00
|
|
|
} else if (factionHere.isSafeZone()) {
|
|
|
|
row += ChatColor.GOLD+"+";
|
2011-05-29 23:28:29 +02:00
|
|
|
} else if (factionHere.isWarZone()) {
|
|
|
|
row += ChatColor.DARK_RED+"+";
|
2011-02-06 13:36:11 +01:00
|
|
|
} else {
|
2011-05-29 23:28:29 +02:00
|
|
|
if (!fList.containsKey(factionHere.getTag()))
|
|
|
|
fList.put(factionHere.getTag(), Conf.mapKeyChrs[chrIdx++]);
|
|
|
|
char tag = fList.get(factionHere.getTag());
|
|
|
|
row += factionHere.getRelation(faction).getColor() + "" + tag;
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.add(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the compass
|
|
|
|
ArrayList<String> asciiCompass = AsciiCompass.getAsciiCompass(inDegrees, ChatColor.RED, Conf.colorChrome);
|
2011-02-07 21:42:14 +01:00
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
// Add the compass
|
|
|
|
ret.set(1, asciiCompass.get(0)+ret.get(1).substring(3*3));
|
|
|
|
ret.set(2, asciiCompass.get(1)+ret.get(2).substring(3*3));
|
|
|
|
ret.set(3, asciiCompass.get(2)+ret.get(3).substring(3*3));
|
|
|
|
|
2011-05-29 23:28:29 +02:00
|
|
|
// Add the faction key
|
|
|
|
if (Conf.showMapFactionKey) {
|
|
|
|
String fRow = "";
|
|
|
|
for(String key : fList.keySet()) {
|
|
|
|
fRow += String.format("%s%s: %s ", ChatColor.GRAY, fList.get(key), key);
|
|
|
|
}
|
|
|
|
ret.add(fRow);
|
|
|
|
}
|
|
|
|
|
2011-02-06 13:36:11 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// Persistance
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-03-23 12:45:21 +01:00
|
|
|
public static Map<String,Map<String,Integer>> dumpAsSaveFormat() {
|
|
|
|
Map<String,Map<String,Integer>> worldCoordIds = new HashMap<String,Map<String,Integer>>();
|
|
|
|
|
|
|
|
for (Entry<FLocation, Integer> entry : flocationIds.entrySet()) {
|
|
|
|
String worldName = entry.getKey().getWorldName();
|
|
|
|
String coords = entry.getKey().getCoordString();
|
|
|
|
Integer id = entry.getValue();
|
|
|
|
if ( ! worldCoordIds.containsKey(worldName)) {
|
|
|
|
worldCoordIds.put(worldName, new TreeMap<String,Integer>());
|
|
|
|
}
|
|
|
|
|
|
|
|
worldCoordIds.get(worldName).put(coords, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return worldCoordIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void loadFromSaveFormat(Map<String,Map<String,Integer>> worldCoordIds) {
|
|
|
|
flocationIds.clear();
|
|
|
|
|
|
|
|
for (Entry<String,Map<String,Integer>> entry : worldCoordIds.entrySet()) {
|
|
|
|
String worldName = entry.getKey();
|
|
|
|
for (Entry<String,Integer> entry2 : entry.getValue().entrySet()) {
|
|
|
|
String[] coords = entry2.getKey().trim().split("[,\\s]+");
|
|
|
|
int x = Integer.parseInt(coords[0]);
|
|
|
|
int z = Integer.parseInt(coords[1]);
|
|
|
|
int factionId = entry2.getValue();
|
|
|
|
flocationIds.put(new FLocation(worldName, x, z), factionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
public static boolean save() {
|
2011-03-22 22:31:04 +01:00
|
|
|
//Factions.log("Saving board to disk");
|
2011-03-18 17:33:23 +01:00
|
|
|
|
|
|
|
try {
|
2011-03-23 12:45:21 +01:00
|
|
|
DiscUtil.write(file, Factions.gson.toJson(dumpAsSaveFormat()));
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception e) {
|
2011-03-18 17:33:23 +01:00
|
|
|
e.printStackTrace();
|
2011-04-06 11:08:08 +02:00
|
|
|
Factions.log("Failed to save the board to disk.");
|
2011-03-18 17:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
2011-03-23 12:45:21 +01:00
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean load() {
|
2011-03-23 12:00:38 +01:00
|
|
|
Factions.log("Loading board from disk");
|
|
|
|
|
2011-03-18 17:33:23 +01:00
|
|
|
if ( ! file.exists()) {
|
2011-03-26 15:01:48 +01:00
|
|
|
if ( ! loadOld())
|
|
|
|
Factions.log("No board to load from disk. Creating new file.");
|
2011-03-18 17:33:23 +01:00
|
|
|
save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2011-03-19 13:00:03 +01:00
|
|
|
Type type = new TypeToken<Map<String,Map<String,Integer>>>(){}.getType();
|
2011-03-23 12:45:21 +01:00
|
|
|
Map<String,Map<String,Integer>> worldCoordIds = Factions.gson.fromJson(DiscUtil.read(file), type);
|
|
|
|
loadFromSaveFormat(worldCoordIds);
|
2011-04-06 11:08:08 +02:00
|
|
|
} catch (Exception e) {
|
2011-03-18 17:33:23 +01:00
|
|
|
e.printStackTrace();
|
2011-04-06 11:08:08 +02:00
|
|
|
Factions.log("Failed to load the board from disk.");
|
2011-03-18 17:33:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-26 15:01:48 +01:00
|
|
|
|
|
|
|
private static boolean loadOld() {
|
|
|
|
File folderBoard = new File(Factions.instance.getDataFolder(), "board");
|
|
|
|
|
|
|
|
if ( ! folderBoard.isDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Factions.log("Board file doesn't exist, attempting to load old pre-1.1 data.");
|
|
|
|
|
|
|
|
String ext = ".json";
|
|
|
|
|
|
|
|
class jsonFileFilter implements FileFilter {
|
|
|
|
@Override
|
|
|
|
public boolean accept(File file) {
|
|
|
|
return (file.getName().toLowerCase().endsWith(".json") && file.isFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
File[] jsonFiles = folderBoard.listFiles(new jsonFileFilter());
|
|
|
|
for (File jsonFile : jsonFiles) {
|
|
|
|
// Extract the name from the filename. The name is filename minus ".json"
|
|
|
|
String name = jsonFile.getName();
|
|
|
|
name = name.substring(0, name.length() - ext.length());
|
|
|
|
try {
|
|
|
|
JsonParser parser = new JsonParser();
|
|
|
|
JsonObject json = (JsonObject) parser.parse(DiscUtil.read(jsonFile));
|
|
|
|
JsonArray coords = json.getAsJsonArray("coordFactionIds");
|
2011-03-31 15:13:02 +02:00
|
|
|
Iterator<JsonElement> coordSet = coords.iterator();
|
2011-03-26 15:01:48 +01:00
|
|
|
while(coordSet.hasNext()) {
|
|
|
|
JsonArray coordDat = (JsonArray) coordSet.next();
|
|
|
|
JsonObject coord = coordDat.get(0).getAsJsonObject();
|
|
|
|
int coordX = coord.get("x").getAsInt();
|
|
|
|
int coordZ = coord.get("z").getAsInt();
|
|
|
|
int factionId = coordDat.get(1).getAsInt();
|
|
|
|
flocationIds.put(new FLocation(name, coordX, coordZ), factionId);
|
|
|
|
}
|
|
|
|
Factions.log("loaded pre-1.1 board "+name);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
Factions.log(Level.WARNING, "failed to load board "+name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-06 13:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|