Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make reconnects possible #113

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class UserCreatingAuthenticationProvider extends AbstractUserDetailsAuthenticati

private User updatePassword(User user, String password) {
user.setPassword(password);
// TODO:find better fix
user.getPlayer().setPassword(password);
return userRepository.save(user);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public CompletableFuture<Game> createGame(String title, String featuredModName,
is to let the game time out, but as usual, that's not a good solution since no matter what timeout one chooses,
there is always some drawback. Therefore, we don't timeout games but reset a player's game state when he tries
to create a new game. Or when he logs out. */
removePlayer(currentGame, player);
removePlayer(currentGame, player, false);
}

Requests.verify(currentGame == null, ErrorCode.ALREADY_IN_GAME);
Expand Down Expand Up @@ -581,8 +581,14 @@ public void onPlayerOfflineEvent(PlayerOfflineEvent event) {
playerGameStateCounters.get(event.getPlayer().getGameState()).decrementAndGet();
}

/**
* Removes player from his current game. It removes player completely by default, if running it does leave the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removes player from their current game ;)

* possibility to reconnect.
*
* @param player the player to remove from his game
*/
public void removePlayer(Player player) {
Optional.ofNullable(player.getCurrentGame()).ifPresent(game -> removePlayer(game, player));
Optional.ofNullable(player.getCurrentGame()).ifPresent(game -> removePlayer(game, player, game.getState() == GameState.PLAYING));
}

/**
Expand Down Expand Up @@ -710,7 +716,7 @@ private void addPlayer(Game game, Player player) {
markDirty(game, DEFAULT_MIN_DELAY, DEFAULT_MAX_DELAY);
}

private void removePlayer(Game game, Player player) {
private void removePlayer(Game game, Player player, boolean reconnectPossible) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowReconnect?

log.debug("Removing player '{}' from game '{}'", player, game);

int playerId = player.getId();
Expand All @@ -721,12 +727,15 @@ private void removePlayer(Game game, Player player) {
Map<Integer, Player> connectedPlayers = game.getConnectedPlayers();
connectedPlayers.remove(playerId);

clientService.disconnectPlayerFromGame(player.getId(), connectedPlayers.values());
if (!reconnectPossible) {
//Doing this makes all players drop the connection to the player which makes reconnects impossible
clientService.disconnectPlayerFromGame(player.getId(), connectedPlayers.values());
}

// Checking for GameState.INITIALIZING isn't necessary since in this case, connectedPlayers will already be empty
if (game.getState() == GameState.OPEN && game.getHost().equals(player)) {
// A copy of the connected players is required as otherwise we run into a ConcurrentModificationException
new ArrayList<>(connectedPlayers.values()).forEach(connectedPlayer -> removePlayer(game, connectedPlayer));
new ArrayList<>(connectedPlayers.values()).forEach(connectedPlayer -> removePlayer(game, connectedPlayer, false));
}

if (connectedPlayers.isEmpty()) {
Expand Down Expand Up @@ -757,7 +766,7 @@ private void onPlayerGameEnded(Player player, Game game) {

private void onPlayerGameClosed(Player player, Game game) {
log.debug("Player '{}' closed game: {}", player, game);
removePlayer(game, player);
removePlayer(game, player, false);
}

private void onGameCancelled(Game game) {
Expand Down