A class to manage complicated user cookies on a GameControl. Using this class, user
cookies can contain a list of various different data types, which are read from the server and
saved back to the server automatically. The data structure is compressed into a ByteArray to
save space (user cookies are only allowed to go up to 4k). Data is saved to the server as it is
updated on this object, but no faster than once per every 2 seconds so that this class doesn't
add too much to the game's networking activity, as games are limited to 100 messages per every
10 seconds.
This class enables versioning, up to a point. It currently supports adding parameters to the
cookie definition, but does not support removing them or changing their data type. The only
overhead added by this class to the cookie itself is a single int that holds the version number
of the cookie.
example usage: This could be used for a game that has 5 levels. At first the developer only
needed to know which was the last level the player played on, so it was stored in the cookie.
Later he wanted to know how many times each level had been played by the player, so he added it
to the cookie definition in a new version.
protected var LAST_LEVEL_PLAYED :String = "lastLevelPlayed";
protected var TIMES_LEVELS_PLAYED :String = "timesLevelsPlayed";
public function getCookie () :void
{
var timesPlayed :Array = [];
for (level = 0; level < 5; level++) {
// parameter names are not used if the parameter is nested in an array. Arrays can also
// hold array parameters as children.
timesPlayed.push(UserCookie.getIntParameter("", 0));
}
var cookieDef :Array = [
// start at version 1
UserCookie.getVersionParameter(),
UserCookie.getIntParameter(LAST_LEVEL_PLAYED, 0),
// version 2 added the number of times each level was played
UserCookie.getVersionParameter(),
UserCookie.getArrayParameter(TIMES_LEVELS_PLAYED, timesPlayed)
];
UserCookie.getCookie(wgc, function (cookie :UserCookie) :void {
// notify those that need to know that the UserCookie is valid and available.
_cookie = cookie;
}, cookieDef);
}
public function setLastLevelPlayed (level :int) :void
{
if (_cookie != null) {
_cookie.set(LAST_LEVEL_PLAYED, level);
}
}
public function playedLevel (level :int) :void
{
if (_cookie != null) {
// increment the array value for this level.
var previousValue :int = _cookie.get(TIMES_LEVELS_PLAYED, level);
_cookie.set(TIMES_LEVELS_PLAYED, previousValue + 1, level);
}
}
One more note: this class creates a timer and registers the TimerEvent.TIMER listener with
com.threerings.util.EventHandlers. To make sure that this class stops checking the timer when
your game is unloaded, call EventHandlers.freeAllHandlers() when your game receives the
Event.UNLOAD event.
protected var _control:GameControl
protected var _cookieDef:Array
protected var _dirty:Boolean = false
protected var _logDebug:Boolean = false
protected var _parameters:Map
protected var _readOnly:Boolean = false
protected var _timer:Timer
public function UserCookie(eventMgr:EventHandlerManager = null)
This function should not be called directly. Instead, call UserCookie.getCookie().
Parameters
| eventMgr:EventHandlerManager (default = null) |
protected function arrayChildrenAsString(param:ArrayParameter):StringParameters
Returns
protected function debugLog(logLine:String):voidParameters
protected function flush(... ignored):voidParameters
public function get(name:String, ... indices):*
Get the value of the cookie parameter identified by name.
Parameters
| name:String — The first arg should be the parameter identified as a String. Any further
arguments are the array indices to use. There can be multiple array indices, if a value
in a nested array is being retrieved.
|
| |
| ... indices |
Returns
public static function getArrayParameter(name:String, children:Array):CookieParameter
Returns an Array typped parameter for use in the cookieDef argument to getCookie(). All of
the children must be CookieParameters returned from a getarameter() function, or an
ArgumentError will be thrown. Also, you cannot embed a version in an array.
Parameters
| name:String |
| |
| children:Array |
Returns
public static function getCookie(wgc:GameControl, validCallback:Function, cookieDef:Array, eventMgr:EventHandlerManager = null, enableDebugLogging:Boolean = false, occId:int = -1):void
Get a player's user cookie via GameControl.getUserCookie, wrapped in a UserCookie
object.
Parameters
| wgc:GameControl — The GameControl of the current instance
|
| |
| validCallback:Function — This function is called with a single UserCookie parameter when the
cookie has been retrieved and validated.
|
| |
| cookieDef:Array — An array of cookie parameters that define the format of the user cookie.
See the various getarameter() functions for more detail.
|
| |
| eventMgr:EventHandlerManager (default = null) — If an EventHandlerManager is provided, it will be used to register all event
listeners. If not provided, EventHandlers will be used instead.
|
| |
| enableDebugLogging:Boolean (default = false) — Enable logging of some debug messages, including the values read
out of the user cookie in the initial read. Logging is done via
com.threerings.util.Log.
|
| |
| occId:int (default = -1) — The player's id to fetch the cookie for. Defaults to the current player. If
a different player is specified, this UserCookie will be read-only - attempting
to set a value will generate an IllegalOperationError.
|
protected function getFromArray(array:ArrayParameter, indices:Array):*Parameters
| array:ArrayParameter |
| |
| indices:Array |
Returns
public static function getIntParameter(name:String, defaultValue:int):CookieParameter
Returns an int typped parameter for use in the cookieDef argument to getCookie().
Parameters
| name:String |
| |
| defaultValue:int |
Returns
public static function getStringParameter(name:String, defaultValue:String):CookieParameter
Returns a String typped parameter for use in the cookieDef argument to getCookie().
Parameters
| name:String |
| |
| defaultValue:String |
Returns
public static function getVersionParameter():CookieParameter
Returns a version flag for use in the cookieDef argument to getCookie(). If the cookie
definition for a game is extended after some players may have the old cookie already set,
there should be a version flag added before adding in the new parameters. This will allow
the old players to gracefully add in the new values when they play the game again.
If a player with an old cookie or no cookie plays the game, each parameter will return
its default type. The default type will also get set for this player on the server until
a new value is defined.
Returns
protected function read(bytes:ByteArray):voidParameters
public function set(name:String, ... value, indices:*):void
Set the value of the cookie parameter identified by name. If the type of value does not
match the type from the cookieDef parameter to getCookie, an ArgumentError is thrown.
Parameters
| name:String — The first arg should be the parameter identifier as a String. The next
argument should be the value to set. Any further arguments are the array indices to use.
There can be multiple array indices, if a value in a nested array is being set.
|
| |
| ... value |
| |
| indices:* |
protected function setInArray(array:ArrayParameter, value:Array, indices:*):voidParameters
| array:ArrayParameter |
| |
| value:Array |
| |
| indices:* |
protected function write():ByteArray
Returns
protected static const SEND_TIME:int = 2000