Issue 182: Leaderboard changes (#194)

Implement new stats
This commit is contained in:
Woosle Park
2019-04-11 18:35:41 -04:00
committed by Simon Fortier
parent d71b3dc97c
commit f99f327480
13 changed files with 155 additions and 12 deletions

View File

@@ -176,6 +176,8 @@ public class GameServer implements Runnable {
user.getControlledUnit().getCpu().reset();
int cost = user.getControlledUnit().getCpu().execute(timeout);
user.getControlledUnit().spendEnergy(cost);
user.addTime(cost);
} catch (Exception e) {
LogManager.LOGGER.severe("Error executing " + user.getUsername() + "'s code");
e.printStackTrace();

View File

@@ -70,6 +70,10 @@ public class User implements MongoSerializable {
return user;
}
public void addTime(int time){
this.stats.incrementStat("executionTime", time);
}
public String getUserCode() {
return userCode;
}

View File

@@ -58,7 +58,28 @@ public class UserStats implements MongoSerializable {
* @return The value of the stat. Returns 0 if not found
*/
public int getInt(String name) {
return stats.getInteger(name, 0);
return stats.getInteger(name, 0);
}
/**
* Set the value of a stat
*
* @param name Name of the stat
* @param value new value
*/
public void setDouble(String name, double value) {
stats.put(name, value);
}
/**
* Get the value of at stat
*
* @param name Name of the value
* @return The value of the stat. Returns 0 if not found
*/
public double getDouble(String name) {
return stats.getDouble(name);
}
/**

View File

@@ -38,7 +38,35 @@ public class UserStatsHelper {
for (Document dbUser : users.find().sort(orderBy).limit(n)) {
User user = GameServer.INSTANCE.getGameUniverse().getUser((String) dbUser.get("username"));
rows.add(new AbstractMap.SimpleEntry<>(user, user.getStats().getInt(statName)));
int val = 0;
if (user.getStats().getInt(statName) > 0) {
val = user.getStats().getInt(statName);
}
rows.add(new AbstractMap.SimpleEntry<>(user, val));
}
return rows;
}
/**
* Get top n players along with all their stat values, in descending order of completed vaults
*
* @param n Maximum number of players
* @return Top n players, in User,value format, in descending order
*/
public ArrayList<Map.Entry<User, Map<String, Integer>>> getLeaderboardStats(int n) {
ArrayList<Map.Entry<User, Map<String, Integer>>> rows = new ArrayList<>(n);
List<User> users = getTopNSetSize("completedVaults", n);
for (User user : users) {
Map<String, Integer> allStats = new HashMap<>();
allStats.put("completedVaults", user.getStats().getSet("completedVaults").size());
allStats.put("death", user.getStats().getInt("death"));
allStats.put("executionTime", user.getStats().getInt("executionTime"));
allStats.put("walkDistance", user.getStats().getInt("walkDistance"));
rows.add(new AbstractMap.SimpleEntry<>(user, allStats));
}
return rows;
@@ -51,9 +79,9 @@ public class UserStatsHelper {
* @param n Maximum number of players
* @return Top n players, in User,set format, in descending order
*/
public ArrayList<Map.Entry<User, ArrayList>> getTopNSetLength(String statName, int n) {
private List<User> getTopNSetSize(String statName, int n) {
ArrayList<Map.Entry<User, ArrayList>> rows = new ArrayList<>();
ArrayList<User> rows = new ArrayList<>();
List<Object> ifNullList = new ArrayList<>(2);
ifNullList.add("$stats." + statName);
@@ -69,7 +97,7 @@ public class UserStatsHelper {
new Document("$limit", n))
)) {
User user = GameServer.INSTANCE.getGameUniverse().getUser((String) document.get("username"));
rows.add(new AbstractMap.SimpleEntry<>(user, user.getStats().getSet(statName)));
rows.add(user);
}
return rows;

View File

@@ -9,13 +9,14 @@ import spark.TemplateViewRoute;
import java.util.HashMap;
import java.util.Map;
public class LeaderBoardPage implements TemplateViewRoute {
@Override
public ModelAndView handle(Request request, Response response) {
Map<String, Object> model = new HashMap<>(2);
model.put("session", request.session());
model.put("stats", GameServer.INSTANCE.getUserStatsHelper().getTopNSetLength("completedVaults", 25));
model.put("stats", GameServer.INSTANCE.getUserStatsHelper().getLeaderboardStats(25));
return new ModelAndView(model, "leaderboard.vm");
}
}

View File

@@ -12,7 +12,7 @@ server_name=MAR dev
# ALLOW | BLOCK
guest_policy=ALLOW
# DEBUG
autologin=simon
#autologin=simon
#Database
mongo_dbname=mar_beta

View File

@@ -16,13 +16,19 @@
<tr>
<th>Player</th>
<th>Completed vaults</th>
<th>Death counts</th>
<th>Total execution time (ms)</th>
<th>Walk distance</th>
</tr>
</thead>
<tbody>
#foreach($row in $stats)
<tr>
<td>$row.getKey().getUsername()</td>
<td>$row.getValue().size()</td>
<td>$row.getValue().get("completedVaults")</td>
<td>$row.getValue().get("death")</td>
<td>$row.getValue().get("executionTime")</td>
<td>$row.getValue().get("walkDistance")</td>
</tr>
#end
</tbody>