From d9d39750270465e4695f0581982fccdda5a26592 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 23 Nov 2004 02:47:38 +0000 Subject: [PATCH] Messages to receiver/service. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3228 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../card/client/CardGameController.java | 17 ++- ...{CardDecoder.java => CardGameDecoder.java} | 25 ++-- ...ardReceiver.java => CardGameReceiver.java} | 17 ++- .../parlor/card/client/CardGameService.java | 26 +++++ .../parlor/card/client/CardSprite.java | 2 +- .../parlor/card/data/CardGameMarshaller.java | 36 ++++++ .../parlor/card/data/CardGameObject.dobj | 33 ++++++ .../parlor/card/data/CardGameObject.java | 50 ++++++++ .../card/server/CardGameDispatcher.java | 53 +++++++++ .../parlor/card/server/CardGameManager.java | 110 +++++++++++++++++- .../parlor/card/server/CardGameProvider.java | 30 +++++ .../parlor/card/server/CardGameSender.java | 43 +++++++ .../parlor/card/server/CardSender.java | 30 ----- 13 files changed, 424 insertions(+), 48 deletions(-) rename src/java/com/threerings/parlor/card/client/{CardDecoder.java => CardGameDecoder.java} (51%) rename src/java/com/threerings/parlor/card/client/{CardReceiver.java => CardGameReceiver.java} (71%) create mode 100644 src/java/com/threerings/parlor/card/client/CardGameService.java create mode 100644 src/java/com/threerings/parlor/card/data/CardGameMarshaller.java create mode 100644 src/java/com/threerings/parlor/card/data/CardGameObject.dobj create mode 100644 src/java/com/threerings/parlor/card/data/CardGameObject.java create mode 100644 src/java/com/threerings/parlor/card/server/CardGameDispatcher.java create mode 100644 src/java/com/threerings/parlor/card/server/CardGameProvider.java create mode 100644 src/java/com/threerings/parlor/card/server/CardGameSender.java delete mode 100644 src/java/com/threerings/parlor/card/server/CardSender.java diff --git a/src/java/com/threerings/parlor/card/client/CardGameController.java b/src/java/com/threerings/parlor/card/client/CardGameController.java index 455fe29c8..162b60647 100644 --- a/src/java/com/threerings/parlor/card/client/CardGameController.java +++ b/src/java/com/threerings/parlor/card/client/CardGameController.java @@ -23,6 +23,7 @@ package com.threerings.parlor.card.client; import com.threerings.crowd.data.PlaceObject; +import com.threerings.parlor.card.data.Card; import com.threerings.parlor.card.data.CardCodes; import com.threerings.parlor.card.data.Hand; @@ -33,7 +34,7 @@ import com.threerings.parlor.game.GameController; * accepting dealt hands. */ public abstract class CardGameController extends GameController - implements CardCodes, CardReceiver + implements CardCodes, CardGameReceiver { // Documentation inherited. public void willEnterPlace (PlaceObject plobj) @@ -41,7 +42,7 @@ public abstract class CardGameController extends GameController super.willEnterPlace(plobj); _ctx.getClient().getInvocationDirector().registerReceiver( - new CardDecoder(this)); + new CardGameDecoder(this)); } // Documentation inherited. @@ -50,7 +51,7 @@ public abstract class CardGameController extends GameController super.didLeavePlace(plobj); _ctx.getClient().getInvocationDirector().unregisterReceiver( - CardDecoder.RECEIVER_CODE); + CardGameDecoder.RECEIVER_CODE); } /** @@ -61,4 +62,14 @@ public abstract class CardGameController extends GameController */ public void receivedHand (Hand hand) {} + + /** + * Dispatched to the client when it has received a set of cards + * from another player. Default implementation does nothing. + * + * @param plidx the index of the player providing the cards + * @param cards the cards received + */ + public void receivedCardsFromPlayer (int plidx, Card[] cards) + {} } diff --git a/src/java/com/threerings/parlor/card/client/CardDecoder.java b/src/java/com/threerings/parlor/card/client/CardGameDecoder.java similarity index 51% rename from src/java/com/threerings/parlor/card/client/CardDecoder.java rename to src/java/com/threerings/parlor/card/client/CardGameDecoder.java index d894cb7ae..0645a4e3a 100644 --- a/src/java/com/threerings/parlor/card/client/CardDecoder.java +++ b/src/java/com/threerings/parlor/card/client/CardGameDecoder.java @@ -3,27 +3,32 @@ package com.threerings.parlor.card.client; -import com.threerings.parlor.card.client.CardReceiver; +import com.threerings.parlor.card.client.CardGameReceiver; +import com.threerings.parlor.card.data.Card; import com.threerings.parlor.card.data.Hand; import com.threerings.presents.client.InvocationDecoder; /** - * Dispatches calls to a {@link CardReceiver} instance. + * Dispatches calls to a {@link CardGameReceiver} instance. */ -public class CardDecoder extends InvocationDecoder +public class CardGameDecoder extends InvocationDecoder { /** The generated hash code used to identify this receiver class. */ - public static final String RECEIVER_CODE = "bfa3311cedc30969d04d8b6259d6ffe0"; + public static final String RECEIVER_CODE = "0718199d459e31d8d673744c71b0e788"; - /** The method id used to dispatch {@link CardReceiver#receivedHand} + /** The method id used to dispatch {@link CardGameReceiver#receivedHand} * notifications. */ public static final int RECEIVED_HAND = 1; + /** The method id used to dispatch {@link CardGameReceiver#receivedCardsFromPlayer} + * notifications. */ + public static final int RECEIVED_CARDS_FROM_PLAYER = 2; + /** * Creates a decoder that may be registered to dispatch invocation * service notifications to the specified receiver. */ - public CardDecoder (CardReceiver receiver) + public CardGameDecoder (CardGameReceiver receiver) { this.receiver = receiver; } @@ -39,11 +44,17 @@ public class CardDecoder extends InvocationDecoder { switch (methodId) { case RECEIVED_HAND: - ((CardReceiver)receiver).receivedHand( + ((CardGameReceiver)receiver).receivedHand( (Hand)args[0] ); return; + case RECEIVED_CARDS_FROM_PLAYER: + ((CardGameReceiver)receiver).receivedCardsFromPlayer( + ((Integer)args[0]).intValue(), (Card[])args[1] + ); + return; + default: super.dispatchNotification(methodId, args); } diff --git a/src/java/com/threerings/parlor/card/client/CardReceiver.java b/src/java/com/threerings/parlor/card/client/CardGameReceiver.java similarity index 71% rename from src/java/com/threerings/parlor/card/client/CardReceiver.java rename to src/java/com/threerings/parlor/card/client/CardGameReceiver.java index 34e363780..9be934aa6 100644 --- a/src/java/com/threerings/parlor/card/client/CardReceiver.java +++ b/src/java/com/threerings/parlor/card/client/CardGameReceiver.java @@ -21,16 +21,16 @@ package com.threerings.parlor.card.client; +import com.threerings.parlor.card.data.Card; import com.threerings.parlor.card.data.Hand; import com.threerings.presents.client.InvocationReceiver; /** - * Defines, for the parlor services, a set of notifications delivered - * asynchronously by the server to the client. These are handled by the - * {@link ParlorDirector}. + * Defines, for the card game services, a set of notifications delivered + * asynchronously by the server to the client. */ -public interface CardReceiver extends InvocationReceiver +public interface CardGameReceiver extends InvocationReceiver { /** * Dispatched to the client when it has received a hand of cards. @@ -38,4 +38,13 @@ public interface CardReceiver extends InvocationReceiver * @param hand the received hand */ public void receivedHand (Hand hand); + + /** + * Dispatched to the client when it has received a set of cards + * from another player. + * + * @param plidx the index of the player providing the cards + * @param cards the cards received + */ + public void receivedCardsFromPlayer (int plidx, Card[] cards); } diff --git a/src/java/com/threerings/parlor/card/client/CardGameService.java b/src/java/com/threerings/parlor/card/client/CardGameService.java new file mode 100644 index 000000000..1aa9ae3f8 --- /dev/null +++ b/src/java/com/threerings/parlor/card/client/CardGameService.java @@ -0,0 +1,26 @@ +// +// $Id: RoisterService.java 17829 2004-11-12 20:24:43Z mdb $ + +package com.threerings.parlor.card.client; + +import com.threerings.parlor.card.data.Card; + +import com.threerings.presents.client.Client; +import com.threerings.presents.client.InvocationService; + +/** + * Service calls related to card games. + */ +public interface CardGameService extends InvocationService +{ + /** + * Sends a set of cards to another player. + * + * @param client the client object + * @param playerIndex the index of the player to receive the cards + * @param cards the cards to send + * @param cl a listener to notify on success/failure + */ + public void sendCardsToPlayer (Client client, int playerIndex, Card[] cards, + ConfirmListener cl); +} diff --git a/src/java/com/threerings/parlor/card/client/CardSprite.java b/src/java/com/threerings/parlor/card/client/CardSprite.java index 301e80d79..fcb8864c4 100644 --- a/src/java/com/threerings/parlor/card/client/CardSprite.java +++ b/src/java/com/threerings/parlor/card/client/CardSprite.java @@ -130,7 +130,7 @@ public class CardSprite extends OrientableImageSprite /** * Updates the mirage according to the current state. */ - private void updateMirage () + protected void updateMirage () { setMirage(_facingUp ? _panel.getCardImage(_card) : _panel.getCardBackImage()); diff --git a/src/java/com/threerings/parlor/card/data/CardGameMarshaller.java b/src/java/com/threerings/parlor/card/data/CardGameMarshaller.java new file mode 100644 index 000000000..dcbb4cf79 --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/CardGameMarshaller.java @@ -0,0 +1,36 @@ +// +// $Id$ + +package com.threerings.parlor.card.data; + +import com.threerings.parlor.card.client.CardGameService; +import com.threerings.parlor.card.data.Card; +import com.threerings.presents.client.Client; +import com.threerings.presents.client.InvocationService.ConfirmListener; +import com.threerings.presents.data.InvocationMarshaller; +import com.threerings.presents.dobj.InvocationResponseEvent; + +/** + * Provides the implementation of the {@link CardGameService} interface + * that marshalls the arguments and delivers the request to the provider + * on the server. Also provides an implementation of the response listener + * interfaces that marshall the response arguments and deliver them back + * to the requesting client. + */ +public class CardGameMarshaller extends InvocationMarshaller + implements CardGameService +{ + /** The method id used to dispatch {@link #sendCardsToPlayer} requests. */ + public static final int SEND_CARDS_TO_PLAYER = 1; + + // documentation inherited from interface + public void sendCardsToPlayer (Client arg1, int arg2, Card[] arg3, ConfirmListener arg4) + { + ConfirmMarshaller listener4 = new ConfirmMarshaller(); + listener4.listener = arg4; + sendRequest(arg1, SEND_CARDS_TO_PLAYER, new Object[] { + new Integer(arg2), arg3, listener4 + }); + } + +} diff --git a/src/java/com/threerings/parlor/card/data/CardGameObject.dobj b/src/java/com/threerings/parlor/card/data/CardGameObject.dobj new file mode 100644 index 000000000..2611003a2 --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/CardGameObject.dobj @@ -0,0 +1,33 @@ +// +// $Id: TurnGameObject.java 3099 2004-08-27 02:21:06Z mdb $ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.parlor.card.data; + +import com.threerings.parlor.game.GameObject; + +/** + * Game object class for card games. + */ +public class CardGameObject extends GameObject +{ + /** The card game service interface. */ + public CardGameMarshaller cardGameService; +} diff --git a/src/java/com/threerings/parlor/card/data/CardGameObject.java b/src/java/com/threerings/parlor/card/data/CardGameObject.java new file mode 100644 index 000000000..868643b0a --- /dev/null +++ b/src/java/com/threerings/parlor/card/data/CardGameObject.java @@ -0,0 +1,50 @@ +// +// $Id: TurnGameObject.java 3099 2004-08-27 02:21:06Z mdb $ +// +// Narya library - tools for developing networked games +// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved +// http://www.threerings.net/code/narya/ +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.threerings.parlor.card.data; + +import com.threerings.parlor.game.GameObject; + +/** + * Game object class for card games. + */ +public class CardGameObject extends GameObject +{ + /** The field name of the cardGameService field. */ + public static final String CARD_GAME_SERVICE = "cardGameService"; + + /** The card game service interface. */ + public CardGameMarshaller cardGameService; + + /** + * Requests that the cardGameService field be set to the specified + * value. The local value will be updated immediately and an event + * will be propagated through the system to notify all listeners that + * the attribute did change. Proxied copies of this object (on + * clients) will apply the value change when they received the + * attribute changed notification. + */ + public void setCardGameService (CardGameMarshaller cardGameService) + { + requestAttributeChange(CARD_GAME_SERVICE, cardGameService); + this.cardGameService = cardGameService; + } +} diff --git a/src/java/com/threerings/parlor/card/server/CardGameDispatcher.java b/src/java/com/threerings/parlor/card/server/CardGameDispatcher.java new file mode 100644 index 000000000..dd4c07769 --- /dev/null +++ b/src/java/com/threerings/parlor/card/server/CardGameDispatcher.java @@ -0,0 +1,53 @@ +// +// $Id$ + +package com.threerings.parlor.card.server; + +import com.threerings.parlor.card.client.CardGameService; +import com.threerings.parlor.card.data.Card; +import com.threerings.parlor.card.data.CardGameMarshaller; +import com.threerings.presents.client.Client; +import com.threerings.presents.client.InvocationService.ConfirmListener; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.data.InvocationMarshaller; +import com.threerings.presents.server.InvocationDispatcher; +import com.threerings.presents.server.InvocationException; + +/** + * Dispatches requests to the {@link CardGameProvider}. + */ +public class CardGameDispatcher extends InvocationDispatcher +{ + /** + * Creates a dispatcher that may be registered to dispatch invocation + * service requests for the specified provider. + */ + public CardGameDispatcher (CardGameProvider provider) + { + this.provider = provider; + } + + // documentation inherited + public InvocationMarshaller createMarshaller () + { + return new CardGameMarshaller(); + } + + // documentation inherited + public void dispatchRequest ( + ClientObject source, int methodId, Object[] args) + throws InvocationException + { + switch (methodId) { + case CardGameMarshaller.SEND_CARDS_TO_PLAYER: + ((CardGameProvider)provider).sendCardsToPlayer( + source, + ((Integer)args[0]).intValue(), (Card[])args[1], (ConfirmListener)args[2] + ); + return; + + default: + super.dispatchRequest(source, methodId, args); + } + } +} diff --git a/src/java/com/threerings/parlor/card/server/CardGameManager.java b/src/java/com/threerings/parlor/card/server/CardGameManager.java index 9a2ee7d88..b96a14288 100644 --- a/src/java/com/threerings/parlor/card/server/CardGameManager.java +++ b/src/java/com/threerings/parlor/card/server/CardGameManager.java @@ -21,16 +21,23 @@ package com.threerings.parlor.card.server; +import com.threerings.crowd.data.BodyObject; + import com.threerings.parlor.card.Log; +import com.threerings.parlor.card.data.Card; import com.threerings.parlor.card.data.CardCodes; +import com.threerings.parlor.card.data.CardGameMarshaller; +import com.threerings.parlor.card.data.CardGameObject; import com.threerings.parlor.card.data.Deck; import com.threerings.parlor.card.data.Hand; import com.threerings.parlor.game.GameManager; +import com.threerings.presents.client.InvocationService.ConfirmListener; import com.threerings.presents.data.ClientObject; import com.threerings.presents.dobj.MessageEvent; +import com.threerings.presents.server.InvocationException; import com.threerings.presents.server.PresentsServer; /** @@ -38,8 +45,28 @@ import com.threerings.presents.server.PresentsServer; * hands of cards to all players. */ public class CardGameManager extends GameManager - implements CardCodes + implements CardCodes, CardGameProvider { + // Documentation inherited. + protected void didStartup () + { + super.didStartup(); + + _cardgameobj = (CardGameObject)_gameobj; + + _cardgameobj.setCardGameService( + (CardGameMarshaller)PresentsServer.invmgr.registerDispatcher( + new CardGameDispatcher(this), false)); + } + + // Documentation inherited. + protected void didShutdown () + { + super.didShutdown(); + + PresentsServer.invmgr.clearDispatcher(_cardgameobj.cardGameService); + } + /** * Deals a hand of cards to the player at the specified index from * the given Deck. @@ -58,7 +85,7 @@ public class CardGameManager extends GameManager else { Hand hand = deck.dealHand(size); - CardSender.sendHand( + CardGameSender.sendHand( (ClientObject)PresentsServer.omgr.getObject( _playerOids[playerIndex]), hand); @@ -90,5 +117,82 @@ public class CardGameManager extends GameManager return hands; } - } + } + + /** + * Gets the player index of the specified client object, or -1 + * if the client object does not represent a player. + */ + public int getPlayerIndex (ClientObject client) + { + int oid = client.getOid(); + for (int i=0;i<_playerOids.length;i++) { + if (_playerOids[i] == oid) { + return i; + } + } + return -1; + } + + /** + * Checks whether or not it is acceptable to transfer the given set of + * cards from the first player to the second. Default implementation + * simply returns true. + * + * @param fromPlayerIdx the index of the sending player + * @param toPlayerIdx the index of the receiving player + * @param cards the cards to send + * @return true if the transfer is should proceed, false otherwise + */ + protected boolean acceptTransferBetweenPlayers(int fromPlayerIdx, + int toPlayerIdx, Card[] cards) + { + return true; + } + + /** + * Processes a request to send a set of cards from one player to another. + * Calls {@link #acceptTransferBetweenPlayers + * acceptTransferBetweenPlayers} to determine whether or not + * to process the transfer, and {@link #transferCardsBetweenPlayers + * transferCardsBetweenPlayers} to + * perform the transfer if accepted. + * + * @param client the client object + * @param playerIndex the index of the player to receive the cards + * @param cards the cards to send + * @param cl a listener to notify on success/failure + */ + public void sendCardsToPlayer (ClientObject client, int playerIndex, + Card[] cards, ConfirmListener cl) + throws InvocationException + { + int fromPlayerIdx = getPlayerIndex(client); + + if (acceptTransferBetweenPlayers(fromPlayerIdx, playerIndex, cards)) { + transferCardsBetweenPlayers(getPlayerIndex(client), playerIndex, + cards); + cl.requestProcessed(); + } else { + throw new InvocationException("m.transfer_rejected"); + } + } + + /** + * Sends a set of cards from one player to another. + * + * @param fromPlayerIdx the index of the player sending the cards + * @param toPlayerIdx the index of the player receiving the cards + * @param cards the cards to be exchanged + */ + public void transferCardsBetweenPlayers (int fromPlayerIdx, + int toPlayerIdx, Card[] cards) + { + CardGameSender.sendCardsFromPlayer( + (ClientObject)PresentsServer.omgr.getObject( + _playerOids[toPlayerIdx]), fromPlayerIdx, cards); + } + + /** The card game object. */ + protected CardGameObject _cardgameobj; } diff --git a/src/java/com/threerings/parlor/card/server/CardGameProvider.java b/src/java/com/threerings/parlor/card/server/CardGameProvider.java new file mode 100644 index 000000000..096a39ad7 --- /dev/null +++ b/src/java/com/threerings/parlor/card/server/CardGameProvider.java @@ -0,0 +1,30 @@ +// +// $Id: RoisterService.java 17829 2004-11-12 20:24:43Z mdb $ + +package com.threerings.parlor.card.server; + +import com.threerings.parlor.card.data.Card; + +import com.threerings.presents.client.InvocationService.ConfirmListener; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.server.InvocationException; +import com.threerings.presents.server.InvocationProvider; + +/** + * Service calls related to card games. + */ +public interface CardGameProvider extends InvocationProvider +{ + /** + * Sends a set of cards to another player. + * + * @param client the client object + * @param playerIndex the index of the player to receive the cards + * @param cards the cards to send + * @param cl a listener to notify on success/failure + * @exception InvocationException if an error occurs + */ + public void sendCardsToPlayer (ClientObject client, int playerIndex, + Card[] cards, ConfirmListener cl) + throws InvocationException; +} diff --git a/src/java/com/threerings/parlor/card/server/CardGameSender.java b/src/java/com/threerings/parlor/card/server/CardGameSender.java new file mode 100644 index 000000000..a64df48ad --- /dev/null +++ b/src/java/com/threerings/parlor/card/server/CardGameSender.java @@ -0,0 +1,43 @@ +// +// $Id$ + +package com.threerings.parlor.card.server; + +import com.threerings.parlor.card.client.CardGameDecoder; +import com.threerings.parlor.card.client.CardGameReceiver; +import com.threerings.parlor.card.data.Card; +import com.threerings.parlor.card.data.Hand; +import com.threerings.presents.data.ClientObject; +import com.threerings.presents.server.InvocationSender; + +/** + * Used to issue notifications to a {@link CardGameReceiver} instance on a + * client. + */ +public class CardGameSender extends InvocationSender +{ + /** + * Issues a notification that will result in a call to {@link + * CardGameReceiver#receivedHand} on a client. + */ + public static void sendHand ( + ClientObject target, Hand arg1) + { + sendNotification( + target, CardGameDecoder.RECEIVER_CODE, CardGameDecoder.RECEIVED_HAND, + new Object[] { arg1 }); + } + + /** + * Issues a notification that will result in a call to {@link + * CardGameReceiver#receivedCardsFromPlayer} on a client. + */ + public static void sendCardsFromPlayer ( + ClientObject target, int arg1, Card[] arg2) + { + sendNotification( + target, CardGameDecoder.RECEIVER_CODE, CardGameDecoder.RECEIVED_CARDS_FROM_PLAYER, + new Object[] { new Integer(arg1), arg2 }); + } + +} diff --git a/src/java/com/threerings/parlor/card/server/CardSender.java b/src/java/com/threerings/parlor/card/server/CardSender.java deleted file mode 100644 index d22010238..000000000 --- a/src/java/com/threerings/parlor/card/server/CardSender.java +++ /dev/null @@ -1,30 +0,0 @@ -// -// $Id$ - -package com.threerings.parlor.card.server; - -import com.threerings.parlor.card.client.CardDecoder; -import com.threerings.parlor.card.client.CardReceiver; -import com.threerings.parlor.card.data.Hand; -import com.threerings.presents.data.ClientObject; -import com.threerings.presents.server.InvocationSender; - -/** - * Used to issue notifications to a {@link CardReceiver} instance on a - * client. - */ -public class CardSender extends InvocationSender -{ - /** - * Issues a notification that will result in a call to {@link - * CardReceiver#receivedHand} on a client. - */ - public static void sendHand ( - ClientObject target, Hand arg1) - { - sendNotification( - target, CardDecoder.RECEIVER_CODE, CardDecoder.RECEIVED_HAND, - new Object[] { arg1 }); - } - -}