Refactor boards

This commit is contained in:
Magnus Ulf 2019-03-03 10:16:41 +01:00
parent b936923bb1
commit de9c4e273a
5 changed files with 116 additions and 194 deletions

View File

@ -96,7 +96,7 @@ public class CmdFactionsWarpGo extends FactionsCommandWarp
{ {
if (MUtil.isntPlayer(p)) continue; if (MUtil.isntPlayer(p)) continue;
if (p == null || !p.isOnline() || p.isDead() || p == me || p.getWorld() != w) continue; if (!p.isOnline() || p.isDead() || p == me || p.getWorld() != w) continue;
MPlayer fp = MPlayer.get(p); MPlayer fp = MPlayer.get(p);
if (msender.getRelationTo(fp) != Rel.ENEMY) continue; if (msender.getRelationTo(fp) != Rel.ENEMY) continue;

View File

@ -3,18 +3,17 @@ package com.massivecraft.factions.entity;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.massivecraft.factions.Factions; import com.massivecraft.factions.Factions;
import com.massivecraft.factions.TerritoryAccess; import com.massivecraft.factions.TerritoryAccess;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.store.Entity;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Board extends Entity<Board> implements BoardInterface public class Board extends Entity<Board> implements BoardInterface
{ {
@ -82,7 +81,8 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public TerritoryAccess getTerritoryAccessAt(PS ps) public TerritoryAccess getTerritoryAccessAt(PS ps)
{ {
if (ps == null) return null; if (ps == null) throw new NullPointerException("ps");
ps = ps.getChunkCoords(true); ps = ps.getChunkCoords(true);
TerritoryAccess ret = this.map.get(ps); TerritoryAccess ret = this.map.get(ps);
if (ret == null || ret.getHostFaction() == null) ret = TerritoryAccess.valueOf(Factions.ID_NONE); if (ret == null || ret.getHostFaction() == null) ret = TerritoryAccess.valueOf(Factions.ID_NONE);
@ -92,9 +92,7 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public Faction getFactionAt(PS ps) public Faction getFactionAt(PS ps)
{ {
if (ps == null) return null; return this.getTerritoryAccessAt(ps).getHostFaction();
TerritoryAccess ta = this.getTerritoryAccessAt(ps);
return ta.getHostFaction();
} }
// SET // SET
@ -138,16 +136,7 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public void removeAll(Faction faction) public void removeAll(Faction faction)
{ {
String factionId = faction.getId(); this.getChunks(faction).forEach(this::removeAt);
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
{
TerritoryAccess territoryAccess = entry.getValue();
if ( ! territoryAccess.getHostFactionId().equals(factionId)) continue;
PS ps = entry.getKey();
this.removeAt(ps);
}
} }
// CHUNKS // CHUNKS
@ -161,46 +150,36 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public Set<PS> getChunks(String factionId) public Set<PS> getChunks(String factionId)
{ {
Set<PS> ret = new HashSet<>(); return this.map.entrySet().stream()
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet()) .filter(e -> e.getValue().getHostFactionId().equals(factionId))
{ .map(Entry::getKey)
TerritoryAccess ta = entry.getValue(); .map(ps -> ps.withWorld(this.getId()))
if (!ta.getHostFactionId().equals(factionId)) continue; .collect(Collectors.toSet());
PS ps = entry.getKey();
ps = ps.withWorld(this.getId());
ret.add(ps);
}
return ret;
} }
@Override @Override
@Deprecated
public Map<Faction, Set<PS>> getFactionToChunks() public Map<Faction, Set<PS>> getFactionToChunks()
{ {
Map<Faction, Set<PS>> ret = new MassiveMap<>(); return this.getFactionToChunks(true);
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet())
{
// Get Faction
TerritoryAccess ta = entry.getValue();
Faction faction = ta.getHostFaction();
if (faction == null) continue;
// Get Chunks
Set<PS> chunks = ret.get(faction);
if (chunks == null)
{
chunks = new MassiveSet<>();
ret.put(faction, chunks);
} }
// Add Chunk @Override
PS chunk = entry.getKey(); public Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld)
chunk = chunk.withWorld(this.getId()); {
chunks.add(chunk); Function<Entry<PS, TerritoryAccess>, PS> mapper = Entry::getKey;
if (withWorld) mapper = mapper.andThen(ps -> ps.withWorld(this.getId()));
return map.entrySet().stream().collect(Collectors.groupingBy(
entry -> entry.getValue().getHostFaction(), // This specifies how to get the key
Collectors.mapping(mapper, Collectors.toSet()) // This maps the entries and puts them in the collection
));
} }
return ret; @Override
public Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld)
{
return Collections.singletonMap(this.getId(), this.getFactionToChunks(withWorld));
} }
// COUNT // COUNT
@ -208,45 +187,30 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public int getCount(Faction faction) public int getCount(Faction faction)
{ {
if (faction == null) throw new NullPointerException("faction");
return this.getCount(faction.getId()); return this.getCount(faction.getId());
} }
@Override @Override
public int getCount(String factionId) public int getCount(String factionId)
{ {
int ret = 0; if (factionId == null) throw new NullPointerException("factionId");
for (TerritoryAccess ta : this.map.values())
{ return (int) this.map.values().stream()
if (!ta.getHostFactionId().equals(factionId)) continue; .map(TerritoryAccess::getHostFactionId)
ret += 1; .filter(factionId::equals)
} .count();
return ret;
} }
@Override @Override
public Map<Faction, Integer> getFactionToCount() public Map<Faction, Long> getFactionToCount()
{ {
Map<Faction, Integer> ret = new MassiveMap<>(); return this.map.entrySet().stream()
.collect(Collectors.groupingBy(
for (Entry<PS, TerritoryAccess> entry : this.map.entrySet()) e -> e.getValue().getHostFaction(),
{ Collectors.counting()
// Get Faction ));
TerritoryAccess ta = entry.getValue();
Faction faction = ta.getHostFaction();
if (faction == null) continue;
// Get Count
Integer count = ret.get(faction);
if (count == null)
{
count = 0;
}
// Add Chunk
ret.put(faction, count + 1);
}
return ret;
} }
// CLAIMED // CLAIMED
@ -260,12 +224,9 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public boolean hasClaimed(String factionId) public boolean hasClaimed(String factionId)
{ {
for (TerritoryAccess ta : this.map.values()) return this.map.values().stream()
{ .map(TerritoryAccess::getHostFactionId)
if ( ! ta.getHostFactionId().equals(factionId)) continue; .anyMatch(factionId::equals);
return true;
}
return false;
} }
// NEARBY DETECTION // NEARBY DETECTION
@ -298,11 +259,7 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public boolean isAnyBorderPs(Set<PS> pss) public boolean isAnyBorderPs(Set<PS> pss)
{ {
for (PS ps : pss) return pss.stream().anyMatch(this::isBorderPs);
{
if (this.isBorderPs(ps)) return true;
}
return false;
} }
// Is this coord connected to any coord claimed by the specified faction? // Is this coord connected to any coord claimed by the specified faction?
@ -331,11 +288,7 @@ public class Board extends Entity<Board> implements BoardInterface
@Override @Override
public boolean isAnyConnectedPs(Set<PS> pss, Faction faction) public boolean isAnyConnectedPs(Set<PS> pss, Faction faction)
{ {
for (PS ps : pss) return pss.stream().anyMatch(ps -> this.isConnectedPs(ps, faction));
{
if (this.isConnectedPs(ps, faction)) return true;
}
return false;
} }
} }

View File

@ -8,12 +8,10 @@ import com.massivecraft.massivecore.store.Coll;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
public class BoardColl extends Coll<Board> implements BoardInterface public class BoardColl extends Coll<Board> implements BoardInterface
{ {
@ -60,7 +58,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public TerritoryAccess getTerritoryAccessAt(PS ps) public TerritoryAccess getTerritoryAccessAt(PS ps)
{ {
if (ps == null) return null; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return null; if (board == null) return null;
return board.getTerritoryAccessAt(ps); return board.getTerritoryAccessAt(ps);
@ -69,7 +67,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public Faction getFactionAt(PS ps) public Faction getFactionAt(PS ps)
{ {
if (ps == null) return null; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return null; if (board == null) return null;
return board.getFactionAt(ps); return board.getFactionAt(ps);
@ -80,7 +78,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess) public void setTerritoryAccessAt(PS ps, TerritoryAccess territoryAccess)
{ {
if (ps == null) return; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return; if (board == null) return;
board.setTerritoryAccessAt(ps, territoryAccess); board.setTerritoryAccessAt(ps, territoryAccess);
@ -89,7 +87,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public void setFactionAt(PS ps, Faction faction) public void setFactionAt(PS ps, Faction faction)
{ {
if (ps == null) return; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return; if (board == null) return;
board.setFactionAt(ps, faction); board.setFactionAt(ps, faction);
@ -100,7 +98,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public void removeAt(PS ps) public void removeAt(PS ps)
{ {
if (ps == null) return; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return; if (board == null) return;
board.removeAt(ps); board.removeAt(ps);
@ -120,37 +118,28 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public Set<PS> getChunks(Faction faction) public Set<PS> getChunks(Faction faction)
{ {
// Create return this.getAll().stream()
Set<PS> ret = new HashSet<>(); .flatMap(board -> board.getChunks(faction).stream())
.collect(Collectors.toSet());
// Fill
for (Board board : this.getAll())
{
ret.addAll(board.getChunks(faction));
}
// Return
return ret;
} }
@Override @Override
public Set<PS> getChunks(String factionId) public Set<PS> getChunks(String factionId)
{ {
// Create return this.getAll().stream()
Set<PS> ret = new HashSet<>(); .flatMap(board -> board.getChunks(factionId).stream())
.collect(Collectors.toSet());
// Fill
for (Board board : this.getAll())
{
ret.addAll(board.getChunks(factionId));
}
// Return
return ret;
} }
@Override @Override
@Deprecated
public Map<Faction, Set<PS>> getFactionToChunks() public Map<Faction, Set<PS>> getFactionToChunks()
{
return this.getFactionToChunks(false);
}
@Override
public Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld)
{ {
// Create // Create
Map<Faction, Set<PS>> ret = null; Map<Faction, Set<PS>> ret = null;
@ -159,7 +148,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
for (Board board : this.getAll()) for (Board board : this.getAll())
{ {
// Use the first board directly // Use the first board directly
Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks(); Map<Faction, Set<PS>> factionToChunks = board.getFactionToChunks(withWorld);
if (ret == null) if (ret == null)
{ {
ret = factionToChunks; ret = factionToChunks;
@ -189,6 +178,13 @@ public class BoardColl extends Coll<Board> implements BoardInterface
return ret; return ret;
} }
@Override
public Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld)
{
return this.getAll().stream()
.collect(Collectors.toMap(Board::getId, board -> board.getFactionToChunks(withWorld)));
}
// COUNT // COUNT
@Override @Override
@ -200,46 +196,25 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public int getCount(String factionId) public int getCount(String factionId)
{ {
int ret = 0; return this.getAll().stream()
for (Board board : this.getAll()) .mapToInt(board -> board.getCount(factionId))
{ .sum();
ret += board.getCount(factionId);
}
return ret;
} }
@Override @Override
public Map<Faction, Integer> getFactionToCount() public Map<Faction, Long> getFactionToCount()
{ {
Map<Faction, Integer> ret = null; // Get them all and map them to sets of entries
for (Board board : this.getAll()) return this.getAll().stream()
{ .map(Board::getFactionToCount)
// Use the first board directly .map(Map::entrySet)
Map<Faction, Integer> factionToCount = board.getFactionToCount(); .flatMap(Set::stream)
if (ret == null) // Collect the entries in a map of <Faction, Long> by summing the values
{ .collect(Collectors.groupingBy(
ret = factionToCount; Entry::getKey,
continue; Collectors.summingLong(Entry::getValue)
} ))
;
// Merge the following boards
for (Entry<Faction, Integer> entry : factionToCount.entrySet())
{
Faction faction = entry.getKey();
Integer count = ret.get(faction);
if (count == null)
{
ret.put(faction, entry.getValue());
}
else
{
ret.put(faction, count + entry.getValue());
}
}
}
if (ret == null) ret = new MassiveMap<>();
return ret;
} }
// COUNT // COUNT
@ -253,11 +228,8 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public boolean hasClaimed(String factionId) public boolean hasClaimed(String factionId)
{ {
for (Board board : this.getAll()) return this.getAll().stream()
{ .anyMatch(board -> board.hasClaimed(factionId));
if (board.hasClaimed(factionId)) return true;
}
return false;
} }
// NEARBY DETECTION // NEARBY DETECTION
@ -265,7 +237,7 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public boolean isBorderPs(PS ps) public boolean isBorderPs(PS ps)
{ {
if (ps == null) return false; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return false; if (board == null) return false;
return board.isBorderPs(ps); return board.isBorderPs(ps);
@ -274,17 +246,13 @@ public class BoardColl extends Coll<Board> implements BoardInterface
@Override @Override
public boolean isAnyBorderPs(Set<PS> pss) public boolean isAnyBorderPs(Set<PS> pss)
{ {
for (PS ps : pss) return pss.stream().anyMatch(this::isBorderPs);
{
if (this.isBorderPs(ps)) return true;
}
return false;
} }
@Override @Override
public boolean isConnectedPs(PS ps, Faction faction) public boolean isConnectedPs(PS ps, Faction faction)
{ {
if (ps == null) return false; if (ps == null) throw new NullPointerException("ps");
Board board = this.get(ps.getWorld()); Board board = this.get(ps.getWorld());
if (board == null) return false; if (board == null) return false;
return board.isConnectedPs(ps, faction); return board.isConnectedPs(ps, faction);
@ -311,17 +279,11 @@ public class BoardColl extends Coll<Board> implements BoardInterface
public Set<String> getClaimedWorlds(String factionId) public Set<String> getClaimedWorlds(String factionId)
{ {
// Create if (factionId == null) throw new NullPointerException("factionId");
Set<String> ret = new MassiveSet<>(); return this.getAll().stream()
.filter(board -> board.hasClaimed(factionId))
// Fill .map(Board::getId)
for (Board board : this.getAll()) .collect(Collectors.toSet());
{
if (board.hasClaimed(factionId)) ret.add(board.getId());
}
// Return
return ret;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -23,12 +23,14 @@ public interface BoardInterface
// CHUNKS // CHUNKS
Set<PS> getChunks(Faction faction); Set<PS> getChunks(Faction faction);
Set<PS> getChunks(String factionId); Set<PS> getChunks(String factionId);
Map<Faction, Set<PS>> getFactionToChunks(); @Deprecated Map<Faction, Set<PS>> getFactionToChunks();
Map<Faction, Set<PS>> getFactionToChunks(boolean withWorld);
Map<String, Map<Faction, Set<PS>>> getWorldToFactionToChunks(boolean withWorld);
// COUNT // COUNT
int getCount(Faction faction); int getCount(Faction faction);
int getCount(String factionId); int getCount(String factionId);
Map<Faction, Integer> getFactionToCount(); Map<Faction, Long> getFactionToCount();
// CLAIMED // CLAIMED
boolean hasClaimed(Faction faction); boolean hasClaimed(Faction faction);

View File

@ -1092,6 +1092,11 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return ret.get(0); return ret.get(0);
} }
public Set<String> getMPlayerIds()
{
return this.getMPlayers().stream().map(MPlayer::getId).collect(Collectors.toSet());
}
public List<CommandSender> getOnlineCommandSenders() public List<CommandSender> getOnlineCommandSenders()
{ {
// Create Ret // Create Ret
@ -1141,7 +1146,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
MPlayer oldLeader = this.getLeader(); MPlayer oldLeader = this.getLeader();
Rank leaderRank = oldLeader.getRank(); Rank leaderRank = oldLeader.getRank();
List<MPlayer> replacements = Collections.<MPlayer>emptyList(); List<MPlayer> replacements = Collections.emptyList();
for (Rank rank = leaderRank; rank != null; rank = rank.getRankBelow()) for (Rank rank = leaderRank; rank != null; rank = rank.getRankBelow())
{ {
//Skip first //Skip first