Updates to EZGame to allow games to store 'user cookies'.

Then intention is that other games besides EZGame may use this as well,
so it's semi-separated, but for now it's part of EZGame.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@123 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Ray Greenwell
2006-11-15 03:01:49 +00:00
parent a4ae0dd2dc
commit cff4238b3e
19 changed files with 857 additions and 13 deletions
@@ -21,6 +21,7 @@
package com.threerings.ezgame.client {
import flash.utils.ByteArray;
import com.threerings.ezgame.client.EZGameService;
import com.threerings.presents.client.Client;
import com.threerings.presents.client.InvocationService;
@@ -42,6 +43,9 @@ public interface EZGameService extends InvocationService
// from Java interface EZGameService
function endTurn (arg1 :Client, arg2 :int, arg3 :InvocationService_InvocationListener) :void;
// from Java interface EZGameService
function getCookie (arg1 :Client, arg2 :int, arg3 :InvocationService_InvocationListener) :void;
// from Java interface EZGameService
function getFromCollection (arg1 :Client, arg2 :String, arg3 :Boolean, arg4 :int, arg5 :String, arg6 :int, arg7 :InvocationService_ConfirmListener) :void;
@@ -51,6 +55,9 @@ public interface EZGameService extends InvocationService
// from Java interface EZGameService
function sendMessage (arg1 :Client, arg2 :String, arg3 :Object, arg4 :int, arg5 :InvocationService_InvocationListener) :void;
// from Java interface EZGameService
function setCookie (arg1 :Client, arg2 :ByteArray, arg3 :InvocationService_InvocationListener) :void;
// from Java interface EZGameService
function setProperty (arg1 :Client, arg2 :String, arg3 :Object, arg4 :int, arg5 :InvocationService_InvocationListener) :void;
@@ -7,6 +7,7 @@ import flash.events.EventDispatcher;
import flash.utils.IExternalizable;
import flash.utils.ByteArray;
import flash.utils.Dictionary;
import com.threerings.io.TypedArray;
@@ -18,11 +19,16 @@ import com.threerings.util.StringUtil;
import com.threerings.presents.client.ConfirmAdapter;
import com.threerings.presents.client.InvocationService_ConfirmListener;
import com.threerings.presents.dobj.EntryAddedEvent;
import com.threerings.presents.dobj.EntryUpdatedEvent;
import com.threerings.presents.dobj.SetAdapter;
import com.threerings.crowd.data.BodyObject;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.ezgame.data.EZGameObject;
import com.threerings.ezgame.data.PropertySetEvent;
import com.threerings.ezgame.data.UserCookie;
import com.threerings.ezgame.util.EZObjectMarshaller;
import com.threerings.ezgame.EZGame;
@@ -41,6 +47,8 @@ public class GameObjectImpl extends EventDispatcher
_ctx = ctx;
_ezObj = ezObj;
_gameData = new GameData(this, _ezObj.getUserProps());
_ezObj.addListener(new SetAdapter(entryAdded, entryUpdated, null));
}
// from EZGame
@@ -259,6 +267,52 @@ public class GameObjectImpl extends EventDispatcher
return arr;
}
// from EZGame
public function getUserCookie (playerIndex :int, callback :Function) :void
{
// see if that cookie is already published
if (_ezObj.userCookies != null) {
var uc :UserCookie =
(_ezObj.userCookies.get(playerIndex) as UserCookie);
if (uc != null) {
callback(uc.cookie);
return;
}
}
if (_cookieCallbacks == null) {
_cookieCallbacks = new Dictionary();
}
var arr :Array = (_cookieCallbacks[playerIndex] as Array);
if (arr == null) {
arr = [];
_cookieCallbacks[playerIndex] = arr;
}
arr.push(callback);
// request it to be made so by the server
_ezObj.ezGameService.getCookie(_ctx.getClient(), playerIndex,
createLoggingListener("getUserCookie"));
}
// from EZGame
public function setUserCookie (cookie :Object) :Boolean
{
var ba :ByteArray =
(EZObjectMarshaller.encode(cookie, false) as ByteArray);
if (ba.length > MAX_USER_COOKIE) {
// not saved!
return false;
}
_ezObj.ezGameService.setCookie(_ctx.getClient(), null, // ba,
// TODO
///
/// TODO
createLoggingListener("setUserCookie"));
return true;
}
// from EZGame
public function isMyTurn () :Boolean
{
@@ -480,10 +534,55 @@ public class GameObjectImpl extends EventDispatcher
}
}
/**
* Handle entry updated
*/
private function entryAdded (event :EntryAddedEvent) :void
{
if (EZGameObject.USER_COOKIES == event.getName()) {
receivedUserCookie(event.getEntry() as UserCookie);
}
}
/**
* Handle entry updated
*/
private function entryUpdated (event :EntryUpdatedEvent) :void
{
if (EZGameObject.USER_COOKIES == event.getName()) {
receivedUserCookie(event.getEntry() as UserCookie);
}
}
/**
* Handle the arrival of a new UserCookie.
*/
private function receivedUserCookie (cookie :UserCookie) :void
{
if (_cookieCallbacks != null) {
var arr :Array = (_cookieCallbacks[cookie.playerIndex] as Array);
if (arr != null) {
delete _cookieCallbacks[cookie.playerIndex];
for each (var fn :Function in arr) {
try {
fn(cookie.cookie);
} catch (err :Error) {
// cope
}
}
}
}
}
protected var _ctx :CrowdContext;
protected var _ezObj :EZGameObject;
protected var _gameData :GameData;
/** playerIndex -> callback functions waiting for the cookie. */
protected var _cookieCallbacks :Dictionary;
protected static const MAX_USER_COOKIE :int = 4096;
}
}