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
This commit is contained in:
Andrzej Kapolka
2005-04-13 01:15:43 +00:00
parent 259870ee81
commit d2b4be178a
7 changed files with 136 additions and 12 deletions
@@ -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;
@@ -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);
}
@@ -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)
@@ -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.
*
@@ -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,
@@ -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.
@@ -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;
}