Tournament stats and decks - server side.

This commit is contained in:
marcins78@gmail.com
2012-07-03 14:56:31 +00:00
parent cc109418ed
commit b974099212
6 changed files with 227 additions and 1 deletions

View File

@@ -124,4 +124,33 @@ public class DbTournamentPlayerDAO implements TournamentPlayerDAO {
throw new RuntimeException(exp);
}
}
@Override
public LotroDeck getPlayerDeck(String tournamentId, String playerName) {
try {
Connection connection = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = connection.prepareStatement("select deck_name, deck from tournament_player where tournament_id=? and player=?");
try {
statement.setString(1, tournamentId);
statement.setString(2, tournamentId);
ResultSet rs = statement.executeQuery();
try {
if (rs.next())
return DeckSerialization.buildDeckFromContents(rs.getString(1), rs.getString(2));
else
return null;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
connection.close();
}
} catch (SQLException exp) {
throw new RuntimeException(exp);
}
}
}

View File

@@ -49,7 +49,7 @@ public abstract class AbstractTournament implements Tournament {
_playersInContention.removeAll(_tournamentService.getDroppedPlayers(tournamentId));
int round = 1;
while (true) {
while (!isFinished()) {
List<TournamentMatch> matches = _tournamentService.getMatches(tournamentId, round);
if (matches.size() > 0) {
Map<String, String> gamesToCreate = new HashMap<String, String>();

View File

@@ -9,4 +9,6 @@ public interface TournamentDAO {
public void markTournamentFinished(String tournamentId);
public List<TournamentInfo> getUnfinishedTournaments();
public TournamentInfo getTournamentById(String tournamentId);
}

View File

@@ -13,4 +13,6 @@ public interface TournamentPlayerDAO {
public Map<String, LotroDeck> getPlayers(String tournamentId);
public Set<String> getDroppedPlayers(String tournamentId);
public LotroDeck getPlayerDeck(String tournamentId, String playerName);
}

View File

@@ -32,6 +32,10 @@ public class TournamentService {
return _tournamentPlayerDao.getDroppedPlayers(tournamentId);
}
public LotroDeck getPlayerDeck(String tournamentId, String player) {
return _tournamentPlayerDao.getPlayerDeck(tournamentId, player);
}
public void addMatch(String tournamentId, int round, String playerOne, String playerTwo) {
_tournamentMatchDao.addMatch(tournamentId, round, playerOne, playerTwo);
}
@@ -68,4 +72,20 @@ public class TournamentService {
}
return result;
}
public Tournament getTournamentById(String tournamentId) {
TournamentInfo tournamentInfo = _tournamentDao.getTournamentById(tournamentId);
if (tournamentInfo == null)
return null;
String clazz = tournamentInfo.getTournamentClass();
try {
Class<?> aClass = Class.forName(clazz);
Constructor<?> constructor = aClass.getConstructor(TournamentService.class, String.class, String.class);
Tournament tournament = (Tournament) constructor.newInstance(this, tournamentInfo.getTournamentId(), tournamentInfo.getParameters());
return tournament;
} catch (Exception exp) {
throw new RuntimeException("Unable to create Tournament", exp);
}
}
}

View File

@@ -0,0 +1,173 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.competitive.PlayerStanding;
import com.gempukku.lotro.game.CardCollection;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.SortAndFilterCards;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.logic.GameUtils;
import com.gempukku.lotro.logic.vo.LotroDeck;
import com.gempukku.lotro.tournament.Tournament;
import com.gempukku.lotro.tournament.TournamentService;
import com.sun.jersey.spi.resource.Singleton;
import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.text.DecimalFormat;
import java.util.List;
@Singleton
@Path("/tournament")
public class TournamentResource extends AbstractResource {
@Context
private TournamentService _tournamentService;
@Context
private LotroFormatLibrary _lotroFormatLibrary;
@Context
private LotroCardBlueprintLibrary _library;
private SortAndFilterCards _sortAndFilterCards = new SortAndFilterCards();
@GET
public Document getCurrentTournaments(
@QueryParam("participantId") String participantId,
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Element tournaments = doc.createElement("tournaments");
for (Tournament tournament : _tournamentService.getLiveTournaments()) {
Element tournamentElem = doc.createElement("tournament");
tournamentElem.setAttribute("name", tournament.getTournamentName());
tournamentElem.setAttribute("format", _lotroFormatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("collection", tournament.getCollectionType().getFullName());
tournamentElem.setAttribute("round", String.valueOf(tournament.getCurrentRound()));
tournaments.appendChild(tournamentElem);
}
doc.appendChild(tournaments);
return doc;
}
@Path("/{tournamentId}")
@GET
@Produces(MediaType.APPLICATION_XML)
public Document getTournamentInformation(
@PathParam("tournamentId") String tournamentId,
@QueryParam("participantId") String participantId,
@Context HttpServletRequest request,
@Context HttpServletResponse response) throws ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
Tournament tournament = _tournamentService.getTournamentById(tournamentId);
if (tournament == null)
sendError(Response.Status.NOT_FOUND);
Element tournamentElem = doc.createElement("tournament");
tournamentElem.setAttribute("name", tournament.getTournamentName());
tournamentElem.setAttribute("format", _lotroFormatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("collection", tournament.getCollectionType().getFullName());
tournamentElem.setAttribute("round", String.valueOf(tournament.getCurrentRound()));
tournamentElem.setAttribute("finished", String.valueOf(tournament.isFinished()));
List<PlayerStanding> leagueStandings = tournament.getCurrentStandings();
for (PlayerStanding standing : leagueStandings) {
Element standingElem = doc.createElement("leagueStanding");
setStandingAttributes(standing, standingElem);
tournamentElem.appendChild(standingElem);
}
doc.appendChild(tournamentElem);
return doc;
}
@Path("/{tournamentId}/deck/{playerName}/html")
@GET
@Produces(MediaType.TEXT_HTML)
public String getDeckList(
@PathParam("tournamentId") String tournamentId,
@PathParam("playerName") String playerName,
@QueryParam("participantId") String participantId,
@Context HttpServletRequest request,
@Context HttpServletResponse response) {
Tournament tournament = _tournamentService.getTournamentById(tournamentId);
if (tournament == null)
sendError(Response.Status.NOT_FOUND);
if (tournament.isFinished())
sendError(Response.Status.FORBIDDEN);
LotroDeck deck = _tournamentService.getPlayerDeck(tournamentId, playerName);
if (deck == null)
sendError(Response.Status.NOT_FOUND);
StringBuilder result = new StringBuilder();
result.append("<html><body>");
result.append("<h1>" + StringEscapeUtils.escapeHtml(deck.getDeckName()) + "</h1>");
String ringBearer = deck.getRingBearer();
if (ringBearer != null)
result.append("<b>Ring-bearer:</b> " + GameUtils.getFullName(_library.getLotroCardBlueprint(ringBearer)) + "<br/>");
String ring = deck.getRing();
if (ring != null)
result.append("<b>Ring:</b> " + GameUtils.getFullName(_library.getLotroCardBlueprint(ring)) + "<br/>");
DefaultCardCollection deckCards = new DefaultCardCollection();
for (String card : deck.getAdventureCards())
deckCards.addItem(_library.getBaseBlueprintId(card), 1);
for (String site : deck.getSites())
deckCards.addItem(_library.getBaseBlueprintId(site), 1);
result.append("<br/>");
result.append("<b>Adventure deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("cardType:SITE sort:siteNumber,twilight", deckCards.getAll().values(), _library, null))
result.append(GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Free Peoples Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:FREE_PEOPLE sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Shadow Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:SHADOW sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("</body></html>");
response.setCharacterEncoding("UTF-8");
return result.toString();
}
private void setStandingAttributes(PlayerStanding standing, Element standingElem) {
standingElem.setAttribute("player", standing.getPlayerName());
standingElem.setAttribute("standing", String.valueOf(standing.getStanding()));
standingElem.setAttribute("points", String.valueOf(standing.getPoints()));
standingElem.setAttribute("gamesPlayed", String.valueOf(standing.getGamesPlayed()));
DecimalFormat format = new DecimalFormat("##0.00%");
standingElem.setAttribute("opponentWin", format.format(standing.getOpponentWin()));
}
}