Implement quick join

This commit is contained in:
Andrew Guibert 2018-04-05 14:30:16 -05:00
parent 4f474a30c9
commit 0c09785857
6 changed files with 49 additions and 10 deletions

View File

@ -3,18 +3,24 @@
<div id="login-title">
<h1>Liberty Bikes</h1>
</div>
<div id="login-form">
<div class="login-form">
<div class="form-item">
<label>Username</label>
<input type="text" id="username" name="username" autofocus>
</div>
</div>
<div class="button-bar">
<button type="button" (click)="quickJoin()">Quick Join</button>
</div>
<div></div>
<div class="login-form">
<div class="form-item">
<label>Code</label>
<input type="text" id="roundid" name="roundid" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
</div>
</div>
<div id="button-bar">
<button type="button" (click)="joinRound()">Login</button>
<div class="button-bar">
<button type="button" (click)="joinRound()">Join Round</button>
<button type="button" (click)="createRound()">Create Round</button>
<button id="hostButton" type="button" (click)="hostRound()" data-loading-text="Connecting..." autocomplete="off">Host Round</button>
</div>

View File

@ -42,7 +42,7 @@
padding-bottom: 50px;
}
#login-form {
.login-form {
width: 100%;
margin-bottom: 15px;
}
@ -85,7 +85,7 @@
text-transform: uppercase;
}
#button-bar {
.button-bar {
display: flex;
flex-flow: column;
width: 100%;
@ -93,7 +93,7 @@
padding-right: 10px;
}
#button-bar button {
.button-bar button {
width: 100%;
margin-top: 6px;
margin-bottom: 6px;

View File

@ -36,11 +36,23 @@ export class LoginComponent implements OnInit {
}
}
async quickJoin() {
// First get an unstarted round ID
let roundID = await this.http.post(`${environment.API_URL_GAME_ROUND}/available`, "", { responseType: 'text' }).toPromise();
// Then join the round
this.joinRoundById(roundID);
}
async joinRound() {
let roundID: string = $('#roundid').val();
this.joinRoundById(roundID);
}
async joinRound() {
async joinRoundById(roundID: string) {
let ngZone = this.ngZone;
let router = this.router;
let roundID: string = $('#roundid').val();
let username: string = $('#username').val();
roundID = roundID.toUpperCase().replace(/[^A-Z]/g, '');
let gameBoard = true;

View File

@ -399,6 +399,9 @@ public class GameRound implements Runnable {
// If there are any clients still connected to this game, keep sending heartbeats
if (round.clients.size() == 0) {
log("No clients remaining. Cancelling heartbeat.");
// Ensure that game state is closed off so that no other players can quick join while a round is marked for deletion
if (gameState == State.OPEN)
gameState = State.FINISHED;
return null;
}
return Date.from(Instant.now().plusSeconds(HEARTBEAT_INTERVAL_SEC));

View File

@ -2,6 +2,7 @@ package org.libertybikes.game.round.service;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@ -16,6 +17,7 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.libertybikes.game.core.GameRound;
import org.libertybikes.game.core.GameRound.State;
@Path("/")
@ApplicationScoped
@ -44,6 +46,19 @@ public class GameRoundService {
return p.id;
}
@POST
@Path("/available")
public String getAvailableRound() {
Optional<GameRound> availableRound = allRounds.values()
.stream()
.filter(r -> r.gameState == GameRound.State.OPEN)
.findFirst();
if (availableRound.isPresent())
return availableRound.get().id;
else
return createRound();
}
@GET
@Path("/{roundId}")
@Produces(MediaType.APPLICATION_JSON)
@ -71,7 +86,10 @@ public class GameRoundService {
return nextRound;
}
public void deleteRound(String roundId) {
public void deleteRound(GameRound round) {
String roundId = round.id;
if (round.gameState == State.OPEN)
round.gameState = State.FINISHED;
System.out.println("Scheduling round id=" + roundId + " for deletion in 5 minutes");
// Do not immediately delete rounds in order to give players/spectators time to move along to the next game
exec.schedule(() -> {

View File

@ -51,7 +51,7 @@ public class GameRoundWebsocket {
GameRound round = gameSvc.getRound(roundId);
if (round != null)
if (round.removeClient(session) == 0)
gameSvc.deleteRound(roundId);
gameSvc.deleteRound(round);
} catch (Exception e) {
e.printStackTrace();
}