Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 27th, 2012  |  syntax: None  |  size: 5.75 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package playerio {
  2.         import flash.display.Stage
  3.         import flash.display.Loader
  4.         import flash.events.Event
  5.         import flash.events.IOErrorEvent
  6.         import flash.events.SecurityErrorEvent
  7.         import flash.net.URLRequest;
  8.         import flash.net.URLLoader
  9.         import flash.net.URLLoaderDataFormat
  10.         import flash.system.ApplicationDomain
  11.         import flash.system.LoaderContext
  12.        
  13.         /**
  14.          * API wrapper that is used to connect to the PlayerIO webservices
  15.          *
  16.          */
  17.         public final class PlayerIO{
  18.                 private static var wrapper:Loader;
  19.                 private static var queue:Array = [];
  20.                 private static var apiError:PlayerIOError
  21.                 private static var wo:Object = {};
  22.                
  23.                 /**
  24.                  * You cannot create an instance of the PlayerIO class, all use happens via static methods.
  25.                  * @throws Error You cannot create an instance of the PlayerIO class!
  26.                  */            
  27.                 public function PlayerIO(){
  28.                         throw new Error("You cannot create an instance of the PlayerIO class!")
  29.                 }
  30.                
  31.                 /**
  32.                  * Authenticates and connects the game to the Player.IO webservices.
  33.                  * @param stage A reference to the base stage of your project.
  34.                  * @param gameid Unique ID that identifies which game the client will try to connect to
  35.                  * @param connectionid Id of the connection to use when connecting to the game. Usually this is "public" unless you've set up different connections in the Player.IO admin panel.
  36.                  * @param userid Unique identifier of the current user playing the game. This can be supplied by you, or a a third party. Example userids are Username, Facebook UserID, OpenID Url etc.
  37.                  * @param auth User auth. Can be left blank if the connection identified by connectionid does not require authentication.
  38.                  * @param partnerId String that identifies a possible affiliate partner.
  39.                  * @param callback Function executed on successful connect: function(client:Client):void{...}
  40.                  * @param errorhandler Function executed if the request failed: function(error:PlayerIOError):void{...}
  41.                  *
  42.                  * @see Client Client returned on successfull connect
  43.                  */            
  44.                 public static function connect(stage:Stage, gameid:String, connectionid:String, userid:String, auth:String, partnerId:String, callback:Function, errorhandler:Function = null):void{
  45.                         proxy("connect", arguments);
  46.                 }
  47.                
  48.                 /**
  49.                  * Referance to a QuickConnect instance that allows you to easly connect with 3rd party user databases.  
  50.                  * @return instance of QuickConnect
  51.                  *
  52.                  */
  53.                 public static function get quickConnect():QuickConnect{
  54.                         return new QuickConnect(proxy);
  55.                 }
  56.                
  57.                 /**
  58.                  * Referance to a GameFS instance that allows you to access GameFS
  59.                  * @param gameId the GameID of your game.
  60.                  * @return An instance of GameFS
  61.                  * @example Example of how to request the file game.swf from your games GameFS via PlayerIO
  62.                  * <listing version="3.0">
  63.                  *      var url:String = PlayerIO.gameFS("game-id").getURL("game.swf")
  64.                  * </listing>
  65.                  *
  66.                  */
  67.                 public static function gameFS(gameId:String):GameFS{
  68.                         return new SimpleGameFS(gameId, wo)
  69.                 }
  70.                
  71.                 /**
  72.                  * Gives you greater control over when and where the Player.IO logo is shown. If this method is called, the logo will not appear when you connect to player.io via playerio.connect nor via QuickConnect.
  73.                  * @param stage A reference to the base stage of your project.
  74.                  * @param align Where should the logo appear. Valid values are TL, CL, BL, TC, CC, BC, TR, CR, BR. The first letter stands for vertical position Top, Center or Bottom. The second letter is for horizontal position Left, Center or Right.
  75.                  *
  76.                  */
  77.                 public static function showLogo(stage:Stage, align:String):void{
  78.                         proxy("showLogo", arguments)
  79.                 }
  80.                
  81.                 //For future use.
  82.                 /*public static function clearAnonymousUser(errorHandler:Function = null):void{
  83.                         proxy("clearAnonymousUser", arguments);
  84.                 }*/
  85.                
  86.                 private static function proxy(target:String, args:Object):void{
  87.                         if(apiError){
  88.                                 throwError(apiError,args[args.callee.length-1])
  89.                         }else if(wrapper && wrapper.content){
  90.                                 try{
  91.                                         var api:* = wrapper.content
  92.                                         var path:Array = target.split(".");
  93.                                         while(path.length > 1){
  94.                                                 api = api[path.shift()]
  95.                                         }
  96.                                         api[path[0]].apply(null, args)
  97.                                 }catch(e:Error){
  98.                                         throwError(new PlayerIOError(e.message, e.errorID),args[args.callee.length-1])
  99.                                 }
  100.                         }else{
  101.                                 queue.push(function():void{ args.callee.apply(null, args) })
  102.                         }
  103.                         if(!wrapper) loadAPI()
  104.                 }
  105.                
  106.                 private static function loadAPI():void{
  107.                         wrapper = new Loader();
  108.                         var loader:URLLoader = new URLLoader();
  109.                         loader.dataFormat = URLLoaderDataFormat.BINARY;
  110.                         loader.addEventListener(Event.COMPLETE, function(e:Event):void{
  111.                                 wrapper.contentLoaderInfo.addEventListener(Event.COMPLETE, emptyQueue)
  112.                                 wrapper.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError)
  113.                                 wrapper.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleLoadError)
  114.                                 wrapper.loadBytes(loader.data, new LoaderContext(false, ApplicationDomain.currentDomain));
  115.                                 wo.wrapper = wrapper
  116.                         })
  117.                         loader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError )
  118.                         loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleLoadError)
  119.                         //loader.load(new URLRequest("http://n.c/flashbridge/1"));
  120.                         loader.load(new URLRequest("http://api.playerio.com/flashbridge/1"));
  121.                 }
  122.                
  123.                 private static function handleLoadError(e:Event = null):void{
  124.                         apiError = new PlayerIOError("Unable to connect to the API due to " + e.type + ". Please verify that your internet connection is working!",0)
  125.                         emptyQueue()
  126.                 }
  127.                
  128.                 private static function emptyQueue(e:Event = null):void{
  129.                         while(queue.length)
  130.                                 queue.shift()();
  131.                 }
  132.                
  133.                 private static function throwError(error:PlayerIOError, target:Function):void{
  134.                         if( target != null ){
  135.                                 target(error)
  136.                         }else{
  137.                                 throw PlayerIOError
  138.                         }
  139.                 }
  140.         }
  141. }