Take away requeu button on mobile and replace it with prompt

This commit is contained in:
Andrew Guibert 2018-05-14 16:00:34 -05:00
parent 17cf9906d7
commit c08ec14c33
6 changed files with 62 additions and 72 deletions

View File

@ -2,10 +2,6 @@
<div id="titlebar" class="navbar">
<h1>Liberty Bikes</h1>
</div>
<div id="game-buttons" class="navbar">
<button type="button" (click)="startGame()">Start Game</button>
<button type="button" (click)="requeue()">Requeue</button>
</div>
<div id="controller">
<canvas id="dpad-canvas" width="800" height="800"></canvas>
</div>

View File

@ -32,6 +32,8 @@ export class ControlsComponent implements OnInit, OnDestroy {
leftPressed: boolean;
downPressed: boolean;
rightPressed: boolean;
currentDirection: string;
private preventScrolling = (evt: TouchEvent) => {
evt.preventDefault();
@ -54,6 +56,15 @@ export class ControlsComponent implements OnInit, OnDestroy {
if (json.keepAlive) {
this.gameService.send({ keepAlive: true });
}
if (json.gameStatus === 'FINISHED') {
if (confirm('Game is over, would you like to requeue?')) {
this.requeue();
} else {
this.ngZone.run(() => {
this.router.navigate(['/login']);
});
}
}
}, (err) => {
console.log(err);
});
@ -65,20 +76,6 @@ export class ControlsComponent implements OnInit, OnDestroy {
this.initCanvas();
window.onkeydown = (e: KeyboardEvent): any => {
const key = e.keyCode ? e.keyCode : e.which;
if (key === 38) {
this.moveUp();
} else if (key === 40) {
this.moveDown();
} else if (key === 37) {
this.moveLeft();
} else if (key === 39) {
this.moveRight();
}
};
// Make sure the view is at the top of the page so touch event coordinates line up
window.scrollTo(0, 0);
window.addEventListener('orientationchange', this.pageWasResized);
@ -253,31 +250,26 @@ export class ControlsComponent implements OnInit, OnDestroy {
}
touchStarted(evt: TouchEvent) {
console.log(evt);
if (evt.touches.length > 0) {
this.canvasPressed(evt.touches[0].pageX, evt.touches[0].pageY);
}
}
touchMoved(evt: TouchEvent) {
console.log(evt);
if (evt.changedTouches.length > 0) {
this.canvasPressed(evt.changedTouches[0].pageX, evt.changedTouches[0].pageY);
}
}
touchEnded(evt: TouchEvent) {
console.log(evt);
this.canvasReleased(evt.changedTouches[0].pageX, evt.changedTouches[0].pageY);
}
mouseDown(evt: MouseEvent) {
console.log(evt);
this.canvasPressed(evt.pageX, evt.pageY);
}
mouseUp(evt: MouseEvent) {
console.log(evt);
this.canvasReleased(evt.pageX, evt.pageY);
}
@ -289,28 +281,28 @@ export class ControlsComponent implements OnInit, OnDestroy {
if (this.upTriangle.containsPoint(location)) {
this.upPressed = true;
this.moveUp();
this.setDirection('UP');
} else {
this.upPressed = false;
}
if (this.leftTriangle.containsPoint(location)) {
this.leftPressed = true;
this.moveLeft();
this.setDirection('LEFT');
} else {
this.leftPressed = false;
}
if (this.downTriangle.containsPoint(location)) {
this.downPressed = true;
this.moveDown();
this.setDirection('DOWN');
} else {
this.downPressed = false;
}
if (this.rightTriangle.containsPoint(location)) {
this.rightPressed = true;
this.moveRight();
this.setDirection('RIGHT');
} else {
this.rightPressed = false;
}
@ -349,8 +341,8 @@ export class ControlsComponent implements OnInit, OnDestroy {
}
requeue() {
let partyId = sessionStorage.getItem('partyId');
if (sessionStorage.getItem('isSpectator') === 'true' || partyId === null) {
let partyId: string = sessionStorage.getItem('partyId');
if (partyId === null) {
this.gameService.send({ message: 'GAME_REQUEUE' });
} else {
let queueCallback = new EventSource(`${environment.API_URL_PARTY}/${partyId}/queue`);
@ -377,20 +369,11 @@ export class ControlsComponent implements OnInit, OnDestroy {
}
}
moveUp() {
this.gameService.send({ direction: 'UP' });
}
moveDown() {
this.gameService.send({ direction: 'DOWN' });
}
moveLeft() {
this.gameService.send({ direction: 'LEFT' });
}
moveRight() {
this.gameService.send({ direction: 'RIGHT' });
setDirection(newDir: string) {
if (this.currentDirection !== null && this.currentDirection !== newDir) {
this.currentDirection = newDir;
this.gameService.send({ direction: `${newDir}` });
}
}
ngOnDestroy() {

View File

@ -1,7 +1,7 @@
package org.libertybikes.game.core;
import static org.libertybikes.game.round.service.GameRoundWebsocket.sendTextToClient;
import static org.libertybikes.game.round.service.GameRoundWebsocket.sendTextToClients;
import static org.libertybikes.game.round.service.GameRoundWebsocket.sendToClients;
import java.time.Instant;
import java.util.ArrayDeque;
@ -115,7 +115,7 @@ public class GameRound implements Runnable {
}
}
if (!isPhone)
sendTextToClient(s, jsonb.toJson(new OutboundMessage.AwaitPlayersCountdown(lobbyCountdown.roundStartCountdown)));
sendTextToClient(s, new OutboundMessage.AwaitPlayersCountdown(lobbyCountdown.roundStartCountdown));
}
public void updatePlayerDirection(Session playerSession, InboundMessage msg) {
@ -170,8 +170,8 @@ public class GameRound implements Runnable {
public void addSpectator(Session s) {
log("A spectator has joined.");
clients.put(s, new Client(s));
sendTextToClient(s, jsonb.toJson(new OutboundMessage.PlayerList(getPlayers())));
sendTextToClient(s, jsonb.toJson(board));
sendTextToClient(s, new OutboundMessage.PlayerList(getPlayers()));
sendTextToClient(s, board);
beginHeartbeat();
beginLobbyCountdown(s, false);
}
@ -189,7 +189,7 @@ public class GameRound implements Runnable {
log("Initiating heartbeat to clients");
exec.schedule(() -> {
log("Sending heartbeat to " + clients.size() + " clients");
sendTextToClients(clients.keySet(), jsonb.toJson(new OutboundMessage.Heartbeat()));
sendToClients(clients.keySet(), new OutboundMessage.Heartbeat());
}, new HeartbeatTrigger());
} catch (NamingException e) {
log("Unable to obtain executor service reference");
@ -243,8 +243,10 @@ public class GameRound implements Runnable {
int numGames = runningGames.incrementAndGet();
if (numGames > 3)
log("WARNING: There are currently " + numGames + " game instances running.");
long nextTick = System.currentTimeMillis() + GAME_TICK_SPEED;
while (gameRunning.get()) {
delay(GAME_TICK_SPEED);
delayTo(nextTick);
nextTick += GAME_TICK_SPEED;
gameTick();
if (ticksFromGameEnd > DELAY_BETWEEN_ROUNDS)
gameRunning.set(false); // end the game if nobody can move anymore
@ -279,32 +281,30 @@ public class GameRound implements Runnable {
board.broadcastToAI();
boolean boardUpdated = board.moveObjects();
boolean death = false;
// Move all living players forward 1
boolean playerStatusChange = false;
boolean playerDied = false;
boolean playersMoved = false;
// Move all living players forward 1
for (Player p : getPlayers()) {
if (p.isAlive()) {
if (p.movePlayer(board.board)) {
playersMoved = true;
} else {
death = true;
playerDied = true;
playerRanks.push(p);
}
}
}
if (death) {
if (playerDied) {
checkForWinner();
playerStatusChange = true;
broadcastPlayerList();
}
if (playersMoved || boardUpdated)
broadcastGameBoard();
}
if (playerStatusChange)
broadcastPlayerList();
private void delayTo(long wakeUpTime) {
delay(wakeUpTime - System.currentTimeMillis());
}
private void delay(long ms) {
@ -325,15 +325,15 @@ public class GameRound implements Runnable {
}
private void broadcastTimeUntilGameStarts(int time) {
sendTextToClients(getNonMobileSessions(), jsonb.toJson(new OutboundMessage.AwaitPlayersCountdown(time)));
sendToClients(getNonMobileSessions(), new OutboundMessage.AwaitPlayersCountdown(time));
}
private void broadcastGameBoard() {
sendTextToClients(getNonMobileSessions(), jsonb.toJson(board));
sendToClients(getNonMobileSessions(), board);
}
private void broadcastPlayerList() {
sendTextToClients(getNonMobileSessions(), jsonb.toJson(new OutboundMessage.PlayerList(getPlayers())));
sendToClients(getNonMobileSessions(), new OutboundMessage.PlayerList(getPlayers()));
}
private void checkForWinner() {
@ -381,7 +381,7 @@ public class GameRound implements Runnable {
// Issue a countdown to all of the clients
gameState = State.STARTING;
sendTextToClients(clients.keySet(), jsonb.toJson(new OutboundMessage.StartingCountdown(STARTING_COUNTDOWN)));
sendToClients(clients.keySet(), new OutboundMessage.StartingCountdown(STARTING_COUNTDOWN));
delay(TimeUnit.SECONDS.toMillis(STARTING_COUNTDOWN));
paused.set(false);
@ -428,7 +428,8 @@ public class GameRound implements Runnable {
for (Client c : clients.values())
if (c.autoRequeue)
GameRoundWebsocket.requeueClient(gameSvc, this, c.session);
else
sendTextToClient(c.session, new OutboundMessage.GameStatus(State.FINISHED));
}
private void log(String msg) {

View File

@ -21,6 +21,15 @@ public class OutboundMessage {
}
}
public static class GameStatus {
@JsonbProperty("gameStatus")
public final String gameStatus;
public GameStatus(GameRound.State status) {
this.gameStatus = status.toString();
}
}
public static class RequeueGame {
@JsonbProperty("requeue")
public final String roundId;

View File

@ -16,8 +16,8 @@ import org.libertybikes.game.round.service.GameRoundService;
public class Party {
private static final Random r = new Random();
// Chars that will be used to generate party IDs (0-9 A-Z minus most commonly used chars in words)
private static final char[] SAFE_CHARS = "346789BCDGHJKMPQRTVWXY".toCharArray();
// Chars that will be used to generate party IDs (A-Z minus most commonly used chars in words)
private static final char[] SAFE_CHARS = "BCDGHJKMPQRTVWXY".toCharArray();
@Inject
@JsonbTransient

View File

@ -95,24 +95,25 @@ public class GameRoundWebsocket {
GameRound nextGame = gameSvc.requeue(oldRound, oldRound.isPlayer(s));
if (nextGame == null)
return;
String requeueMsg = jsonb.toJson(new OutboundMessage.RequeueGame(nextGame.id));
sendTextToClient(s, requeueMsg);
sendTextToClient(s, new OutboundMessage.RequeueGame(nextGame.id));
}
public static void sendTextToClient(Session client, String message) {
public static void sendTextToClient(Session client, Object message) {
if (client != null) {
String msg = message instanceof String ? (String) message : jsonb.toJson(message);
try {
client.getBasicRemote().sendText(message);
client.getBasicRemote().sendText(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void sendTextToClients(Set<Session> clients, String message) {
public static void sendToClients(Set<Session> clients, Object message) {
String msg = message instanceof String ? (String) message : jsonb.toJson(message);
// System.out.println("Sending " + clients.size() + " clients the message: " + message);
for (Session client : clients)
sendTextToClient(client, message);
sendTextToClient(client, msg);
}
private static void log(String roundId, String msg) {