From d2b4be178ad4e11d38dc873cde4cad1cb6fa9e91 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 13 Apr 2005 01:15:43 +0000 Subject: [PATCH] Moved rematch handling up the hierarchy, made Card constructor public in order to create specific cards for searching, etc. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3474 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/parlor/card/data/Card.java | 22 ++++---- .../trick/client/TrickCardGameService.java | 7 +++ .../trick/data/TrickCardGameMarshaller.java | 13 ++++- .../card/trick/data/TrickCardGameObject.java | 38 +++++++++++++ .../trick/server/TrickCardGameDispatcher.java | 6 +++ .../server/TrickCardGameManagerDelegate.java | 54 +++++++++++++++++++ .../trick/server/TrickCardGameProvider.java | 8 +++ 7 files changed, 136 insertions(+), 12 deletions(-) diff --git a/src/java/com/threerings/parlor/card/data/Card.java b/src/java/com/threerings/parlor/card/data/Card.java index f831e110f..f2a864f96 100644 --- a/src/java/com/threerings/parlor/card/data/Card.java +++ b/src/java/com/threerings/parlor/card/data/Card.java @@ -39,6 +39,17 @@ public class Card implements DSet.Entry, Comparable, CardCodes public Card () {} + /** + * Creates a new card. + * + * @param number the number of the card + * @param suit the suit of the card + */ + public Card (int number, int suit) + { + _value = (byte)((suit << 5) | number); + } + /** * Returns the value of the card, either from 2 to 11 or * KING, QUEEN, JACK, ACE, RED_JOKER, or BLACK_JOKER. @@ -225,17 +236,6 @@ public class Card implements DSet.Entry, Comparable, CardCodes } } - /** - * Protected constructor for Deck. - * - * @param number the number of the card - * @param suit the suit of the card - */ - protected Card (int number, int suit) - { - _value = (byte)((suit << 5) | number); - } - /** The number of the card. */ protected byte _value; diff --git a/src/java/com/threerings/parlor/card/trick/client/TrickCardGameService.java b/src/java/com/threerings/parlor/card/trick/client/TrickCardGameService.java index 2751fe6dc..7e5ecbe6b 100644 --- a/src/java/com/threerings/parlor/card/trick/client/TrickCardGameService.java +++ b/src/java/com/threerings/parlor/card/trick/client/TrickCardGameService.java @@ -29,4 +29,11 @@ public interface TrickCardGameService extends InvocationService * @param card the card to play */ public void playCard (Client client, Card card); + + /** + * A request for a rematch. + * + * @param client the client object + */ + public void requestRematch (Client client); } diff --git a/src/java/com/threerings/parlor/card/trick/data/TrickCardGameMarshaller.java b/src/java/com/threerings/parlor/card/trick/data/TrickCardGameMarshaller.java index 9a5ef48bf..8c20b4bba 100644 --- a/src/java/com/threerings/parlor/card/trick/data/TrickCardGameMarshaller.java +++ b/src/java/com/threerings/parlor/card/trick/data/TrickCardGameMarshaller.java @@ -48,8 +48,19 @@ public class TrickCardGameMarshaller extends InvocationMarshaller }); } + /** The method id used to dispatch {@link #requestRematch} requests. */ + public static final int REQUEST_REMATCH = 2; + + // documentation inherited from interface + public void requestRematch (Client arg1) + { + sendRequest(arg1, REQUEST_REMATCH, new Object[] { + + }); + } + /** The method id used to dispatch {@link #sendCardsToPlayer} requests. */ - public static final int SEND_CARDS_TO_PLAYER = 2; + public static final int SEND_CARDS_TO_PLAYER = 3; // documentation inherited from interface public void sendCardsToPlayer (Client arg1, int arg2, Card[] arg3) diff --git a/src/java/com/threerings/parlor/card/trick/data/TrickCardGameObject.java b/src/java/com/threerings/parlor/card/trick/data/TrickCardGameObject.java index ad4091897..29328fd58 100644 --- a/src/java/com/threerings/parlor/card/trick/data/TrickCardGameObject.java +++ b/src/java/com/threerings/parlor/card/trick/data/TrickCardGameObject.java @@ -43,6 +43,15 @@ public interface TrickCardGameObject extends TurnGameObject /** The number of states defined for the base trick card game object. */ public static final int TRICK_STATE_COUNT = 3; + /** Indicates that the player has not requested or accepted a rematch. */ + public static final int NO_REQUEST = 0; + + /** Indicates that the player has requested a rematch. */ + public static final int REQUESTS_REMATCH = 1; + + /** Indicates that the player has accepted the rematch request. */ + public static final int ACCEPTS_REMATCH = 2; + /** * Returns a reference to the trick card game service used to make * requests to the server. @@ -128,6 +137,35 @@ public interface TrickCardGameObject extends TurnGameObject */ public void setLastCardsPlayed (PlayerCard[] lastCardsPlayed); + /** + * Returns the name of the field that contains the rematch requests. + * + * @return the name of the rematchRequests field + */ + public String getRematchRequestsFieldName (); + + /** + * Returns the array of rematch requests. + * + * @return the array of rematch requests + */ + public int[] getRematchRequests (); + + /** + * Sets the array of rematch requests. + * + * @param rematchRequests the array of rematch requests + */ + public void setRematchRequests (int[] rematchRequests); + + /** + * Sets an element of the rematch request array. + * + * @param rematchRequest the rematch request value + * @param index the index at which to set the value + */ + public void setRematchRequestsAt (int rematchRequest, int index); + /** * Checks whether a user can play the specified card at this time. * diff --git a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameDispatcher.java b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameDispatcher.java index a0964f1b4..7bf1aef35 100644 --- a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameDispatcher.java +++ b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameDispatcher.java @@ -63,6 +63,12 @@ public class TrickCardGameDispatcher extends InvocationDispatcher ); return; + case TrickCardGameMarshaller.REQUEST_REMATCH: + ((TrickCardGameProvider)provider).requestRematch( + source + ); + return; + case TrickCardGameMarshaller.SEND_CARDS_TO_PLAYER: ((TrickCardGameProvider)provider).sendCardsToPlayer( source, diff --git a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameManagerDelegate.java b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameManagerDelegate.java index 9fa052a1c..f9187922e 100644 --- a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameManagerDelegate.java +++ b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameManagerDelegate.java @@ -116,6 +116,10 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate _trickCardGame.setTrickState(TrickCardGameObject.BETWEEN_HANDS); } + // initialize the array of rematch requests + _trickCardGame.setRematchRequests( + new int[_cardGame.getPlayerCount()]); + super.gameDidEnd(); } @@ -258,6 +262,56 @@ public class TrickCardGameManagerDelegate extends TurnGameManagerDelegate } } + // Documentation inherited. + public void requestRematch (ClientObject client) + throws InvocationException + { + // make sure the game is over + if (_cardGame.state != CardGameObject.GAME_OVER) { + throw new InvocationException("m.game_not_over"); + } + + // make sure the requester is one of the players + int pidx = _cgmgr.getPlayerIndex(client); + if (pidx == -1) { + throw new InvocationException("m.not_playing"); + } + + // make sure the player hasn't already requested + if (_trickCardGame.getRematchRequests()[pidx] != + TrickCardGameObject.NO_REQUEST) { + throw new InvocationException("m.already_requested"); + } + + // if player is first requesting, set to request; else set + // to accept + int req = (getRematchRequestCount() == 0 ? + TrickCardGameObject.REQUESTS_REMATCH : + TrickCardGameObject.ACCEPTS_REMATCH); + _trickCardGame.setRematchRequestsAt(req, pidx); + + // if all players accept the rematch, restart the game + if (getRematchRequestCount() == _cardGame.getPlayerCount()) { + _cgmgr.startGame(); + } + } + + /** + * Returns the number of players currently requesting or accepting + * a rematch. + */ + protected int getRematchRequestCount () + { + int[] rematchRequests = _trickCardGame.getRematchRequests(); + int count = 0; + for (int i = 0; i < rematchRequests.length; i++) { + if (rematchRequests[i] != TrickCardGameObject.NO_REQUEST) { + count++; + } + } + return count; + } + /** * Checks whether the trick is complete--that is, whether each player has * played a card. diff --git a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameProvider.java b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameProvider.java index b8b420331..fb76f71f1 100644 --- a/src/java/com/threerings/parlor/card/trick/server/TrickCardGameProvider.java +++ b/src/java/com/threerings/parlor/card/trick/server/TrickCardGameProvider.java @@ -33,4 +33,12 @@ public interface TrickCardGameProvider extends InvocationProvider */ public void playCard (ClientObject client, Card card) throws InvocationException; + + /** + * Processes a request for a rematch. + * + * @param client the client object + */ + public void requestRematch (ClientObject client) + throws InvocationException; }