Advertisement
player_03

KongregateFlashAPI.hx

Jan 19th, 2014
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 4.27 KB | None | 0 0
  1. /**
  2.  * KongregateFlashAPI.hx - A Haxe implementation of Kongregate's Client API.
  3.  *
  4.  * Copyright 2014 Joseph Cloutier
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *    http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18.  
  19. import flash.display.Loader;
  20. import flash.display.Stage;
  21. import flash.events.Event;
  22. import flash.events.EventDispatcher;
  23. import flash.events.IOErrorEvent;
  24. import flash.Lib;
  25. import flash.net.URLRequest;
  26. import flash.Vector;
  27.  
  28. /**
  29.  * @usage
  30.  * var api:KongregateFlashAPI = new KongregateFlashAPI();
  31.  * api.submit("myStatName", myStatValue);
  32.  *
  33.  * @usage
  34.  * var api = new KongregateFlashAPI();
  35.  * api.addEventListener(KongregateFlashAPI.EVENT_LOAD_COMPLETE, function(e) { api.showRegistrationBox(); } );
  36.  * api.addEventListener(KongregateFlashAPI.EVENT_LOGIN, myLoginCallback);
  37.  */
  38. class KongregateFlashAPI extends EventDispatcher {
  39.     public static var EVENT_LOAD_COMPLETE:String = Event.COMPLETE;
  40.     public static var EVENT_IO_ERROR:String = IOErrorEvent.IO_ERROR;
  41.     public static inline var EVENT_LOGIN:String = "login";
  42.    
  43.     public var kongregate(default, null):Dynamic;
  44.    
  45.     private var loader:Loader;
  46.     private var statsBacklog:Vector< {name:String, value:Int} >;
  47.    
  48.     public function new() {
  49.         super();
  50.        
  51.         statsBacklog = new Vector< {name:String, value:Int} >();
  52.        
  53.         var stage:Stage = Lib.current.stage;
  54.         var apiUrl:String = stage.loaderInfo.parameters.kongregate_api_path;
  55.        
  56.         //Note: the shadow API does work, but the folder containing your
  57.         //.swf file has to be marked as a "Trusted location." For more
  58.         //information, refer to this ridiculously long url:
  59.         //http://help.adobe.com/en_US/FlashPlayer/LSM/WS6aa5ec234ff3f285139dc56112e3786b68c-7ff0.html
  60.        
  61.         //Also, rather than printing to the screen, it prints to Flash's
  62.         //log file, which is documented here:
  63.         //http://help.adobe.com/en_US/flex/using/WSda78ed3a750d6b8f-4867184d1239f9d0558-8000.html
  64.         if(apiUrl == null) {
  65.             apiUrl = "http://www.kongregate.com/flash/API_AS3_Local.swf";
  66.         }
  67.        
  68.         var urlRequest:URLRequest = new URLRequest(apiUrl);
  69.        
  70.         loader = new Loader();
  71.         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  72.         loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
  73.         loader.load(urlRequest);
  74.        
  75.         stage.addChild(loader);
  76.     }
  77.    
  78.     private function onLoadError(e:IOErrorEvent):Void {
  79.         Lib.current.stage.removeChild(loader);
  80.         loader = null;
  81.        
  82.         dispatchEvent(e);
  83.     }
  84.    
  85.     private function onLoadComplete(e:Event):Void {
  86.         Lib.current.stage.removeChild(loader);
  87.         loader = null;
  88.        
  89.         kongregate = e.target.content;
  90.         kongregate.services.connect();
  91.        
  92.         kongregate.services.addEventListener(EVENT_LOGIN, onLogin);
  93.        
  94.         for(stat in statsBacklog) {
  95.             kongregate.stats.submit(stat.name, stat.value);
  96.         }
  97.        
  98.         dispatchEvent(e);
  99.     }
  100.    
  101.     public function isReady():Bool {
  102.         return kongregate != null;
  103.     }
  104.    
  105.     public function submit(name:String, value:Int):Void {
  106.         if(kongregate != null) {
  107.             kongregate.stats.submit(name, value);
  108.         } else if(statsBacklog != null) {
  109.             statsBacklog.push( {name:name, value:value} );
  110.         }
  111.     }
  112.    
  113.     private function onLogin(e:Event):Void {
  114.         dispatchEvent(e);
  115.     }
  116.    
  117.     public function showRegistrationBox():Void {
  118.         if(kongregate != null && isGuest()) {
  119.             if(Reflect.hasField(kongregate.services, "showRegistrationBox")) {
  120.                 kongregate.services.showRegistrationBox();
  121.             } else {
  122.                 //Just pretend that this exists in the shadow API, ok?
  123.                 Lib.trace("Kongregate API: IKongregateServices.showRegistrationBox()");
  124.             }
  125.         }
  126.     }
  127.    
  128.     public function getUsername():String {
  129.         if(kongregate != null) {
  130.             return kongregate.services.getUsername();
  131.         } else {
  132.             return "Guest";
  133.         }
  134.     }
  135.    
  136.     public function isGuest():Bool {
  137.         if(kongregate != null) {
  138.             return kongregate.services.isGuest();
  139.         }
  140.        
  141.         return true;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement