Messages to receiver/service.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3228 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-11-23 02:47:38 +00:00
parent a9ccfc8553
commit d9d3975027
13 changed files with 424 additions and 48 deletions
@@ -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)
{}
}
@@ -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);
}
@@ -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);
}
@@ -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);
}
@@ -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());
@@ -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
});
}
}
@@ -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;
}
@@ -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 <code>cardGameService</code> field. */
public static final String CARD_GAME_SERVICE = "cardGameService";
/** The card game service interface. */
public CardGameMarshaller cardGameService;
/**
* Requests that the <code>cardGameService</code> 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;
}
}
@@ -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);
}
}
}
@@ -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;
}
@@ -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;
}
@@ -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 });
}
}
@@ -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 });
}
}