Prior to acting upon any call from usercode, ensure we're still connected.

Otherwise, throw an error.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@267 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Ray Greenwell
2007-03-30 19:58:26 +00:00
parent 4c696a579d
commit 744127a192
2 changed files with 46 additions and 0 deletions
@@ -123,6 +123,8 @@ public class GameControlBackend
{ {
_ezObj.removeListener(this); _ezObj.removeListener(this);
_ctx.getClient().getClientObject().removeListener(_userListener); _ctx.getClient().getClientObject().removeListener(_userListener);
_userFuncs = null; // disconnect
} }
protected function handleEZQuery (evt :Object) :void protected function handleEZQuery (evt :Object) :void
@@ -216,6 +218,7 @@ public class GameControlBackend
public function setProperty_v1 ( public function setProperty_v1 (
propName :String, value :Object, index :int, immediate :Boolean = true) :void propName :String, value :Object, index :int, immediate :Boolean = true) :void
{ {
validateConnected();
validatePropertyChange(propName, value, index); validatePropertyChange(propName, value, index);
var encoded :Object = EZObjectMarshaller.encode(value, (index == -1)); var encoded :Object = EZObjectMarshaller.encode(value, (index == -1));
@@ -230,6 +233,7 @@ public class GameControlBackend
public function testAndSetProperty_v1 ( public function testAndSetProperty_v1 (
propName :String, value :Object, testValue :Object, index :int) :void propName :String, value :Object, testValue :Object, index :int) :void
{ {
validateConnected();
validatePropertyChange(propName, value, index); validatePropertyChange(propName, value, index);
var encodedValue :Object = EZObjectMarshaller.encode(value, (index == -1)); var encodedValue :Object = EZObjectMarshaller.encode(value, (index == -1));
@@ -243,6 +247,7 @@ public class GameControlBackend
public function mergeCollection_v1 ( public function mergeCollection_v1 (
srcColl :String, intoColl :String) :void srcColl :String, intoColl :String) :void
{ {
validateConnected();
validateName(srcColl); validateName(srcColl);
validateName(intoColl); validateName(intoColl);
_ezObj.ezGameService.mergeCollection(_ctx.getClient(), _ezObj.ezGameService.mergeCollection(_ctx.getClient(),
@@ -252,6 +257,7 @@ public class GameControlBackend
public function sendMessage_v2 ( public function sendMessage_v2 (
messageName :String, value :Object, playerId :int) :void messageName :String, value :Object, playerId :int) :void
{ {
validateConnected();
validateName(messageName); validateName(messageName);
validateValue(value); validateValue(value);
@@ -263,6 +269,7 @@ public class GameControlBackend
public function setTicker_v1 (tickerName :String, msOfDelay :int) :void public function setTicker_v1 (tickerName :String, msOfDelay :int) :void
{ {
validateConnected();
validateName(tickerName); validateName(tickerName);
_ezObj.ezGameService.setTicker(_ctx.getClient(), _ezObj.ezGameService.setTicker(_ctx.getClient(),
tickerName, msOfDelay, createLoggingConfirmListener("setTicker")); tickerName, msOfDelay, createLoggingConfirmListener("setTicker"));
@@ -270,6 +277,7 @@ public class GameControlBackend
public function sendChat_v1 (msg :String) :void public function sendChat_v1 (msg :String) :void
{ {
validateConnected();
validateChat(msg); validateChat(msg);
// Post a message to the game object, the controller // Post a message to the game object, the controller
// will listen and call localChat(). // will listen and call localChat().
@@ -278,6 +286,7 @@ public class GameControlBackend
public function localChat_v1 (msg :String) :void public function localChat_v1 (msg :String) :void
{ {
validateConnected();
validateChat(msg); validateChat(msg);
// The sendChat() messages will end up being routed // The sendChat() messages will end up being routed
// through this method on each client. // through this method on each client.
@@ -287,6 +296,7 @@ public class GameControlBackend
public function getOccupants_v1 () :Array public function getOccupants_v1 () :Array
{ {
validateConnected();
var occs :Array = []; var occs :Array = [];
for (var ii :int = _ezObj.occupants.size() - 1; ii >= 0; ii--) { for (var ii :int = _ezObj.occupants.size() - 1; ii >= 0; ii--) {
occs.push(_ezObj.occupants.get(ii)); occs.push(_ezObj.occupants.get(ii));
@@ -297,6 +307,7 @@ public class GameControlBackend
public function getPlayers_v1 () :Array public function getPlayers_v1 () :Array
{ {
validateConnected();
if (_ezObj.players.length == 0) { if (_ezObj.players.length == 0) {
// party game // party game
return getOccupants_v1(); return getOccupants_v1();
@@ -311,6 +322,7 @@ public class GameControlBackend
public function getOccupantName_v1 (playerId :int) :String public function getOccupantName_v1 (playerId :int) :String
{ {
validateConnected();
var occInfo :OccupantInfo = var occInfo :OccupantInfo =
(_ezObj.occupantInfo.get(playerId) as OccupantInfo); (_ezObj.occupantInfo.get(playerId) as OccupantInfo);
return (occInfo == null) ? null : occInfo.username.toString(); return (occInfo == null) ? null : occInfo.username.toString();
@@ -318,17 +330,20 @@ public class GameControlBackend
public function getMyId_v1 () :int public function getMyId_v1 () :int
{ {
validateConnected();
return _ctx.getClient().getClientObject().getOid(); return _ctx.getClient().getClientObject().getOid();
} }
public function getControllerId_v1 () :int public function getControllerId_v1 () :int
{ {
validateConnected();
return _ezObj.controllerOid; return _ezObj.controllerOid;
} }
// TODO: table games only // TODO: table games only
public function getPlayerPosition_v1 (playerId :int) :int public function getPlayerPosition_v1 (playerId :int) :int
{ {
validateConnected();
var occInfo :OccupantInfo = var occInfo :OccupantInfo =
(_ezObj.occupantInfo.get(playerId) as OccupantInfo); (_ezObj.occupantInfo.get(playerId) as OccupantInfo);
if (occInfo == null) { if (occInfo == null) {
@@ -340,6 +355,7 @@ public class GameControlBackend
// TODO: table only // TODO: table only
public function getMyPosition_v1 () :int public function getMyPosition_v1 () :int
{ {
validateConnected();
return _ezObj.getPlayerIndex( return _ezObj.getPlayerIndex(
(_ctx.getClient().getClientObject() as BodyObject).getVisibleName()); (_ctx.getClient().getClientObject() as BodyObject).getVisibleName());
} }
@@ -347,18 +363,21 @@ public class GameControlBackend
// TODO: table only // TODO: table only
public function getTurnHolder_v1 () :int public function getTurnHolder_v1 () :int
{ {
validateConnected();
var occInfo :OccupantInfo = _ezObj.getOccupantInfo(_ezObj.turnHolder); var occInfo :OccupantInfo = _ezObj.getOccupantInfo(_ezObj.turnHolder);
return (occInfo == null) ? 0 : occInfo.bodyOid; return (occInfo == null) ? 0 : occInfo.bodyOid;
} }
public function getRound_v1 () :int public function getRound_v1 () :int
{ {
validateConnected();
return _ezObj.roundId; return _ezObj.roundId;
} }
public function getUserCookie_v2 ( public function getUserCookie_v2 (
playerId :int, callback :Function) :void playerId :int, callback :Function) :void
{ {
validateConnected();
// see if that cookie is already published // see if that cookie is already published
if (_ezObj.userCookies != null) { if (_ezObj.userCookies != null) {
var uc :UserCookie = var uc :UserCookie =
@@ -386,6 +405,7 @@ public class GameControlBackend
public function setUserCookie_v1 (cookie :Object) :Boolean public function setUserCookie_v1 (cookie :Object) :Boolean
{ {
validateConnected();
var ba :ByteArray = var ba :ByteArray =
(EZObjectMarshaller.encode(cookie, false) as ByteArray); (EZObjectMarshaller.encode(cookie, false) as ByteArray);
if (ba.length > MAX_USER_COOKIE) { if (ba.length > MAX_USER_COOKIE) {
@@ -400,28 +420,33 @@ public class GameControlBackend
public function isMyTurn_v1 () :Boolean public function isMyTurn_v1 () :Boolean
{ {
validateConnected();
return getUsername().equals(_ezObj.turnHolder); return getUsername().equals(_ezObj.turnHolder);
} }
public function isInPlay_v1 () :Boolean public function isInPlay_v1 () :Boolean
{ {
validateConnected();
return _ezObj.isInPlay(); return _ezObj.isInPlay();
} }
public function endTurn_v2 (nextPlayerId :int) :void public function endTurn_v2 (nextPlayerId :int) :void
{ {
validateConnected();
_ezObj.ezGameService.endTurn( _ezObj.ezGameService.endTurn(
_ctx.getClient(), nextPlayerId, createLoggingConfirmListener("endTurn")); _ctx.getClient(), nextPlayerId, createLoggingConfirmListener("endTurn"));
} }
public function endRound_v1 (nextRoundDelay :int) :void public function endRound_v1 (nextRoundDelay :int) :void
{ {
validateConnected();
_ezObj.ezGameService.endRound( _ezObj.ezGameService.endRound(
_ctx.getClient(), nextRoundDelay, createLoggingConfirmListener("endRound")); _ctx.getClient(), nextRoundDelay, createLoggingConfirmListener("endRound"));
} }
public function endGame_v2 (... winnerIds) :void public function endGame_v2 (... winnerIds) :void
{ {
validateConnected();
var winners :TypedArray = TypedArray.create(int); var winners :TypedArray = TypedArray.create(int);
while (winnerIds.length > 0) { while (winnerIds.length > 0) {
winners.push(int(winnerIds.shift())); winners.push(int(winnerIds.shift()));
@@ -432,6 +457,7 @@ public class GameControlBackend
public function getDictionaryLetterSet_v1 (locale :String, count :int, callback :Function) :void public function getDictionaryLetterSet_v1 (locale :String, count :int, callback :Function) :void
{ {
validateConnected();
var listener :InvocationService_ResultListener; var listener :InvocationService_ResultListener;
if (callback != null) { if (callback != null) {
var failure :Function = function (cause :String = null) :void { var failure :Function = function (cause :String = null) :void {
@@ -455,6 +481,7 @@ public class GameControlBackend
public function checkDictionaryWord_v1 ( public function checkDictionaryWord_v1 (
locale :String, word :String, callback :Function) :void locale :String, word :String, callback :Function) :void
{ {
validateConnected();
var listener :InvocationService_ResultListener; var listener :InvocationService_ResultListener;
if (callback != null) { if (callback != null) {
var failure :Function = function (cause :String = null) :void { var failure :Function = function (cause :String = null) :void {
@@ -482,6 +509,7 @@ public class GameControlBackend
public function populateCollection_v1 ( public function populateCollection_v1 (
collName :String, values :Array, clearExisting :Boolean) :void collName :String, values :Array, clearExisting :Boolean) :void
{ {
validateConnected();
validateName(collName); validateName(collName);
if (values == null) { if (values == null) {
throw new ArgumentError("Collection values may not be null."); throw new ArgumentError("Collection values may not be null.");
@@ -503,6 +531,7 @@ public class GameControlBackend
collName :String, count :int, msgOrPropName :String, playerId :int, collName :String, count :int, msgOrPropName :String, playerId :int,
consume :Boolean, callback :Function) :void consume :Boolean, callback :Function) :void
{ {
validateConnected();
validateName(collName); validateName(collName);
validateName(msgOrPropName); validateName(msgOrPropName);
if (count < 1) { if (count < 1) {
@@ -534,6 +563,7 @@ public class GameControlBackend
public function alterKeyEvents_v1 ( public function alterKeyEvents_v1 (
keyEventType :String, add :Boolean) :void keyEventType :String, add :Boolean) :void
{ {
validateConnected();
if (add) { if (add) {
_container.addEventListener(keyEventType, handleKeyEvent); _container.addEventListener(keyEventType, handleKeyEvent);
} else { } else {
@@ -543,6 +573,7 @@ public class GameControlBackend
public function focusContainer_v1 () :void public function focusContainer_v1 () :void
{ {
validateConnected();
_container.setFocus(); _container.setFocus();
} }
@@ -589,6 +620,16 @@ public class GameControlBackend
}); });
} }
/**
* Validate that we're not shutdown.
*/
public function validateConnected () :void
{
if (_userFuncs == null) {
throw new Error("Not connected.");
}
}
/** /**
* Verify that the property name / value are valid. * Verify that the property name / value are valid.
*/ */
@@ -89,6 +89,7 @@ public class GameControlCompatibility
public function getPlayerCount_v1 () :int public function getPlayerCount_v1 () :int
{ {
_backend.validateConnected();
if (_ezObj.players.length == 0) { if (_ezObj.players.length == 0) {
// party game // party game
return _ezObj.occupants.size(); return _ezObj.occupants.size();
@@ -100,6 +101,7 @@ public class GameControlCompatibility
public function getPlayerNames_v1 () :Array public function getPlayerNames_v1 () :Array
{ {
_backend.validateConnected();
var names :Array = []; var names :Array = [];
if (_ezObj.players.length == 0) { if (_ezObj.players.length == 0) {
// party game, count all occupants // party game, count all occupants
@@ -119,6 +121,7 @@ public class GameControlCompatibility
public function getMyIndex_v1 () :int public function getMyIndex_v1 () :int
{ {
_backend.validateConnected();
if (_ezObj.players.length == 0) { if (_ezObj.players.length == 0) {
// TODO: this shouldn't be based off of the String form of the name. // TODO: this shouldn't be based off of the String form of the name.
var array :Array = getPlayerNames_v1(); var array :Array = getPlayerNames_v1();
@@ -131,11 +134,13 @@ public class GameControlCompatibility
public function getTurnHolderIndex_v1 () :int public function getTurnHolderIndex_v1 () :int
{ {
_backend.validateConnected();
return _ezObj.getPlayerIndex(_ezObj.turnHolder); return _ezObj.getPlayerIndex(_ezObj.turnHolder);
} }
public function getWinnerIndexes_v1 () :Array /* of int */ public function getWinnerIndexes_v1 () :Array /* of int */
{ {
_backend.validateConnected();
var arr :Array = new Array(); var arr :Array = new Array();
if (_ezObj.winners != null) { if (_ezObj.winners != null) {
for (var ii :int = 0; ii < _ezObj.winners.length; ii++) { for (var ii :int = 0; ii < _ezObj.winners.length; ii++) {