| 7 | | |
| 8 | | function updateStatus( stat ) { |
| | 7 | var connection_state = "disconnected"; |
| | 8 | var baseplayer = null; |
| | 9 | var player_status = null; |
| | 10 | var you = null; |
| | 11 | var action_box = null; |
| | 12 | var players = {} |
| | 13 | var n_requests = 0; |
| | 14 | |
| | 15 | function Player(bp) { |
| | 16 | this.pl = bp; |
| | 17 | this.money = $(".money",bp); |
| | 18 | this.id = $(".id",bp); |
| | 19 | this.owner = $(".owner",bp); |
| | 20 | this.seat = $(".seat",bp); |
| | 21 | this.actions = $(".actions",bp); |
| | 22 | this.cards = $(".cards",bp); |
| | 23 | } |
| | 24 | Player.prototype = { |
| | 25 | updatePrivate: function( privateData ) { |
| | 26 | if( privateData == null ) { |
| | 27 | this.cards.text(""); |
| | 28 | } |
| | 29 | }, |
| | 30 | updatePublic: function( publicData ) { |
| | 31 | pd = publicData; //make it shorter |
| | 32 | this.money.text(pd.money); |
| | 33 | this.seat.text(pd.seat); |
| | 34 | this.owner.text(pd.owner); |
| | 35 | //this.actions.text(actionList.toString()); |
| | 36 | }, |
| | 37 | updateActions: function( actions ) { |
| | 38 | this.actions.empty(); |
| | 39 | this.actions.append(makeListList(actions)); |
| | 40 | } |
| | 41 | } |
| | 42 | |
| | 43 | function makeListList( list ) { |
| | 44 | var root = $("<table/>"); |
| | 45 | for( i in list ) { |
| | 46 | row = $("<tr/>"); |
| | 47 | act = list[i]; |
| | 48 | for( j in act ) { |
| | 49 | row.append($("<td/>").text(act[j])); |
| | 50 | } |
| | 51 | root.append(row); |
| | 52 | } |
| | 53 | return root; |
| | 54 | } |
| | 55 | function processStatus( stat ) { |
| 12 | | $.getJSON(host, |
| 13 | | {"token":token, |
| 14 | | "method":"get_update"}, |
| 15 | | function(json) { updateStatus( json ) }) |
| 16 | | } else { |
| 17 | | alert(token); |
| 18 | | } |
| 19 | | } |
| | 59 | if( connection_state == "disconnected" ) { |
| | 60 | $("form#connect_form").hide(); |
| | 61 | state = "connected"; |
| | 62 | gstatus.text(state); |
| | 63 | } |
| | 64 | //Do status generating code here |
| | 65 | var player_names = [] |
| | 66 | |
| | 67 | |
| | 68 | if( stat.table_info ) { |
| | 69 | ti = stat.table_info; |
| | 70 | player_names = ti.player_names; |
| | 71 | for( i in player_names ) { |
| | 72 | pn = player_names[i] |
| | 73 | if( players[pn] == null ) { //if it's null let's create a new player object |
| | 74 | newPlayer = baseplayer.clone(true); |
| | 75 | |
| | 76 | players[pn] = new Player(newPlayer); |
| | 77 | players[pn].id.text(pn); |
| | 78 | if( pn == stat.you.id ) { |
| | 79 | you = players[stat.you.id] |
| | 80 | $("#you").append(you.pl) |
| | 81 | } else { |
| | 82 | player_status.append( newPlayer ); |
| | 83 | } |
| | 84 | } |
| | 85 | players[pn].updatePublic( ti.players[pn] ); |
| | 86 | } |
| | 87 | } |
| | 88 | if( stat.game_state ) { |
| | 89 | gs = stat.game_state; |
| | 90 | for( i in player_names ) { |
| | 91 | pn = player_names[i] |
| | 92 | if( gs.player_actions[pn] ) { //if it's null let's create a new player object |
| | 93 | players[pn].updateActions( gs.player_actions[pn] ); |
| | 94 | } |
| | 95 | } |
| | 96 | } |
| | 97 | |
| | 98 | if( stat.you ) { |
| | 99 | you.updatePrivate(you); |
| | 100 | if( stat.you.required_action ) { |
| | 101 | for( i in stat.you.possible_actions ) { |
| | 102 | act = new Action( i, stat.you.possible_actions[i][0], stat.you.possible_actions[i][1] ); |
| | 103 | action_box.append( act.form ); |
| | 104 | } |
| | 105 | action_box.show(); |
| | 106 | } |
| | 107 | } |
| | 108 | } |
| | 109 | } |
| | 110 | |
| | 111 | function makeRequest( data ) { |
| | 112 | $.getJSON(host, |
| | 113 | data, |
| | 114 | updateStatus ) |
| | 115 | n_requests += 1; |
| | 116 | } |
| | 117 | |
| | 118 | function updateStatus( statarr ) { |
| | 119 | n_requests -= 1; |
| | 120 | for( i in statarr ) { |
| | 121 | processStatus( statarr[i] ); |
| | 122 | if( n_requests == 0 ) { |
| | 123 | makeRequest( {"token":token, "method":"get_update"} ); |
| | 124 | } |
| | 125 | } |
| | 126 | } |
| | 127 | |
| | 128 | function Action( action, min, max ) { |
| | 129 | this.name = action; |
| | 130 | this.min = min; |
| | 131 | this.max = max; |
| | 132 | this.requiresInput = this.min && !(this.min == this.max); |
| | 133 | this.title = this.name + " "; |
| | 134 | if( this.min ) { |
| | 135 | if( this.min != this.max ) { |
| | 136 | this.title += this.min + " < x < " + this.max; |
| | 137 | } else { |
| | 138 | this.title += this.min; |
| | 139 | } |
| | 140 | } |
| | 141 | this.val = null; |
| | 142 | this.form = $("<form/>"); |
| | 143 | this.form.append($("<input type='submit' value='" + this.title + "'/>")); |
| | 144 | if( this.requiresInput ) { |
| | 145 | this.val = $("<input type='input' value='" + this.min + "' name='money'/>"); |
| | 146 | this.form.append(this.val); |
| | 147 | } |
| | 148 | var myself = this; |
| | 149 | this.form.submit(function() { |
| | 150 | myself.submitForm(); |
| | 151 | return false; }); |
| | 152 | |
| | 153 | |
| | 154 | } |
| | 155 | |
| | 156 | Action.prototype = { |
| | 157 | checkRange : function() { |
| | 158 | var v = null; |
| | 159 | if( this.requiresInput ) { |
| | 160 | v = parseFloat(this.val.val()); |
| | 161 | if( !v || v < this.min || v > this.max ) { |
| | 162 | alert( "requires valid input for " + this.name ); |
| | 163 | return null; |
| | 164 | } |
| | 165 | } else { |
| | 166 | v = this.min; |
| | 167 | } |
| | 168 | return v; |
| | 169 | }, |
| | 170 | submitForm : function() { |
| | 171 | var v = null |
| | 172 | if(this.min != null && (v=this.checkRange()) == null ) { |
| | 173 | return false; |
| | 174 | } |
| | 175 | makeRequest({"method":"action", "action":this.name,"value":v,"token":token}); |
| | 176 | action_box.hide(); |
| | 177 | action_box.empty(); |
| | 178 | return false; |
| | 179 | } |
| | 180 | |
| | 181 | } |
| | 182 | |
| 26 | | table_name = this.table_name.value |
| 27 | | user_id = this.user_id.value |
| 28 | | host = this.host.value + "?jsoncallback=?" |
| 29 | | client_id = this.client_id.value |
| 30 | | gstatus.text("connecting..."); |
| 31 | | $.getJSON(host, |
| 32 | | {"table_name":table_name, |
| 33 | | "client_id":client_id, |
| 34 | | "user_id":user_id, |
| 35 | | "method":"connect"}, |
| 36 | | function(json) { updateStatus( json ) } ); |
| 37 | | return false; }); |
| | 194 | table_name = this.table_name.value; |
| | 195 | user_id = this.user_id.value; |
| | 196 | host = this.host.value + "?jsoncallback=?"; |
| | 197 | client_id = this.client_id.value; |
| | 198 | state = "connecting"; |
| | 199 | gstatus.text(state); |
| | 200 | makeRequest({"table_name":table_name, |
| | 201 | "client_id":client_id, |
| | 202 | "user_id":user_id, |
| | 203 | "method":"connect"}); |
| | 204 | return false; }); |