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
+37
View File
@@ -178,5 +178,42 @@ public interface EZGame
* End the game. The specified player indexes are winners!
*/
function endGame (winnerIndex :int, ... rest) :void;
// function getCurrentRoom () :int;
//
// function sendPlayerToRoom (playerIndex :int, room :int) :void
/**
* Get the user-specific game data for the specified user. The
* first time this is requested per game instance it will be retrieved
* from the database. After that, it will be returned from memory.
*/
function getUserCookie (playerIndex :int, callback :Function) :void;
/**
* Store persistent data that can later be retrieved by an instance
* of this game. The maximum size of this data is 4096 bytes AFTER
* AMF3 encoding.
*
* Note: there is no playerIndex parameter because a cookie may only
* be stored for the current player.
*
* @return false if the cookie could not be encoded to 4096 bytes
* or less; true if the cookie is going to try to be saved. There is
* no guarantee it will be saved and no way to find out if it failed,
* but if it fails it will be because the shit hit the fan so hard that
* there's nothing you can do anyway.
*/
function setUserCookie (cookie :Object) :Boolean;
//
// /**
// * Check to see if the user has the specified token.
// */
// function checkUserToken (token :String, callback :Function) :void;
//
// /**
// * Take the user to a purchase page.
// */
// function purchaseUserToken (token :String, callback :Function) :void;
}
}
@@ -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;
}
}
@@ -29,6 +29,10 @@ public class EZGameConfig extends GameConfig
// For now, the configData is either a classname or url.
public var configData :String;
/** If non-zero, a game id used to persistently identify the game.
* This could be thought of as a new-style rating id. */
public var persistentGameId:int;
public function EZGameConfig ()
{
// nothing needed
@@ -89,6 +93,7 @@ public class EZGameConfig extends GameConfig
super.readObject(ins);
configData = (ins.readField(String) as String);
persistentGameId = ins.readInt();
}
// from interface Streamable
@@ -97,6 +102,7 @@ public class EZGameConfig extends GameConfig
super.writeObject(out);
out.writeField(configData);
out.writeInt(persistentGameId);
}
}
}
@@ -21,6 +21,7 @@
package com.threerings.ezgame.data {
import flash.utils.ByteArray;
import com.threerings.util.*; // for Float, Integer, etc.
import com.threerings.ezgame.client.EZGameService;
@@ -80,8 +81,21 @@ public class EZGameMarshaller extends InvocationMarshaller
]);
}
/** The method id used to dispatch {@link #getCookie} requests. */
public static const GET_COOKIE :int = 4;
// from interface EZGameService
public function getCookie (arg1 :Client, arg2 :int, arg3 :InvocationService_InvocationListener) :void
{
var listener3 :InvocationMarshaller_ListenerMarshaller = new InvocationMarshaller_ListenerMarshaller();
listener3.listener = arg3;
sendRequest(arg1, GET_COOKIE, [
Integer.valueOf(arg2), listener3
]);
}
/** The method id used to dispatch {@link #getFromCollection} requests. */
public static const GET_FROM_COLLECTION :int = 4;
public static const GET_FROM_COLLECTION :int = 5;
// from interface EZGameService
public function getFromCollection (arg1 :Client, arg2 :String, arg3 :Boolean, arg4 :int, arg5 :String, arg6 :int, arg7 :InvocationService_ConfirmListener) :void
@@ -94,7 +108,7 @@ public class EZGameMarshaller extends InvocationMarshaller
}
/** The method id used to dispatch {@link #mergeCollection} requests. */
public static const MERGE_COLLECTION :int = 5;
public static const MERGE_COLLECTION :int = 6;
// from interface EZGameService
public function mergeCollection (arg1 :Client, arg2 :String, arg3 :String, arg4 :InvocationService_InvocationListener) :void
@@ -107,7 +121,7 @@ public class EZGameMarshaller extends InvocationMarshaller
}
/** The method id used to dispatch {@link #sendMessage} requests. */
public static const SEND_MESSAGE :int = 6;
public static const SEND_MESSAGE :int = 7;
// from interface EZGameService
public function sendMessage (arg1 :Client, arg2 :String, arg3 :Object, arg4 :int, arg5 :InvocationService_InvocationListener) :void
@@ -119,8 +133,21 @@ public class EZGameMarshaller extends InvocationMarshaller
]);
}
/** The method id used to dispatch {@link #setCookie} requests. */
public static const SET_COOKIE :int = 8;
// from interface EZGameService
public function setCookie (arg1 :Client, arg2 :ByteArray, arg3 :InvocationService_InvocationListener) :void
{
var listener3 :InvocationMarshaller_ListenerMarshaller = new InvocationMarshaller_ListenerMarshaller();
listener3.listener = arg3;
sendRequest(arg1, SET_COOKIE, [
arg2, listener3
]);
}
/** The method id used to dispatch {@link #setProperty} requests. */
public static const SET_PROPERTY :int = 7;
public static const SET_PROPERTY :int = 9;
// from interface EZGameService
public function setProperty (arg1 :Client, arg2 :String, arg3 :Object, arg4 :int, arg5 :InvocationService_InvocationListener) :void
@@ -133,7 +160,7 @@ public class EZGameMarshaller extends InvocationMarshaller
}
/** The method id used to dispatch {@link #setTicker} requests. */
public static const SET_TICKER :int = 8;
public static const SET_TICKER :int = 10;
// from interface EZGameService
public function setTicker (arg1 :Client, arg2 :String, arg3 :int, arg4 :InvocationService_InvocationListener) :void
@@ -10,6 +10,8 @@ import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.TypedArray;
import com.threerings.presents.dobj.DSet;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.turn.data.TurnGameObject;
@@ -31,6 +33,9 @@ public class EZGameObject extends GameObject
/** The field name of the <code>turnHolder</code> field. */
public static const TURN_HOLDER :String = "turnHolder";
/** The field name of the <code>userCookies</code> field. */
public static const USER_COOKIES :String = "userCookies";
/** The field name of the <code>ezGameService</code> field. */
public static const EZ_GAME_SERVICE :String = "ezGameService";
// AUTO-GENERATED: FIELDS END
@@ -38,6 +43,9 @@ public class EZGameObject extends GameObject
/** The current turn holder. */
public var turnHolder :Name;
/** A set of loaded user cookies. */
public var userCookies :DSet;
/** The service interface for requesting special things from the server. */
public var ezGameService :EZGameMarshaller;
@@ -134,6 +142,7 @@ public class EZGameObject extends GameObject
// first read any regular bits
turnHolder = (ins.readObject() as Name);
userCookies = (ins.readObject() as DSet);
ezGameService = (ins.readObject() as EZGameMarshaller);
// then user properties
@@ -0,0 +1,44 @@
//
// $Id$
package com.threerings.ezgame.data {
import flash.utils.ByteArray;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.presents.dobj.DSet_Entry;
import com.threerings.ezgame.util.EZObjectMarshaller;
public class UserCookie
implements DSet_Entry
{
/** The index of the player that has this cookie. */
public var playerIndex :int;
/** The decoded cookie value. */
public var cookie :Object;
// from DSet_Entry
public function getKey () :Object
{
return playerIndex;
}
// from superinterface Streamable
public function readObject (ins :ObjectInputStream) :void
{
playerIndex = ins.readInt();
var ba :ByteArray = (ins.readObject() as ByteArray);
cookie = EZObjectMarshaller.decode(ba);
}
// from superinterface Streamable
public function writeObject (out :ObjectOutputStream) :void
{
throw new Error();
}
}
}
@@ -7,6 +7,8 @@ import flash.system.ApplicationDomain;
import flash.utils.ByteArray;
import flash.utils.Endian;
import com.threerings.util.StringUtil;
import com.threerings.io.TypedArray;
/**
@@ -45,6 +47,8 @@ public class EZObjectMarshaller
bytes.endian = Endian.BIG_ENDIAN;
bytes.objectEncoding = ObjectEncoding.AMF3;
bytes.writeObject(obj);
Log.getLog(EZObjectMarshaller).info(
"The encoded bytes are: " + StringUtil.hexlate(bytes));
return bytes;
}