Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 182: Leaderboard changes #194

Merged
merged 15 commits into from Apr 11, 2019
Expand Up @@ -11,6 +11,8 @@
import net.simon987.server.game.item.ItemVoid;
import net.simon987.server.game.objects.*;
import net.simon987.server.user.User;
import net.simon987.server.event.GameEvent;
import net.simon987.cubotplugin.event.*;
import org.bson.Document;
import org.json.simple.JSONObject;

Expand Down Expand Up @@ -157,12 +159,14 @@ public char getMapInfo() {
*/
@Override
public void update() {

if (currentAction == Action.WALKING) {
if (spendEnergy(100)) {
if (!incrementLocation()) {
//Couldn't walk
currentAction = Action.IDLE;
}else{
GameEvent event2 = new WalkEvent(this);
GameServer.INSTANCE.getEventDispatcher().dispatch(event2);
}
} else {
currentAction = Action.IDLE;
Expand Down Expand Up @@ -272,6 +276,9 @@ private void reset() {

@Override
public boolean onDeadCallback() {
GameEvent event = new DeathEvent(this);
GameServer.INSTANCE.getEventDispatcher().dispatch(event);

reset();

//Teleport to spawn point
Expand Down
Expand Up @@ -18,6 +18,9 @@ public void init(GameServer gameServer) {
listeners.add(new SetInventoryPosition());
listeners.add(new PutItemCommandListener());
listeners.add(new PopItemCommandListener());
//Leaderboard
listeners.add(new DeathListener());
listeners.add(new WalkListener());

GameRegistry registry = gameServer.getRegistry();

Expand Down
@@ -0,0 +1,16 @@
package net.simon987.cubotplugin.event;

import net.simon987.server.event.GameEvent;
import net.simon987.server.game.objects.GameObject;

public class DeathEvent extends GameEvent {

public DeathEvent(GameObject object) {
setSource(object);
}

@Override
public GameObject getSource() {
return (GameObject) super.getSource();
}
}
@@ -0,0 +1,25 @@
package net.simon987.cubotplugin.event;

import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.logging.LogManager;

public class DeathListener implements GameEventListener {

@Override
public Class getListenedEventType() {
return DeathEvent.class;
}

@Override
public void handle(GameEvent event) {
DeathEvent DeathEvent = (DeathEvent) event;
GameObject object = DeathEvent.getSource();
if (object instanceof ControllableUnit) {
((ControllableUnit) object).getParent().getStats().incrementStat("death",
1);
}
}
}
@@ -0,0 +1,17 @@
package net.simon987.cubotplugin.event;

import net.simon987.server.event.GameEvent;
import net.simon987.server.game.objects.GameObject;

public class WalkEvent extends GameEvent {

public WalkEvent(GameObject object) {
setSource(object);
}

@Override
public GameObject getSource() {
return (GameObject) super.getSource();
}

}
@@ -0,0 +1,26 @@
package net.simon987.cubotplugin.event;

import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.UserStats;

public class WalkListener implements GameEventListener {

@Override
public Class getListenedEventType() {
return WalkEvent.class;
}

@Override
public void handle(GameEvent event) {
WalkEvent WalkEvent = (WalkEvent) event;
GameObject object = WalkEvent.getSource();
if (object instanceof ControllableUnit) {
((ControllableUnit) object).getParent().getStats().incrementStat("walkDistance",
1);
}
}
}
2 changes: 2 additions & 0 deletions Server/src/main/java/net/simon987/server/GameServer.java
Expand Up @@ -176,6 +176,8 @@ private void tick() {
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();
Expand Down
4 changes: 4 additions & 0 deletions Server/src/main/java/net/simon987/server/user/User.java
Expand Up @@ -70,6 +70,10 @@ public static User deserialize(Document obj) throws CancelledException {
return user;
}

public void addTime(int time){
this.stats.incrementStat("executionTime", time);
}

public String getUserCode() {
return userCode;
}
Expand Down
23 changes: 22 additions & 1 deletion Server/src/main/java/net/simon987/server/user/UserStats.java
Expand Up @@ -58,7 +58,28 @@ public void setInt(String name, int value) {
* @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);
}

/**
Expand Down
Expand Up @@ -38,7 +38,38 @@ public ArrayList<Map.Entry<User, Integer>> getTopN(String statName, int n) {

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
*
* @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>>> getTopNAll(int n) {

ArrayList<Map.Entry<User, Map<String, Integer>>> rows = new ArrayList<>();

ArrayList<Map.Entry<User, ArrayList>> vaults = new ArrayList<>(this.getTopNSetLength("completedVaults", n));
ArrayList<Map.Entry<User, Integer>> deaths = new ArrayList<>(this.getTopN("death", n));
ArrayList<Map.Entry<User, Integer>> time = new ArrayList<>(this.getTopN("executionTime", n));
ArrayList<Map.Entry<User, Integer>> distance = new ArrayList<>(this.getTopN("walkDistance", n));

for (int i = 0; i < vaults.size() ; i++) {
User user = vaults.get(i).getKey();
Map<String, Integer> allStats = new HashMap();
allStats.put("completedVaults", vaults.get(i).getValue().size());
allStats.put("death", deaths.get(i).getValue());
allStats.put("executionTime", time.get(i).getValue());
allStats.put("walkDistance", distance.get(i).getValue());
rows.add(new AbstractMap.SimpleEntry<>(user, allStats));
}

return rows;
Expand Down
Expand Up @@ -9,13 +9,14 @@
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().getTopNAll(25));
return new ModelAndView(model, "leaderboard.vm");
}
}
8 changes: 7 additions & 1 deletion Server/src/main/resources/templates/leaderboard.vm
Expand Up @@ -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>
Expand Down