// Copyright 2005 David Caldwell <david@porkrind.org> All rights reserved. -*- c -*-
var Login;function stats(game){this.game=game;this.queue=[];this.anonymous_queue=[];this.sent=[];this.pending_stats=doc.text("");doc.id("pending_stats").appendChild(this.pending_stats);doc.id("pending").onclick=wrap(this,function(){this.send_queued();});this.pending=doc.id("pending");this.spinner=doc.id("spinner");this.start=function(){this.done();this.current={"name":this.game,"start date":(new Date()).getTime()};}
this.started=function(){return this.current&&!this.current._done;}
this.pause=function(){if(!this.current._pause)
this.current._pause=(new Date()).getTime();}
this.unpause=function(){if(this.current._pause)
this.inc("pause",(new Date()).getTime()-this.current._pause);delete this.current._pause;}
this.done=function(callback){if(!this.current)return;if(!this.current._done){this.unpause();this.current["end date"]=(new Date()).getTime();this.current._callback=callback;this.current._done=true;this.send(this.current);if(piwikTracker)
piwikTracker.trackGoal(this.current&&this.current.moves?1:2);}else if(callback){if(this.current._play_id)
callback(this.current._play_id);}
this.current=undefined;}
this.set=function(stat,val){if(!this.current)return;return this.current[stat]=val;}
this.inc=function(stat,inc){if(!this.current)return;if(inc==undefined)inc=1;if(this.current[stat]==undefined)this.current[stat]=0;return this.current[stat]+=inc;}
this.dec=function(stat,dec){if(!this.current)return;if(dec==undefined)dec=1;if(this.current[stat]==undefined)this.current[stat]=0;return this.current[stat]-=dec;}
this.max=function(stat,val){if(!this.current)return;if(this.current[stat]==undefined)
return this.current[stat]=val;return this.current[stat]=Math.max(this.current[stat],val);}
this.min=function(stat,val){if(!this.current)return;if(this.current[stat]==undefined)
return this.current[stat]=val;return this.current[stat]=Math.min(this.current[stat],val);}
this.send=function(data){this.queue.push(data);this.update_status();this.send_queued();}
this.update_status=function(){if(this.queue.length){this.pending.style.display="inline";this.pending.title=this.queue.length+" game"+(this.queue.length>1?"s":"")+" are waiting to be sent.  Click to send immediately.";this.pending_stats.data=this.queue.length;}else{this.pending.style.display="none";this.pending_stats.data="";}}
this.fix=function(){if(this.outstanding){this.sending=false;this.queue.unshift(this.outstanding);this.outstanding.fixed=(this.outstanding.fixed||0)+1;delete this.outstanding;printf("Found outstanding stats. Sending again.\n");this.send();}else
printf("No outstanding stats to fix.\n");}
this.send_queued=function(){if(this.sending)
return;var data,url,payload;if(this.anonymous_queue.length&&Cookie.list.greenfelt_user){data=this.anonymous_queue.shift();data.identify_anonymous=true;url=makeurl('play','identify',data._play_id,data._anon_key,{user:Cookie.list.greenfelt_user});}else if(this.queue.length){this.outstanding=data=this.queue.shift();url=makeurl('play','create');payload=merge(map_obj(function(k,o){return k.charAt(0)=="_"||o[k]==undefined?{}:o;},data),Login&&Login.is_logged_in()?{user:Login.user}:{});}else{this.update_status();return;}
this.sending=true;this.spinner.style.display="inline";var callback=wrap(this,function(result,error,status){this.spinner.style.display="none";this.sending=false;if(result==undefined){printf("Error sending stats, re-queueing: %j\n",error);data.stats_send_retry=(data.stats_send_retry||0)+1;if(data.identify_anonymous)
this.anonymous_queue.unshift(data);else
this.queue.unshift(data);delete this.outstanding;if(data._callback)data._callback(undefined,"Your game could not be saved. You can click the little card icon in the top right corner to try to save it again. ("+error+")");return;}
delete this.outstanding;this.update_status();data._play_id=result.play_id;data._anon_key=result.anon_key;if(result.anon_key){this.anonymous_queue.push(data);if(Login&&Login.is_logged_in())
Login.set_error("Please log in again.");}
if(data._callback)data._callback(result.play_id,undefined,result.play);this.sent.push(data);this.send_queued();});if(payload)
postForm(url,payload,interpJSON(callback));else
loadJSON(url,callback);}
if(Login)Login.on_login(wrap(this,function(){this.send_queued()}));}
function Stat(div,stat_name,stats){this.div=div;if(div)
div.appendChild(this.text=document.createTextNode(this.value=0));this.stat_name=stat_name;this.stats=stats;}
Stat.prototype.upd=function(){return this.stats.set(this.stat_name,this.value);}
Stat.prototype.inc=function(inc){(this.value+=inc);if(this.div)this.text.data=this.value;return this.upd();}
Stat.prototype.set=function(val){this.value=val;if(this.div)this.text.data=this.value;return this.upd();}
Stat.prototype.get=function(){return this.value}
