Search in sources :

Example 6 with Table

use of mage.game.Table in project mage by magefree.

the class TournamentController method startMultiplayerMatch.

private void startMultiplayerMatch(MultiplayerRound round, MatchOptions matchOptions) {
    try {
        TableManager tableManager = managerFactory.tableManager();
        Table table = tableManager.createTable(managerFactory.gamesRoomManager().getMainRoomId(), matchOptions);
        table.setTournamentSubTable(true);
        table.setTournament(tournament);
        table.setState(TableState.WAITING);
        if (round.getAllPlayers().stream().allMatch(tournamentPlayer -> getPlayerUserId(tournamentPlayer.getPlayer().getId()).isPresent())) {
            for (TournamentPlayer player : round.getAllPlayers()) {
                tableManager.addPlayer(getPlayerUserId(player.getPlayer().getId()).get(), table.getId(), player);
            }
            table.setState(TableState.STARTING);
            tableManager.startTournamentSubMatch(null, table.getId());
            tableManager.getMatch(table.getId()).ifPresent(match -> {
                match.setTableId(tableId);
                round.setMatch(match);
                round.setTableId(table.getId());
                for (TournamentPlayer player : round.getAllPlayers()) {
                    player.setState(TournamentPlayerState.DUELING);
                }
            });
        }
    } catch (GameException ex) {
        logger.fatal("TournamentController startMatch error", ex);
    }
}
Also used : Table(mage.game.Table) TournamentPlayer(mage.game.tournament.TournamentPlayer) TableManager(mage.server.managers.TableManager) GameException(mage.game.GameException)

Example 7 with Table

use of mage.game.Table in project mage by magefree.

the class TournamentController method startMatch.

private void startMatch(TournamentPairing pair, MatchOptions matchOptions) {
    try {
        TableManager tableManager = managerFactory.tableManager();
        Table table = tableManager.createTable(managerFactory.gamesRoomManager().getMainRoomId(), matchOptions);
        table.setTournamentSubTable(true);
        table.setTournament(tournament);
        table.setState(TableState.WAITING);
        TournamentPlayer player1 = pair.getPlayer1();
        TournamentPlayer player2 = pair.getPlayer2();
        UUID user1Uuid = null;
        UUID user2Uuid = null;
        if (player1.getPlayerType() == PlayerType.HUMAN) {
            Optional<UUID> user1Id = getPlayerUserId(player1.getPlayer().getId());
            if (!user1Id.isPresent()) {
                logger.fatal("Player 1 not found");
            } else {
                user1Uuid = user1Id.get();
            }
        }
        if (player2.getPlayerType() == PlayerType.HUMAN) {
            Optional<UUID> user2Id = getPlayerUserId(player2.getPlayer().getId());
            if (!user2Id.isPresent()) {
                logger.fatal("Player 2 not found");
            } else {
                user2Uuid = user2Id.get();
            }
        }
        tableManager.addPlayer(user1Uuid, table.getId(), player1);
        tableManager.addPlayer(user2Uuid, table.getId(), player2);
        table.setState(TableState.STARTING);
        tableManager.startTournamentSubMatch(null, table.getId());
        tableManager.getMatch(table.getId()).ifPresent(match -> {
            match.setTableId(tableId);
            pair.setMatch(match);
            pair.setTableId(table.getId());
            player1.setState(TournamentPlayerState.DUELING);
            player2.setState(TournamentPlayerState.DUELING);
        });
    } catch (GameException ex) {
        logger.fatal("TournamentController startMatch error", ex);
    }
}
Also used : Table(mage.game.Table) TournamentPlayer(mage.game.tournament.TournamentPlayer) TableManager(mage.server.managers.TableManager) UUID(java.util.UUID) GameException(mage.game.GameException)

Example 8 with Table

use of mage.game.Table in project mage by magefree.

the class User method getGameInfo.

