Adding Game History.

This commit is contained in:
marcins78@gmail.com
2011-10-29 18:20:14 +00:00
parent 4ac860a3cb
commit 3820b52523
8 changed files with 123 additions and 20 deletions

View File

@@ -49,7 +49,7 @@ public class GameHistoryDAO {
try {
Connection connection = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = connection.prepareStatement("select winner, loser, win_reason, lose_reason, win_recording_id, lose_recording_id, start_date, end_date from game_history where winner=? or loser=? order by end_date deck limit ?, ?");
PreparedStatement statement = connection.prepareStatement("select winner, loser, win_reason, lose_reason, win_recording_id, lose_recording_id, start_date, end_date from game_history where winner=? or loser=? order by end_date desc limit ?, ?");
try {
statement.setString(1, player.getName());
statement.setString(2, player.getName());

View File

@@ -155,11 +155,10 @@ public class ServerResource {
};
}
@Path("/gameHistory/{playerId}")
@Path("/gameHistory")
@GET
@Produces(MediaType.APPLICATION_XML)
public Document getGameHistory(
@PathParam("playerId") String playerId,
@QueryParam("start") int start,
@QueryParam("count") int count,
@QueryParam("participantId") String participantId,
@@ -194,9 +193,9 @@ public class ServerResource {
historyEntry.setAttribute("winReason", gameHistoryEntry.getWinReason());
historyEntry.setAttribute("loseReason", gameHistoryEntry.getLoseReason());
if (gameHistoryEntry.getWinner().equals(participantId))
if (gameHistoryEntry.getWinner().equals(participantId) && gameHistoryEntry.getWinnerRecording() != null)
historyEntry.setAttribute("gameRecordingId", gameHistoryEntry.getWinnerRecording());
else if (gameHistoryEntry.getLoser().equals(participantId))
else if (gameHistoryEntry.getLoser().equals(participantId) && gameHistoryEntry.getLoserRecording() != null)
historyEntry.setAttribute("gameRecordingId", gameHistoryEntry.getLoserRecording());
historyEntry.setAttribute("startTime", String.valueOf(gameHistoryEntry.getStartTime().getTime()));

View File

@@ -82,6 +82,11 @@
padding: .2em .5em;
}
.gameHistory td, .gameHistory th {
padding: 2px;
border: 1px solid white;
}
</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">
@@ -90,6 +95,7 @@
<script type="text/javascript" src="js/jquery/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="js/inheritance.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/logging.js"></script>
<script type="text/javascript" src="js/chat.js"></script>
<script type="text/javascript" src="js/hallUi.js"></script>
@@ -132,6 +138,7 @@
<div id="tabs" width="100%" height="800">
<ul>
<li><a href="#gameHall">Game Hall</a></li>
<li><a href="includes/gameHistory.html">Game History</a></li>
<li><a href="includes/instruction.html">Manual</a></li>
<li><a href="includes/formatRules.html">Format Rules</a></li>
<li><a href="includes/leagueRules.html">League Rules</a></li>

View File

@@ -0,0 +1,9 @@
<script type="text/javascript" src="js/gameHistoryUi.js"></script>
<script type="text/javascript">
$(document).ready(
function () {
var ui = new GameHistoryUI("/gemp-lotr/server");
});
</script>
<div id="gameHistory"></div>

View File

@@ -0,0 +1,12 @@
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function formatToTwoDigits(no) {
if (no < 10)
return "0" + no;
else
return no;
}
function formatDate(date) {
return monthNames[date.getMonth()] + " " + date.getDate() + " " + formatToTwoDigits(date.getHours()) + ":" + formatToTwoDigits(date.getMinutes()) + ":" + formatToTwoDigits(date.getSeconds());
}

View File

@@ -7,12 +7,15 @@ var GempLotrCommunication = Class.extend({
this.failure = failure;
},
startGameSession: function(callback) {
getGameHistory: function(start, count, callback) {
$.ajax({
type: "GET",
url: this.url + "/game/" + getUrlParam("gameId"),
url: this.url + "/gameHistory",
cache: false,
data: { participantId: getUrlParam("participantId") },
data: {
start: start,
count: count,
participantId: getUrlParam("participantId") },
success: callback,
error: this.failure,
dataType: "xml"
@@ -28,6 +31,17 @@ var GempLotrCommunication = Class.extend({
dataType: "xml"
});
},
startGameSession: function(callback) {
$.ajax({
type: "GET",
url: this.url + "/game/" + getUrlParam("gameId"),
cache: false,
data: { participantId: getUrlParam("participantId") },
success: callback,
error: this.failure,
dataType: "xml"
});
},
updateGameState: function(callback) {
$.ajax({
type: "POST",

View File

@@ -0,0 +1,59 @@
var GameHistoryUI = Class.extend({
communication: null,
itemStart: 0,
pageSize: 20,
init: function(url) {
this.communication = new GempLotrCommunication(url,
function(xhr, ajaxOptions, thrownError) {
});
this.loadHistory();
},
loadHistory: function() {
var that = this;
this.communication.getGameHistory(this.itemStart, this.pageSize,
function(xml) {
that.loadedGameHistory(xml);
});
},
loadedGameHistory: function(xml) {
log(xml);
var root = xml.documentElement;
if (root.tagName == 'gameHistory') {
var historyTable = $("<table class='gameHistory'></table>");
historyTable.append("<tr><th>Winner</th><th>Loser</th><th>Win reason</th><th>Lose reason</th><th>Finished on</th><th>Replay link</th></tr>");
var entries = root.getElementsByTagName("historyEntry");
for (var i = 0; i < entries.length; i++) {
var historyEntry = entries[i];
var winner = historyEntry.getAttribute("winner");
var loser = historyEntry.getAttribute("loser");
var winReason = historyEntry.getAttribute("winReason");
var loseReason = historyEntry.getAttribute("loseReason");
var endTime = formatDate(new Date(parseInt(historyEntry.getAttribute("endTime"))));
var gameRecordingId = historyEntry.getAttribute("gameRecordingId");
var row = $("<tr></tr>");
row.append($("<td></td>").html(winner));
row.append($("<td></td>").html(loser));
row.append($("<td></td>").html(winReason));
row.append($("<td></td>").html(loseReason));
row.append($("<td></td>").html(endTime));
if (gameRecordingId != null) {
var link = "game.html?replayId=" + root.getAttribute("playerId") + "$" + gameRecordingId;
var linkElem = $("<a>replay game</a>");
linkElem.attr("href", link);
row.append($("<td></td>").html(linkElem));
} else {
row.append($("<td></td>").html("<i>not stored</i>"));
}
historyTable.append(row);
}
$("#gameHistory").append(historyTable);
}
}
});

View File

@@ -67,7 +67,7 @@ var GempLotrGameUI = Class.extend({
this.animations = new GameAnimations(this);
this.communication = new GempLotrCommunication("/gemp-lotr/server",
this.communication = new GempLotrCommunication(url,
function(xhr, ajaxOptions, thrownError) {
if (thrownError != "abort") {
if (xhr != null) {
@@ -642,20 +642,23 @@ var GempLotrGameUI = Class.extend({
}
if (this.allPlayerIds != null) {
var clocks = element.getElementsByTagName("clocks")[0].getElementsByTagName("clock");
for (var i = 0; i < clocks.length; i++) {
var clock = clocks[i];
var participantId = clock.getAttribute("participantId");
var index = this.getPlayerIndex(participantId);
var clocksXml = element.getElementsByTagName("clocks");
if (clocksXml.length > 0) {
var clocks = clocksXml[0].getElementsByTagName("clock");
for (var i = 0; i < clocks.length; i++) {
var clock = clocks[i];
var participantId = clock.getAttribute("participantId");
var index = this.getPlayerIndex(participantId);
var value = parseInt(clock.childNodes[0].nodeValue);
var value = parseInt(clock.childNodes[0].nodeValue);
var sign = (value < 0) ? "-" : "";
value = Math.abs(value);
var minutes = Math.floor(value / 60);
var seconds = value % 60;
var sign = (value < 0) ? "-" : "";
value = Math.abs(value);
var minutes = Math.floor(value / 60);
var seconds = value % 60;
$("#clock" + index).text(sign + minutes + ":" + ((seconds < 10) ? ("0" + seconds) : seconds));
$("#clock" + index).text(sign + minutes + ":" + ((seconds < 10) ? ("0" + seconds) : seconds));
}
}
}