* Server: fixed that multiplayer game can be closed on "1x human + 1x AI" remain (see #6178);

This commit is contained in:
Oleg Agafonov 2021-08-19 01:42:35 +04:00
parent 3da525520c
commit e488997124
2 changed files with 21 additions and 12 deletions

View file

@ -48,7 +48,7 @@ public class TableController {
private final UUID chatId; private final UUID chatId;
private final String controllerName; private final String controllerName;
private final Table table; private final Table table;
private final ConcurrentHashMap<UUID, UUID> userPlayerMap = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, UUID> userPlayerMap = new ConcurrentHashMap<>(); // human players only, use table.seats for AI
private Match match; private Match match;
private MatchOptions options; private MatchOptions options;
@ -985,10 +985,14 @@ public class TableController {
// return false; // return false;
} }
} }
// check for active players // check for active players
int validHumanPlayers = 0; int validHumanPlayers = 0;
int validAIPlayers = 0;
int aiPlayers = 0; int aiPlayers = 0;
int humanPlayers = 0; int humanPlayers = 0;
// check humans
for (Map.Entry<UUID, UUID> userPlayerEntry : userPlayerMap.entrySet()) { for (Map.Entry<UUID, UUID> userPlayerEntry : userPlayerMap.entrySet()) {
MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue()); MatchPlayer matchPlayer = match.getPlayer(userPlayerEntry.getValue());
if (matchPlayer == null) { if (matchPlayer == null) {
@ -1016,12 +1020,21 @@ public class TableController {
// user exits on the server and match player has not quit -> player is valid // user exits on the server and match player has not quit -> player is valid
validHumanPlayers++; validHumanPlayers++;
} }
} else { }
}
// check AI
for (MatchPlayer matchPlayer : match.getPlayers()) {
if (!matchPlayer.getPlayer().isHuman()) {
aiPlayers++; aiPlayers++;
if (matchPlayer.getPlayer().isInGame()) {
validAIPlayers++;
} }
} }
// 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;
// table must contain minimum two active players
return (validAIPlayers + validHumanPlayers) >= 2;
} }
return true; return true;
} }

View file

@ -47,12 +47,8 @@ public class TableManagerImpl implements TableManager {
private final ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<>(); private final ConcurrentHashMap<UUID, Table> tables = new ConcurrentHashMap<>();
private final ReadWriteLock tablesLock = new ReentrantReadWriteLock(); private final ReadWriteLock tablesLock = new ReentrantReadWriteLock();
/** // defines how often checking process should be run on server (in minutes)
* Defines how often checking process should be run on server. private static final int TABLE_HEALTH_CHECK_TIMEOUT_MINS = 10;
* <p>
* In minutes.
*/
private static final int EXPIRE_CHECK_PERIOD = 10;
public TableManagerImpl(ManagerFactory managerFactory) { public TableManagerImpl(ManagerFactory managerFactory) {
this.managerFactory = managerFactory; this.managerFactory = managerFactory;
@ -66,7 +62,7 @@ public class TableManagerImpl implements TableManager {
} catch (Exception ex) { } catch (Exception ex) {
logger.fatal("Check table health state job error:", 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 @Override