Skip to content

Commit

Permalink
Add checking of name when player login
Browse files Browse the repository at this point in the history
  • Loading branch information
Hronom committed Jan 24, 2017
1 parent ba5fd43 commit 3da6152
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,27 @@ public void handle(ActionEvent event) {
loginStreamObserver = new StreamObserver<LoginReply>() {
@Override
public void onNext(LoginReply value) {
playerId.set(value.getPlayerId());
logged.set(true);
println("playerLogged", value.getPlayerId());
tfServerAddress.setDisable(true);
tfPlayerName.setDisable(true);
bLogin.setDisable(true);
if (value.getValueCase() == LoginReply.ValueCase.PLAYERID) {
playerId.set(value.getPlayerId());
logged.set(true);
println("playerLogged", value.getPlayerId());
tfServerAddress.setDisable(true);
tfPlayerName.setDisable(true);
bLogin.setDisable(true);
} else if (value.getValueCase() == LoginReply.ValueCase.ERROR) {
LoginReply.Errors error = value.getError();
if (error == LoginReply.Errors.PLAYER_BAD_NAME) {
println("playerBadName");
tfServerAddress.setDisable(true);
tfPlayerName.setDisable(true);
bLogin.setDisable(true);
} else if (error == LoginReply.Errors.PLAYER_ALREADY_LOGGED) {
println("playerAlreadyLogged");
tfServerAddress.setDisable(true);
tfPlayerName.setDisable(true);
bLogin.setDisable(true);
}
}
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion ccg-curator-client/src/main/resources/app_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ diceNotThrowed=Dice not throwed "{0}"
roomNotJoinedAlreadyInRoom=Room "{0}" not joined (Player already in room)
gui.lServerAddress=Server address
error=Error {0}
cardSubmitted=Player "{0}" submit card
cardSubmitted=Player "{0}" submit card
playerBadName=Player name contains bad characters or empty
playerAlreadyLogged=Player with this name already logged
4 changes: 3 additions & 1 deletion ccg-curator-client/src/main/resources/app_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ diceNotThrowed=Не могу кинуть кость со значениями "
roomNotJoinedAlreadyInRoom=Не могу войти в комнату "{0}" (Игрок уже вошёл в комнату)
gui.lServerAddress=Адрес сервера
error=Ошибка {0}
cardSubmitted=Игрок "{0}" выбрал карту
cardSubmitted=Игрок "{0}" выбрал карту
playerBadName=Имя игрока содержит недопустимые символы или пустое
playerAlreadyLogged=Игрок с таким именем уже вошёл
9 changes: 8 additions & 1 deletion ccg-curator-common/src/main/proto/ccg-curator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ message LoginRequest {
}

message LoginReply {
int64 playerId = 1;
enum Errors {
PLAYER_ALREADY_LOGGED = 0;
PLAYER_BAD_NAME = 1;
}
oneof value {
int64 playerId = 1;
Errors error = 2;
}
}

message JoinRoomRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import com.github.hronom.ccg.curator.SubmitCardRequest;
import com.github.hronom.ccg.curator.ThrowDiceReply;
import com.github.hronom.ccg.curator.ThrowDiceRequest;
import com.github.hronom.ccg.curator.server.components.business.CardAlreadySubmittedException;
import com.github.hronom.ccg.curator.server.components.business.exception.CardAlreadySubmittedException;
import com.github.hronom.ccg.curator.server.components.business.MainManager;
import com.github.hronom.ccg.curator.server.components.business.Player;
import com.github.hronom.ccg.curator.server.components.business.PlayersManager;
import com.github.hronom.ccg.curator.server.components.business.Room;
import com.github.hronom.ccg.curator.server.components.business.RoomBadPasswordException;
import com.github.hronom.ccg.curator.server.components.business.exception.PlayerAlreadyLoggedException;
import com.github.hronom.ccg.curator.server.components.business.exception.PlayerBadNameException;
import com.github.hronom.ccg.curator.server.components.business.exception.RoomBadPasswordException;
import com.github.hronom.ccg.curator.server.components.business.RoomsManager;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -70,9 +72,39 @@ public StreamObserver<LoginRequest> login(StreamObserver<LoginReply> responseObs

@Override
public void onNext(LoginRequest value) {
player = playersManager.createPlayer(value.getPlayerName());
LoginReply loginReply = LoginReply.newBuilder().setPlayerId(player.getId()).build();
responseObserver.onNext(loginReply);
try {
player = playersManager.createPlayer(value.getPlayerName());
LoginReply loginReply =
LoginReply
.newBuilder()
.setPlayerId(player.getId())
.build();
responseObserver.onNext(loginReply);
} catch (PlayerBadNameException exception) {
if (logger.isDebugEnabled()) {
logger.debug("Error", exception);
}
LoginReply loginReply =
LoginReply
.newBuilder()
.setError(LoginReply.Errors.PLAYER_BAD_NAME)
.build();
responseObserver.onNext(loginReply);
responseObserver.onCompleted();
} catch (PlayerAlreadyLoggedException exception) {
if (logger.isDebugEnabled()) {
logger.debug("Error", exception);
}
exception.printStackTrace();
exception.printStackTrace();
LoginReply loginReply =
LoginReply
.newBuilder()
.setError(LoginReply.Errors.PLAYER_ALREADY_LOGGED)
.build();
responseObserver.onNext(loginReply);
responseObserver.onCompleted();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.hronom.ccg.curator.server.components.business;

import com.github.hronom.ccg.curator.server.components.business.exception.PlayerAlreadyLoggedException;
import com.github.hronom.ccg.curator.server.components.business.exception.PlayerBadNameException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +25,8 @@ public class PlayersManager {

private final AtomicLong playerIdGenerator = new AtomicLong();

private final Object playerModificationLock = new Object();
private final ConcurrentHashMap<String, Player> playersByName = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Long, Player> playersById = new ConcurrentHashMap<>();

private final ApplicationContext context;
Expand All @@ -35,19 +40,36 @@ public PlayersManager(ApplicationContext contextArg) {
public void cleanUp() throws Exception {
}

public Player createPlayer(String playerName) {
long id = playerIdGenerator.incrementAndGet();
Player player = context.getBean(Player.class, id, playerName);
playersById.put(id, player);
return player;
public Player createPlayer(String playerName)
throws PlayerAlreadyLoggedException, PlayerBadNameException {
if (playerName == null) {
throw new PlayerBadNameException();
} else if(playerName.isEmpty()) {
throw new PlayerBadNameException();
}

synchronized (playerModificationLock) {
if (!playersByName.containsKey(playerName)) {
long id = playerIdGenerator.incrementAndGet();
Player player = context.getBean(Player.class, id, playerName);
playersByName.put(playerName, player);
playersById.put(id, player);
return player;
} else {
throw new PlayerAlreadyLoggedException();
}
}
}

public void removePlayer(Player player) {
playersById.forEach((idArg, playerArg) -> {
if (Objects.equals(player, playerArg)) {
playersById.remove(idArg);
}
});
synchronized (playerModificationLock) {
playersById.forEach((idArg, playerArg) -> {
if (Objects.equals(player, playerArg)) {
playersByName.remove(player.getName());
playersById.remove(idArg);
}
});
}
}

public Player getPlayer(long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.hronom.ccg.curator.server.components.business;

import com.github.hronom.ccg.curator.server.components.MainServiceManager;
import com.github.hronom.ccg.curator.server.components.business.exception.CardAlreadySubmittedException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -53,7 +54,7 @@ public String getPassword() {

public void addPlayer(Player player) {
players.add(player);
// Send notifications

for (Player playerInRooom : players) {
if (player != playerInRooom) {
mainServiceManager.sendPlayerEnterRoom(player, playerInRooom);
Expand All @@ -68,10 +69,16 @@ public void addPlayer(Player player) {

public void removePlayer(Player player) {
players.remove(player);
// Send notifications

for (Player playerToSend : players) {
mainServiceManager.sendPlayerLeftRoom(playerToSend, player);
}

submitedCards.remove(player);

if (isAllPlayersSubmitCards()) {
sendSubmittedCards();
}
}

public Collection<Player> getPlayers() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.hronom.ccg.curator.server.components.business;

import com.github.hronom.ccg.curator.server.components.MainServiceManager;
import com.github.hronom.ccg.curator.server.components.business.exception.RoomBadPasswordException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hronom.ccg.curator.server.components.business;
package com.github.hronom.ccg.curator.server.components.business.exception;

public class CardAlreadySubmittedException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.hronom.ccg.curator.server.components.business.exception;

public class PlayerAlreadyLoggedException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.hronom.ccg.curator.server.components.business.exception;

public class PlayerBadNameException extends Exception {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.hronom.ccg.curator.server.components.business;
package com.github.hronom.ccg.curator.server.components.business.exception;

public class RoomBadPasswordException extends Exception {
}

0 comments on commit 3da6152

Please sign in to comment.