55import org .slf4j .LoggerFactory ;
66import sc .api .plugins .IGameInstance ;
77import sc .api .plugins .IGameState ;
8- import sc .api .plugins .exceptions .GameLogicException ;
9- import sc .api .plugins .exceptions .RescuableClientException ;
10- import sc .api .plugins .exceptions .TooManyPlayersException ;
8+ import sc .api .plugins .exceptions .*;
119import sc .api .plugins .host .IGameListener ;
1210import sc .framework .plugins .AbstractGame ;
1311import sc .framework .plugins .Player ;
@@ -263,7 +261,7 @@ public String getId() {
263261 *
264262 * @return true if successfully joined
265263 */
266- public synchronized boolean join (Client client ) throws RescuableClientException {
264+ public synchronized boolean join (Client client ) throws GameRoomException {
267265 PlayerSlot openSlot = null ;
268266
269267 for (PlayerSlot slot : this .playerSlots ) {
@@ -295,7 +293,7 @@ public synchronized boolean join(Client client) throws RescuableClientException
295293 * @param openSlot PlayerSlot to fill
296294 * @param client Client to fill PlayerSlot
297295 */
298- synchronized void fillSlot (PlayerSlot openSlot , Client client ) throws RescuableClientException {
296+ synchronized void fillSlot (PlayerSlot openSlot , Client client ) throws GameRoomException {
299297 openSlot .setClient (client ); // set role of Slot as PlayerRole
300298
301299 if (!this .prepared ) // is set when game is game is created or prepared
@@ -313,7 +311,7 @@ synchronized void fillSlot(PlayerSlot openSlot, Client client) throws RescuableC
313311 * Registers player to role in given slot.
314312 * Sends JoinGameProtocolMessage when successful.
315313 */
316- private void syncSlot (PlayerSlot slot ) throws RescuableClientException {
314+ private void syncSlot (PlayerSlot slot ) throws GameRoomException {
317315 // create new player in gameState of game
318316 Player player = game .onPlayerJoined ();
319317 // set attributes for player
@@ -347,7 +345,7 @@ private boolean isReady() {
347345 }
348346
349347 /** Starts game if ready and not over. */
350- private void startIfReady () throws RescuableClientException {
348+ private void startIfReady () throws GameRoomException {
351349 logger .debug ("startIfReady called" );
352350 if (isOver ()) {
353351 logger .warn ("Game already over: {}" , game );
@@ -364,7 +362,7 @@ private void startIfReady() throws RescuableClientException {
364362 }
365363
366364 /** If the Game is prepared, sync all slots. */
367- private synchronized void start () throws RescuableClientException {
365+ private synchronized void start () throws GameRoomException {
368366 if (this .prepared ) // sync slots for prepared game. This was already called for PlayerSlots in a game created by a join
369367 {
370368 for (PlayerSlot slot : this .playerSlots ) {
@@ -427,34 +425,43 @@ public synchronized List<String> reserveAllSlots() {
427425 * @param source Client which caused the event
428426 * @param data ProtocolMessage containing the action
429427 */
430- public synchronized void onEvent (Client source , ProtocolMessage data ) throws RescuableClientException , InvalidGameStateException {
428+ public synchronized void onEvent (Client source , ProtocolMessage data ) throws GameRoomException {
431429 if (isOver ())
432- throw new RescuableClientException ("Game is already over, but got message: " + data . getClass () );
430+ throw new GameException ("Game is already over, but got " + data );
433431
432+ Player player = resolvePlayer (source );
434433 try {
435- this . game .onAction (resolvePlayer ( source ) , data );
434+ game .onAction (player , data );
436435 } catch (InvalidMoveException e ) {
437- this .observerBroadcast (new RoomPacket (this .id , new ProtocolErrorMessage (e .move , e .getMessage ())));
438- this .game .onPlayerLeft (resolvePlayer (source ), ScoreCause .RULE_VIOLATION );
439- throw new GameLogicException (e .toString ());
436+ final String error = String .format ("Ungueltiger Zug von '%s'.\n %s" , player .getDisplayName (), e );
437+ logger .error (error );
438+ player .setViolationReason (e .getMessage ());
439+ ProtocolErrorMessage errorMessage = new ProtocolErrorMessage (e .move , error );
440+ player .notifyListeners (errorMessage );
441+ observerBroadcast (new RoomPacket (id , errorMessage ));
442+ game .onPlayerLeft (player , ScoreCause .RULE_VIOLATION );
443+ throw new GameLogicException (e .toString (), e );
444+ } catch (GameLogicException e ) {
445+ player .notifyListeners (new ProtocolErrorMessage (data , e .getMessage ()));
446+ throw e ;
440447 }
441448 }
442449
443450 /** Finds player matching the given client. */
444- private Player resolvePlayer (Client client ) throws RescuableClientException {
451+ private Player resolvePlayer (Client client ) throws GameRoomException {
445452 for (PlayerRole role : getPlayers ()) {
446453 if (role .getClient ().equals (client )) {
447454 Player resolvedPlayer = role .getPlayer ();
448455
449456 if (resolvedPlayer == null ) {
450- throw new RescuableClientException ("Game isn't ready. Please wait before sending messages." );
457+ throw new GameException ("Game isn't ready. Please wait before sending messages." );
451458 }
452459
453460 return resolvedPlayer ;
454461 }
455462 }
456463
457- throw new RescuableClientException ("Client is not a member of game " + this .id );
464+ throw new GameRoomException ("Client is not a member of game " + this .id );
458465 }
459466
460467 /** Get {@link PlayerRole Players} that occupy a slot. */
@@ -519,7 +526,7 @@ public synchronized void pause(boolean pause) {
519526 * @param forced If true, the game will be forcibly started if starting
520527 * conditions are not met. This should result in a GameOver.
521528 */
522- public synchronized void step (boolean forced ) throws RescuableClientException {
529+ public synchronized void step (boolean forced ) throws GameRoomException {
523530 if (this .status == GameStatus .CREATED ) {
524531 if (forced ) {
525532 logger .warn ("Forcing game start for {}" , game );
0 commit comments