Search in sources :

Example 1 with PriorityTimer

use of mage.utils.timer.PriorityTimer in project mage by magefree.

the class PlayerPanelExt method init.

public void init(UUID gameId, UUID playerId, boolean controlled, BigCard bigCard, int priorityTime) {
    this.gameId = gameId;
    this.playerId = playerId;
    this.bigCard = bigCard;
    cheat.setVisible(SessionHandler.isTestMode() && controlled);
    cheat.setFocusable(false);
    flagName = null;
    if (priorityTime > 0) {
        long delay = 1000L;
        timer = new PriorityTimer(priorityTime, delay, () -> {
        // do nothing
        });
        final PriorityTimer pt = timer;
        timer.setTaskOnTick(() -> {
            int priorityTimeValue = pt.getCount();
            String text = getPriorityTimeLeftString(priorityTimeValue);
            PlayerPanelExt.this.avatar.setTopText(text);
            PlayerPanelExt.this.timerLabel.setText(text);
            PlayerPanelExt.this.avatar.repaint();
        });
        timer.init(gameId);
    }
}
Also used : PriorityTimer(mage.utils.timer.PriorityTimer)

Example 2 with PriorityTimer

use of mage.utils.timer.PriorityTimer in project mage by magefree.

the class GameController method cleanUp.

public void cleanUp() {
    cancelTimeout();
    for (GameSessionPlayer gameSessionPlayer : getGameSessions()) {
        gameSessionPlayer.cleanUp();
    }
    managerFactory.chatManager().destroyChatSession(chatId);
    for (PriorityTimer priorityTimer : timers.values()) {
        priorityTimer.cancel();
    }
}
Also used : PriorityTimer(mage.utils.timer.PriorityTimer)

Example 3 with PriorityTimer

use of mage.utils.timer.PriorityTimer in project mage by magefree.

the class GameController method createPlayerTimer.

/**
 * We create a timer that will run every 250 ms individually for a player
 * decreasing their internal game counter. Later on this counter is used to
 * get time left to play the whole match.
 * <p>
 * What we also do here is passing Action to PriorityTimer that is the
 * action that will be executed once game timer is over.
 *
 * @param playerId
 * @param count
 * @return
 */
private PriorityTimer createPlayerTimer(UUID playerId, int count) {
    final UUID initPlayerId = playerId;
    // run each 250 ms
    long delayMs = 250L;
    Action executeOnNoTimeLeft = () -> {
        game.timerTimeout(initPlayerId);
        logger.debug("Player has no time left to end the match: " + initPlayerId + ". Conceding.");
    };
    PriorityTimer timer = new PriorityTimer(count, delayMs, executeOnNoTimeLeft);
    timer.init(game.getId());
    timers.put(playerId, timer);
    return timer;
}
Also used : PlayerAction(mage.constants.PlayerAction) Action(mage.interfaces.Action) PriorityTimer(mage.utils.timer.PriorityTimer)

Example 4 with PriorityTimer

use of mage.utils.timer.PriorityTimer in project mage by magefree.

the class GameController method getGameSession.

private GameSessionPlayer getGameSession(UUID playerId) {
    if (!timers.isEmpty()) {
        Player player = game.getState().getPlayer(playerId);
        PriorityTimer timer = timers.get(playerId);
        if (timer != null) {
            // logger.warn("Timer Player " + player.getName()+ " " + player.getPriorityTimeLeft() + " Timer: " + timer.getCount());
            player.setPriorityTimeLeft(timer.getCount());
        }
    }
    return gameSessions.get(playerId);
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) Player(mage.players.Player) PriorityTimer(mage.utils.timer.PriorityTimer)

Example 5 with PriorityTimer

use of mage.utils.timer.PriorityTimer in project mage by magefree.

the class GameController method init.

