PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/CCJ/src/mochi/as3/MochiScores.as

http://chinesechessjam.googlecode.com/
ActionScript | 250 lines | 153 code | 51 blank | 46 comment | 70 complexity | f2d1778e9c8373a05c637cf8514a0650 MD5 | raw file
  1. /**
  2. * MochiSscores
  3. * Class that provides API access to MochiAds Scores Service
  4. * @author Mochi Media
  5. */
  6. package mochi.as3 {
  7. import flash.display.MovieClip;
  8. import flash.display.Sprite;
  9. import flash.text.TextField;
  10. import mochi.as3.*;
  11. public class MochiScores {
  12. public static function onClose (args:Object = null):void {
  13. if (args && args.error == true && onErrorHandler) {
  14. if (args.errorCode == null) args.errorCode = "IOError";
  15. onErrorHandler(args.errorCode);
  16. MochiServices.doClose();
  17. return;
  18. }
  19. onCloseHandler();
  20. MochiServices.doClose();
  21. }
  22. public static var onCloseHandler:Object;
  23. public static var onErrorHandler:Object;
  24. private static var boardID:String;
  25. /**
  26. * Method: setBoardID
  27. * Sets the name of the mode to use for categorizing submitted and displayed scores. The board ID is assigned in the online interface.
  28. * @param boardID: The unique string name of the mode
  29. */
  30. public static function setBoardID (boardID:String):void {
  31. MochiServices.warnID( boardID, true );
  32. MochiScores.boardID = boardID; MochiServices.send("scores_setBoardID", { boardID: boardID } );
  33. };
  34. /**
  35. * Method: showLeaderBoard
  36. * Displays the leaderboard GUI showing the current top scores. The callback event is triggered when the leaderboard is closed.
  37. * @param options object containing variables representing the changeable parameters <see: GUI Options>
  38. */
  39. public static function showLeaderboard (options:Object = null):void {
  40. if (options != null) {
  41. delete options.clip;
  42. MochiServices.setContainer();
  43. //MochiServices.stayOnTop();
  44. MochiServices.bringToTop();
  45. if (options.name != null) {
  46. if (options.name is TextField) {
  47. if (options.name.text.length > 0) {
  48. options.name = options.name.text;
  49. }
  50. }
  51. }
  52. if (options.score != null) {
  53. if (options.score is TextField) {
  54. if (options.score.text.length > 0) {
  55. options.score = options.score.text;
  56. }
  57. }
  58. else if (options.score is MochiDigits)
  59. {
  60. options.score = options.score.value;
  61. }
  62. var n:Number = Number(options.score);
  63. // check if score is a numeric value
  64. if( isNaN(n) ) {
  65. trace( "ERROR: Submitted score '" + options.score + "' will be rejected, score is 'Not a Number'" );
  66. }
  67. else if( n == Number.NEGATIVE_INFINITY || n == Number.POSITIVE_INFINITY ) {
  68. trace( "ERROR: Submitted score '" + options.score + "' will be rejected, score is an infinite" );
  69. }
  70. else {
  71. if( Math.floor(n) != n )
  72. trace( "WARNING: Submitted score '" + options.score + "' will be truncated" );
  73. options.score = n;
  74. }
  75. }
  76. if (options.onDisplay != null) {
  77. options.onDisplay();
  78. } else {
  79. if (MochiServices.clip != null) {
  80. if (MochiServices.clip is MovieClip) {
  81. MochiServices.clip.stop();
  82. } else {
  83. trace("Warning: Container is not a MovieClip, cannot call default onDisplay.");
  84. }
  85. }
  86. }
  87. } else {
  88. options = { };
  89. if (MochiServices.clip is MovieClip) {
  90. MochiServices.clip.stop();
  91. } else {
  92. trace("Warning: Container is not a MovieClip, cannot call default onDisplay.");
  93. }
  94. }
  95. if (options.onClose != null) {
  96. onCloseHandler = options.onClose;
  97. } else {
  98. onCloseHandler = function ():void {
  99. if (MochiServices.clip is MovieClip) {
  100. MochiServices.clip.play();
  101. } else {
  102. trace("Warning: Container is not a MovieClip, cannot call default onClose.");
  103. }
  104. }
  105. }
  106. if (options.onError != null) {
  107. onErrorHandler = options.onError;
  108. } else {
  109. onErrorHandler = null;
  110. }
  111. if (options.boardID == null) {
  112. if (MochiScores.boardID != null) {
  113. options.boardID = MochiScores.boardID;
  114. }
  115. }
  116. MochiServices.warnID( options.boardID, true );
  117. trace("[MochiScores] NOTE: Security Sandbox Violation errors below are normal");
  118. MochiServices.send("scores_showLeaderboard", { options: options }, null, onClose );
  119. }
  120. /**
  121. * Method: closeLeaderboard
  122. * Closes the leaderboard immediately
  123. */
  124. public static function closeLeaderboard ():void { MochiServices.send("scores_closeLeaderboard"); }
  125. /**
  126. * Method: getPlayerInfo
  127. * Retrieves all persistent player data that has been saved in a SharedObject. Will send to the callback an object containing key->value pairs contained in the player cookie.
  128. */
  129. public static function getPlayerInfo (callbackObj:Object, callbackMethod:Object = null):void { MochiServices.send("scores_getPlayerInfo", null, callbackObj, callbackMethod); }
  130. /**
  131. * Method: submit
  132. * Submits a score to the server using the current id and mode.
  133. * @param name - the string name of the user as entered or defined by MochiBridge.
  134. * @param score - the number representing a score. Can be an integer or float. If the score is time, send it in seconds - can be float
  135. * @param callbackObj - the object or class instance containing the callback method
  136. * @param callbackMethod - the string name of the method to call when the score has been sent
  137. */
  138. public static function submit (score:Number, name:String, callbackObj:Object = null, callbackMethod:Object = null):void
  139. {
  140. score = Number(score);
  141. // check if score is a numeric value
  142. if( isNaN(score) ) {
  143. trace( "ERROR: Submitted score '" + String(score) + "' will be rejected, score is 'Not a Number'" );
  144. }
  145. else if( score == Number.NEGATIVE_INFINITY || score == Number.POSITIVE_INFINITY ) {
  146. trace( "ERROR: Submitted score '" + String(score) + "' will be rejected, score is an infinite" );
  147. }
  148. else {
  149. if( Math.floor(score) != score )
  150. trace( "WARNING: Submitted score '" + String(score) + "' will be truncated" );
  151. score = Number(score);
  152. }
  153. MochiServices.send("scores_submit", {score: score, name: name}, callbackObj, callbackMethod);
  154. };
  155. /**
  156. * Method: requestList
  157. * Requests a listing from the server using the current game id and mode. Returns an array of at most 50 score objects. Will send to the callback an array of objects [{name, score, timestamp}, ...]
  158. * @param callbackObj the object or class instance containing the callback method
  159. * @param callbackMethod the string name of the method to call when the score has been sent. default: "onLoad"
  160. */
  161. public static function requestList (callbackObj:Object, callbackMethod:Object = null):void { MochiServices.send("scores_requestList", null, callbackObj, callbackMethod); }
  162. /**
  163. * Method: scoresArrayToObjects
  164. * Converts the cols/rows array format retrieved from the server into an array of objects - one object for each row containing key-value pairs.
  165. * @param scores the scores object received from the server
  166. * @return
  167. */
  168. public static function scoresArrayToObjects (scores:Object):Object {
  169. var so:Object = { };
  170. var i:Number;
  171. var j:Number;
  172. var o:Object;
  173. var row_obj:Object;
  174. for (var item:String in scores) {
  175. if (typeof(scores[item]) == "object") {
  176. if (scores[item].cols != null && scores[item].rows != null) {
  177. so[item] = [ ];
  178. o = scores[item];
  179. for (j = 0; j < o.rows.length; j++) {
  180. row_obj = { };
  181. for (i = 0; i < o.cols.length; i++) {
  182. row_obj[o.cols[i]] = o.rows[j][i];
  183. }
  184. so[item].push(row_obj);
  185. }
  186. } else {
  187. so[item] = { };
  188. for (var param:String in scores[item]) {
  189. so[item][param] = scores[item][param];
  190. }
  191. }
  192. } else {
  193. so[item] = scores[item];
  194. }
  195. }
  196. return so;
  197. }
  198. }
  199. }