Switched to receiver for dealing cards.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3224 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2004-11-19 19:04:56 +00:00
parent f5fc8f9d2a
commit 72a6f2f966
6 changed files with 149 additions and 32 deletions
@@ -0,0 +1,51 @@
//
// $Id$
package com.threerings.parlor.card.client;
import com.threerings.parlor.card.client.CardReceiver;
import com.threerings.parlor.card.data.Hand;
import com.threerings.presents.client.InvocationDecoder;
/**
* Dispatches calls to a {@link CardReceiver} instance.
*/
public class CardDecoder extends InvocationDecoder
{
/** The generated hash code used to identify this receiver class. */
public static final String RECEIVER_CODE = "bfa3311cedc30969d04d8b6259d6ffe0";
/** The method id used to dispatch {@link CardReceiver#receivedHand}
* notifications. */
public static final int RECEIVED_HAND = 1;
/**
* Creates a decoder that may be registered to dispatch invocation
* service notifications to the specified receiver.
*/
public CardDecoder (CardReceiver receiver)
{
this.receiver = receiver;
}
// documentation inherited
public String getReceiverCode ()
{
return RECEIVER_CODE;
}
// documentation inherited
public void dispatchNotification (int methodId, Object[] args)
{
switch (methodId) {
case RECEIVED_HAND:
((CardReceiver)receiver).receivedHand(
(Hand)args[0]
);
return;
default:
super.dispatchNotification(methodId, args);
}
}
}
@@ -1,5 +1,5 @@
//
// $Id: CardGameController.java,v 1.4 2004/10/15 18:20:28 andrzej Exp $
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -21,42 +21,42 @@
package com.threerings.parlor.card.client;
import com.threerings.crowd.data.PlaceConfig;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.parlor.card.Log;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.parlor.card.data.CardCodes;
import com.threerings.parlor.card.data.Hand;
import com.threerings.parlor.game.GameController;
import com.threerings.presents.dobj.MessageEvent;
import com.threerings.presents.dobj.MessageListener;
/**
* A controller class for card games. Handles common functions like
* accepting dealt hands.
*/
public abstract class CardGameController extends GameController
implements MessageListener,
CardCodes
implements CardCodes
{
// Documentation inhertied
public void init (CrowdContext context, PlaceConfig config)
// Documentation inherited.
public void willEnterPlace (PlaceObject plobj)
{
super.init(context, config);
super.willEnterPlace(plobj);
_ctx.getClient().getClientObject().addListener(this);
CardReceiver cgr = new CardReceiver() {
public void receivedHand (Hand hand) {
handDealt(hand);
}
};
_ctx.getClient().getInvocationDirector().registerReceiver(
new CardDecoder(cgr));
}
// Documentation inherited
public void messageReceived (MessageEvent event)
{
if (event.getName().equals(TAKE_HAND)) {
handDealt((Hand)event.getArgs()[0]);
}
// Documentation inherited.
public void didLeavePlace (PlaceObject plobj)
{
super.didLeavePlace(plobj);
_ctx.getClient().getInvocationDirector().unregisterReceiver(
CardDecoder.RECEIVER_CODE);
}
/**
@@ -0,0 +1,41 @@
//
// $Id: ParlorReceiver.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.client;
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}.
*/
public interface CardReceiver extends InvocationReceiver
{
/**
* Dispatched to the client when it has received a hand of cards.
*
* @param hand the received hand
*/
public void receivedHand (Hand hand);
}
@@ -1,5 +1,5 @@
//
// $Id: CardCodes.java,v 1.3 2004/11/01 22:10:21 andrzej Exp $
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -57,7 +57,4 @@ public interface CardCodes extends InvocationCodes
/** The number of the black joker. */
public static final int BLACK_JOKER = 16;
/** A message that carries a Hand of cards to a player. */
public static final String TAKE_HAND = "take_hand";
}
@@ -29,7 +29,9 @@ import com.threerings.parlor.card.data.Hand;
import com.threerings.parlor.game.GameManager;
import com.threerings.presents.data.ClientObject;
import com.threerings.presents.dobj.MessageEvent;
import com.threerings.presents.server.PresentsServer;
/**
* A manager class for card games. Handles common functions like dealing
@@ -56,13 +58,9 @@ public class CardGameManager extends GameManager
else {
Hand hand = deck.dealHand(size);
_omgr.postEvent(
new MessageEvent(
_playerOids[playerIndex],
TAKE_HAND,
new Object[] { hand }
)
);
CardSender.sendHand(
(ClientObject)PresentsServer.omgr.getObject(
_playerOids[playerIndex]), hand);
return hand;
}
@@ -0,0 +1,30 @@
//
// $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 });
}
}