Commit ede598ce authored by Winfried's avatar Winfried

added GameResult telegram

parent bd442106
......@@ -3,7 +3,7 @@ plugins {
id 'net.ltgt.errorprone' version '0.0.8'
}
version = '0.2.6'
version = '0.3.0'
group = 'gone.corewars.library'
apply plugin: 'java'
......
......@@ -3,6 +3,7 @@ package gone.lib.network;
import gone.lib.network.contracts.IClient;
import gone.lib.network.contracts.IClientCallback;
import gone.lib.network.contracts.ITcpListener;
import gone.lib.network.json.CwGameResultContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwMessageType;
import gone.lib.network.json.CwReadyContent;
......
......@@ -3,10 +3,7 @@ package gone.lib.network;
import gone.lib.network.contracts.IServer;
import gone.lib.network.contracts.IServerCallback;
import gone.lib.network.contracts.ITcpListener;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwMessageType;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.*;
import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
import gone.lib.network.tcp.TcpServerModule;
......@@ -62,6 +59,11 @@ public class CwServer implements IServer, ITcpListener {
server.sendMessage(sessionId, serializer.serializeGameStatusMessage(telegram));
}
@Override
public void sendGameResultTelegram(CwGameResultContent telegram, String sessionId) {
server.sendMessage(sessionId, serializer.serializeGameResultMessage(telegram));
}
@Override
public boolean getIsRunning() {
return running;
......
package gone.lib.network.contracts;
import gone.lib.network.json.CwGameResultContent;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwStartContent;
......@@ -13,4 +14,6 @@ public interface IClientCallback {
void receiveStartTelegram(CwStartContent telegram);
void receiveGameStatusTelegram(CwGameStatusContent telegram);
void receiveGameResultTelegram(CwGameResultContent telegram);
}
package gone.lib.network.contracts;
import gone.lib.network.json.CwGameResultContent;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwStartContent;
......@@ -20,5 +21,7 @@ public interface IServer {
void sendGameStatusTelegram(CwGameStatusContent telegram, String sessionId);
void sendGameResultTelegram(CwGameResultContent telegram, String sessionId);
boolean getIsRunning();
}
package gone.lib.network.json;
/**
* Created by winfr on 30.09.2016.
*/
public class CwGameResultContent {
public String state;
}
\ No newline at end of file
......@@ -9,5 +9,6 @@ public enum CwMessageType {
Ready,
Start,
GameStatus,
GameResult,
Unknown
}
......@@ -3,12 +3,7 @@ package gone.lib.network.serialization;
import gone.lib.common.ClientRole;
import gone.lib.common.NetworkConstants;
import gone.lib.common.RcStandard;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwMessageType;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.*;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
......@@ -38,6 +33,8 @@ public class IncomingMessagesInterpreter {
return CwMessageType.Ready;
} else if (object.getString("packageType").equals("status")) {
return CwMessageType.GameStatus;
} else if (object.getString("packageType").equals("finish")) {
return CwMessageType.GameResult;
}
return CwMessageType.Unknown;
......@@ -127,6 +124,16 @@ public class IncomingMessagesInterpreter {
return result;
}
public CwGameResultContent parseGameResultMessage(String json) {
JSONObject object = new JSONObject(json);
validate(object, "finishSchema.json");
CwGameResultContent result = new CwGameResultContent();
result.state = object.getJSONObject("content").getString("state");
return result;
}
private void validate(JSONObject object, String name) {
try (InputStream inputStream = getClass().getResourceAsStream(name)) {
JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream));
......
package gone.lib.network.serialization;
import gone.lib.common.NetworkConstants;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.*;
import org.json.JSONStringer;
......@@ -95,4 +91,17 @@ public class OutgoingMessagesSerializer {
.endObject().toString();
return result;
}
public String serializeGameResultMessage(CwGameResultContent content) {
String result = new JSONStringer()
.object()
.key("content")
.object()
.key("state").value(content.state)
.endObject()
.key("version").value(packetVersion)
.key("packageType").value("finish")
.endObject().toString();
return result;
}
}
\ No newline at end of file
{
"$schema": "http://json-schema.org/draft-04/schema#",
"packagetype": "must be finish",
"version":
{
"description": "version of standard",
"type": "integer"
},
"content":
{
"type": "object",
"properties":
{
"state": {"type": "string"}
},
"required": ["name"]
}
}
\ No newline at end of file
......@@ -14,11 +14,7 @@ import gone.lib.network.contracts.IClient;
import gone.lib.network.contracts.IClientCallback;
import gone.lib.network.contracts.IServer;
import gone.lib.network.contracts.IServerCallback;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.*;
import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
import gone.lib.network.tcp.TcpClientModule;
......@@ -107,6 +103,11 @@ public class ComplexMessageOverNetworkTest {
}
@Override
public void receiveGameResultTelegram(CwGameResultContent telegram) {
}
public boolean receivedLoginOk;
}
......
......@@ -5,11 +5,7 @@ import static org.junit.Assert.assertThat;
import gone.lib.common.ClientRole;
import gone.lib.common.RcStandard;
import gone.lib.network.json.CwGameStatusContent;
import gone.lib.network.json.CwLoginContent;
import gone.lib.network.json.CwLoginOkContent;
import gone.lib.network.json.CwReadyContent;
import gone.lib.network.json.CwStartContent;
import gone.lib.network.json.*;
import gone.lib.network.serialization.IncomingMessagesInterpreter;
import gone.lib.network.serialization.OutgoingMessagesSerializer;
......@@ -36,6 +32,7 @@ public class SerializationTest {
String parsedMessage = serializer.serializeGameStatusMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.GameStatus));
CwGameStatusContent newContent = interpreter.parseGameStatusMessage(parsedMessage);
assertThat(content.indexOfChangedCoreElement, is(newContent.indexOfChangedCoreElement));
......@@ -57,6 +54,7 @@ public class SerializationTest {
String parsedMessage = serializer.serializeLoginMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.Login));
CwLoginContent newContent = interpreter.parseLoginMessage(parsedMessage);
assertThat(content.name, is(newContent.name));
......@@ -77,6 +75,7 @@ public class SerializationTest {
String parsedMessage = serializer.serializeLoginOkMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.LoginOk));
CwLoginOkContent newContent = interpreter.parseLoginOkMessage(parsedMessage);
assertThat(content.lineLength, is(newContent.lineLength));
......@@ -98,6 +97,7 @@ public class SerializationTest {
String parsedMessage = serializer.serializeReadyMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.Ready));
CwReadyContent newContent = interpreter.parseReadyMessage(parsedMessage);
assertThat(newContent.warriorRedcode.size(), is(content.warriorRedcode.size()));
......@@ -123,6 +123,7 @@ public class SerializationTest {
String parsedMessage = serializer.serializeStartMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.Start));
CwStartContent newContent = interpreter.parseStartMessage(parsedMessage);
assertThat(content.players, is(newContent.players));
......@@ -134,4 +135,20 @@ public class SerializationTest {
assertThat(newContent.fullState.get(3), is(content.fullState.get(3)));
assertThat(newContent.fullState.get(1), is(content.fullState.get(1)));
}
@Test
public void testGameResultMessage() {
IncomingMessagesInterpreter interpreter = new IncomingMessagesInterpreter();
OutgoingMessagesSerializer serializer = new OutgoingMessagesSerializer();
CwGameResultContent content = new CwGameResultContent();
content.state = "draw";
String parsedMessage = serializer.serializeGameResultMessage(content);
assertThat(interpreter.getMessageType(parsedMessage), is(CwMessageType.GameResult));
CwGameResultContent newContent = interpreter.parseGameResultMessage(parsedMessage);
assertThat(content.state, is(newContent.state));
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment