From ad54acf55d09dae63c225e7a16988e508d240b1e Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Fri, 16 Sep 2011 01:36:56 +0000 Subject: [PATCH] Fixing some errors. --- .../gempukku/lotro/hall/HallException.java | 7 ++ .../gempukku/lotro/hall/HallInfoVisitor.java | 2 + .../com/gempukku/lotro/hall/HallServer.java | 51 +++++++++----- .../gempukku/lotro/server/ServerResource.java | 43 ++++++++++-- .../gemp-lotr-web/src/main/webapp/index.html | 10 +++ .../src/main/webapp/js/communication.js | 17 ++++- .../src/main/webapp/js/hallUi.js | 70 +++++++++++++++---- 7 files changed, 162 insertions(+), 38 deletions(-) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallException.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallException.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallException.java new file mode 100644 index 000000000..a41f66bee --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallException.java @@ -0,0 +1,7 @@ +package com.gempukku.lotro.hall; + +public class HallException extends Exception { + public HallException(String message) { + super(message); + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java index 16ab95d2e..152d0f989 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallInfoVisitor.java @@ -3,6 +3,8 @@ package com.gempukku.lotro.hall; import java.util.Set; public interface HallInfoVisitor { + public void playerIsWaiting(boolean waiting); + public void visitTable(String tableId, String tableStatus, Set playerIds); public void runningPlayerGame(String gameId); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java index 96f2a1ada..e1ca4581a 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/hall/HallServer.java @@ -32,38 +32,47 @@ public class HallServer extends AbstractServer { * @param playerId * @return If table created, otherwise false (if the user already is sitting at a table or playing). */ - public synchronized boolean createNewTable(String playerId) { - if (isPlayerBusy(playerId)) - return false; + public synchronized void createNewTable(String playerId) throws HallException { + LotroDeck lotroDeck = validateUserAndDeck(playerId); String tableId = String.valueOf(_nextTableId++); AwaitingTable table = new AwaitingTable(); _awaitingTables.put(tableId, table); - joinTableAsPlayer(tableId, playerId); - return true; + joinTableInternal(tableId, playerId, table, lotroDeck); + } + + private LotroDeck validateUserAndDeck(String playerId) throws HallException { + if (isPlayerBusy(playerId)) + throw new HallException("You can't play more than one game at a time or wait at more than one table"); + + LotroDeck lotroDeck = _lotroServer.getParticipantDeck(playerId); + if (lotroDeck == null) + throw new HallException("You don't have a deck registered yet"); + + boolean valid = _lotroFormat.validateDeck(lotroDeck); + if (!valid) + throw new HallException("Your registered deck is not valid for this format"); + return lotroDeck; } /** * @param playerId * @return If table joined, otherwise false (if the user already is sitting at a table or playing). */ - public synchronized boolean joinTableAsPlayer(String tableId, String playerId) { - if (isPlayerBusy(playerId)) - return false; - + public synchronized boolean joinTableAsPlayer(String tableId, String playerId) throws HallException { AwaitingTable awaitingTable = _awaitingTables.get(tableId); if (awaitingTable == null) - return false; + throw new HallException("Table is already taken or was removed"); - LotroDeck lotroDeck = _lotroServer.getParticipantDeck(playerId); - if (lotroDeck == null) - return false; + LotroDeck lotroDeck = validateUserAndDeck(playerId); - boolean valid = _lotroFormat.validateDeck(lotroDeck); - if (!valid) - return false; + joinTableInternal(tableId, playerId, awaitingTable, lotroDeck); + return true; + } + + private void joinTableInternal(String tableId, String playerId, AwaitingTable awaitingTable, LotroDeck lotroDeck) { boolean tableFull = awaitingTable.addPlayer(new LotroGameParticipant(playerId, lotroDeck)); if (tableFull) { Set players = awaitingTable.getPlayers(); @@ -74,7 +83,6 @@ public class HallServer extends AbstractServer { _runningTables.put(tableId, gameId); _awaitingTables.remove(tableId); } - return true; } public synchronized void leaveAwaitingTables(String playerId) { @@ -88,7 +96,16 @@ public class HallServer extends AbstractServer { } } + private boolean isPlayerWaiting(String playerId) { + for (AwaitingTable awaitingTable : _awaitingTables.values()) + if (awaitingTable.hasPlayer(playerId)) + return true; + return false; + } + public void processTables(String participantId, HallInfoVisitor visitor) { + visitor.playerIsWaiting(isPlayerWaiting(participantId)); + Map copy = new HashMap(_awaitingTables); for (Map.Entry table : copy.entrySet()) visitor.visitTable(table.getKey(), "Waiting", table.getValue().getPlayerNames()); diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java index c19ac4b90..c45034d1a 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -8,6 +8,7 @@ import com.gempukku.lotro.db.PlayerDAO; import com.gempukku.lotro.db.vo.Deck; import com.gempukku.lotro.db.vo.Player; import com.gempukku.lotro.game.*; +import com.gempukku.lotro.hall.HallException; import com.gempukku.lotro.hall.HallInfoVisitor; import com.gempukku.lotro.hall.HallServer; import com.gempukku.lotro.logic.decisions.AwaitingDecision; @@ -397,17 +398,46 @@ public class ServerResource { @Path("/hall/{table}") @POST - public void joinTable( + public Document joinTable( @PathParam("table") String tableId, @FormParam("participantId") String participantId) throws ParserConfigurationException { - boolean joined = _hallServer.joinTableAsPlayer(tableId, participantId); + try { + _hallServer.joinTableAsPlayer(tableId, participantId); + return null; + } catch (HallException e) { + return marshalException(e); + } + } + + private Document marshalException(HallException e) throws ParserConfigurationException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + + Document doc = documentBuilder.newDocument(); + + Element error = doc.createElement("error"); + error.setAttribute("message", e.getMessage()); + doc.appendChild(error); + return doc; } @Path("/hall") @POST - public void createTable( + public Document createTable( @FormParam("participantId") String participantId) throws ParserConfigurationException { - boolean created = _hallServer.createNewTable(participantId); + try { + _hallServer.createNewTable(participantId); + return null; + } catch (HallException e) { + return marshalException(e); + } + } + + @Path("/hall/leave") + @POST + public void leaveTable( + @FormParam("participantId") String participantId) throws ParserConfigurationException { + _hallServer.leaveAwaitingTables(participantId); } private class SerializeHallInfoVisitor implements HallInfoVisitor { @@ -419,6 +449,11 @@ public class ServerResource { _hall = hall; } + @Override + public void playerIsWaiting(boolean waiting) { + _hall.setAttribute("waiting", String.valueOf(waiting)); + } + @Override public void visitTable(String tableId, String tableStatus, Set playerIds) { Element table = _doc.createElement("table"); diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html b/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html index a9487a19a..0a47b655c 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html @@ -10,6 +10,16 @@ body { font-size: 12px; } + + .tableStatus { + text-align: center; + font-style: italic; + font-size: 120%; + } + + .tablePlayer { + text-align: center; + } diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js index f058e809f..01e79ba54 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js @@ -144,22 +144,35 @@ var GempLotrCommunication = Class.extend({ dataType: "xml" }); }, - joinTable: function(tableId) { + joinTable: function(tableId, callback) { $.ajax({ type: "POST", url: this.url + "/hall/" + tableId, cache: false, data: { participantId: getUrlParam("participantId")}, + success: callback, error: this.failure, dataType: "xml" }); }, - createTable: function() { + createTable: function(callback) { $.ajax({ type: "POST", url: this.url + "/hall", cache: false, + data: { + participantId: getUrlParam("participantId")}, + success: callback, + error: this.failure, + dataType: "xml" + }); + }, + leaveTable: function() { + $.ajax({ + type: "POST", + url: this.url + "/hall/leave", + cache: false, data: { participantId: getUrlParam("participantId")}, error: this.failure, diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/hallUi.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/hallUi.js index 488374db1..cd0a8eb77 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/hallUi.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/hallUi.js @@ -1,6 +1,8 @@ var GempLotrHallUI = Class.extend({ div : null, comm: null, + createTableButton: null, + leaveTableButton: null, tablesDiv: null, @@ -22,13 +24,27 @@ var GempLotrHallUI = Class.extend({ var that = this; - var createTableButton = $(""); - $(createTableButton).button().click( + this.createTableButton = $(""); + $(this.createTableButton).button().click( function() { - that.comm.createTable(); + that.createTableButton.hide(); + that.comm.createTable(function(xml) { + that.processResponse(xml); + }); }); + this.createTableButton.hide(); - buttonsDiv.append(createTableButton); + buttonsDiv.append(this.createTableButton); + + this.leaveTableButton = $(""); + $(this.leaveTableButton).button().click( + function() { + that.leaveTableButton.hide(); + that.comm.leaveTable(); + }); + this.leaveTableButton.hide(); + + buttonsDiv.append(this.leaveTableButton); this.div.append(buttonsDiv); @@ -43,11 +59,23 @@ var GempLotrHallUI = Class.extend({ }); }, + processResponse: function(xml) { + if (xml != null) { + var root = xml.documentElement; + if (root.tagName == "error") { + var message = root.getAttribute("message"); + alert(message); + } + } + }, + processHall: function(xml) { var root = xml.documentElement; - if (root.tagName == 'hall') { + if (root.tagName == "hall") { this.tablesDiv.html(""); + var waiting = root.getAttribute("waiting") == "true"; + var tables = root.getElementsByTagName("table"); for (var i = 0; i < tables.length; i++) { var table = tables[i]; @@ -58,7 +86,7 @@ var GempLotrHallUI = Class.extend({ if (playersAttr.length > 0) players = playersAttr.split(","); - var tableDiv = this.createTableDiv(id, status, players); + var tableDiv = this.createTableDiv(id, status, players, waiting); this.tablesDiv.append(tableDiv); } @@ -72,6 +100,14 @@ var GempLotrHallUI = Class.extend({ location.href = "/gemp-lotr/game.html?gameId=" + waitingGameId + participantIdAppend; } + if (waiting) { + this.createTableButton.hide(); + this.leaveTableButton.show(); + } else { + this.createTableButton.show(); + this.leaveTableButton.hide(); + } + var that = this; setTimeout(function() { @@ -80,23 +116,27 @@ var GempLotrHallUI = Class.extend({ } }, - createTableDiv: function(id, status, players) { + createTableDiv: function(id, status, players, waiting) { var tableDiv = $("
"); tableDiv.css({ display: "inline-block", width: "120px", height: "120px", margin: "5px", "background-color": "#333300", color: "#ffffff"}); - tableDiv.append("" + status + "
"); + tableDiv.append("
" + status + "
"); tableDiv.append("Players:
"); for (var i = 0; i < players.length; i++) - tableDiv.append(players[i] + "
"); + tableDiv.append("
" + players[i] + "
"); if (players.length < 2) { var that = this; - var but = $(""); - $(but).button().click( - function(event) { - that.comm.joinTable(id); - }); - tableDiv.append(but); + if (!waiting) { + var but = $(""); + $(but).button().click( + function(event) { + that.comm.joinTable(id, function(xml) { + that.processResponse(xml); + }); + }); + tableDiv.append(but); + } } return tableDiv;