Permalink
Browse files

Updated UserData class for modified data returned and made more data …

…loaded from friends available.
  • Loading branch information...
1 parent b7f6d01 commit 72b02193c383241ed72ed021172ea4e893825e70 @skyboy committed Mar 27, 2011
Showing with 150 additions and 104 deletions.
  1. +150 −104 skyboy/kongregate/UserData.as
@@ -57,44 +57,50 @@ package skyboy.kongregate {
import flash.net.URLRequest;
import flash.utils.setTimeout;
public class UserData {
- public function UserData(username:String = null, callback:Function = null) {
+ public static const errorID:uint = 0xA9C0;
+ public static const NOT_STARTED:uint = 0;
+ public static const LOADING_USER:uint = 1;
+ public static const LOADED_USER:uint = 2;
+ public static const LOADING_FRIENDS:uint = 3;
+ public static const COMPLETE:uint = 4;
+ public static const ERROR:uint = 5;
+ private static const ADMINISTRATOR:uint = 0x10;
+ private static const MODERATOR:uint = 0x000008;
+ private static const CURATOR:uint = 0x00000004;
+ private static const FORUM_MOD:uint = 0x000002;
+ private static const DEVELOPER:uint = 0x000001;
+ private static const USER:uint = 0x00000000000;
+ public function UserData(username:String = null, callback:Function = null):void {
if (username && callback != null) loadUser(username, callback);
- else setTimeout(thrower, 0, new Error("You must provide both a username and a callback to "+UserData+"."));
- }
- private var _callback:Function;
- public static const errorID:int = 0xA9C0;
- public static const ADMINISTRATOR:uint = 0x10;
- public static const MODERATOR:uint = 0x000008;
- public static const CURATOR:uint = 0x00000004;
- public static const FORUM_MOD:uint = 0x000002;
- public static const DEVELOPER:uint = 0x000001;
- public static const USER:uint = 0x00000000000;
- private var p:int;
- public function get progress():int {
- return p;
- }
- public function get friends():uint {
- return friendCount;
}
- public function getFriend(username:String):Object {
+ protected var _silenced:Boolean, _mutual:Boolean;
+ protected var _friends:Object, _friendlist:Array, _callback:Function;
+ protected var _age:int, _level:int, inx:int, p:int, friendCount:int, _points:uint, _kreds:uint, _status:uint, _id:uint;
+ protected var _username:String, _gender:String, _game:String, _gameTitle:String, _friendOf:String, _avatar:String, _chatAvatar:String;
+ public function getFriend(username:String):UserData {
if (!_friends) return null;
return _friends[username];
}
- public function eachFriend(closure:Function, thisObj:* = null):void {
- var i:String;
- if (thisObj == this) {
- thisObj = null;
- }
- for(i in _friends) {
+ public function eachFriend(closure:Function):void {
+ for(var i:String in _friends) {
try {
- closure.call(thisObj, i, _friends[i]);
+ closure(i, _friends[i]);
} catch (e:Error) {
var a:Error = new Error("An Error occured for friend " + i + ".\n" + e.message);
- setTimeout(thrower, 0, a);
+ delayedError(a);
}
}
}
- private function thrower(a:*):void { throw a }
+ protected function delayedError(a:*, Throw:Boolean = false):void {
+ if (Throw) throw a;
+ else setTimeout(delayedError, 0, a, true);
+ }
+ public function get progress():int {
+ return p;
+ }
+ public function get friends():uint {
+ return friendCount;
+ }
public function get friendsList():Array {
if (!_friendlist) return [];
return _friendlist.slice();
@@ -105,18 +111,12 @@ package skyboy.kongregate {
public function get avatar():String {
return _avatar;
}
+ public function get chatAvatar():String {
+ return _chatAvatar;
+ }
public function get gender():String {
return _gender;
}
- public function get game():String {
- return _game;
- }
- public function get gameTitle():String {
- return _gameTitle;
- }
- public function get kreds():uint {
- return _kreds;
- }
public function get id():uint {
return _id;
}
@@ -150,15 +150,33 @@ package skyboy.kongregate {
public function isForumMod():Boolean {
return (_status & FORUM_MOD) != 0;
}
- private var _age:int, _level:int, _points:uint, _avatar:String, _kreds:uint, _id:uint;
- private var _friends:Object, _friendlist:Array, friendCount:int, _username:String, _gender:String;
- private var _status:uint, _silenced:Boolean, _game:String, _gameTitle:String, inx:int;
+ public function get mutual():Boolean {
+ return false;
+ }
public function equals(o:UserData):Boolean {
- return o.id == _id;
+ return o._id == _id;
+ }
+ public function toString():String {
+ return _username;
+ }
+ public function loadFriends(reload:Boolean = false):void {
+ if (p == 4 && reload) {
+ var i:int = friendCount;
+ inx = i;
+ l = 0;
+ i = 5;
+ while (i--) {
+ doThing();
+ }
+ }
}
public function loadUser(username:String, callback:Function):void {
- friendCount = _id = _age = _level = _points = _status = _kreds = p = 0;
- _friends = { }, _username = _game = _gameTitle = _avatar = "", _silenced = false;
+ if (!username || callback == null) throw new Error("You must provide both a username and a callback to "+UserData+".");
+ friendCount = _id = _age = _level = _points = _status = _kreds = p = 0;
+ _friends = { };
+ _friendOf = _username = _game = _gameTitle = _avatar = "";
+ _mutual = _silenced = false;
+ _username = username;
_callback = callback;
var b:URLRequest = new URLRequest("http://api.kongregate.com/api/user_info.json?friends=true&username=" + username);
var c:URLLoader = new URLLoader();
@@ -168,88 +186,116 @@ package skyboy.kongregate {
c.load(b);
p = 1;
}
- private function loadUserComplete(e:Event):void {
+ protected function loadUserComplete(e:Event):void {
var d:String = e.target.data, i:int;
if (d.indexOf('"success":true') == -1) {
p = 5;
- throw new Error(d.substring(i = d.indexOf('n":"') + 4, d.indexOf('"', i)), errorID);
+ d = "(" + _username + ") " + d.substring(i = d.indexOf('n":"') + 4, d.indexOf('"', i));
+ _username = null;
+ delayedError(new Error(d, errorID), true);
} else if (d.indexOf('"private":false') == -1) {
p = 5;
- throw new Error("The Profile of " + d.substring(i = d.indexOf('"username":"') + 12, d.indexOf('"', i)) + " is private", errorID);
+ d = "The Profile of " + d.substring(i = d.indexOf('"username":"') + 12, d.indexOf('"', i)) + " is private";
+ _username = null;
+ delayedError(new Error(d, errorID), true);
}
- i = d.lastIndexOf('}');
- _id = parseInt(d.substring(i = d.indexOf('"user_id":') + 10, (d.indexOf(',', i) + 1 || d.indexOf('}', i) + 1) - 1), 10);
- _username = d.substring(i = d.indexOf('"username":"') + 12, d.indexOf('"', i));
- _avatar = d.substring(i = d.indexOf('"avatar_url":"') + 14, d.indexOf('"', i));
- _gender = d.substring(i = d.indexOf('"gender":"') + 10, d.indexOf('"', i));
- if ((i = d.indexOf('"game_title":"')) != -1) {
- _gameTitle = d.substring(i += 14, d.indexOf('"', i));
- _game = d.substring(i = d.indexOf('"game_url":"') + 12, d.indexOf('"', i));
- }
- _silenced = d.indexOf('"silenced_until":') != -1;
_status = int(d.indexOf('"admin":false') == -1) << 4 | int(d.indexOf('"curator":false') == -1) << 2 | int(d.indexOf('"moderator":false') == -1) << 3 | int(d.indexOf('"forum_moderator":false') == -1) << 1 | int(d.indexOf('"developer":false') == -1);
_points = parseInt(d.substring(i = d.indexOf('"points":') + 9, (d.indexOf(',', i) + 1 || d.indexOf('}', i) + 1) - 1), 10);
_level = parseInt(d.substring(i = d.indexOf('"level":') + 8, (d.indexOf(',', i) + 1 || d.indexOf('}', i) + 1) - 1), 10);
+ _id = parseInt(d.substring(i = d.indexOf('"user_id":') + 10, (d.indexOf(',', i) + 1 || d.indexOf('}', i) + 1) - 1), 10);
_age = parseInt(d.substring(i = d.indexOf('"age":') + 6, (d.indexOf(',', i) + 1 || d.indexOf('}', i) + 1) - 1), 10);
+ _chatAvatar = d.substring(i = d.indexOf('"chat_avatar_url":"') + 19, d.indexOf('"', i));
+ _avatar = d.substring(i = d.indexOf('"avatar_url":"') + 14, d.indexOf('"', i));
+ _username = d.substring(i = d.indexOf('"username":"') + 12, d.indexOf('"', i));
+ _gender = d.substring(i = d.indexOf('"gender":"') + 10, d.indexOf('"', i));
p = 2;
- var friendsA:Array = (_friendlist = d.substring(i = d.indexOf('"friends":[') + 11, d.indexOf(']', i)).replace(/"/g, "").split(',')).slice();
- friendCount = i = friendsA.length;
- if (i > 5) i = 5;
- inx = friendCount - i;
+ var friendsA:Array = d.substring(i = d.indexOf('"friends":[') + 11, d.indexOf(']', i)).replace(/"/g, "").split(',');
+ _friendlist = friendsA;
+ i = friendsA.length;
+ inx = i;
+ friendCount = i;
+ i = 5;
while (i--) {
- d = friendsA.pop();
- _friends[d] = false;
- loadFriend(d);
+ if (inx > 0) loadFriend(_friendlist[--inx]);
}
p = 3;
}
- private function loadFriend(a:String):void {
- var b:URLRequest = new URLRequest("http://api.kongregate.com/api/user_info.json?friends=trueusername=" + a);
- var c:URLLoader = new URLLoader();
- c.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFError);
- c.addEventListener(IOErrorEvent.IO_ERROR, onFError);
- c.addEventListener(Event.COMPLETE, loadFriendComplete);
- setTimeout(callLoad, 250, c, b);
- }
- private function callLoad(c:URLLoader, b:URLRequest):void {
- c.load(b);
+ protected function loadFriend(a:String):void {
+ _friends[a] = new FriendData(a, _username, doThing);
}
- private var l:uint;
- private function loadFriendComplete(e:Event):void {
- var d:String = e.target.data, i:int;
- doThing();
- if (d.indexOf('"success":true') == -1) {
- return;
- } else if (d.indexOf('"private":false') == -1) {
- return;
- }
- i = d.lastIndexOf('}');
- var u:String = d.substring(i = d.indexOf('"username":"') + 12, d.indexOf('"', i));
- var data:Object = _friends[u] = { game:null, title:null, avatar:null, mutual:null, username:u };
- data['avatar'] = d.substring(i = d.indexOf('"avatar_url":"') + 14, d.indexOf('"', i));
- data['mutual'] = d.substring(i = d.indexOf('"friends":[') + 11, d.indexOf(']', i)).indexOf('"'+_username+'"') !== -1
- if ((i = d.indexOf('"game_title":"')) != -1) {
- data['title'] = d.substring(i += 14, d.indexOf('"', i));
- data['game'] = d.substring(i = d.indexOf('"game_url":"') + 12, d.indexOf('"', i));
- }
- }
- private function doThing():void {
- ++l;
- if (l == friendCount) {
- p = 4;
- setTimeout(_callback, 0, this);
+ protected var l:uint;
+ protected function doThing():void {
+ if (p == 3) {
+ ++l;
+ if (l >= friendCount) {
+ setTimeout(_callback, 0, this);
+ p = 4;
+ }
}
if (inx > 0) loadFriend(_friendlist[--inx]);
}
- private function onFError(e:Event):void {
- doThing();
- trace(e.toString());
- throw new Error("<" + e.type + "> has occured while trying to load a file.", errorID);
- }
- private function onError(e:Event):void {
+ protected function onError(e:Event):void {
p = 5;
- trace(e.toString());
- throw new Error("<" + e.type + "> has occured while trying to load a file.", errorID);
+ delayedError(new Error("<" + e.type + "> has occured while trying to load a file.", errorID), true);
}
}
}
+import flash.events.Event;
+internal class FriendData extends skyboy.kongregate.UserData {
+ private var _loadFriends:Boolean, calledback:Boolean;
+ public function set constructor(a:*):void { }
+ public function get constructor():Class {
+ return skyboy.kongregate.UserData;
+ }
+ public function FriendData(username:String, friendOf:String, callback:Function):void {
+ super(username, callback);
+ _friendOf = friendOf
+ }
+ public override function get mutual():Boolean {
+ return _mutual;
+ }
+ protected override function loadUserComplete(e:Event):void {
+ try {
+ super.loadUserComplete(e);
+ _mutual = _friendlist.indexOf(_friendOf) != -1;
+ } catch (a:Error) {
+ delayedError(a);
+ }
+ if (!calledback) {
+ _callback();
+ calledback = true;
+ _callback = equals;
+ p = 4;
+ }
+ }
+ protected override function loadFriend(a:String):void {
+ if (_loadFriends) super.loadFriend(a);
+ else if (!calledback) {
+ _callback();
+ calledback = true;
+ _callback = equals;
+ p = 4;
+ }
+ }
+ public override function loadFriends(reload:Boolean = false):void {
+ if (!_loadFriends || (p == 4 && reload)) {
+ _loadFriends = true;
+ var i:int = friendCount;
+ inx = i;
+ if (i > 5) i = 5;
+ l = 0;
+ while (i--) {
+ doThing();
+ }
+ }
+ }
+ protected override function onError(e:Event):void {
+ if (!calledback) {
+ _callback();
+ calledback = true;
+ _callback = equals;
+ p = 4;
+ }
+ super.onError(e);
+ }
+}

0 comments on commit 72b0219

Please sign in to comment.