I have a MongoDB db on a server operated by Kryonet. Obviously I need to be able to query the database from the client for adding, removing, requesting all kind of assets. Now I have written the code to request, add and remove fighters on the Kryonet network I am wondering if there is a better way to do this. I feel it's a bit repetitive, especially when I need to implement this for all other assets the player can own and other players assets when needed. The way I am currently approaching this is the same as my chat/lobby system which works great but I was wondering if anyone could see improvement on my code or a complete different way that is much more scalable perhaps.
public class ClientAssets {
public static final int FIGHTER_REQUEST = 1;
public static final int FIGHTER_RESPONSE = 2;
public static final int FIGHTER_ADD = 3;
public static final int FIGHTER_REMOVE = 4;
public static void Register(EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
kryo.register(FighterRequest.class);
kryo.register(FighterResponse.class);
kryo.register(FighterAdd.class);
kryo.register(FighterRemove.class);
}
static public abstract class AssetPacket {
public int packetId;
public AssetPacket() {
}
}
/**
* Packet to request all owned fighters
*/
public static class FighterRequest extends AssetPacket {
public ObjectId playerId;
public FighterRequest(ObjectId playerId) {
packetId = FIGHTER_REQUEST;
this.playerId = playerId;
}
public FighterRequest() {
}
}
/**
* Receiving fighter data from server
*/
public static class FighterResponse extends AssetPacket {
public Fighter fighter;
public boolean add; // Add or remove
public FighterResponse(Fighter fighter, boolean add) {
packetId = FIGHTER_RESPONSE;
this.fighter = fighter;
this.add = add;
}
public FighterResponse() {
}
}
/**
* Adds a fighter to player assets
*/
public static class FighterAdd extends AssetPacket {
public ObjectId fighterTemplateID;
public FighterAdd(ObjectId fighterTemplateID) {
packetId = FIGHTER_ADD;
this.fighterTemplateID = fighterTemplateID;
}
public FighterAdd() {
}
}
/**
* Removes fighter from assets.
*/
public static class FighterRemove extends AssetPacket {
public ObjectId fighterId;
public FighterRemove(ObjectId fighterId) {
packetId = FIGHTER_REMOVE;
this.fighterId = fighterId;
}
public FighterRemove() {
}
}
}
To elaborate a bit more, this code will communicate between client and server. When receiving a request on the server it will lookup the request in the database. The client will store it for displaying the assets. A specific thing I am unsure about is the FighterResponse.add boolean. I need to be able to remove and add fighters, I guess I am better off with a FighterAddResponse and a FighterRemove response so I will send one boolean less each time this packet is send. But this will create even more repetitive code.