home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / mochi / MochiScores.as next >
Encoding:
Text File  |  2008-09-03  |  6.3 KB  |  222 lines

  1. package mochi
  2. {
  3.    import flash.display.MovieClip;
  4.    import flash.display.Sprite;
  5.    import flash.text.TextField;
  6.    
  7.    public class MochiScores
  8.    {
  9.       
  10.       private static var boardID:String;
  11.       
  12.       public static var onErrorHandler:Object;
  13.       
  14.       public static var onCloseHandler:Object;
  15.        
  16.       
  17.       public function MochiScores()
  18.       {
  19.          super();
  20.       }
  21.       
  22.       public static function showLeaderboard(options:Object = null) : void
  23.       {
  24.          if(options != null)
  25.          {
  26.             if(options.clip != null)
  27.             {
  28.                if(options.clip is Sprite)
  29.                {
  30.                   MochiServices.setContainer(options.clip);
  31.                }
  32.                delete options.clip;
  33.             }
  34.             else
  35.             {
  36.                MochiServices.setContainer();
  37.             }
  38.             MochiServices.stayOnTop();
  39.             if(options.name != null)
  40.             {
  41.                if(options.name is TextField)
  42.                {
  43.                   if(options.name.text.length > 0)
  44.                   {
  45.                      options.name = options.name.text;
  46.                   }
  47.                }
  48.             }
  49.             if(options.score != null)
  50.             {
  51.                if(options.score is TextField)
  52.                {
  53.                   if(options.score.text.length > 0)
  54.                   {
  55.                      options.score = options.score.text;
  56.                   }
  57.                }
  58.             }
  59.             if(options.onDisplay != null)
  60.             {
  61.                options.onDisplay();
  62.             }
  63.             else if(MochiServices.clip != null)
  64.             {
  65.                if(MochiServices.clip is MovieClip)
  66.                {
  67.                   MochiServices.clip.stop();
  68.                }
  69.                else
  70.                {
  71.                   trace("Warning: Container is not a MovieClip, cannot call default onDisplay.");
  72.                }
  73.             }
  74.          }
  75.          else
  76.          {
  77.             options = {};
  78.             if(MochiServices.clip is MovieClip)
  79.             {
  80.                MochiServices.clip.stop();
  81.             }
  82.             else
  83.             {
  84.                trace("Warning: Container is not a MovieClip, cannot call default onDisplay.");
  85.             }
  86.          }
  87.          if(options.onClose != null)
  88.          {
  89.             onCloseHandler = options.onClose;
  90.          }
  91.          else
  92.          {
  93.             onCloseHandler = function():void
  94.             {
  95.                if(MochiServices.clip is MovieClip)
  96.                {
  97.                   MochiServices.clip.play();
  98.                }
  99.                else
  100.                {
  101.                   trace("Warning: Container is not a MovieClip, cannot call default onClose.");
  102.                }
  103.             };
  104.          }
  105.          if(options.onError != null)
  106.          {
  107.             onErrorHandler = options.onError;
  108.          }
  109.          else
  110.          {
  111.             onErrorHandler = null;
  112.          }
  113.          if(options.boardID == null)
  114.          {
  115.             if(MochiScores.boardID != null)
  116.             {
  117.                options.boardID = MochiScores.boardID;
  118.             }
  119.          }
  120.          MochiServices.send("scores_showLeaderboard",{"options":options},null,onClose);
  121.       }
  122.       
  123.       public static function closeLeaderboard() : void
  124.       {
  125.          MochiServices.send("scores_closeLeaderboard");
  126.       }
  127.       
  128.       public static function getPlayerInfo(callbackObj:Object, callbackMethod:Object = null) : void
  129.       {
  130.          MochiServices.send("scores_getPlayerInfo",null,callbackObj,callbackMethod);
  131.       }
  132.       
  133.       public static function requestList(callbackObj:Object, callbackMethod:Object = null) : void
  134.       {
  135.          MochiServices.send("scores_requestList",null,callbackObj,callbackMethod);
  136.       }
  137.       
  138.       public static function scoresArrayToObjects(scores:Object) : Object
  139.       {
  140.          var i:Number = NaN;
  141.          var j:Number = NaN;
  142.          var o:Object = null;
  143.          var row_obj:Object = null;
  144.          var item:String = null;
  145.          var param:String = null;
  146.          var so:Object = {};
  147.          for(item in scores)
  148.          {
  149.             if(typeof scores[item] == "object")
  150.             {
  151.                if(scores[item].cols != null && scores[item].rows != null)
  152.                {
  153.                   so[item] = [];
  154.                   o = scores[item];
  155.                   for(j = 0; j < o.rows.length; j++)
  156.                   {
  157.                      row_obj = {};
  158.                      for(i = 0; i < o.cols.length; i++)
  159.                      {
  160.                         row_obj[o.cols[i]] = o.rows[j][i];
  161.                      }
  162.                      so[item].push(row_obj);
  163.                   }
  164.                }
  165.                else
  166.                {
  167.                   so[item] = {};
  168.                   for(param in scores[item])
  169.                   {
  170.                      so[item][param] = scores[item][param];
  171.                   }
  172.                }
  173.             }
  174.             else
  175.             {
  176.                so[item] = scores[item];
  177.             }
  178.          }
  179.          return so;
  180.       }
  181.       
  182.       public static function submit(score:Number, name:String, callbackObj:Object = null, callbackMethod:Object = null) : void
  183.       {
  184.          MochiServices.send("scores_submit",{
  185.             "score":score,
  186.             "name":name
  187.          },callbackObj,callbackMethod);
  188.       }
  189.       
  190.       public static function onClose(args:Object = null) : void
  191.       {
  192.          if(args != null)
  193.          {
  194.             if(args.error != null)
  195.             {
  196.                if(args.error == true)
  197.                {
  198.                   if(onErrorHandler != null)
  199.                   {
  200.                      if(args.errorCode == null)
  201.                      {
  202.                         args.errorCode = "IOError";
  203.                      }
  204.                      onErrorHandler(args.errorCode);
  205.                      MochiServices.doClose();
  206.                      return;
  207.                   }
  208.                }
  209.             }
  210.          }
  211.          onCloseHandler();
  212.          MochiServices.doClose();
  213.       }
  214.       
  215.       public static function setBoardID(boardID:String) : void
  216.       {
  217.          MochiScores.boardID = boardID;
  218.          MochiServices.send("scores_setBoardID",{"boardID":boardID});
  219.       }
  220.    }
  221. }
  222.