Fixing some errors.

This commit is contained in:
marcins78@gmail.com
2011-09-16 01:36:56 +00:00
parent b28339160b
commit ad54acf55d
7 changed files with 162 additions and 38 deletions

View File

@@ -0,0 +1,7 @@
package com.gempukku.lotro.hall;
public class HallException extends Exception {
public HallException(String message) {
super(message);
}
}

View File

@@ -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<String> playerIds);
public void runningPlayerGame(String gameId);

View File

@@ -32,38 +32,47 @@ public class HallServer extends AbstractServer {
* @param playerId
* @return If table created, otherwise <code>false</code> (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 <code>false</code> (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<LotroGameParticipant> 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<String, AwaitingTable> copy = new HashMap<String, AwaitingTable>(_awaitingTables);
for (Map.Entry<String, AwaitingTable> table : copy.entrySet())
visitor.visitTable(table.getKey(), "Waiting", table.getValue().getPlayerNames());

View File

@@ -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<String> playerIds) {
Element table = _doc.createElement("table");

View File

@@ -10,6 +10,16 @@
body {
font-size: 12px;
}
.tableStatus {
text-align: center;
font-style: italic;
font-size: 120%;
}
.tablePlayer {
text-align: center;
}
</style>
<link rel="stylesheet" type="text/css" href="css/dark-hive/jquery-ui-1.8.16.custom.css">
<link rel="stylesheet" type="text/css" href="js/jquery/styles/jquery.spinnercontrol.css">

View File

@@ -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,

View File

@@ -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 = $("<button>Create table</button>");
$(createTableButton).button().click(
this.createTableButton = $("<button>Create table</button>");
$(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 = $("<button>Leave table</button>");
$(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 = $("<div></div>");
tableDiv.css({ display: "inline-block", width: "120px", height: "120px", margin: "5px", "background-color": "#333300", color: "#ffffff"});
tableDiv.append("<i>" + status + "</i><br/>");
tableDiv.append("<div class='tableStatus'>" + status + "</div>");
tableDiv.append("Players:<br/>");
for (var i = 0; i < players.length; i++)
tableDiv.append(players[i] + "<br/>");
tableDiv.append("<div class='tablePlayer'>" + players[i] + "</div>");
if (players.length < 2) {
var that = this;
var but = $("<button>Join game</button>");
$(but).button().click(
function(event) {
that.comm.joinTable(id);
});
tableDiv.append(but);
if (!waiting) {
var but = $("<button>Join table</button>");
$(but).button().click(
function(event) {
that.comm.joinTable(id, function(xml) {
that.processResponse(xml);
});
});
tableDiv.append(but);
}
}
return tableDiv;