/**
* KongregateFlashAPI.hx - A Haxe implementation of Kongregate's Client API.
*
* Copyright 2014 Joseph Cloutier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import flash.display.Loader;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.Lib;
import flash.net.URLRequest;
import flash.Vector;
/**
* @usage
* var api:KongregateFlashAPI = new KongregateFlashAPI();
* api.submit("myStatName", myStatValue);
*
* @usage
* var api = new KongregateFlashAPI();
* api.addEventListener(KongregateFlashAPI.EVENT_LOAD_COMPLETE, function(e) { api.showRegistrationBox(); } );
* api.addEventListener(KongregateFlashAPI.EVENT_LOGIN, myLoginCallback);
*/
class KongregateFlashAPI extends EventDispatcher {
public static var EVENT_LOAD_COMPLETE:String = Event.COMPLETE;
public static var EVENT_IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static inline var EVENT_LOGIN:String = "login";
public var kongregate(default, null):Dynamic;
private var loader:Loader;
private var statsBacklog:Vector< {name:String, value:Int} >;
public function new() {
super();
statsBacklog = new Vector< {name:String, value:Int} >();
var stage:Stage = Lib.current.stage;
var apiUrl:String = stage.loaderInfo.parameters.kongregate_api_path;
//Note: the shadow API does work, but the folder containing your
//.swf file has to be marked as a "Trusted location." For more
//information, refer to this ridiculously long url:
//http://help.adobe.com/en_US/FlashPlayer/LSM/WS6aa5ec234ff3f285139dc56112e3786b68c-7ff0.html
//Also, rather than printing to the screen, it prints to Flash's
//log file, which is documented here:
//http://help.adobe.com/en_US/flex/using/WSda78ed3a750d6b8f-4867184d1239f9d0558-8000.html
if(apiUrl == null) {
apiUrl = "http://www.kongregate.com/flash/API_AS3_Local.swf";
}
var urlRequest:URLRequest = new URLRequest(apiUrl);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
loader.load(urlRequest);
stage.addChild(loader);
}
private function onLoadError(e:IOErrorEvent):Void {
Lib.current.stage.removeChild(loader);
loader = null;
dispatchEvent(e);
}
private function onLoadComplete(e:Event):Void {
Lib.current.stage.removeChild(loader);
loader = null;
kongregate = e.target.content;
kongregate.services.connect();
kongregate.services.addEventListener(EVENT_LOGIN, onLogin);
for(stat in statsBacklog) {
kongregate.stats.submit(stat.name, stat.value);
}
dispatchEvent(e);
}
public function isReady():Bool {
return kongregate != null;
}
public function submit(name:String, value:Int):Void {
if(kongregate != null) {
kongregate.stats.submit(name, value);
} else if(statsBacklog != null) {
statsBacklog.push( {name:name, value:value} );
}
}
private function onLogin(e:Event):Void {
dispatchEvent(e);
}
public function showRegistrationBox():Void {
if(kongregate != null && isGuest()) {
if(Reflect.hasField(kongregate.services, "showRegistrationBox")) {
kongregate.services.showRegistrationBox();
} else {
//Just pretend that this exists in the shadow API, ok?
Lib.trace("Kongregate API: IKongregateServices.showRegistrationBox()");
}
}
}
public function getUsername():String {
if(kongregate != null) {
return kongregate.services.getUsername();
} else {
return "Guest";
}
}
public function isGuest():Bool {
if(kongregate != null) {
return kongregate.services.isGuest();
}
return true;
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