index.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. var wordlist = null;
  10. var questionlist = null;
  11. $('#join').click(function() {
  12. var game_id = $('#gameId').val();
  13. var nick = $('#nick').val();
  14. var msg = {
  15. join: {
  16. game_id: game_id,
  17. nick: nick,
  18. }
  19. };
  20. connect(msg);
  21. });
  22. $('#create').click(function() {
  23. var game_id = $('#gameId').val();
  24. var nick = $('#nick').val();
  25. var msg = {
  26. create_game: {
  27. game_id: game_id,
  28. nick: nick,
  29. }
  30. };
  31. connect(msg);
  32. });
  33. $('#ready').click(function() {
  34. var game_id = $('#gameId').val();
  35. var nick = $('#nick').val();
  36. var msg = "ready";
  37. send(msg);
  38. });
  39. $('#submit').click(function() {
  40. var word = $('#word').val();
  41. var msg = {
  42. submit_word: {
  43. word: word
  44. }
  45. };
  46. send(msg);
  47. });
  48. $('#submitGuess').click(function() {
  49. var list = [];
  50. for(var i = 0; i < wordlist.length; i++) {
  51. var number = parseInt($("g" + i).val());
  52. if (number == NaN) {
  53. $("#status").html("Please enter a valid number");
  54. return;
  55. }
  56. else if (number > 0 && number <= questionlist.length) {
  57. list.push([wordlist[i], questionlist[number - 1]]);
  58. }
  59. }
  60. var msg = {
  61. submit_guess: {
  62. guesses: list
  63. }
  64. };
  65. send(msg);
  66. });
  67. function connect(initial_message) {
  68. if (connection != null) {
  69. disconnect();
  70. }
  71. var uri = 'ws://' + window.location.host + '/ws/';
  72. connection = new WebSocket(uri);
  73. connection.onopen = function() {
  74. connection.send(JSON.stringify(initial_message));
  75. };
  76. connection.onmessage = function(e) {
  77. onReceive(e);
  78. };
  79. connection.onclose = function() {
  80. };
  81. }
  82. function send(message) {
  83. if (connection != null) {
  84. connection.send(JSON.stringify(message));
  85. }
  86. }
  87. function disconnect() {
  88. connection.close();
  89. connection = null;
  90. }
  91. function onReceive(msg) {
  92. var obj = jQuery.parseJSON(msg.data);
  93. if (obj.game_not_found != null) {
  94. $('#status').html("Game \"" + obj.game_not_found.game_id + "\" not found");
  95. }
  96. else if (obj.game_already_exists != null) {
  97. $('#status').html("Game \"" + obj.game_already_exists.game_id + "\" already exists");
  98. }
  99. else if (obj.game_state != null) {
  100. var gs = obj.game_state;
  101. if (gs.state_data === "starting") {
  102. $('#startingform').show();
  103. }
  104. else if (gs.state_data.creating != null) {
  105. var players = gs.players;
  106. playerlist = "";
  107. for (var i = 0; i < players.length; i++) {
  108. playerlist += players[i].nick + "(" + players[i].points + ")";
  109. if (i + 1 < players.length)
  110. playerlist += ", ";
  111. }
  112. $('#status').html("Players: " + playerlist);
  113. var creating = gs.state_data.creating;
  114. var chars = creating.available_chars;
  115. $('#question').val(creating.question);
  116. $('#letters').val(chars.join());
  117. $('#createform').show();
  118. }
  119. else if (gs.state_data.guessing != null) {
  120. var guesses = gs.state_data.guessing;
  121. var sub_words = guesses.submitted_words;
  122. var questions = guesses.questions;
  123. var questionsHtml = "";
  124. for (var i = 0; i < questions.length; i++) {
  125. questionsHtml += (i + 1) + ": " + questions[i] + "<br>";
  126. }
  127. $('#guessingDynTable').html(questionsHtml);
  128. $('#guessingDyn').html("");
  129. for (var i = 0; i < sub_words.length; i++) {
  130. var $label = $("<label to='g" + i + "'>" + sub_words[i][0] + "</label>");
  131. var $field = $("<input name='g" + i + "' type='text' /><br>");
  132. $('#guessingDyn').append($label);
  133. $('#guessingDyn').append($field);
  134. }
  135. questionlist = questions;
  136. wordlist = sub_words.map(pair => pair[0]);
  137. $('#createform').hide();
  138. $('#guessing').show();
  139. }
  140. }
  141. else {
  142. $('#status').html("Unknown Message recieved");
  143. }
  144. }
  145. });
  146. </script>
  147. </head>
  148. <body>
  149. <form id="loginform">
  150. <label for="gameId">Game ID:</label>
  151. <input id="gameId" type="text" /><br>
  152. <label for="nick">Nickname:</label>
  153. <input id="nick" type="text" /><br>
  154. <input id="join" type="button" value="Join" />
  155. <input id="create" type="button" value="Create Game" />
  156. </form>
  157. <form id="startingform" style="display:none">
  158. <input id="ready" type="button" value="Ready" />
  159. </form>
  160. <form id="createform" style="display:none">
  161. <input id="question" type="text" readonly="readonly" /><br>
  162. <label for="letters">Create a word with the following letters</label>
  163. <input id="letters" type="text" readonly="readonly" /><br>
  164. <label for="word">Word:</label>
  165. <input id="word" type="text" /><br>
  166. <input id="submit" type="button" value="Submit Word" />
  167. </form>
  168. <div id="guessing" style="display:none">
  169. You now have to guess!
  170. <div id="guessingDynTable">
  171. </div>
  172. <form id="guessingform">
  173. <div id="guessingDyn">
  174. </div>
  175. <input id="submitGuess" type="button" value="Submit Guess" />
  176. </form>
  177. </div>
  178. <div id="status">
  179. </div>
  180. </body>
  181. </html>