private void init() {
    game.addTableEventListener((Listener<TableEvent>) event -> {
        try {
            PriorityTimer timer;
            UUID playerId;
            switch(event.getEventType()) {
                case UPDATE:
                    updateGame();
                    break;
                case INFO:
                    managerFactory.chatManager().broadcast(chatId, "", event.getMessage(), MessageColor.BLACK, true, event.getGame(), MessageType.GAME, null);
                    logger.trace(game.getId() + " " + event.getMessage());
                    break;
                case STATUS:
                    managerFactory.chatManager().broadcast(chatId, "", event.getMessage(), MessageColor.ORANGE, event.getWithTime(), event.getWithTurnInfo() ? event.getGame() : null, MessageType.GAME, null);
                    logger.trace(game.getId() + " " + event.getMessage());
                    break;
                case ERROR:
                    error(event.getMessage(), event.getException());
                    break;
                case END_GAME_INFO:
                    endGameInfo();
                    break;
                case INIT_TIMER:
                    final UUID initPlayerId = event.getPlayerId();
                    if (initPlayerId == null) {
                        throw new MageException("INIT_TIMER: playerId can't be null");
                    }
                    createPlayerTimer(event.getPlayerId(), game.getPriorityTime());
                    break;
                case RESUME_TIMER:
                    playerId = event.getPlayerId();
                    if (playerId == null) {
                        throw new MageException("RESUME_TIMER: playerId can't be null");
                    }
                    timer = timers.get(playerId);
                    if (timer == null) {
                        Player player = game.getState().getPlayer(playerId);
                        if (player != null) {
                            timer = createPlayerTimer(event.getPlayerId(), player.getPriorityTimeLeft());
                        } else {
                            throw new MageException("RESUME_TIMER: player can't be null");
                        }
                    }
                    timer.resume();
                    break;
                case PAUSE_TIMER:
                    playerId = event.getPlayerId();
                    if (playerId == null) {
                        throw new MageException("PAUSE_TIMER: playerId can't be null");
                    }
                    timer = timers.get(playerId);
                    if (timer == null) {
                        throw new MageException("PAUSE_TIMER: couldn't find timer for player: " + playerId);
                    }
                    timer.pause();
                    break;
            }
        } catch (MageException ex) {
            logger.fatal("Table event listener error ", ex);
        }
    });
    game.addPlayerQueryEventListener((Listener<PlayerQueryEvent>) event -> {
        logger.trace(new StringBuilder(event.getPlayerId().toString()).append("--").append(event.getQueryType()).append("--").append(event.getMessage()).toString());
        try {
            switch(event.getQueryType()) {
                case ASK:
                    ask(event.getPlayerId(), event.getMessage(), event.getOptions());
                    break;
                case PICK_TARGET:
                    target(event.getPlayerId(), event.getMessage(), event.getCards(), event.getPerms(), event.getTargets(), event.isRequired(), event.getOptions());
                    break;
                case PICK_ABILITY:
                    target(event.getPlayerId(), event.getMessage(), event.getAbilities(), event.isRequired(), event.getOptions());
                    break;
                case SELECT:
                    select(event.getPlayerId(), event.getMessage(), event.getOptions());
                    break;
                case PLAY_MANA:
                    playMana(event.getPlayerId(), event.getMessage(), event.getOptions());
                    break;
                case PLAY_X_MANA:
                    playXMana(event.getPlayerId(), event.getMessage());
                    break;
                case CHOOSE_ABILITY:
                    String objectName = null;
                    if (event.getChoices() != null && !event.getChoices().isEmpty()) {
                        objectName = event.getChoices().iterator().next();
                    }
                    chooseAbility(event.getPlayerId(), objectName, event.getAbilities(), event.getMessage());
                    break;
                case CHOOSE_PILE:
                    choosePile(event.getPlayerId(), event.getMessage(), event.getPile1(), event.getPile2());
                    break;
                case CHOOSE_MODE:
                    chooseMode(event.getPlayerId(), event.getModes(), event.getMessage());
                    break;
                case CHOOSE_CHOICE:
                    chooseChoice(event.getPlayerId(), event.getChoice());
                    break;
                case AMOUNT:
                    amount(event.getPlayerId(), event.getMessage(), event.getMin(), event.getMax());
                    break;
                case MULTI_AMOUNT:
                    multiAmount(event.getPlayerId(), event.getMessages(), event.getMin(), event.getMax(), event.getOptions());
                    break;
                case PERSONAL_MESSAGE:
                    informPersonal(event.getPlayerId(), event.getMessage());
                    break;
            }
        } catch (MageException ex) {
            logger.fatal("Player event listener error ", ex);
        }
    });
    joinWaitingExecutor.scheduleAtFixedRate(() -> {
        try {
            sendInfoAboutPlayersNotJoinedYetAndTryToFixIt();
        } catch (Exception ex) {
            logger.fatal("Send info about player not joined yet:", ex);
        }
    }, GAME_TIMEOUTS_CHECK_JOINING_STATUS_EVERY_SECS, GAME_TIMEOUTS_CHECK_JOINING_STATUS_EVERY_SECS, TimeUnit.SECONDS);
    checkStart();
}
Also used : MatchPlayer(mage.game.match.MatchPlayer) java.util(java.util) Zone(mage.constants.Zone) MessageType(mage.view.ChatMessage.MessageType) Splitter(mage.server.util.Splitter) mage.game(mage.game) MessageColor(mage.view.ChatMessage.MessageColor) TableEvent(mage.game.events.TableEvent) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Player(mage.players.Player) mage.view(mage.view) Logger(org.apache.log4j.Logger) ManaType(mage.constants.ManaType) PlayerAction(mage.constants.PlayerAction) User(mage.server.User) SystemUtil(mage.server.util.SystemUtil) StreamUtils(mage.utils.StreamUtils) Card(mage.cards.Card) DeckCardLists(mage.cards.decks.DeckCardLists) PriorityTimer(mage.utils.timer.PriorityTimer) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Choice(mage.choices.Choice) PassAbility(mage.abilities.common.PassAbility) java.util.concurrent(java.util.concurrent) Cards(mage.cards.Cards) Deck(mage.cards.decks.Deck) Plane(mage.game.command.Plane) MageException(mage.MageException) CardRepository(mage.cards.repository.CardRepository) Collectors(java.util.stream.Collectors) Phase(mage.game.turn.Phase) Action(mage.interfaces.Action) Lock(java.util.concurrent.locks.Lock) PlayerQueryEvent(mage.game.events.PlayerQueryEvent) ManagerFactory(mage.server.managers.ManagerFactory) Main(mage.server.Main) java.io(java.io) CardInfo(mage.cards.repository.CardInfo) Permanent(mage.game.permanent.Permanent) Entry(java.util.Map.Entry) Listener(mage.game.events.Listener) GZIPOutputStream(java.util.zip.GZIPOutputStream) Ability(mage.abilities.Ability) MatchPlayer(mage.game.match.MatchPlayer) Player(mage.players.Player) TableEvent(mage.game.events.TableEvent) MageException(mage.MageException) PriorityTimer(mage.utils.timer.PriorityTimer) PlayerQueryEvent(mage.game.events.PlayerQueryEvent) MageException(mage.MageException)

Aggregations

PriorityTimer (mage.utils.timer.PriorityTimer)5 PlayerAction (mage.constants.PlayerAction)2 MatchPlayer (mage.game.match.MatchPlayer)2 Action (mage.interfaces.Action)2 Player (mage.players.Player)2 java.io (java.io)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 java.util.concurrent (java.util.concurrent)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Collectors (java.util.stream.Collectors)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 MageException (mage.MageException)1 Ability (mage.abilities.Ability)1 PassAbility (mage.abilities.common.PassAbility)1 Card (mage.cards.Card)1 Cards (mage.cards.Cards)1 Deck (mage.cards.decks.Deck)1