public String getGameInfo() {
    StringBuilder sb = new StringBuilder();
    int draft = 0, match = 0, sideboard = 0, tournament = 0, construct = 0, waiting = 0;
    for (Map.Entry<UUID, Table> tableEntry : tables.entrySet()) {
        if (tableEntry != null) {
            Table table = tableEntry.getValue();
            if (table != null) {
                if (table.isTournament()) {
                    if (tableEntry.getKey() != null) {
                        TournamentPlayer tournamentPlayer = table.getTournament().getPlayer(tableEntry.getKey());
                        if (tournamentPlayer != null) {
                            if (!tournamentPlayer.isEliminated()) {
                                switch(table.getState()) {
                                    case WAITING:
                                    case STARTING:
                                    case READY_TO_START:
                                        waiting++;
                                        break;
                                    case CONSTRUCTING:
                                        construct++;
                                        break;
                                    case DRAFTING:
                                        draft++;
                                        break;
                                    case DUELING:
                                        tournament++;
                                        break;
                                }
                                switch(getUserState()) {
                                    case Disconnected:
                                        tournamentPlayer.setDisconnectInfo(" (discon. " + getDisconnectDuration() + ')');
                                        break;
                                    case Offline:
                                        tournamentPlayer.setDisconnectInfo(" Offline");
                                        break;
                                    default:
                                        tournamentPlayer.setDisconnectInfo("");
                                }
                            }
                        } else {
                            // can happen if tournamet has just ended
                            logger.debug(userName + " tournament player missing - tableId:" + table.getId(), null);
                            tablesToDelete.add(tableEntry.getKey());
                        }
                    } else {
                        logger.error(userName + " tournament key missing - tableId: " + table.getId(), null);
                    }
                } else {
                    switch(table.getState()) {
                        case WAITING:
                        case STARTING:
                        case READY_TO_START:
                            waiting++;
                            break;
                        case SIDEBOARDING:
                            sideboard++;
                            break;
                        case DUELING:
                            match++;
                            break;
                    }
                }
            }
        }
    }
    if (!tablesToDelete.isEmpty()) {
        for (UUID keyId : tablesToDelete) {
            removeTable(keyId);
        }
        tablesToDelete.clear();
    }
    if (waiting > 0) {
        sb.append("Wait: ").append(waiting).append(' ');
    }
    if (match > 0) {
        sb.append("Match: ").append(match).append(' ');
    }
    if (sideboard > 0) {
        sb.append("Sideb: ").append(sideboard).append(' ');
    }
    if (draft > 0) {
        sb.append("Draft: ").append(draft).append(' ');
    }
    if (construct > 0) {
        sb.append("Const: ").append(construct).append(' ');
    }
    if (tournament > 0) {
        sb.append("Tourn: ").append(tournament).append(' ');
    }
    if (!watchedGames.isEmpty()) {
        sb.append("Watch: ").append(watchedGames.size()).append(' ');
    }
    return sb.toString();
}
Also used : Table(mage.game.Table) TournamentPlayer(mage.game.tournament.TournamentPlayer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with Table

use of mage.game.Table in project mage by magefree.

the class UserNameSorter method createTournamentTable.

@Override
public TableView createTournamentTable(UUID userId, TournamentOptions options) {
    Table table = managerFactory.tableManager().createTournamentTable(this.getRoomId(), userId, options);
    tables.put(table.getId(), table);
    return new TableView(table);
}
Also used : Table(mage.game.Table) TableView(mage.view.TableView)

Example 10 with Table

use of mage.game.Table in project mage by magefree.

the class TableManagerImpl method removeTable.

@Override
public void removeTable(UUID tableId) {
    TableController tableController = controllers.get(tableId);
    if (tableController != null) {
        Lock w = controllersLock.writeLock();
        w.lock();
        try {
            controllers.remove(tableId);
        } finally {
            w.unlock();
        }
        // deletes the table chat and references to users
        tableController.cleanUp();
        Table table = tables.get(tableId);
        w = tablesLock.writeLock();
        w.lock();
        try {
            tables.remove(tableId);
        } finally {
            w.unlock();
        }
        Match match = table.getMatch();
        Game game = null;
        if (match != null) {
            game = match.getGame();
            if (game != null && !game.hasEnded()) {
                game.end();
            }
        }
        // If table is not finished, the table has to be removed completly because it's not a normal state (if finished it will be removed in GamesRoomImpl.Update())
        if (table.getState() != TableState.FINISHED) {
            if (game != null) {
                managerFactory.gameManager().removeGame(game.getId());
            // something goes wrong, so don't add it to ended stats
            }
            managerFactory.gamesRoomManager().removeTable(tableId);
        }
    }
}
Also used : Table(mage.game.Table) Game(mage.game.Game) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) Match(mage.game.match.Match)

Aggregations

Table (mage.game.Table)12 GameException (mage.game.GameException)4 MageException (mage.MageException)3 TournamentPlayer (mage.game.tournament.TournamentPlayer)3 TableView (mage.view.TableView)3 Lock (java.util.concurrent.locks.Lock)2 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)2 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)2 TableManager (mage.server.managers.TableManager)2 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Game (mage.game.Game)1 Match (mage.game.match.Match)1 MageVersionException (mage.remote.MageVersionException)1 User (mage.server.User)1 DraftSession (mage.server.draft.DraftSession)1 GameSessionPlayer (mage.server.game.GameSessionPlayer)1 MatchView (mage.view.MatchView)1 RoomUsersView (mage.view.RoomUsersView)1 UsersView (mage.view.UsersView)1