import com.xfactorstudio.xml.xpath. *; import mx.events.EventDispatcher; import mx.utils.Delegate; class Highscore { private static var listClip : MovieClip; private static var urlTemplate = "http://mitchell.dma.nl/games/2/scores.xml";///funda-no-waste";//"scores.xml";//"http://api.rangerclub.nl/rest/?method=getHighscores&apikey=[apikey]&gameid=[gameid]&nscores=10" //private static var apikey = "8729A619-908C-1D15-E8E89C0DB57ED40A"; private static var gameid : String; private static var userid : String; //private static var score : Number; private static var table : MovieClip; private static var xml; private static var statusField : TextField; private static var nrWidth = 25; // breedte van de nr-kolom private static var scoreWidth = 60; // breedte van de score-kolom private static var statusInterval:Object; private static var button:Button; private static var color = 0xFFffff; //private static var result:LoadVars = new LoadVars(); private static var event_dispatcher : EventDispatcher; public static var ON_POST = "on_post"; public static var ON_POST_ERROR = "on_post_error"; //public static var ON_HIGHSCORE = "on_highscore"; public static var ON_HIGHSCORE_FAIL = "on_highscore_fail"; public static var CANCEL_HIGHSCORE = "cancel_highscore"; private static var TOKEN = "006d0c293b206d8c3fb9"; private static var MSG_LOAD = "De highscore wordt geladen"; private static var CHECK_HIGHSCORE_URL = "http://mitchell.dma.nl/games/2/hiscore"; private static var check_vars : LoadVars; private static var form : MovieClip; /** * Initialiseer de highscore * * @param listClip : een clip met daarin een movieclip 'table' waarin de score wordt getoond * @param form_clip : een clip met daarin 2 buttons 'ok_button' en 'cancel_button' en 3 velden: 'email', 'name', 'company' * * en een button met de naam Button */ public static function initialize (theListClip, form_clip, theGameid)//, theUserid) { trace ("ini hiscore"); //score = 0; listClip = theListClip; listClip._visible = false; form = form_clip; form._visible=false; table = listClip.table; table._visible = false; button = listClip.button; statusField = createStatusField(); event_dispatcher = new EventDispatcher; if (CustomActions) simulate (); else { gameid = theGameid; // userid = theUserid; } button.onRelease = function () { _root.loadMovie (_url);//+"?gameid="+gameid+"&userid="+userid); } } public static function addEventListener (type:String, method:Function) { event_dispatcher.addEventListener (type,method); } /** * Post de highscore en laad daarna de tabel * * @param theScore */ public static function post (theScore,username,email,company):Void { trace ("post! score="+theScore); listClip._visible = true; var load_vars = new LoadVars(); load_vars.points = theScore; load_vars.username = username; load_vars.email = email; load_vars.company = company; var checksum = Sha1.encode (TOKEN+"_"+theScore); load_vars.checksum = checksum trace ("checksum="+checksum); xml = new XPathDocument (); xml.contentType = "text/xml"; xml.ignoreWhite = true; xml.onLoad = function (succes) { trace ("on load"); if (succes) { trace ("succes"); event_dispatcher.dispatchEvent({type:ON_POST}); parseXML (true); } else { showStatus ("Helaas, de highscore kon niet worden gepost vanwege een technische fout."); event_dispatcher.dispatchEvent({type:ON_POST_ERROR}); } }/* function (succes) { for (var i in result) trace (i+"-->"+result); //trace ("load "+result.toString()); if (succes) { event_dispatcher.dispatchEvent({type:ON_POST}); } else { showStatus ("Helaas, de highscore kon niet worden gepost vanwege een technische fout."); event_dispatcher.dispatchEvent({type:ON_POST_ERROR}); } }*/ showStatus (MSG_LOAD, true); load_vars.sendAndLoad (url, xml, "POST"); /* xml = new XPathDocument (); xml.contentType = "text/xml"; xml.ignoreWhite = true; xml.onLoad = function (succes) { if (succes) { load (); // laad de highscore else showStatus ("Postfout: de highscore-server geeft een foutmelding"); } else { showStatus ("Postfout: de highscore-server kon niet worden bereikt"); } } xml.load (postUrl); */ } /** haal de score-xml op * */ public static function load () { trace ("load hi"); listClip._visible=true; xml = new XPathDocument (); xml.contentType = "text/xml"; xml.ignoreWhite = true; xml.onLoad = parseXML; showStatus (MSG_LOAD, true); xml.load (url); } /* * * Ga na of score een hiscore is, en stuur een event als het niet zo is. * Toon ook automatisch een form als het wel zo is */ public static function check_highscore (score:Number):Void { xml = new XPathDocument (); xml.contentType = "text/xml"; xml.ignoreWhite = true; xml.onLoad = parse_highscore_check check_vars = new LoadVars(); check_vars.points = score; //check_vars.limit=10; check_vars.sendAndLoad (CHECK_HIGHSCORE_URL, xml, "GET"); } /** * Geef actions aan de knoppen op het hiscore-form en toon het form **/ private static function show_form (score:Number):Void { form.score = score; form.ok_button.onRelease = function () { form._visible=false; post (form.score,form.username.text,form.email.text,form.company.text) }; form.cancel_button.onRelease = function () { form._visible=false; event_dispatcher.dispatchEvent({type:CANCEL_HIGHSCORE}); }; form._visible=true; } /** * Interpreteer de feedback van de highscore server mbt een mogelijke hiscore */ private static function parse_highscore_check (succes:Boolean) : Void { trace ("parse_highscore_check"); if (succes) { //clearStatus (); //var stat = xml.selectSingleNode(".//rsp").attributes.stat; //if (stat=="ok") { var is_hiscore = (xml.selectSingleNode("//hiscore").firstChild.nodeValue=="true"); var points = new Number(xml.selectSingleNode ("//hiscore").attributes.points); // TODO if (is_hiscore) show_form (points); else event_dispatcher.dispatchEvent({type:ON_HIGHSCORE_FAIL}); } else { showStatus ("Helaas, de highscore-server kon niet worden bereikt. Excuses voor het ongemak."); } } /** * bouw de tabel op * * @param succes : is het laden gelukt? */ private static function parseXML (succes:Boolean) : Void { if (succes) { clearStatus (); //var stat = xml.selectSingleNode(".//rsp").attributes.stat; //if (stat=="ok") { var nodes = xml.selectNodes (".//score"); if (nodes.length>0) { for (var i=0; i0) counter--; else { counter=1; dir=1; } } var suffix=""; while (suffix.length