execution time added

This commit is contained in:
Woosle Park 2019-03-28 20:28:41 -04:00
parent eb2635db73
commit 89f53a159a
5 changed files with 75 additions and 4 deletions

View File

@ -103,6 +103,16 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
private Map<Integer, HardwareModule> hardwareAddresses = new HashMap<>(); private Map<Integer, HardwareModule> hardwareAddresses = new HashMap<>();
private Map<Class<? extends HardwareModule>, Integer> hardwareModules = new HashMap<>(); private Map<Class<? extends HardwareModule>, Integer> hardwareModules = new HashMap<>();
/**
* In game timer
*/
private int time;
/**
* Real Life Timer for getting execution time
*/
private RealLifeTimer timer;
/** /**
* Cubot's brain box * Cubot's brain box
*/ */
@ -130,6 +140,10 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
shield = document.getInteger("shield"); shield = document.getInteger("shield");
setDirection(Direction.getDirection(document.getInteger("direction"))); setDirection(Direction.getDirection(document.getInteger("direction")));
timer = new RealLifeTimer();
timer.run();
time = timer.getTime();
IServerConfiguration config = GameServer.INSTANCE.getConfig(); IServerConfiguration config = GameServer.INSTANCE.getConfig();
maxHp = config.getInt("cubot_max_hp"); maxHp = config.getInt("cubot_max_hp");
maxShield = config.getInt("cubot_max_shield"); maxShield = config.getInt("cubot_max_shield");
@ -159,6 +173,12 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
*/ */
@Override @Override
public void update() { public void update() {
int executionTime = timer.getTime();
if(executionTime > time && currentAction != Action.IDLE){
GameEvent event1 = new ExecutionTimeEvent(this, executionTime - time);
GameServer.INSTANCE.getEventDispatcher().dispatch(event1);
}
time = executionTime;
if (currentAction == Action.WALKING) { if (currentAction == Action.WALKING) {
if (spendEnergy(100)) { if (spendEnergy(100)) {
@ -166,8 +186,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
//Couldn't walk //Couldn't walk
currentAction = Action.IDLE; currentAction = Action.IDLE;
}else{ }else{
GameEvent event = new WalkDistanceEvent(this); GameEvent event2 = new WalkDistanceEvent(this);
GameServer.INSTANCE.getEventDispatcher().dispatch(event); GameServer.INSTANCE.getEventDispatcher().dispatch(event2);
} }
} else { } else {
currentAction = Action.IDLE; currentAction = Action.IDLE;
@ -269,6 +289,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
currentStatus = 0; currentStatus = 0;
lastStatus = 0; lastStatus = 0;
addStatus(CubotStatus.FACTORY_NEW); addStatus(CubotStatus.FACTORY_NEW);
time = timer.getTime();
for (HardwareModule module : hardwareAddresses.values()) { for (HardwareModule module : hardwareAddresses.values()) {
module.reset(); module.reset();

View File

@ -18,6 +18,7 @@ public class CubotPlugin extends ServerPlugin {
listeners.add(new SetInventoryPosition()); listeners.add(new SetInventoryPosition());
listeners.add(new PutItemCommandListener()); listeners.add(new PutItemCommandListener());
listeners.add(new PopItemCommandListener()); listeners.add(new PopItemCommandListener());
//Leaderboard
listeners.add(new DeathCountListener()); listeners.add(new DeathCountListener());
listeners.add(new ExecutionTimeListener()); listeners.add(new ExecutionTimeListener());
listeners.add(new WalkDistanceListener()); listeners.add(new WalkDistanceListener());

View File

@ -5,8 +5,15 @@ import net.simon987.server.game.objects.GameObject;
public class ExecutionTimeEvent extends GameEvent { public class ExecutionTimeEvent extends GameEvent {
public ExecutionTimeEvent(GameObject object) { private int Time = 0;
public ExecutionTimeEvent(GameObject object, int time) {
setSource(object); setSource(object);
this.Time = time;
}
public int getTime(){
return this.Time;
} }
@Override @Override

View File

@ -21,7 +21,10 @@ public class ExecutionTimeListener implements GameEventListener {
GameObject object = executionTimeEvent.getSource(); GameObject object = executionTimeEvent.getSource();
if (object instanceof ControllableUnit) { if (object instanceof ControllableUnit) {
count = ((ControllableUnit) object).getParent().getStats().getInt("executionTime"); count = ((ControllableUnit) object).getParent().getStats().getInt("executionTime");
count++; count += executionTimeEvent.getTime();
LogManager.LOGGER.info(((ControllableUnit) object).getParent().getUsername() + " Execution Time " +
count);
((ControllableUnit) object).getParent().getStats().setInt("executionTime", ((ControllableUnit) object).getParent().getStats().setInt("executionTime",
count); count);

View File

@ -0,0 +1,39 @@
package net.simon987.cubotplugin.event;
import java.util.Timer;
import java.util.TimerTask;
public class RealLifeTimer{
private int time;
private Timer timer;
public RealLifeTimer(){
timer = new Timer();
time = 0;
}
public RealLifeTimer(int startTime){
timer = new Timer();
time = startTime;
}
public void run(){
timer.scheduleAtFixedRate(new TimerTask() {
public void run(){
time++;
}
},0,1);
}
public void stop(){
timer.cancel();
timer.purge();
}
public int getTime(){
return this.time;
}
}