From 1ebb6a3f6997c7d8cc02098deeb1208bd37f16b5 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 11 Apr 2013 09:25:26 +0200 Subject: [PATCH] Make both Board and BoardColl implement the same BoardInterface. --- src/com/massivecraft/factions/Board.java | 47 +++--- src/com/massivecraft/factions/BoardColl.java | 135 +++++++++++++++++- .../massivecraft/factions/BoardInterface.java | 34 +++++ 3 files changed, 195 insertions(+), 21 deletions(-) create mode 100644 src/com/massivecraft/factions/BoardInterface.java diff --git a/src/com/massivecraft/factions/Board.java b/src/com/massivecraft/factions/Board.java index 6f07e793..10f232ae 100644 --- a/src/com/massivecraft/factions/Board.java +++ b/src/com/massivecraft/factions/Board.java @@ -15,7 +15,7 @@ import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.util.Txt; -public class Board extends Entity +public class Board extends Entity implements BoardInterface { // -------------------------------------------- // // META @@ -27,7 +27,7 @@ public class Board extends Entity } // -------------------------------------------- // - // OVERRIDE + // OVERRIDE: ENTITY // -------------------------------------------- // @Override @@ -54,26 +54,31 @@ public class Board extends Entity private ConcurrentSkipListMap map = new ConcurrentSkipListMap(); // -------------------------------------------- // - // GET + // OVERRIDE: BOARD // -------------------------------------------- // + // GET + + @Override public TerritoryAccess getTerritoryAccessAt(PS ps) { + if (ps == null) return null; ps = ps.getChunkCoords(true); TerritoryAccess ret = this.map.get(ps); if (ret == null) ret = new TerritoryAccess("0"); return ret; } + @Override public Faction getFactionAt(PS ps) { + if (ps == null) return null; return this.getTerritoryAccessAt(ps).getHostFaction(); } - // -------------------------------------------- // // SET - // -------------------------------------------- // + @Override public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess) { ps = ps.getChunkCoords(true); @@ -97,6 +102,7 @@ public class Board extends Entity this.changed(); } + @Override public void setFactionAt(PS ps, Faction faction) { TerritoryAccess territoryAccess = null; @@ -107,17 +113,19 @@ public class Board extends Entity this.setTerritoryAccessAt(ps, territoryAccess); } - // -------------------------------------------- // // REMOVE - // -------------------------------------------- // + @Override public void removeAt(PS ps) { this.setTerritoryAccessAt(ps, null); } - public void removeAll(String factionId) + @Override + public void removeAll(Faction faction) { + String factionId = faction.getId(); + for (Entry entry : this.map.entrySet()) { TerritoryAccess territoryAccess = entry.getValue(); @@ -129,6 +137,7 @@ public class Board extends Entity } // Removes orphaned foreign keys + @Override public void clean() { for (Entry entry : this.map.entrySet()) @@ -143,9 +152,13 @@ public class Board extends Entity } } - // -------------------------------------------- // // COUNT - // -------------------------------------------- // + + @Override + public int getCount(Faction faction) + { + return this.getCount(faction.getId()); + } public int getCount(String factionId) { @@ -157,17 +170,11 @@ public class Board extends Entity return ret; } - public int getCount(Faction faction) - { - return this.getCount(faction.getId()); - } - - // -------------------------------------------- // // NEARBY DETECTION - // -------------------------------------------- // - + // 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? + @Override public boolean isBorderPs(PS ps) { PS nearby = null; @@ -189,6 +196,7 @@ public class Board extends Entity } // Is this coord connected to any coord claimed by the specified faction? + @Override public boolean isConnectedPs(PS ps, Faction faction) { PS nearby = null; @@ -208,10 +216,9 @@ public class Board extends Entity return false; } - // -------------------------------------------- // // MAP GENERATION - // -------------------------------------------- // + @Override public ArrayList getMap(RelationParticipator observer, PS centerPs, double inDegrees) { centerPs = centerPs.getChunkCoords(true); diff --git a/src/com/massivecraft/factions/BoardColl.java b/src/com/massivecraft/factions/BoardColl.java index d212bf0a..35f53147 100644 --- a/src/com/massivecraft/factions/BoardColl.java +++ b/src/com/massivecraft/factions/BoardColl.java @@ -1,9 +1,13 @@ package com.massivecraft.factions; +import java.util.ArrayList; +import com.massivecraft.factions.iface.RelationParticipator; +import com.massivecraft.mcore.ps.PS; import com.massivecraft.mcore.store.Coll; import com.massivecraft.mcore.store.MStore; +import com.massivecraft.mcore.util.MUtil; -public class BoardColl extends Coll +public class BoardColl extends Coll implements BoardInterface { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -16,4 +20,133 @@ public class BoardColl extends Coll super(MStore.getDb(ConfServer.dburi), Factions.get(), "ai", Const.COLLECTION_BASENAME_BOARD, Board.class, String.class, true); } + // -------------------------------------------- // + // OVERRIDE: COLL + // -------------------------------------------- // + + @Override + public String fixId(Object oid) + { + if (oid == null) return null; + if (oid instanceof String) return (String)oid; + if (oid instanceof Board) return this.getId(oid); + + return MUtil.extract(String.class, "worldName", oid); + } + + // -------------------------------------------- // + // OVERRIDE: BOARD + // -------------------------------------------- // + + @Override + public TerritoryAccess getTerritoryAccessAt(PS ps) + { + if (ps == null) return null; + Board board = this.get(ps.getWorld()); + if (board == null) return null; + return board.getTerritoryAccessAt(ps); + } + + @Override + public Faction getFactionAt(PS ps) + { + if (ps == null) return null; + Board board = this.get(ps.getWorld()); + if (board == null) return null; + return board.getFactionAt(ps); + } + + // SET + + @Override + public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess) + { + if (ps == null) return; + Board board = this.get(ps.getWorld()); + if (board == null) return; + board.setTerritoryAccessAt(ps, territoryAccess); + } + + @Override + public void setFactionAt(PS ps, Faction faction) + { + if (ps == null) return; + Board board = this.get(ps.getWorld()); + if (board == null) return; + board.setFactionAt(ps, faction); + } + + // REMOVE + + @Override + public void removeAt(PS ps) + { + if (ps == null) return; + Board board = this.get(ps.getWorld()); + if (board == null) return; + board.removeAt(ps); + } + + @Override + public void removeAll(Faction faction) + { + for (Board board : this.getAll()) + { + board.removeAll(faction); + } + } + + @Override + public void clean() + { + for (Board board : this.getAll()) + { + board.clean(); + } + } + + // COUNT + + @Override + public int getCount(Faction faction) + { + int ret = 0; + for (Board board : this.getAll()) + { + ret += board.getCount(faction); + } + return ret; + } + + // NEARBY DETECTION + + @Override + public boolean isBorderPs(PS ps) + { + if (ps == null) return false; + Board board = this.get(ps.getWorld()); + if (board == null) return false; + return board.isBorderPs(ps); + } + + @Override + public boolean isConnectedPs(PS ps, Faction faction) + { + if (ps == null) return false; + Board board = this.get(ps.getWorld()); + if (board == null) return false; + return board.isConnectedPs(ps, faction); + } + + // MAP GENERATION + + @Override + public ArrayList getMap(RelationParticipator observer, PS centerPs, double inDegrees) + { + if (centerPs == null) return null; + Board board = this.get(centerPs.getWorld()); + if (board == null) return null; + return board.getMap(observer, centerPs, inDegrees); + } + } diff --git a/src/com/massivecraft/factions/BoardInterface.java b/src/com/massivecraft/factions/BoardInterface.java new file mode 100644 index 00000000..ebcf8986 --- /dev/null +++ b/src/com/massivecraft/factions/BoardInterface.java @@ -0,0 +1,34 @@ +package com.massivecraft.factions; + +import java.util.ArrayList; + +import com.massivecraft.factions.iface.RelationParticipator; +import com.massivecraft.mcore.ps.PS; + +public interface BoardInterface +{ + // GET + public TerritoryAccess getTerritoryAccessAt(PS ps); + public Faction getFactionAt(PS ps); + + // SET + public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess); + public void setFactionAt(PS ps, Faction faction); + + // REMOVE + public void removeAt(PS ps); + public void removeAll(Faction faction); + public void clean(); + + // COUNT + public int getCount(Faction faction); + + // NEARBY DETECTION + public boolean isBorderPs(PS ps); + public boolean isConnectedPs(PS ps, Faction faction); + + // MAP + // TODO: Could the degrees be embedded in centerPs yaw instead? + public ArrayList getMap(RelationParticipator observer, PS centerPs, double inDegrees); + +}