Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Performance improvements by switching from LinkedList to HashMap on

Spleefer lists.
  • Loading branch information...
commit 9131c475f7811131dc594f73581b7b794f48a92d 1 parent 9aa0204
Max authored
19 src/main/java/de/beimax/simplespleef/game/GameStandard.java
View
@@ -1349,9 +1349,8 @@ public boolean endGame() {
// do we need to teleport players?
if (oldStatus == Game.STATUS_COUNTDOWN || oldStatus == Game.STATUS_INTERRUPTED) { // game ended during countdown - teleport players back to lounge
- for (Spleefer spleefer : spleefers.get()) {
- if (!spleefer.hasLost()) // will leave out players that left, died, lost, etc.
- teleportPlayer(spleefer.getPlayer(), "lounge");
+ for (Spleefer spleefer : spleefers.getLost()) {
+ teleportPlayer(spleefer.getPlayer(), "lounge");
}
}
@@ -1488,10 +1487,7 @@ public String getListOfUnreadySpleefers() {
// no spleefers - return null
if (spleefers == null || spleefers.size() == 0) return null;
// get unready spleefers
- LinkedList<Spleefer> list = new LinkedList<Spleefer>();
- for (Spleefer spleefer : spleefers.get()) {
- if (!spleefer.isReady()) list.add(spleefer);
- }
+ List<Spleefer> list = spleefers.getUnready();
// is the list empty?
if (list.size() == 0) return null; // no unready spleefes
// compile list
@@ -1703,9 +1699,9 @@ protected boolean checkGameOver() {
* @param player
*/
protected void winByTouching(Player player) {
- for (Spleefer spleefer : spleefers.get()) {
+ for (Spleefer spleefer : spleefers.getNotLost()) {
// if not the winning player and not already lost
- if (spleefer.getPlayer() != player && !spleefer.hasLost())
+ if (spleefer.getPlayer() != player)
playerLoses(spleefer.getPlayer(), true);
}
// check game over manually, if there was no winner yet
@@ -1980,9 +1976,8 @@ protected boolean removeShovelItems() {
// check setting first
if (!configuration.getBoolean("playersLoseShovelAtGameEnd", true)) return false; // no shovels lost
// if yes, remove shovels from remaining players
- for (Spleefer spleefer : spleefers.get()) {
- if (!spleefer.hasLost())
- removeShovelItem(spleefer.getPlayer(), false); // remove shovel without checking setting again
+ for (Spleefer spleefer : spleefers.getNotLost()) {
+ removeShovelItem(spleefer.getPlayer(), false); // remove shovel without checking setting again
}
return true;
}
18 src/main/java/de/beimax/simplespleef/game/GameWithTeams.java
View
@@ -200,12 +200,10 @@ protected boolean checkGameOver() {
// check if teams are still here
boolean[] stillHere = {false, false};
// check spleefer's status
- for (Spleefer spleefer : spleefers.get()) {
- if (!spleefer.hasLost()) { // not lost?
- int team = spleefer.getTeam(); //out-of-bounds check
- if (team == Spleefer.TEAM_BLUE || team == Spleefer.TEAM_RED)
- stillHere[spleefer.getTeam() - 1] = true; // team is still here
- }
+ for (Spleefer spleefer : spleefers.getNotLost()) {
+ int team = spleefer.getTeam(); //out-of-bounds check
+ if (team == Spleefer.TEAM_BLUE || team == Spleefer.TEAM_RED)
+ stillHere[spleefer.getTeam() - 1] = true; // team is still here
}
// do we have a team that isn't here?
for (int i = 0; i < stillHere.length; i++)
@@ -217,11 +215,9 @@ protected boolean checkGameOver() {
protected void winByTouching(Player player) {
// get team
int team = -99;
- for (Spleefer spleefer : spleefers.get()) {
- if (spleefer.getPlayer() == player) {
- team = spleefer.getTeam();
- break;
- }
+ Spleefer s;
+ if ((s = spleefers.getSpleefer(player)) != null) {
+ team = s.getTeam();
}
if (team == -99) {
SimpleSpleef.log.warning("[SimpleSpleef] Could not determine winning team while touching a block.");
89 src/main/java/de/beimax/simplespleef/game/SpleeferList.java
View
@@ -18,6 +18,7 @@
**/
package de.beimax.simplespleef.game;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -36,7 +37,7 @@
* @param players
* @return
*/
- public static String getPrintablePlayerList(LinkedList<Spleefer> players) {
+ public static String getPrintablePlayerList(List<Spleefer> players) {
// build list of winners
StringBuilder builder = new StringBuilder();
int i = 0;
@@ -54,13 +55,13 @@ public static String getPrintablePlayerList(LinkedList<Spleefer> players) {
* list of spleefers currently spleefing
* - Since we have to iterate the list most of the time anyway, we simply use a linked list
*/
- private LinkedList<Spleefer> spleefers;
+ private HashMap<String, Spleefer> spleefers;
/**
* Constructor
*/
public SpleeferList() {
- spleefers = new LinkedList<Spleefer>();
+ spleefers = new HashMap<String, Spleefer>();
}
/**
@@ -69,10 +70,7 @@ public SpleeferList() {
* @return spleefer or null
*/
public Spleefer getSpleefer(Player player) {
- for (Spleefer spleefer : spleefers) {
- if (spleefer.getPlayer() == player) return spleefer;
- }
- return null;
+ return spleefers.get(player.getName());
}
/**
@@ -82,7 +80,7 @@ public Spleefer getSpleefer(Player player) {
*/
public boolean addSpleefer(Player player) {
if (hasSpleefer(player)) return false;
- spleefers.add(new Spleefer(player));
+ spleefers.put(player.getName(), new Spleefer(player));
return true;
}
@@ -92,12 +90,8 @@ public boolean addSpleefer(Player player) {
* @return boolean, true if successful
*/
public boolean removeSpleefer(Player player) {
- for (Spleefer spleefer : spleefers) {
- if (spleefer.getPlayer() == player) {
- spleefers.remove(spleefer);
- return true;
- }
- }
+ if (spleefers.remove(player.getName()) != null) return true;
+
return false;
}
@@ -107,10 +101,7 @@ public boolean removeSpleefer(Player player) {
* @return
*/
public boolean hasSpleefer(Player player) {
- for (Spleefer spleefer : spleefers) {
- if (spleefer.getPlayer() == player) return true;
- }
- return false;
+ return spleefers.containsValue(player.getName());
}
/**
@@ -119,9 +110,9 @@ public boolean hasSpleefer(Player player) {
* @return
*/
public boolean hasLost(Player player) {
- for (Spleefer spleefer : spleefers) {
- if (spleefer.getPlayer() == player) return spleefer.hasLost();
- }
+ Spleefer spleefer = getSpleefer(player);
+ if (spleefer != null) return spleefer.hasLost();
+
return false;
}
@@ -130,9 +121,8 @@ public boolean hasLost(Player player) {
* @param player
*/
public void setLost(Player player) {
- for (Spleefer spleefer : spleefers) {
- if (spleefer.getPlayer() == player) spleefer.setLost(true);
- }
+ Spleefer spleefer = getSpleefer(player);
+ if (spleefer != null) spleefer.setLost(true);
}
/**
@@ -141,7 +131,7 @@ public void setLost(Player player) {
*/
public int inGame() {
int inGame = 0;
- for (Spleefer spleefer : spleefers) {
+ for (Spleefer spleefer : spleefers.values()) {
if (!spleefer.hasLost()) inGame++;
}
return inGame;
@@ -154,7 +144,7 @@ public int inGame() {
*/
public int inGame(int team) {
int inGame = 0;
- for (Spleefer spleefer : spleefers) {
+ for (Spleefer spleefer : spleefers.values()) {
if (spleefer.getTeam() == team && !spleefer.hasLost()) inGame++;
}
return inGame;
@@ -173,7 +163,44 @@ public int size() {
* @return
*/
public List<Spleefer> get() {
- return spleefers;
+ LinkedList<Spleefer> list = new LinkedList<Spleefer>();
+ for (Spleefer spleefer : spleefers.values()) {
+ list.add(spleefer);
+ }
+ return list;
+ }
+
+ /**
+ * get list of players that have lost
+ */
+ public List<Spleefer> getLost() {
+ LinkedList<Spleefer> list = new LinkedList<Spleefer>();
+ for (Spleefer spleefer : spleefers.values()) {
+ if (spleefer.hasLost()) list.add(spleefer);
+ }
+ return list;
+ }
+
+ /**
+ * get list of players that have not lost yet
+ */
+ public List<Spleefer> getNotLost() {
+ LinkedList<Spleefer> list = new LinkedList<Spleefer>();
+ for (Spleefer spleefer : spleefers.values()) {
+ if (!spleefer.hasLost()) list.add(spleefer);
+ }
+ return list;
+ }
+
+ /**
+ * get list of players that are not ready yet
+ */
+ public List<Spleefer> getUnready() {
+ LinkedList<Spleefer> list = new LinkedList<Spleefer>();
+ for (Spleefer spleefer : spleefers.values()) {
+ if (!spleefer.isReady()) list.add(spleefer);
+ }
+ return list;
}
/**
@@ -181,7 +208,7 @@ public int size() {
* @return
*/
public Iterator<Spleefer> iterator() {
- return spleefers.iterator();
+ return spleefers.values().iterator();
}
/**
@@ -191,7 +218,7 @@ public int size() {
*/
public List<Spleefer> getTeam(int team) {
LinkedList<Spleefer> compiledTeam = new LinkedList<Spleefer>();
- for (Spleefer spleefer : spleefers) {
+ for (Spleefer spleefer : spleefers.values()) {
if (spleefer.getTeam() == team) compiledTeam.add(spleefer);
}
@@ -205,7 +232,7 @@ public int size() {
*/
public int countUnreadyPlayers() {
int unready = 0;
- for (Spleefer spleefer : spleefers) {
+ for (Spleefer spleefer : spleefers.values()) {
if (!spleefer.isReady()) unready++;
}
return unready;
@@ -217,7 +244,7 @@ public int countUnreadyPlayers() {
*/
public int countLostPlayers() {
int lost = 0;
- for (Spleefer spleefer : spleefers) {
+ for (Spleefer spleefer : spleefers.values()) {
if (!spleefer.hasLost()) lost++;
}
return lost;
Please sign in to comment.
Something went wrong with that request. Please try again.