Leagerboards for leagues.

This commit is contained in:
marcins78@gmail.com
2011-12-13 20:07:53 +00:00
parent 1df916800e
commit 5ff36ade68
16 changed files with 346 additions and 49 deletions

View File

@@ -74,7 +74,7 @@ public class LeagueDAO {
private void loadActiveLeagues(int currentTime) throws SQLException, IOException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select id, name, type, collection from league where start<=? and end>=?");
PreparedStatement statement = conn.prepareStatement("select id, name, type, collection, start, end from league where start<=? and end>=?");
try {
statement.setInt(1, currentTime);
statement.setInt(2, currentTime);
@@ -84,13 +84,15 @@ public class LeagueDAO {
int id = rs.getInt(1);
String name = rs.getString(2);
String type = rs.getString(3);
int start = rs.getInt(5);
int end = rs.getInt(6);
Blob baseCollection = rs.getBlob(4);
try {
final InputStream inputStream = baseCollection.getBinaryStream();
try {
MutableCardCollection collection = _serializer.deserializeCollection(inputStream);
_activeLeagues.add(new League(id, type, name, collection));
_activeLeagues.add(new League(id, type, name, collection, start, end));
} finally {
inputStream.close();
}

View File

@@ -2,7 +2,7 @@ package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import com.gempukku.lotro.db.vo.LeagueSeason;
import com.gempukku.lotro.db.vo.LeagueSerie;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -17,7 +17,7 @@ public class LeagueMatchDAO {
_dbAccess = dbAccess;
}
public Collection<LeagueMatch> getPlayerMatchesPlayedOn(League league, LeagueSeason leagueSeason, String player) {
public Collection<LeagueMatch> getPlayerMatchesPlayedOn(League league, LeagueSerie leagueSeason, String player) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
@@ -51,7 +51,7 @@ public class LeagueMatchDAO {
}
}
public void addPlayedMatch(League league, LeagueSeason leagueSeason, String winner, String loser) {
public void addPlayedMatch(League league, LeagueSerie leagueSeason, String winner, String loser) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {

View File

@@ -1,11 +1,14 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSeason;
import com.gempukku.lotro.db.vo.LeagueSerie;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
public class LeaguePointsDAO {
private DbAccess _dbAccess;
@@ -14,7 +17,7 @@ public class LeaguePointsDAO {
_dbAccess = dbAccess;
}
public void addPoints(League league, LeagueSeason season, String playerName, int points) {
public void addPoints(League league, LeagueSerie season, String playerName, int points) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
@@ -35,4 +38,65 @@ public class LeaguePointsDAO {
throw new RuntimeException(exp);
}
}
public Map<String, Integer> getLeagueStandings(League league) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points) from league_points where league_type=? order by 2 desc");
try {
statement.setString(1, league.getType());
ResultSet rs = statement.executeQuery();
try {
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
while (rs.next()) {
String playerName = rs.getString(1);
int sumPoints = rs.getInt(2);
result.put(playerName, sumPoints);
}
return result;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
public Map<String, Integer> getSerieStandings(League league, LeagueSerie serie) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select player_name, sum(points) from league_points where league_type=? and season_type=? group by player_name order by 2 desc");
try {
statement.setString(1, league.getType());
statement.setString(2, serie.getType());
ResultSet rs = statement.executeQuery();
try {
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
while (rs.next()) {
String playerName = rs.getString(1);
int sumPoints = rs.getInt(2);
result.put(playerName, sumPoints);
}
return result;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
}

View File

@@ -1,21 +1,23 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSeason;
import com.gempukku.lotro.db.vo.LeagueSerie;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class LeagueSeasonDAO {
public class LeagueSerieDAO {
private DbAccess _dbAccess;
public LeagueSeasonDAO(DbAccess dbAccess) {
public LeagueSerieDAO(DbAccess dbAccess) {
_dbAccess = dbAccess;
}
public void addSeason(String leagueType, String seasonType, int start, int end, int maxMatches) {
public void addSerie(String leagueType, String seasonType, int start, int end, int maxMatches) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
@@ -38,11 +40,43 @@ public class LeagueSeasonDAO {
}
}
public LeagueSeason getSeasonForLeague(League league, int inTime) {
public List<LeagueSerie> getSeriesForLeague(League league) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select season_type, max_matches from league_season where league_type=? and start<=? and end>=?");
PreparedStatement statement = conn.prepareStatement("select season_type, max_matches, start, end from league_season where league_type=? order by start asc");
try {
statement.setString(1, league.getType());
ResultSet rs = statement.executeQuery();
try {
List<LeagueSerie> seasons = new LinkedList<LeagueSerie>();
while (rs.next()) {
String type = rs.getString(1);
int maxMatches = rs.getInt(2);
int start = rs.getInt(3);
int end = rs.getInt(4);
seasons.add(new LeagueSerie(type, maxMatches, start, end));
}
return seasons;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
public LeagueSerie getSerieForLeague(League league, int inTime) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select season_type, max_matches, start, end from league_season where league_type=? and start<=? and end>=?");
try {
statement.setString(1, league.getType());
statement.setInt(2, inTime);
@@ -52,7 +86,9 @@ public class LeagueSeasonDAO {
if (rs.next()) {
String type = rs.getString(1);
int maxMatches = rs.getInt(2);
return new LeagueSeason(type, maxMatches);
int start = rs.getInt(3);
int end = rs.getInt(4);
return new LeagueSerie(type, maxMatches, start, end);
}
return null;
} finally {

View File

@@ -7,12 +7,16 @@ public class League {
private String _type;
private String _name;
private MutableCardCollection _baseCollection;
private int _start;
private int _end;
public League(int id, String type, String name, MutableCardCollection baseCollection) {
public League(int id, String type, String name, MutableCardCollection baseCollection, int start, int end) {
_id = id;
_type = type;
_name = name;
_baseCollection = baseCollection;
_start = start;
_end = end;
}
public int getId() {
@@ -30,4 +34,12 @@ public class League {
public MutableCardCollection getBaseCollection() {
return _baseCollection;
}
public int getEnd() {
return _end;
}
public int getStart() {
return _start;
}
}

View File

@@ -1,19 +0,0 @@
package com.gempukku.lotro.db.vo;
public class LeagueSeason {
private String _type;
private int _maxMatches;
public LeagueSeason(String type, int maxMatches) {
_type = type;
_maxMatches = maxMatches;
}
public int getMaxMatches() {
return _maxMatches;
}
public String getType() {
return _type;
}
}

View File

@@ -0,0 +1,31 @@
package com.gempukku.lotro.db.vo;
public class LeagueSerie {
private String _type;
private int _maxMatches;
private int _start;
private int _end;
public LeagueSerie(String type, int maxMatches, int start, int end) {
_type = type;
_maxMatches = maxMatches;
_start = start;
_end = end;
}
public int getMaxMatches() {
return _maxMatches;
}
public String getType() {
return _type;
}
public int getEnd() {
return _end;
}
public int getStart() {
return _start;
}
}

View File

@@ -3,7 +3,7 @@ package com.gempukku.lotro.league;
import com.gempukku.lotro.db.*;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import com.gempukku.lotro.db.vo.LeagueSeason;
import com.gempukku.lotro.db.vo.LeagueSerie;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.logic.timing.GameResultListener;
@@ -11,13 +11,13 @@ import java.util.*;
public class LeagueService {
private LeagueDAO _leagueDao;
private LeagueSeasonDAO _leagueSeasonDao;
private LeagueSerieDAO _leagueSeasonDao;
private LeaguePointsDAO _leaguePointsDao;
private LeagueMatchDAO _leagueMatchDao;
private CollectionDAO _collectionDao;
private LotroCardBlueprintLibrary _library;
public LeagueService(LeagueDAO leagueDao, LeagueSeasonDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionDAO collectionDao, LotroCardBlueprintLibrary library) {
public LeagueService(LeagueDAO leagueDao, LeagueSerieDAO leagueSeasonDao, LeaguePointsDAO leaguePointsDao, LeagueMatchDAO leagueMatchDao, CollectionDAO collectionDao, LotroCardBlueprintLibrary library) {
_leagueDao = leagueDao;
_leagueSeasonDao = leagueSeasonDao;
_leaguePointsDao = leaguePointsDao;
@@ -63,7 +63,7 @@ public class LeagueService {
public void leagueGameStarting(final League league, LotroGameMediator gameMediator) {
final int startDay = getCurrentDate();
final LeagueSeason season = _leagueSeasonDao.getSeasonForLeague(league, startDay);
final LeagueSerie season = _leagueSeasonDao.getSerieForLeague(league, startDay);
if (season != null && isRanked(league, season, gameMediator)) {
gameMediator.addGameResultListener(
new GameResultListener() {
@@ -86,7 +86,7 @@ public class LeagueService {
return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH);
}
private boolean isRanked(League league, LeagueSeason season, LotroGameMediator gameMediator) {
private boolean isRanked(League league, LeagueSerie season, LotroGameMediator gameMediator) {
Set<String> playersPlaying = gameMediator.getPlayersPlaying();
for (String player : playersPlaying) {
int maxMatches = season.getMaxMatches();

View File

@@ -3,7 +3,7 @@ package com.gempukku.lotro.server;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeagueSeasonDAO;
import com.gempukku.lotro.db.LeagueSerieDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
@@ -30,7 +30,7 @@ public class AdminResource extends AbstractResource {
@Context
private LeagueDAO _leagueDao;
@Context
private LeagueSeasonDAO _leagueSeasonDao;
private LeagueSerieDAO _leagueSeasonDao;
@Context
private LotroCardBlueprintLibrary _library;
@Context
@@ -95,7 +95,7 @@ public class AdminResource extends AbstractResource {
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
_leagueSeasonDao.addSeason(leagueType, type, start, end, maxMatches);
_leagueSeasonDao.addSerie(leagueType, type, start, end, maxMatches);
return "OK";
}

View File

@@ -18,7 +18,7 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
private Injectable<GameHistoryDAO> _gameHistoryDAOInjectable;
private Injectable<LeagueDAO> _leagueDaoInjectable;
private Injectable<LeagueSeasonDAO> _leagueSeasonDAOInjectable;
private Injectable<LeagueSerieDAO> _leagueSeasonDAOInjectable;
private Injectable<LeagueMatchDAO> _leagueMatchDAOInjectable;
private Injectable<LeaguePointsDAO> _leaguePointsDAOInjectable;
@@ -40,7 +40,7 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return getGameHistoryDaoSafely();
else if (type.equals(LeagueDAO.class))
return getLeagueDaoSafely();
else if (type.equals(LeagueSeasonDAO.class))
else if (type.equals(LeagueSerieDAO.class))
return getLeagueSeasonDaoSafely();
else if (type.equals(LeagueMatchDAO.class))
return getLeagueMatchDaoSafely();
@@ -75,12 +75,12 @@ public class DaoProvider implements InjectableProvider<Context, Type> {
return _leagueMatchDAOInjectable;
}
private synchronized Injectable<LeagueSeasonDAO> getLeagueSeasonDaoSafely() {
private synchronized Injectable<LeagueSerieDAO> getLeagueSeasonDaoSafely() {
if (_leagueSeasonDAOInjectable == null) {
final LeagueSeasonDAO leagueSeasonDao = new LeagueSeasonDAO(_dbAccess);
_leagueSeasonDAOInjectable = new Injectable<LeagueSeasonDAO>() {
final LeagueSerieDAO leagueSeasonDao = new LeagueSerieDAO(_dbAccess);
_leagueSeasonDAOInjectable = new Injectable<LeagueSerieDAO>() {
@Override
public LeagueSeasonDAO getValue() {
public LeagueSerieDAO getValue() {
return leagueSeasonDao;
}
};

View File

@@ -0,0 +1,72 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeaguePointsDAO;
import com.gempukku.lotro.db.LeagueSerieDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueSerie;
import com.sun.jersey.spi.resource.Singleton;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.List;
import java.util.Map;
@Singleton
@Path("/league")
public class LeagueResource extends AbstractResource {
@Context
private LeagueDAO _leagueDao;
@Context
private LeagueSerieDAO _leagueSerieDao;
@Context
private LeaguePointsDAO _leaguePointsDao;
@GET
public Document getLeagueInformation() throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element leagues = doc.createElement("leagues");
for (League league : _leagueDao.getActiveLeagues()) {
Element leagueElem = doc.createElement("league");
leagueElem.setAttribute("type", league.getType());
leagueElem.setAttribute("name", league.getName());
leagueElem.setAttribute("start", String.valueOf(league.getStart()));
leagueElem.setAttribute("end", String.valueOf(league.getEnd()));
List<LeagueSerie> series = _leagueSerieDao.getSeriesForLeague(league);
for (LeagueSerie serie : series) {
Element serieElem = doc.createElement("serie");
serieElem.setAttribute("type", serie.getType());
serieElem.setAttribute("maxMatches", String.valueOf(serie.getMaxMatches()));
serieElem.setAttribute("start", String.valueOf(serie.getStart()));
serieElem.setAttribute("end", String.valueOf(serie.getEnd()));
Map<String, Integer> standings = _leaguePointsDao.getSerieStandings(league, serie);
for (Map.Entry<String, Integer> playerPoints : standings.entrySet()) {
Element standing = doc.createElement("standing");
standing.setAttribute("player", playerPoints.getKey());
standing.setAttribute("points", playerPoints.getValue().toString());
serieElem.appendChild(standing);
}
leagueElem.appendChild(serieElem);
}
leagues.appendChild(leagueElem);
}
doc.appendChild(leagues);
return doc;
}
}

View File

@@ -27,7 +27,7 @@ public class ServerProvider implements InjectableProvider<Context, Type> {
@Context
private LeagueDAO _leagueDao;
@Context
private LeagueSeasonDAO _leagueSeasonDao;
private LeagueSerieDAO _leagueSeasonDao;
@Context
private LeagueMatchDAO _leagueMatchDao;
@Context

View File

@@ -87,6 +87,11 @@
padding: .2em .5em;
}
.standings td, .standings th {
padding: 2px;
border: 1px solid white;
}
.gameHistory td, .gameHistory th {
padding: 2px;
border: 1px solid white;
@@ -104,6 +109,7 @@
<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>
<script type="text/javascript" src="js/leagueResultsUi.js"></script>
<script type="text/javascript" src="js/communication.js"></script>
<script type="text/javascript">
@@ -143,6 +149,7 @@
<ul>
<li><a href="#gameHall">Game Hall</a></li>
<li><a href="includes/gameHistory.html">Game History</a></li>
<li><a href="includes/leagueResults.html">League Results</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">
$(document).ready(
function () {
var ui = new LeagueResultsUI("/gemp-lotr/server");
});
</script>
All times are GMT based.
<div id="leagueResults"></div>

View File

@@ -21,6 +21,20 @@ var GempLotrCommunication = Class.extend({
dataType: "xml"
});
},
getLeagues: function(callback) {
$.ajax({
type: "GET",
url: this.url + "/league",
cache: false,
data: {
participanId: getUrlParam("participantId") },
success: callback,
error: this.failure,
dataType: "xml"
});
},
getReplay: function(replayId, callback) {
$.ajax({
type: "GET",

View File

@@ -0,0 +1,69 @@
var LeagueResultsUI = Class.extend({
communication: null,
init: function(url) {
this.communication = new GempLotrCommunication(url,
function(xhr, ajaxOptions, thrownError) {
});
this.loadResults();
},
loadResults: function() {
var that = this;
this.communication.getLeagues(
function(xml) {
that.loadedLeagueResults(xml);
});
},
getDateString: function(date) {
return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
},
loadedLeagueResults: function(xml) {
log(xml);
var root = xml.documentElement;
if (root.tagName == 'leagues') {
$("#leagueResults").html("");
var leagues = root.getElementsByTagName("league");
for (var i = 0; i < leagues.length; i++) {
var league = leagues[i];
var leagueName = league.getAttribute("name");
var start = league.getAttribute("start");
var end = league.getAttribute("end");
var leagueText = leagueName + " - " + this.getDateString(start) + " to " + this.getDateString(end);
$("#leagueResults").append("<h1 class='leagueName'>" + leagueText + "</h1>");
var series = league.getElementsByTagName("serie");
for (var j = 0; j < series.length; j++) {
var serie = series[j];
var serieName = serie.getAttribute("type");
var serieStart = serie.getAttribute("start");
var serieEnd = serie.getAttribute("end");
var serieText = serieName + " - " + this.getDateString(serieStart) + " to " + this.getDateString(serieEnd);
$("#leagueResults").append("<h2 class='serieName'>" + serieText + "</h2>");
var standings = serie.getElementsByTagName("standing");
if (standings.length > 0) {
var standingsTable = $("<table class='standings'></table>");
standingsTable.append("<tr><th>Player</th><th>Points</th></tr>");
for (var k = 0; k < standings.length; k++) {
var standing = standings[k];
var player = standing.getAttribute("player");
var points = standing.getAttribute("points");
standingsTable.append("<tr><td>" + player + "</td><td>" + points + "</td></tr>");
}
$("#leagueResults").append(standingsTable);
}
}
}
}
}
});