index.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE html>
  2. <meta charset="utf-8" />
  3. <html>
  4. <head>
  5. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  6. <script language="javascript" type="text/javascript">
  7. $(function() {
  8. var connection = null;
  9. $('#join').click(function() {
  10. var game_id = $('#gameId').val();
  11. var nick = $('#nick').val();
  12. var msg = {
  13. join: {
  14. game_id: game_id,
  15. nick: nick,
  16. }
  17. };
  18. connect(msg);
  19. });
  20. $('#create').click(function() {
  21. var game_id = $('#gameId').val();
  22. var nick = $('#nick').val();
  23. var msg = {
  24. create_game: {
  25. game_id: game_id,
  26. nick: nick,
  27. }
  28. };
  29. connect(msg);
  30. });
  31. $('#submit').click(function() {
  32. var word = $('#word').val();
  33. var msg = {
  34. submit_word: {
  35. word: word
  36. }
  37. };
  38. send(msg);
  39. });
  40. function connect(initial_message) {
  41. if (connection != null) {
  42. disconnect();
  43. }
  44. var uri = 'ws://' + window.location.host + '/ws/';
  45. connection = new WebSocket(uri);
  46. connection.onopen = function() {
  47. connection.send(JSON.stringify(initial_message));
  48. };
  49. connection.onmessage = function(e) {
  50. onReceive(e);
  51. };
  52. connection.onclose = function() {
  53. };
  54. }
  55. function send(message) {
  56. if (connection != null) {
  57. connection.send(JSON.stringify(message));
  58. }
  59. }
  60. function disconnect() {
  61. connection.close();
  62. connection = null;
  63. }
  64. function onReceive(msg) {
  65. var obj = jQuery.parseJSON(msg.data);
  66. if (obj.game_not_found != null) {
  67. $('#status').html("Game \"" + obj.game_not_found.game_id + "\" not found");
  68. }
  69. else if (obj.game_already_exists != null) {
  70. $('#status').html("Game \"" + obj.game_already_exists.game_id + "\" already exists");
  71. }
  72. else if (obj.game_state != null) {
  73. var gs = obj.game_state;
  74. if (gs.state_data.creating != null) {
  75. var players = gs.players;
  76. playerlist = "";
  77. for (var i = 0; i < players.length; i++) {
  78. playerlist += players[i].nick + "(" + players[i].points + ")";
  79. if (i + 1 < players.length)
  80. playerlist += ", ";
  81. }
  82. $('#status').html("Players: " + playerlist);
  83. var creating = gs.state_data.creating;
  84. var chars = creating.available_chars;
  85. $('#question').val(creating.question);
  86. $('#letters').val(chars.join());
  87. $('#createform').show();
  88. }
  89. else if (gs.state_data.guessing != null) {
  90. $('#createform').hide();
  91. $('#guessing').show();
  92. }
  93. }
  94. else {
  95. $('#status').html("Unknown Message recieved");
  96. }
  97. }
  98. });
  99. </script>
  100. </head>
  101. <body>
  102. <form id="loginform">
  103. <label for="gameId">Game ID:</label>
  104. <input id="gameId" type="text" /><br>
  105. <label for="nick">Nickname:</label>
  106. <input id="nick" type="text" /><br>
  107. <input id="join" type="button" value="Join" />
  108. <input id="create" type="button" value="Create Game" />
  109. </form>
  110. <form id="createform" style="display:none">
  111. <input id="question" type="text" readonly="readonly" /><br>
  112. <label for="letters">Create a word with the following letters</label>
  113. <input id="letters" type="text" readonly="readonly" /><br>
  114. <label for="word">Word:</label>
  115. <input id="word" type="text" /><br>
  116. <input id="submit" type="button" value="Submit Word" />
  117. </form>
  118. <div id="guessing" style="display:none">
  119. You now have to guess!
  120. </div>
  121. <div id="status">
  122. </div>
  123. </body>
  124. </html>