Added many debug commands

This commit is contained in:
simon 2018-02-25 11:55:53 -05:00
parent 95a14ad1ab
commit 6a1519d97d
2 changed files with 206 additions and 11 deletions

View File

@ -6,7 +6,6 @@ import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.awt.*;
@ -112,14 +111,6 @@ public class Factory extends GameObject implements Updatable {
json.put("y", getY());
json.put("t", ID);
JSONArray tmpNpcArray = new JSONArray();
for (NonPlayerCharacter npc : npcs) {
tmpNpcArray.add(npc.getObjectId());
}
json.put("n", tmpNpcArray);
return json;
}

View File

@ -1,8 +1,19 @@
package net.simon987.server.webserver;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import net.simon987.server.GameServer;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.json.simple.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class DebugHandler implements MessageHandler {
@Override
public void handle(OnlineUser user, JSONObject json) {
@ -16,15 +27,65 @@ public class DebugHandler implements MessageHandler {
if (json.containsKey("command")) {
JSONObject response = new JSONObject();
response.put("t", "debug");
switch (command) {
case "":
case "setTileAt":
response.put("message", setTileAt(
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("newTile"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
break;
case "createWorld":
response.put("message", createWorld(
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
break;
case "killAll":
response.put("message", killAll(
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
break;
case "objInfo":
response.put("message", objInfo(
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
break;
case "userInfo":
response.put("message", userInfo((String) json.get("username")));
break;
case "moveObj":
response.put("message", moveObj(
(long) json.get("objectId"),
(int) (long) json.get("x"),
(int) (long) json.get("y")));
break;
case "spawnObj":
response.put("message", spawnObj(
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY"),
(String) json.get("data")));
break;
default:
LogManager.LOGGER.severe("Unknown command: " + command);
response.put("t", "debug");
response.put("message", "Unknown command " + command);
}
@ -34,4 +95,147 @@ public class DebugHandler implements MessageHandler {
}
}
/**
* Create a world at coordinates
*/
private String createWorld(int worldX, int worldY) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, true);
if (world != null) {
return "Success";
} else {
return "Couldn't create world";
}
}
/**
* Change the tile at coordinate
*/
private String setTileAt(int x, int y, int newTile, int worldX, int worldY) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
if (world != null) {
world.getTileMap().setTileAt(newTile, x, y);
return "Success";
} else {
return "Error: World is uncharted";
}
}
private String spawnObj(int worldX, int worldY, String data) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
try {
DBObject dbObj = (DBObject) JSON.parse(data);
dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId());
GameObject object = GameObject.deserialize(dbObj);
if (object != null) {
world.addObject(object);
return "Created object " + object.getObjectId();
} else {
return "Couldn't deserialise the object";
}
} catch (Exception e) {
return Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n");
}
}
private String killAll(int x, int y, int worldX, int worldY) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
try {
ArrayList<GameObject> objs = world.getGameObjectsAt(x, y);
for (GameObject o : objs) {
o.setDead(true);
}
return "Killed " + objs.size() + " objects";
} catch (Exception e) {
return Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n");
}
}
private String objInfo(int x, int y, int worldX, int worldY) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
try {
Collection<GameObject> objs = world.getGameObjects();
String str = objs.size() + "\n";
for (GameObject obj : objs) {
if (obj.isAt(x, y) || (obj.getX() == x && obj.getY() == y)) {
str += "Mongo:" + obj.mongoSerialise() + "\n";
str += "JSON :" + obj.serialise().toJSONString() + "\n\n";
}
}
return str;
} catch (Exception e) {
return Arrays.toString(e.getStackTrace()).replaceAll(", ", "\n");
}
}
private String moveObj(long objectId, int x, int y) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(objectId);
if (object != null) {
object.setX(x);
object.setY(y);
return "Sucess";
} else {
return "Object not found: " + objectId;
}
}
private String userInfo(String username) {
User user = GameServer.INSTANCE.getGameUniverse().getUser(username);
if (user != null) {
String str = "Showing information for user " + username + "\n";
str += "isGuest: " + user.isGuest() + "\n";
ControllableUnit unit = user.getControlledUnit();
str += "ControlledUnit: " + unit.getObjectId() + " at (" + unit.getX() + ", " + unit.getY() + ")\n";
str += "CPU:" + user.getCpu() + "\n";
str += "Code: " + user.getUserCode();
return str;
} else {
return "User not found";
}
}
}