From e488997124a19ee794c3fffeb8eeb55e9decfb28 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Thu, 19 Aug 2021 01:42:35 +0400 Subject: [PATCH] * Server: fixed that multiplayer game can be closed on "1x human + 1x AI" remain (see #6178); --- .../java/mage/server/TableController.java | 23 +++++++++++++++---- .../java/mage/server/TableManagerImpl.java | 10 +++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/TableController.java b/Mage.Server/src/main/java/mage/server/TableController.java index ca2ce8fe04..430f02b263 100644 --- a/Mage.Server/src/main/java/mage/server/TableController.java +++ b/Mage.Server/src/main/java/mage/server/TableController.java @@ -48,7 +48,7 @@ public class TableController { private final UUID chatId; private final String controllerName; private final Table table; - private final ConcurrentHashMap userPlayerMap = new ConcurrentHashMap<>(); + private final ConcurrentHashMap userPlayerMap = new ConcurrentHashMap<>(); // human players only, use table.seats for AI private Match match; private MatchOptions options; @@ -985,10 +985,14 @@ public class TableController { // return false; } } + // check for active players int validHumanPlayers = 0; + int validAIPlayers = 0; int aiPlayers = 0; int humanPlayers = 0; + + // check humans for (Map.Entry userPlayerEntry : userPlayerMap.entrySet()) { MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue()); if (matchPlayer == null) { @@ -1016,12 +1020,21 @@ public class TableController { // user exits on the server and match player has not quit -> player is valid validHumanPlayers++; } - } else { - aiPlayers++; } } - // if at least 2 human players are valid (multiplayer) or all human players are valid the table is valid or it's an AI match - return validHumanPlayers >= 2 || validHumanPlayers == humanPlayers || aiPlayers > 1; + + // check AI + for (MatchPlayer matchPlayer : match.getPlayers()) { + if (!matchPlayer.getPlayer().isHuman()) { + aiPlayers++; + if (matchPlayer.getPlayer().isInGame()) { + validAIPlayers++; + } + } + } + + // table must contain minimum two active players + return (validAIPlayers + validHumanPlayers) >= 2; } return true; } diff --git a/Mage.Server/src/main/java/mage/server/TableManagerImpl.java b/Mage.Server/src/main/java/mage/server/TableManagerImpl.java index 268c4f20fe..8826acb2d4 100644 --- a/Mage.Server/src/main/java/mage/server/TableManagerImpl.java +++ b/Mage.Server/src/main/java/mage/server/TableManagerImpl.java @@ -47,12 +47,8 @@ public class TableManagerImpl implements TableManager { private final ConcurrentHashMap tables = new ConcurrentHashMap<>(); private final ReadWriteLock tablesLock = new ReentrantReadWriteLock(); - /** - * Defines how often checking process should be run on server. - *

- * In minutes. - */ - private static final int EXPIRE_CHECK_PERIOD = 10; + // defines how often checking process should be run on server (in minutes) + private static final int TABLE_HEALTH_CHECK_TIMEOUT_MINS = 10; public TableManagerImpl(ManagerFactory managerFactory) { this.managerFactory = managerFactory; @@ -66,7 +62,7 @@ public class TableManagerImpl implements TableManager { } catch (Exception ex) { logger.fatal("Check table health state job error:", ex); } - }, EXPIRE_CHECK_PERIOD, EXPIRE_CHECK_PERIOD, TimeUnit.MINUTES); + }, TABLE_HEALTH_CHECK_TIMEOUT_MINS, TABLE_HEALTH_CHECK_TIMEOUT_MINS, TimeUnit.MINUTES); } @Override