Make both Board and BoardColl implement the same BoardInterface.

This commit is contained in:
Olof Larsson 2013-04-11 09:25:26 +02:00
parent b06d5f5e55
commit 1ebb6a3f69
3 changed files with 195 additions and 21 deletions

View File

@ -15,7 +15,7 @@ import com.massivecraft.mcore.ps.PS;
import com.massivecraft.mcore.store.Entity; import com.massivecraft.mcore.store.Entity;
import com.massivecraft.mcore.util.Txt; import com.massivecraft.mcore.util.Txt;
public class Board extends Entity<Board, String> public class Board extends Entity<Board, String> implements BoardInterface
{ {
// -------------------------------------------- // // -------------------------------------------- //
// META // META
@ -27,7 +27,7 @@ public class Board extends Entity<Board, String>
} }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // OVERRIDE: ENTITY
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
@ -54,26 +54,31 @@ public class Board extends Entity<Board, String>
private ConcurrentSkipListMap<PS, TerritoryAccess> map = new ConcurrentSkipListMap<PS, TerritoryAccess>(); private ConcurrentSkipListMap<PS, TerritoryAccess> map = new ConcurrentSkipListMap<PS, TerritoryAccess>();
// -------------------------------------------- // // -------------------------------------------- //
// GET // OVERRIDE: BOARD
// -------------------------------------------- // // -------------------------------------------- //
// GET
@Override
public TerritoryAccess getTerritoryAccessAt(PS ps) public TerritoryAccess getTerritoryAccessAt(PS ps)
{ {
if (ps == null) return null;
ps = ps.getChunkCoords(true); ps = ps.getChunkCoords(true);
TerritoryAccess ret = this.map.get(ps); TerritoryAccess ret = this.map.get(ps);
if (ret == null) ret = new TerritoryAccess("0"); if (ret == null) ret = new TerritoryAccess("0");
return ret; return ret;
} }
@Override
public Faction getFactionAt(PS ps) public Faction getFactionAt(PS ps)
{ {
if (ps == null) return null;
return this.getTerritoryAccessAt(ps).getHostFaction(); return this.getTerritoryAccessAt(ps).getHostFaction();
} }
// -------------------------------------------- //
// SET // SET
// -------------------------------------------- //
@Override
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess) public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
{ {
ps = ps.getChunkCoords(true); ps = ps.getChunkCoords(true);
@ -97,6 +102,7 @@ public class Board extends Entity<Board, String>
this.changed(); this.changed();
} }
@Override
public void setFactionAt(PS ps, Faction faction) public void setFactionAt(PS ps, Faction faction)
{ {
TerritoryAccess territoryAccess = null; TerritoryAccess territoryAccess = null;
@ -107,17 +113,19 @@ public class Board extends Entity<Board, String>
this.setTerritoryAccessAt(ps, territoryAccess); this.setTerritoryAccessAt(ps, territoryAccess);
} }
// -------------------------------------------- //
// REMOVE // REMOVE
// -------------------------------------------- //
@Override
public void removeAt(PS ps) public void removeAt(PS ps)
{ {
this.setTerritoryAccessAt(ps, null); this.setTerritoryAccessAt(ps, null);
} }
public void removeAll(String factionId) @Override
public void removeAll(Faction faction)
{ {
String factionId = faction.getId();
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet()) for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
{ {
TerritoryAccess territoryAccess = entry.getValue(); TerritoryAccess territoryAccess = entry.getValue();
@ -129,6 +137,7 @@ public class Board extends Entity<Board, String>
} }
// Removes orphaned foreign keys // Removes orphaned foreign keys
@Override
public void clean() public void clean()
{ {
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet()) for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
@ -143,9 +152,13 @@ public class Board extends Entity<Board, String>
} }
} }
// -------------------------------------------- //
// COUNT // COUNT
// -------------------------------------------- //
@Override
public int getCount(Faction faction)
{
return this.getCount(faction.getId());
}
public int getCount(String factionId) public int getCount(String factionId)
{ {
@ -157,17 +170,11 @@ public class Board extends Entity<Board, String>
return ret; return ret;
} }
public int getCount(Faction faction)
{
return this.getCount(faction.getId());
}
// -------------------------------------------- //
// NEARBY DETECTION // NEARBY DETECTION
// -------------------------------------------- //
// Is this coord NOT completely surrounded by coords claimed by the same faction? // 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? // Simpler: Is there any nearby coord with a faction other than the faction here?
@Override
public boolean isBorderPs(PS ps) public boolean isBorderPs(PS ps)
{ {
PS nearby = null; PS nearby = null;
@ -189,6 +196,7 @@ public class Board extends Entity<Board, String>
} }
// Is this coord connected to any coord claimed by the specified faction? // Is this coord connected to any coord claimed by the specified faction?
@Override
public boolean isConnectedPs(PS ps, Faction faction) public boolean isConnectedPs(PS ps, Faction faction)
{ {
PS nearby = null; PS nearby = null;
@ -208,10 +216,9 @@ public class Board extends Entity<Board, String>
return false; return false;
} }
// -------------------------------------------- //
// MAP GENERATION // MAP GENERATION
// -------------------------------------------- //
@Override
public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees) public ArrayList<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees)
{ {
centerPs = centerPs.getChunkCoords(true); centerPs = centerPs.getChunkCoords(true);

View File

@ -1,9 +1,13 @@
package com.massivecraft.factions; 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.Coll;
import com.massivecraft.mcore.store.MStore; import com.massivecraft.mcore.store.MStore;
import com.massivecraft.mcore.util.MUtil;
public class BoardColl extends Coll<Board, String> public class BoardColl extends Coll<Board, String> implements BoardInterface
{ {
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
@ -16,4 +20,133 @@ public class BoardColl extends Coll<Board, String>
super(MStore.getDb(ConfServer.dburi), Factions.get(), "ai", Const.COLLECTION_BASENAME_BOARD, Board.class, String.class, true); 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<String> 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);
}
} }

View File

@ -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<String> getMap(RelationParticipator observer, PS centerPs, double inDegrees);
}