* Added handling to show all tournament games in tournament view.

This commit is contained in:
LevelX2 2014-10-02 01:15:21 +02:00
parent 8ac8d36c03
commit 845d3a7951
13 changed files with 218 additions and 24 deletions

View file

@ -32,6 +32,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import mage.game.Game;
import mage.game.GameInfo;
import mage.game.tournament.Round;
import mage.game.tournament.TournamentPairing;
@ -45,12 +46,30 @@ public class RoundView implements Serializable {
List<TournamentGameView> games = new ArrayList<>();
public RoundView(Round round) {
for (TournamentPairing pair: round.getPairs()) {
if (pair.getMatch() != null) {
for (Game game: pair.getMatch().getGames()) {
games.add(new TournamentGameView(round.getRoundNumber(), pair, game));
try {
for (TournamentPairing pair: round.getPairs()) {
// get info of finished games from match
if (pair.getMatch() != null) {
for (GameInfo gameInfo: pair.getMatch().getGamesInfo()) {
games.add(new TournamentGameView(round.getRoundNumber(), gameInfo.getMatchId(), gameInfo.getGameId(), gameInfo.getState(), gameInfo.getResult(), gameInfo.getPlayers(), gameInfo.getTableId()));
}
if (!pair.getMatch().hasEnded()) {
int numberSavedGames = pair.getMatch().getGamesInfo().size();
if (pair.getMatch() != null) {
int gameCount = 0;
for (Game game: pair.getMatch().getGames()) {
gameCount++;
if (gameCount > numberSavedGames) {
// only unfinished game info directly from game
games.add(new TournamentGameView(round.getRoundNumber(), pair, game));
}
}
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

View file

@ -33,7 +33,7 @@ import java.util.Date;
import java.util.UUID;
import mage.game.Game;
import mage.game.tournament.TournamentPairing;
import mage.utils.DateFormat;
import mage.util.DateFormat;
/**
*
@ -51,6 +51,16 @@ public class TournamentGameView implements Serializable {
private final String players;
private final UUID tableId;
TournamentGameView(int roundNum, UUID matchId, UUID gameId, String state, String result, String players, UUID tableId) {
this.roundNum = roundNum;
this.matchId = matchId;
this.gameId = gameId;
this.state = state;
this.result = result;
this.players = players;
this.tableId = tableId;
}
TournamentGameView(int roundNum, TournamentPairing pair, Game game) {
this.roundNum = roundNum;
this.matchId = pair.getMatch().getId();
@ -66,7 +76,9 @@ public class TournamentGameView implements Serializable {
this.result = game.getWinner();
}
else {
duelingTime = " (" + DateFormat.getDuration((new Date().getTime() - game.getStartTime().getTime())/1000) + ")";
if (game.getStartTime() != null) {
duelingTime = " (" + DateFormat.getDuration((new Date().getTime() - game.getStartTime().getTime())/1000) + ")";
}
this.state = "Dueling" + duelingTime;
this.result = "";
}

View file

@ -57,8 +57,7 @@ public class TournamentView implements Serializable {
private final List<RoundView> rounds = new ArrayList<>();
private final List<TournamentPlayerView> players = new ArrayList<>();
@SuppressWarnings("unchecked")
public TournamentView(Tournament tournament) {
tournamentName = tournament.getOptions().getName();

View file

@ -741,7 +741,7 @@ public class TableController {
}
}
// free resources no longer needed
match.cleanUpOnMatchEnd(ConfigSettings.getInstance().isSaveGameActivated());
match.cleanUpOnMatchEnd(ConfigSettings.getInstance().isSaveGameActivated(), table.isTournament());
}
}

View file

@ -42,6 +42,7 @@ import mage.game.events.Listener;
import mage.game.events.PlayerQueryEvent;
import mage.game.events.TableEvent;
import static mage.game.events.TableEvent.EventType.CONSTRUCT;
import mage.game.match.Match;
import mage.game.match.MatchOptions;
import mage.game.tournament.Tournament;
import mage.game.tournament.TournamentPairing;
@ -241,7 +242,9 @@ public class TournamentController {
tableManager.addPlayer(getPlayerUserId(player2.getPlayer().getId()), table.getId(), player2.getPlayer(), player2.getPlayerType(), player2.getDeck());
table.setState(TableState.STARTING);
tableManager.startTournamentSubMatch(null, table.getId());
pair.setMatch(tableManager.getMatch(table.getId()));
Match match = tableManager.getMatch(table.getId());
match.setTableId(tableId);
pair.setMatch(match);
pair.setTableId(table.getId());
player1.setState(TournamentPlayerState.DUELING);
player2.setState(TournamentPlayerState.DUELING);

View file

@ -89,7 +89,11 @@ public class TournamentManager {
}
public TournamentView getTournamentView(UUID tournamentId) {
return controllers.get(tournamentId).getTournamentView();
TournamentController tournamentController = controllers.get(tournamentId);
if (tournamentController != null) {
return tournamentController.getTournamentView();
}
return null;
}
public UUID getChatId(UUID tournamentId) {

View file

@ -0,0 +1,93 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.game;
import java.util.UUID;
/**
*
* @author LevelX2
*/
public class GameInfo {
private final UUID matchId;
private final UUID gameId;
private final String state;
private final String result;
private final String players;
private int roundNum;
private UUID tableId;
public GameInfo(int roundNum, UUID matchId, UUID gameId, String state, String result, String players, UUID tableId) {
this.roundNum = roundNum;
this.matchId = matchId;
this.gameId = gameId;
this.state = state;
this.result = result;
this.players = players;
this.tableId = tableId;
}
public int getRoundNum() {
return roundNum;
}
public UUID getMatchId() {
return matchId;
}
public UUID getGameId() {
return gameId;
}
public String getState() {
return state;
}
public String getResult() {
return result;
}
public String getPlayers() {
return players;
}
public UUID getTableId() {
return tableId;
}
public void setRoundNum(int roundNum) {
this.roundNum = roundNum;
}
public void setTableId(UUID tableId) {
this.tableId = tableId;
}
}

View file

@ -148,7 +148,7 @@ public class Table implements Serializable {
*/
public void cleanUp() {
if (match != null) {
match.cleanUpOnMatchEnd(isTournament);
match.cleanUpOnMatchEnd(false, false);
}
}

View file

@ -38,6 +38,7 @@ import mage.players.Player;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import mage.game.GameInfo;
/**
*
@ -95,12 +96,19 @@ public interface Match {
* information purpose.
*
* @param isSaveGameActivated
* @param isTournament
*/
void cleanUpOnMatchEnd(boolean isSaveGameActivated);
void cleanUpOnMatchEnd(boolean isSaveGameActivated, boolean isTournament);
/**
* Free all possible resources
*/
void cleanUp();
GameInfo createGameInfo(Game game);
List<GameInfo> getGamesInfo();
void setTableId(UUID tableId);
void setTournamentRound(int round);
}

View file

@ -28,19 +28,25 @@
package mage.game.match;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import mage.cards.decks.Deck;
import mage.game.Game;
import mage.game.GameException;
import mage.game.GameInfo;
import mage.game.events.Listener;
import mage.game.events.TableEvent;
import mage.game.events.TableEvent.EventType;
import mage.game.events.TableEventSource;
import mage.players.Player;
import java.util.*;
import mage.util.DateFormat;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
@ -52,6 +58,9 @@ public abstract class MatchImpl implements Match {
protected UUID id = UUID.randomUUID();
protected List<MatchPlayer> players = new ArrayList<>();
protected List<Game> games = new ArrayList<>();
protected List<GameInfo> gamesInfo = new ArrayList<>();
protected UUID tableId;
protected MatchOptions options;
protected TableEventSource tableEventSource = new TableEventSource();
@ -259,6 +268,55 @@ public abstract class MatchImpl implements Match {
}
checkIfMatchEnds();
game.fireGameEndInfo();
gamesInfo.add(createGameInfo(game));
}
@Override
public GameInfo createGameInfo(Game game) {
StringBuilder playersInfo = new StringBuilder();
int counter = 0;
for(MatchPlayer matchPlayer: players) {
if (counter > 0) {
playersInfo.append(" - ");
}
playersInfo.append(matchPlayer.getName());
counter++;
}
String state;
String result;
String duelingTime = "";
if (game.hasEnded()) {
if (game.getEndTime() != null) {
duelingTime = " (" + DateFormat.getDuration((game.getEndTime().getTime() - game.getStartTime().getTime())/1000) + ")";
}
state = "Finished" + duelingTime;
result = game.getWinner();
}
else {
if (game.getStartTime() != null) {
duelingTime = " (" + DateFormat.getDuration((new Date().getTime() - game.getStartTime().getTime())/1000) + ")";
}
state = "Dueling" + duelingTime;
result = "";
}
return new GameInfo(0, this.getId(), game.getId(), state, result, playersInfo.toString(), tableId);
}
@Override
public List<GameInfo> getGamesInfo() {
return gamesInfo;
}
@Override
public void setTableId(UUID tableId) {
this.tableId = tableId;
}
@Override
public void setTournamentRound(int round) {
for (GameInfo gameInfo: gamesInfo) {
gameInfo.setRoundNum(round);
}
}
@Override
@ -394,13 +452,11 @@ public abstract class MatchImpl implements Match {
}
@Override
public void cleanUpOnMatchEnd(boolean isSaveGameActivated) {
// this.tableEventSource.removeAllListener();
// this.tableEventSource = null;
public void cleanUpOnMatchEnd(boolean isSaveGameActivated, boolean isTournament) {
for (MatchPlayer matchPlayer: players) {
matchPlayer.cleanUpOnMatchEnd();
}
if (!isSaveGameActivated || this.getGame().isSimulation()) {
if ((!isSaveGameActivated && !isTournament) || this.getGame().isSimulation()) {
this.getGames().clear();
}
}
@ -424,5 +480,4 @@ public abstract class MatchImpl implements Match {
this.getGames().clear();
}
}

View file

@ -252,6 +252,7 @@ public abstract class TournamentImpl implements Tournament {
MatchPlayer mp2 = match.getPlayer(pair.getPlayer2().getPlayer().getId());
// set player state if he finished the round
if (round.getRoundNumber() == rounds.size()) { // for elimination getRoundNumber = 0 so never true here
match.setTournamentRound(round.getRoundNumber());
if (tp1.getState().equals(TournamentPlayerState.DUELING)) {
if (round.getRoundNumber() == getNumberRounds()) {
tp1.setState(TournamentPlayerState.FINISHED);

View file

@ -45,7 +45,7 @@ public class TournamentPairing {
private final TournamentPlayer player1;
private final TournamentPlayer player2;
private boolean alreadyPublished;
public TournamentPairing(TournamentPlayer player1, TournamentPlayer player2) {
this.player1 = player1;
this.player2 = player2;

View file

@ -25,7 +25,7 @@
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.utils;
package mage.util;
import java.util.Date;