Added support for creating game sessions with MCP

Added support for creating game sessions with MCP
Forced debug mode for BNLS and MCP for testing purposes
Fixed type with Result#ALREADY_EXISTS constant
Extended GameSession to support builder pattern and CreateGame copy constructor
This commit is contained in:
Collin Smith
2019-11-30 02:24:13 -08:00
parent ac35310ace
commit 4a5a03edbc
6 changed files with 113 additions and 5 deletions

View File

@ -6,7 +6,7 @@ public final class Result {
private Result() { }
public static final int SUCCESS = 0;
public static final int INVALID_NAME = 30;
public static final int ALREAD_EXISTS = 31;
public static final int ALREADY_EXISTS = 31;
public static final int SERVER_DOWN = 32;
public static final int INVALID_PASSWORD = 41;
public static final int GAME_DOES_NOT_EXIST = 42;

View File

@ -1,5 +1,7 @@
package com.riiablo.net;
import com.riiablo.net.packet.mcp.CreateGame;
public class GameSession {
public String name;
public String password;
@ -12,8 +14,33 @@ public class GameSession {
desc = game.desc();
}
public GameSession(CreateGame game) {
name = game.gameName();
desc = game.description();
}
GameSession(Builder builder) {
name = builder.name;
password = builder.password;
desc = builder.desc;
}
@Override
public String toString() {
return name;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
public String name;
public String password;
public String desc;
public GameSession build() {
return new GameSession(this);
}
}
}

View File

@ -3,7 +3,7 @@ namespace com.riiablo.net.packet.mcp;
enum Result : int {
SUCCESS = 0x00,
INVALID_NAME = 0x1E,
ALREAD_EXISTS = 0x1F,
ALREADY_EXISTS = 0x1F,
SERVER_DOWN = 0x20,
INVALID_PASSWORD = 0x29,
GAME_DOES_NOT_EXIST = 0x2A,

View File

@ -35,9 +35,11 @@ import com.riiablo.codec.DC6;
import com.riiablo.graphics.PaletteIndexedBatch;
import com.riiablo.loader.DC6Loader;
import com.riiablo.net.GameSession;
import com.riiablo.net.packet.mcp.CreateGame;
import com.riiablo.net.packet.mcp.ListGames;
import com.riiablo.net.packet.mcp.MCP;
import com.riiablo.net.packet.mcp.MCPData;
import com.riiablo.net.packet.mcp.Result;
import com.riiablo.server.Account;
import com.riiablo.util.EventUtils;
import com.riiablo.widget.Label;
@ -290,6 +292,33 @@ public class LobbyScreen extends ScreenAdapter {
btnCreateGame.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
final GameSession session = new GameSession.Builder() {{
name = tfGameName.getText();
password = tfPassword.getText();
desc = tfDesc.getText();
}}.build();
CreateGame(session, new ResponseListener() {
@Override
public void handleResponse(MCP packet) {
CreateGame createGame = (CreateGame) packet.data(new CreateGame());
switch (createGame.result()) {
case Result.SUCCESS:
Gdx.app.debug(TAG, "Session created! " + session);
break;
case Result.ALREADY_EXISTS:
Gdx.app.debug(TAG, "ALREADY_EXISTS");
break;
case Result.SERVER_DOWN:
Gdx.app.debug(TAG, "SERVER_DOWN");
break;
}
}
@Override
public void failed(Throwable t) {
Gdx.app.error(TAG, t.getMessage(), t);
}
});
// Net.HttpRequest request = new HttpRequestBuilder()
// .newRequest()
// .method(Net.HttpMethods.POST)
@ -628,6 +657,21 @@ public class LobbyScreen extends ScreenAdapter {
connection.sendRequest(data, listener);
}
private void CreateGame(GameSession session, ResponseListener listener) {
Gdx.app.debug(TAG, "Creating game");
FlatBufferBuilder builder = new FlatBufferBuilder();
int gameNameOffset = builder.createString(session.name);
int descriptionOffset = builder.createString(session.desc);
CreateGame.startCreateGame(builder);
CreateGame.addGameName(builder, gameNameOffset);
CreateGame.addDescription(builder, descriptionOffset);
int createGamesOffset = CreateGame.endCreateGame(builder);
int id = MCP.createMCP(builder, MCPData.CreateGame, createGamesOffset);
builder.finish(id);
ByteBuffer data = builder.dataBuffer();
connection.sendRequest(data, listener);
}
static class TabbedPane extends Container<TabbedPane.TabGroup> {
TextureRegion defaultBackground;
ClickListener clickListener;

View File

@ -2,6 +2,7 @@ package com.riiablo.server.bnls;
import com.google.flatbuffers.FlatBufferBuilder;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
@ -61,6 +62,8 @@ public class BNLS extends ApplicationAdapter {
@Override
public void create() {
Gdx.app.setLogLevel(Application.LOG_DEBUG);
final Calendar calendar = Calendar.getInstance();
DateFormat format = DateFormat.getDateTimeInstance();
Gdx.app.log(TAG, format.format(calendar.getTime()));

View File

@ -2,6 +2,7 @@ package com.riiablo.server.mcp;
import com.google.flatbuffers.FlatBufferBuilder;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
@ -14,8 +15,10 @@ import com.badlogic.gdx.utils.BufferUtils;
import com.riiablo.net.GameSession;
import com.riiablo.net.packet.bnls.ConnectionAccepted;
import com.riiablo.net.packet.bnls.ConnectionClosed;
import com.riiablo.net.packet.mcp.CreateGame;
import com.riiablo.net.packet.mcp.ListGames;
import com.riiablo.net.packet.mcp.MCPData;
import com.riiablo.net.packet.mcp.Result;
import java.io.BufferedReader;
import java.io.IOException;
@ -73,6 +76,8 @@ public class MCP extends ApplicationAdapter {
@Override
public void create() {
Gdx.app.setLogLevel(Application.LOG_DEBUG);
final Calendar calendar = Calendar.getInstance();
DateFormat format = DateFormat.getDateTimeInstance();
Gdx.app.log(TAG, format.format(calendar.getTime()));
@ -169,9 +174,9 @@ public class MCP extends ApplicationAdapter {
case MCPData.ListGames:
ListGames(socket, packet);
break;
// case MCPData.LoginResponse:
// LoginResponse(socket, packet);
// break;
case MCPData.CreateGame:
CreateGame(socket, packet);
break;
default:
Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
}
@ -234,6 +239,35 @@ public class MCP extends ApplicationAdapter {
return false;
}
private boolean CreateGame(Socket socket, com.riiablo.net.packet.mcp.MCP packet) throws IOException {
CreateGame createGame = (CreateGame) packet.data(new CreateGame());
String gameName = createGame.gameName();
Gdx.app.debug(TAG, "Attempting to create " + gameName + " for " + socket.getRemoteAddress());
FlatBufferBuilder builder = new FlatBufferBuilder();
CreateGame.startCreateGame(builder);
if (sessions.containsKey(gameName)) {
CreateGame.addResult(builder, Result.ALREADY_EXISTS);
} else if (sessions.size() >= 4) {
CreateGame.addResult(builder, Result.SERVER_DOWN);
} else {
CreateGame.addResult(builder, Result.SUCCESS);
sessions.put(gameName, new GameSession(createGame));
Gdx.app.debug(TAG, "Created session " + gameName);
}
int createGameOffset = CreateGame.endCreateGame(builder);
int id = com.riiablo.net.packet.mcp.MCP.createMCP(builder, MCPData.CreateGame, createGameOffset);
builder.finish(id);
ByteBuffer data = builder.dataBuffer();
OutputStream out = socket.getOutputStream();
WritableByteChannel channel = Channels.newChannel(out);
channel.write(data);
Gdx.app.log(TAG, "returning game creation response...");
return false;
}
static String generateClientName() {
return String.format("Client-%08X", MathUtils.random(1, Integer.MAX_VALUE - 1));
}