Packagecom.whirled.contrib
Classpublic class UserCookie

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 Properties
 PropertyDefined by
  _control : GameControl
UserCookie
  _cookieDef : Array
UserCookie
  _dirty : Boolean = false
UserCookie
  _logDebug : Boolean = false
UserCookie
  _parameters : Map
UserCookie
  _readOnly : Boolean = false
UserCookie
  _timer : Timer
UserCookie
Public Methods
 MethodDefined by
  
UserCookie(eventMgr:EventHandlerManager = null)
This function should not be called directly.
UserCookie
  
get(name:String, ... indices):*
Get the value of the cookie parameter identified by name.
UserCookie
  
getArrayParameter(name:String, children:Array):CookieParameter
[static] Returns an Array typped parameter for use in the cookieDef argument to getCookie().
UserCookie
  
getCookie(wgc:GameControl, validCallback:Function, cookieDef:Array, eventMgr:EventHandlerManager = null, enableDebugLogging:Boolean = false, occId:int = -1):void
[static] Get a player's user cookie via GameControl.getUserCookie, wrapped in a UserCookie object.
UserCookie
  
getIntParameter(name:String, defaultValue:int):CookieParameter
[static] Returns an int typped parameter for use in the cookieDef argument to getCookie().
UserCookie
  
getStringParameter(name:String, defaultValue:String):CookieParameter
[static] Returns a String typped parameter for use in the cookieDef argument to getCookie().
UserCookie
  
getVersionParameter():CookieParameter
[static] Returns a version flag for use in the cookieDef argument to getCookie().
UserCookie
  
set(name:String, ... value, indices:*):void
Set the value of the cookie parameter identified by name.
UserCookie
Protected Methods
 MethodDefined by
  
arrayChildrenAsString(param:ArrayParameter):String
UserCookie
  
debugLog(logLine:String):void
UserCookie
  
flush(... ignored):void
UserCookie
  
getFromArray(array:ArrayParameter, indices:Array):*
UserCookie
  
read(bytes:ByteArray):void
UserCookie
  
setInArray(array:ArrayParameter, value:Array, indices:*):void
UserCookie
  
write():ByteArray
UserCookie
Protected Constants
 ConstantDefined by
  SEND_TIME : int = 2000
[static]
UserCookie
Property detail
_controlproperty
protected var _control:GameControl
_cookieDefproperty 
protected var _cookieDef:Array
_dirtyproperty 
protected var _dirty:Boolean = false
_logDebugproperty 
protected var _logDebug:Boolean = false
_parametersproperty 
protected var _parameters:Map
_readOnlyproperty 
protected var _readOnly:Boolean = false
_timerproperty 
protected var _timer:Timer
Constructor detail
UserCookie()constructor
public function UserCookie(eventMgr:EventHandlerManager = null)

This function should not be called directly. Instead, call UserCookie.getCookie().

Parameters
eventMgr:EventHandlerManager (default = null)
Method detail
arrayChildrenAsString()method
protected function arrayChildrenAsString(param:ArrayParameter):StringParameters
param:ArrayParameter

Returns
String
debugLog()method 
protected function debugLog(logLine:String):voidParameters
logLine:String
flush()method 
protected function flush(... ignored):voidParameters
... ignored
get()method 
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
*
getArrayParameter()method 
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
CookieParameter
getCookie()method 
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.
getFromArray()method 
protected function getFromArray(array:ArrayParameter, indices:Array):*Parameters
array:ArrayParameter
 
indices:Array

Returns
*
getIntParameter()method 
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
CookieParameter
getStringParameter()method 
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
CookieParameter
getVersionParameter()method 
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
CookieParameter
read()method 
protected function read(bytes:ByteArray):voidParameters
bytes:ByteArray
set()method 
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:*
setInArray()method 
protected function setInArray(array:ArrayParameter, value:Array, indices:*):voidParameters
array:ArrayParameter
 
value:Array
 
indices:*
write()method 
protected function write():ByteArray

Returns
ByteArray
Constant detail
SEND_TIMEconstant
protected static const SEND_TIME:int = 2000