Skip to content

Commit

Permalink
Do I really have Out-of-order execution in Java?
Browse files Browse the repository at this point in the history
Like for real? Is this even real life? What the fuck is actually going
on here? Like, okay, you're doing, what 6 different things within the
same millisecond? Nah fam. That shit doesn't slide. How are you gon be
here, connect to a URL, build a JSON Object, send a POST request,
receive a response for that request, and return that response, all in
THE SAME FUCKING MILLISECOND? I DON'T FUCKING BUY IT. Plus, how you gon
be here and tell me "aight fam I sent the request, but I haven't built the
body for it tho, just gimme a picosecond cuz xoxo, aight it's done bruv". Like. The
fuck?
  • Loading branch information
VictorIJnr committed Nov 28, 2018
1 parent 592e4cd commit 1b08d38
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
44 changes: 30 additions & 14 deletions Game.pde
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ enum PlayState {
class Game {
static final float ROLE_RATIO = 0.4;

int timer;
int pingTimer;

GameState myState = GameState.HOSTING;
PlayState myPlayState = PlayState.NIGHT;

Expand All @@ -23,14 +26,23 @@ class Game {
ArrayList<String> playerNames = new ArrayList<String>();

Game() {
// hostGame.startServer();
pingTimer = millis();
}

void update() {
}

void ping() {
//TODO
//Only ping players when setting up the game
//Once it's set up, no more players can join
pingPlayers();

//Ensuring the server is pinged approximately once a second
if (millis() - pingTimer > 1000) {
pingPlayers();
hostGame.ping();
pingTimer = millis();
}
}

void draw() {
Expand All @@ -50,7 +62,16 @@ class Game {
hostGame.draw();
break;
case ROLES:
//Allow players to see their roles
//Stay here for x seconds. Long enough for everyone to see their roles.
//Then switch the game state once everyone is familiar with their roles
drawText("Your roles have been assigned, you can find them on your device.");

//Waiting for 8 seconds to pass
if (timer < millis() - 8000) {
updateGameState(GameState.PLAYING);
updatePlayState(PlayState.NIGHT);
}
break;
case PLAYING:
myDay.draw();
Expand All @@ -59,14 +80,14 @@ class Game {
break;
}

hostGame.ping();

for (int i = 0; i < playerNames.size(); i++) {
int nameSize = TEXT_SIZE / 3;
int yMod = nameSize * 2 * i;
text(playerNames.get(i) + " joined the lobby.", width / 4, height * 0.1 + yMod);
}

ping();
System.out.println("Current millis: " + millis());
}

void drawText(String displayText) {
Expand Down Expand Up @@ -113,6 +134,8 @@ class Game {
myState = newState;
JSONObject reqBody = new JSONObject();
reqBody.setString("state", newState.name());

System.out.println("Built JSON " + millis());
hostGame.postData("admin/state", reqBody);
}

Expand All @@ -135,16 +158,9 @@ class Game {
hostGame.sendRequest("admin/start");
allocateRoles();

try {
//Allow players to see their roles
TimeUnit.SECONDS.sleep(8);
//Then switch the game state once everyone is familiar with their roles
updateGameState(GameState.PLAYING);
updatePlayState(PlayState.NIGHT);
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
updateGameState(GameState.ROLES);
timer = millis();
System.out.println("Timing...");
}

/*
Expand Down
11 changes: 9 additions & 2 deletions Host.pde
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Host {

Host() {
genGameRoom();
roleAlloc = new ActionButton(Action.START_GAME, new PVector(width / 2, height * 0.65));
roleAlloc = new ActionButton(Action.START_GAME, new PVector(width / 2, height * 0.75));

try {
gameURL = new URL(String.format("%s/admin", baseURL));
Expand Down Expand Up @@ -112,26 +112,33 @@ class Host {
ArrayList<String> postData(String endpoint, JSONObject data) {
ArrayList<String> response = new ArrayList<String>();
try {
StringBuilder bob = new StringBuilder();
URL postURL = new URL(String.format("%s/%s", baseURL, endpoint));
HttpURLConnection post = (HttpURLConnection) postURL.openConnection();
post.setRequestMethod("POST");
post.setRequestProperty("Content-Type", "application/json");
post.setDoOutput(true);

System.out.println("Sent data: " + data.toString());
PrintWriter postWriter = new PrintWriter(post.getOutputStream());
postWriter.write(data.toString());
postWriter.close();

String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(post.getInputStream()));
while ((inputLine = in.readLine()) != null)
while ((inputLine = in.readLine()) != null) {
response.add(inputLine);
bob.append(inputLine);
}
System.out.println("Received response." + millis());
in.close();
System.out.printf("Closed Stream.\n%d\nRespons:\n\t%s\n", millis(), bob.toString());
}
catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
System.out.println("Returning response.");
return response;
}

Expand Down
2 changes: 2 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ router.post("/:room/admin/state", function(req, res) {
roomOutput(req.params.room, `Updating game state to ${req.body.state}`);
updateStateFile(req.params.room, req.body);

//Just to make sure the Java doesn't hang waiting for a response
res.send("Done.");
});

/**
Expand Down

1 comment on commit 1b08d38

@VictorIJnr
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.