-
Notifications
You must be signed in to change notification settings - Fork 398
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
Add /host and /skip commands. #102
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -321,6 +321,34 @@ public void process() { | |
return false; | ||
} | ||
|
||
/** | ||
* Change the game host to a different user. | ||
* | ||
* @param newHost | ||
* The user that should be the new host. | ||
* @return True if the host was changed successfully; false if not. | ||
*/ | ||
public boolean changeHost(final User user) { | ||
final Player newHost = getPlayerForUser(user); | ||
if (newHost == null) { | ||
return false; | ||
} | ||
|
||
final Player oldHost = host; | ||
if (oldHost == newHost) { | ||
return true; | ||
} | ||
host = newHost; | ||
|
||
if (oldHost != null) { | ||
notifyPlayerInfoChange(oldHost); | ||
} | ||
notifyPlayerInfoChange(newHost); | ||
notifyGameOptionsChanged(); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Add a spectator to the game. | ||
* | ||
|
@@ -924,6 +952,68 @@ private void skipIdlePlayers() { | |
} | ||
} | ||
|
||
/** | ||
* Skip an idle player, via an explicit request. | ||
* | ||
* @param target | ||
* The player to be skipped. | ||
* @return True if the player was skipped. | ||
*/ | ||
public boolean skipPlayer(final User target) { | ||
final Player player = getPlayerForUser(target); | ||
if (player == null) { | ||
return false; | ||
} | ||
|
||
if (player == getJudge()) { | ||
skipIdleJudge(); | ||
} else { | ||
synchronized (roundPlayers) { | ||
if (state != GameState.PLAYING) { | ||
return false; // not playing | ||
} | ||
|
||
final List<WhiteCard> cards = playedCards.getCards(player); | ||
if (cards != null && cards.size() >= blackCard.getPick()) { | ||
return false; // player already played | ||
} | ||
if (!roundPlayers.contains(player)) { | ||
return false; // player not in this round (newly joined or already skipped) | ||
} | ||
|
||
logger.info(String.format("Skipping player %s in game %d (requested).", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please include the username that requested the skip. Probably have to pass them in only for the log message... Alternatively the log message could be emitted from the handler. Just seems like it'd be useful for tracking abuse. |
||
player.getUser().toString(), id)); | ||
//player.skipped(); // don't do this for the moment | ||
|
||
final HashMap<ReturnableData, Object> data = getEventMap(); | ||
data.put(LongPollResponse.NICKNAME, player.getUser().getNickname()); | ||
data.put(LongPollResponse.EVENT, LongPollEvent.GAME_PLAYER_SKIPPED.toString()); | ||
broadcastToPlayers(MessageType.GAME_EVENT, data); | ||
|
||
// put their cards back | ||
final List<WhiteCard> returnCards = playedCards.remove(player); | ||
if (returnCards != null) { | ||
player.getHand().addAll(returnCards); | ||
sendCardsToPlayer(player, returnCards); | ||
} | ||
roundPlayers.remove(player); | ||
} | ||
|
||
notifyPlayerInfoChange(player); | ||
if (startJudging()) { | ||
judgingState(); | ||
} else if (roundPlayers.size() < 2) { | ||
// not enough players left to judge | ||
logger.info(String.format( | ||
"Skipping judging on game %d due to insufficient played cards after manual skip.", | ||
id)); | ||
returnCardsToHand(); | ||
startNextRound(); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private void killRoundTimer() { | ||
synchronized (roundTimerLock) { | ||
if (null != lastScheduledFuture) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2012, Andy Janata | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted | ||
* provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
* and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY | ||
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package net.socialgamer.cah.handlers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
import net.socialgamer.cah.Constants.AjaxOperation; | ||
import net.socialgamer.cah.Constants.AjaxRequest; | ||
import net.socialgamer.cah.Constants.ErrorCode; | ||
import net.socialgamer.cah.Constants.ReturnableData; | ||
import net.socialgamer.cah.RequestWrapper; | ||
import net.socialgamer.cah.data.ConnectedUsers; | ||
import net.socialgamer.cah.data.Game; | ||
import net.socialgamer.cah.data.GameManager; | ||
import net.socialgamer.cah.data.User; | ||
|
||
import com.google.inject.Inject; | ||
|
||
|
||
/** | ||
* Handler for host change requests. | ||
* | ||
* @author Gavin Lambert (uecasm) | ||
*/ | ||
public class GameHostHandler extends GameWithPlayerHandler { | ||
|
||
public static final String OP = AjaxOperation.GAME_HOST.toString(); | ||
|
||
private final ConnectedUsers connectedUsers; | ||
|
||
@Inject | ||
public GameHostHandler(final ConnectedUsers connectedUsers, final GameManager gameManager) { | ||
super(gameManager); | ||
this.connectedUsers = connectedUsers; | ||
} | ||
|
||
@Override | ||
public Map<ReturnableData, Object> handleWithUserInGame(final RequestWrapper request, | ||
final HttpSession session, final User user, final Game game) { | ||
if (null == request.getParameter(AjaxRequest.NICKNAME) | ||
|| request.getParameter(AjaxRequest.NICKNAME).isEmpty()) { | ||
return error(ErrorCode.NO_NICK_SPECIFIED); | ||
} | ||
|
||
// only an admin or the current host may change the host of a game | ||
if (!user.isAdmin() && user != game.getHost()) { | ||
return error(ErrorCode.NOT_GAME_HOST); | ||
} | ||
|
||
final User newHost = connectedUsers.getUser(request.getParameter(AjaxRequest.NICKNAME)); | ||
if (null == newHost) { | ||
return error(ErrorCode.NO_SUCH_USER); | ||
} | ||
|
||
if (game.changeHost(newHost)) { | ||
return new HashMap<ReturnableData, Object>(); | ||
} else { | ||
return error(ErrorCode.BAD_REQUEST); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a new error code (and message) for "user is not in this game"? |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2012, Andy Janata | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are permitted | ||
* provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
* and the following disclaimer. | ||
* * Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | ||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY | ||
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package net.socialgamer.cah.handlers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpSession; | ||
|
||
import net.socialgamer.cah.Constants.AjaxOperation; | ||
import net.socialgamer.cah.Constants.AjaxRequest; | ||
import net.socialgamer.cah.Constants.ErrorCode; | ||
import net.socialgamer.cah.Constants.ReturnableData; | ||
import net.socialgamer.cah.RequestWrapper; | ||
import net.socialgamer.cah.data.ConnectedUsers; | ||
import net.socialgamer.cah.data.Game; | ||
import net.socialgamer.cah.data.GameManager; | ||
import net.socialgamer.cah.data.User; | ||
|
||
import com.google.inject.Inject; | ||
|
||
|
||
/** | ||
* Handler for player skip requests. | ||
* | ||
* @author Gavin Lambert (uecasm) | ||
*/ | ||
public class GameSkipHandler extends GameWithPlayerHandler { | ||
|
||
public static final String OP = AjaxOperation.GAME_SKIP.toString(); | ||
|
||
private final ConnectedUsers connectedUsers; | ||
|
||
@Inject | ||
public GameSkipHandler(final ConnectedUsers connectedUsers, final GameManager gameManager) { | ||
super(gameManager); | ||
this.connectedUsers = connectedUsers; | ||
} | ||
|
||
@Override | ||
public Map<ReturnableData, Object> handleWithUserInGame(final RequestWrapper request, | ||
final HttpSession session, final User user, final Game game) { | ||
if (null == request.getParameter(AjaxRequest.NICKNAME) | ||
|| request.getParameter(AjaxRequest.NICKNAME).isEmpty()) { | ||
return error(ErrorCode.NO_NICK_SPECIFIED); | ||
} | ||
|
||
// only an admin or the current host may skip a player (maybe let the judge?) | ||
if (!user.isAdmin() && user != game.getHost()) { | ||
return error(ErrorCode.NOT_GAME_HOST); | ||
} | ||
|
||
final User target = connectedUsers.getUser(request.getParameter(AjaxRequest.NICKNAME)); | ||
if (null == target) { | ||
return error(ErrorCode.NO_SUCH_USER); | ||
} | ||
|
||
if (game.skipPlayer(target)) { | ||
return new HashMap<ReturnableData, Object>(); | ||
} else { | ||
return error(ErrorCode.BAD_REQUEST); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same re: error code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one's a little trickier -- a false return from I suppose I could make |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a log message. Should also include who requested the change (not that I currently think there could be admin abuse, but future proofing). Might just be easier to do the logging in the handler so that user doesn't need to be passed in here.