some server optimizations

This commit is contained in:
BetaSteward 2011-10-27 11:20:10 -04:00
parent e97d655bd1
commit 7ab5649132
4 changed files with 44 additions and 35 deletions

View file

@ -33,7 +33,6 @@ import mage.server.RoomImpl;
import mage.game.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@ -48,10 +47,13 @@ import mage.game.GameException;
import mage.game.match.MatchOptions;
import mage.game.tournament.TournamentOptions;
import mage.MageException;
import mage.server.User;
import mage.server.UserManager;
import mage.view.MatchView;
import mage.view.TableView;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
@ -63,6 +65,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
private static ScheduledExecutorService updateExecutor = Executors.newSingleThreadScheduledExecutor();
private static List<TableView> tableView = new ArrayList<TableView>();
private static List<MatchView> matchView = new ArrayList<MatchView>();
private static List<String> playersView = new ArrayList<String>();
private ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<UUID, Table>();
@ -70,8 +73,7 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
updateExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
updateTables();
updateFinished();
update();
}
}, 2, 2, TimeUnit.SECONDS);
}
@ -81,13 +83,26 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return tableView;
}
private void updateTables() {
private void update() {
ArrayList<TableView> tableList = new ArrayList<TableView>();
for (Table table: tables.values()) {
if (table.getState() != TableState.FINISHED)
ArrayList<MatchView> matchList = new ArrayList<MatchView>();
List<Table> t = new ArrayList<Table>(tables.values());
Collections.sort(t, new TimestampSorter());
for (Table table: t) {
if (table.getState() != TableState.FINISHED) {
tableList.add(new TableView(table));
}
else if (matchList.size() < 50) {
matchList.add(new MatchView(table.getMatch()));
}
}
tableView = tableList;
matchView = matchList;
List<String> players = new ArrayList<String>();
for (User user : UserManager.getInstance().getUsers()) {
players.add(user.getName());
}
playersView = players;
}
@Override
@ -95,20 +110,6 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return matchView;
}
private void updateFinished() {
ArrayList<MatchView> matchList = new ArrayList<MatchView>();
List<Table> t = new ArrayList<Table>(tables.values());
Collections.sort(t, new TimestampSorter());
for (Table table: t) {
if (table.getState() == TableState.FINISHED) {
matchList.add(new MatchView(table.getMatch()));
if (matchList.size() >= 50)
break;
}
}
matchView = matchList;
}
@Override
public boolean joinTable(UUID userId, UUID tableId, String name, String playerType, int skill, DeckCardLists deckList) throws MageException {
if (tables.containsKey(tableId)) {
@ -170,6 +171,11 @@ public class GamesRoomImpl extends RoomImpl implements GamesRoom, Serializable {
return TableManager.getInstance().watchTable(userId, tableId);
}
@Override
public List<String> getPlayers() {
return playersView;
}
}
class TimestampSorter implements Comparator<Table> {