Skip to content

Commit

Permalink
changes from HP
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidmohsseni committed Sep 2, 2019
1 parent 52bafe5 commit 56f1e15
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/ai/AI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ai;

import team.koala.chillin.client.TurnbasedAI;
import ks.KSObject;
import ks.models.*;
import ks.commands.*;


public class AI extends TurnbasedAI<World> {
public class AI extends TurnbasedAI<World, KSObject> {

public AI(World world) {
super(world);
Expand Down
1 change: 0 additions & 1 deletion src/team/koala/chillin/client/AbstractAI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package team.koala.chillin.client;

import team.koala.chillin.client.helper.messages.BaseSnapshot;
import ks.KSObject;

import java.util.*;

Expand Down
40 changes: 30 additions & 10 deletions src/team/koala/chillin/client/BaseAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import team.koala.chillin.client.helper.messages.BaseSnapshot;
import team.koala.chillin.client.helper.parser.Parser;

import ks.KSObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class BaseAI<T extends KSObject> extends AbstractAI {
public class BaseAI<TWorld, TCommand> extends AbstractAI {

protected T world;
protected TWorld world;


BaseAI(T world) {
BaseAI(TWorld world) {
this.world = world;
}

@Override
public void update(BaseSnapshot snapshot) {
this.world.deserialize(Parser.getBytes(snapshot.getWorldPayload()));
// Deserialize world
try {
Method method = this.world.getClass().getMethod("deserialize", byte[].class);
method.invoke(this.world, Parser.getBytes(snapshot.getWorldPayload()));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {}
}

@Override
Expand All @@ -27,23 +33,37 @@ public boolean allowedToDecide() {
}


protected void _sendCommand(KSObject command, BaseCommand msg) {
protected void _sendCommand(TCommand command, BaseCommand msg) {
BaseCommand message;
if (msg == null)
message = new BaseCommand();
else
message = (BaseCommand) msg;

message.setType(command.name());
message.setPayload(Parser.getString(command.serialize()));
// Store command's name
try {
Method method = command.getClass().getMethod("name");
String name = (String) method.invoke(command);
message.setType(name);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {}

// Store command's serialization data
try {
Method method = command.getClass().getMethod("serialize");
byte[] data = (byte[]) method.invoke(command);
message.setPayload(Parser.getString(data));
} catch (NoSuchMethodException | SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {}

commandSendQueue.add(message);
}

protected void _sendCommand(KSObject command) {
protected void _sendCommand(TCommand command) {
_sendCommand(command, null);
}

public void sendCommand(KSObject command) {
public void sendCommand(TCommand command) {
if (allowedToDecide())
_sendCommand(command);
}
Expand Down
1 change: 0 additions & 1 deletion src/team/koala/chillin/client/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import team.koala.chillin.client.helper.logger.Logger;
import team.koala.chillin.client.helper.messages.*;
import ks.KSObject;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
Expand Down
31 changes: 31 additions & 0 deletions src/team/koala/chillin/client/KSObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package team.koala.chillin.client;

import java.lang.*;
import java.util.*;


public abstract class KSObject
{
public static final String nameStatic = "";
public abstract String name();
public abstract byte[] serialize();
public int deserialize(byte[] s) { return deserialize(s, 0); }
protected abstract int deserialize(byte[] s, int offset);


protected static List<Byte> b2B(byte[] bytes)
{
List<Byte> result = new ArrayList<>();
for (byte b : bytes)
result.add(b);
return result;
}

protected static byte[] B2b(List<Byte> bytes)
{
byte[] result = new byte[bytes.size()];
for(int i = 0; i < result.length; i++)
result[i] = bytes.get(i);
return result;
}
}
1 change: 0 additions & 1 deletion src/team/koala/chillin/client/Protocol.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team.koala.chillin.client;

import ks.KSObject;
import team.koala.chillin.client.helper.parser.Parser;


Expand Down
7 changes: 3 additions & 4 deletions src/team/koala/chillin/client/RealtimeAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import team.koala.chillin.client.helper.messages.BaseSnapshot;
import team.koala.chillin.client.helper.messages.RealtimeCommand;
import team.koala.chillin.client.helper.messages.RealtimeSnapshot;
import ks.KSObject;


public class RealtimeAI<T extends KSObject> extends BaseAI<T> {
public class RealtimeAI<TWorld, TCommand> extends BaseAI<TWorld, TCommand> {

protected Integer currentCycle;
protected Float cycleDuration;


public RealtimeAI(T world) {
public RealtimeAI(TWorld world) {
super(world);
}

Expand All @@ -31,7 +30,7 @@ public boolean allowedToDecide() {
}

@Override
protected void _sendCommand(KSObject command, BaseCommand msg) {
protected void _sendCommand(TCommand command, BaseCommand msg) {
RealtimeCommand message;
if (msg == null)
message = new RealtimeCommand();
Expand Down
7 changes: 3 additions & 4 deletions src/team/koala/chillin/client/TurnbasedAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import team.koala.chillin.client.helper.messages.BaseSnapshot;
import team.koala.chillin.client.helper.messages.TurnbasedCommand;
import team.koala.chillin.client.helper.messages.TurnbasedSnapshot;
import ks.KSObject;

import java.util.ArrayList;
import java.util.List;


public class TurnbasedAI<T extends KSObject> extends RealtimeAI<T> {
public class TurnbasedAI<TWorld, TCommand> extends RealtimeAI<TWorld, TCommand> {

protected List<String> turnAllowedSides;


public TurnbasedAI(T world) {
public TurnbasedAI(TWorld world) {
super(world);
turnAllowedSides = new ArrayList<>();
}
Expand All @@ -34,7 +33,7 @@ public boolean allowedToDecide() {
}

@Override
public void _sendCommand(KSObject command, BaseCommand msg) {
public void _sendCommand(TCommand command, BaseCommand msg) {
TurnbasedCommand message;
if (msg == null)
message = new TurnbasedCommand();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class AgentJoined extends KSObject
{
protected String sideName;
Expand Down
4 changes: 2 additions & 2 deletions src/team/koala/chillin/client/helper/messages/AgentLeft.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class AgentLeft extends KSObject
{
protected String sideName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;
import team.koala.chillin.client.KSObject;

public class BaseCommand extends KSObject
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class BaseSnapshot extends KSObject
{
protected String worldPayload;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class ClientJoined extends KSObject
{
protected Boolean joined;
Expand Down
4 changes: 2 additions & 2 deletions src/team/koala/chillin/client/helper/messages/EndGame.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class EndGame extends KSObject
{
protected String winnerSidename;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class JoinOfflineGame extends KSObject
{
protected String teamNickname;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class JoinOnlineGame extends KSObject
{
protected String token;
Expand Down
4 changes: 2 additions & 2 deletions src/team/koala/chillin/client/helper/messages/Message.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class Message extends KSObject
{
protected String type;
Expand Down
5 changes: 2 additions & 3 deletions src/team/koala/chillin/client/helper/messages/StartGame.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package team.koala.chillin.client.helper.messages;

import team.koala.chillin.client.KSObject;

import java.lang.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.Charset;

import ks.KSObject;

public class StartGame extends KSObject
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team.koala.chillin.client.helper.parser;

import team.koala.chillin.client.helper.messages.*;
import ks.KSObject;
import team.koala.chillin.client.KSObject;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
Expand Down
2 changes: 1 addition & 1 deletion src/team/koala/chillin/client/helper/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package team.koala.chillin.client.helper.parser;

import ks.KSObject;
import team.koala.chillin.client.KSObject;
import team.koala.chillin.client.helper.messages.Message;

import java.nio.charset.StandardCharsets;
Expand Down

0 comments on commit 56f1e15

Please sign in to comment.