import rps.Rps; import js.Lib; import js.Dom; import haxe.remoting.Connection; class WebRps extends Rps { static var game : WebRps; static var flcon : haxe.remoting.Connection; var my_choice_cell : js.HtmlDom; var my_stats_cell : js.HtmlDom; var his_choice_cell : js.HtmlDom; var his_stats_cell : js.HtmlDom; var tie_stats_cell : js.HtmlDom; var result_cell : js.HtmlDom; public function game_round(token : Token) : Void { super.game_round(token); refresh_display(); update_gene_player(); } function refresh_display() : Void { my_choice_cell.innerHTML = Rps.string(stats.last_round.mine); his_choice_cell.innerHTML = Rps.string(stats.last_round.his); result_cell.innerHTML = ResultString(stats.last_round.result); var tt = stats.i_won + stats.he_won + stats.ties; my_stats_cell.innerHTML = ""+stats.i_won+"/" + tt + " ("+Math.round(100*stats.i_won/tt)+"%)"; his_stats_cell.innerHTML = ""+stats.he_won+"/" + tt + " ("+Math.round(100*stats.he_won/tt)+"%)"; tie_stats_cell.innerHTML = ""+stats.ties+"/" + tt + " ("+Math.round(100*stats.ties/tt)+"%)"; } static function ResultString(result:Outcome) : String { return switch(result) { case I_WON: "Computer won!"; case TIE: "Tie"; case HE_WON: "Human won!"; } } public function new() : Void { super(); my_choice_cell = Lib.document.getElementById("my.choice"); my_stats_cell = Lib.document.getElementById("my.stats"); his_choice_cell = Lib.document.getElementById("his.choice"); his_stats_cell = Lib.document.getElementById("his.stats"); tie_stats_cell = Lib.document.getElementById("tie.stats"); result_cell = Lib.document.getElementById("result"); var but : js.Button; var game = this; but = cast Lib.document.getElementById("r_button"); but.onclick = function (ev) { game.game_round(ROCK); } but = cast Lib.document.getElementById("p_button"); but.onclick = function (ev) { game.game_round(PAPER); } but = cast Lib.document.getElementById("s_button"); but.onclick = function (ev) { game.game_round(SCISSORS); } } function handle_key(e : js.Event) : Void { if ( cast(e.altKey,Bool) || cast(e.ctrlKey,Bool) || cast(e.shiftKey,Bool)) return; switch (e.keyCode) { case 49: // 1 game_round(ROCK); case 82: // r game_round(ROCK); case 50: // 2 game_round(PAPER); case 80: // p game_round(PAPER); case 51: // 3 game_round(SCISSORS); case 83: // s game_round(SCISSORS); } } static function main() { game = new WebRps(); Lib.document.onkeydown = game.handle_key; } static function connect() { flcon = haxe.remoting.Connection.flashConnect("GenePlayer"); } public function update_gene_player() { if (flcon != null) flcon.GenePlayer.player.start.call([]); } static public function shutup() { if (flcon != null) flcon.GenePlayer.player.shutup.call([]); } static function predict_stat( pattern ) { return game.predict( pattern ); } }