Skip to content

Commit

Permalink
add java support (OMG WHY!?!)
Browse files Browse the repository at this point in the history
  • Loading branch information
firemark committed Oct 24, 2018
1 parent 6a26c6e commit 3aa6214
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@ venv/
__pycache__/
*.py[cod]
*$py.class
*.class

# C extensions
*.so
Expand Down
28 changes: 24 additions & 4 deletions README.md
Expand Up @@ -94,6 +94,7 @@ First, copy example code to main dir with game. Example codes are for:
* [python](battle_city/examples/python/radom.py)
* [nodejs](battle_city/examples/nodejs/random.js)
* [ruby](battle_city/examples/ruby/random.rb)
* [java](battle_city/examples/java/Game.java)

(more examples are [here](battle_city/examples/))

Expand All @@ -102,15 +103,34 @@ remember about virtualenv or pipenv!
* https://docs.python.org/3/tutorial/venv.html
* https://pipenv.readthedocs.io/en/latest/

#### For linux/mingw/osx/wsl
### For linux/mingw/osx/wsl

#### Python
```sh
./run_game.sh --cmd-p1 "python copied_client.py"
```

#### NodeJs
```sh
./run_game.sh --cmd-p1 "nodejs copied_client.js"
```

#### Ruby
```sh
./run_game.sh --cmd-p1 "ruby copied_client.rb"
```

#### Java
oh my god, what a terrible language! You need a json-simple package
```sh
./run_game.sh --cmd-p1 "python|nodejs|ruby copied_client.prefix"
javac -cp /usr/share/java/json-simple.jar Game.java -d .
./run_game.sh --cmd-p1 "java -cp /usr/share/java/json-simple.jar:. Game"
```

The color of your tank will be yellow.


#### maybe windows?
### maybe windows?
Omg why?

You have four options:
Expand All @@ -123,7 +143,7 @@ You have four options:
* `python client_a.py`
* `python -m battle_city.examples.random`

#### tmux version - more hackerable (with split screens in terminal)!
### tmux version - more hackerable (with split screens in terminal)!
```
./run_game_tmux.sh --cmd-p1 "python copied_client.py"
```
Expand Down
79 changes: 79 additions & 0 deletions battle_city/examples/java/Game.java
@@ -0,0 +1,79 @@
import java.io.*;
import java.net.Socket;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class Game {
private OutputStream writer;
private boolean start;
private boolean firstTick;
private long timestamp;

private Game(Socket socket) throws IOException {
start = false;
firstTick = false;
timestamp = 0;

writer = socket.getOutputStream();
}

public void receive(JSONObject data) {
// java with not external libs doesnt have async code
// so I must check timestamp and run tick
long newTimestamp = System.currentTimeMillis();
if (timestamp - newTimestamp > 250) {
timestamp = newTimestamp;
tick();
}

System.out.println(data.toJSONString());
}

public void tick() {
if (!firstTick) {
JSONObject obj = new JSONObject();
obj.put("action", "greet");
obj.put("name", "JAVA");
send(obj);
firstTick = true;
}
if (start) {
// do sth
}
}

public void send(JSONObject obj) {
try {
writer.write(obj.toJSONString().getBytes());
writer.write('\n');
writer.flush();
} catch (IOException e) {}
}

public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8888);
try {
Game game = new Game(socket);
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream())
);
System.out.println("\033[1mCONNECTED!\033[0m");

game.tick();

JSONParser parser = new JSONParser();
String line;
while((line = reader.readLine()) != null) {
try {
JSONObject obj = (JSONObject) parser.parse(line);
game.receive(obj);
} catch (ParseException e) {}
}
} catch (IOException e) {
throw e;
} finally {
socket.close();
}
}
}

0 comments on commit 3aa6214

Please sign in to comment.