/www/index.html

https://gitlab.com/Sxw1212/questions · HTML · 211 lines · 200 code · 11 blank · 0 comment · 0 complexity · fe4d3074f0c74af73bf3e0989427fb17 MD5 · raw file

  1. <html>
  2. <head>
  3. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  4. <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  5. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/css/materialize.min.css">
  6. <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
  8. <script src="build/react.js"></script>
  9. <script src="build/react-dom.js"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
  11. <script src="/socket.io/socket.io.js"></script>
  12. <title>Questions</title>
  13. </head>
  14. <body>
  15. <div class="container" id="game"></div>
  16. <script type="text/babel">
  17. var AnswerBox = React.createClass({
  18. getInitialState() {
  19. return {answer: ""}
  20. },
  21. handleSubmit() {
  22. this.props.socket.emit("answer:submit");
  23. },
  24. handleAnswerChange(e) {
  25. this.setState({answer: e.target.value});
  26. this.props.socket.emit("answer:update", e.target.value);
  27. },
  28. render() {
  29. return (
  30. <div className="AnswerBox">
  31. <div className="row">
  32. <div className="input-field">
  33. <input type="text" id="answerbox-answer"
  34. onChange={this.handleAnswerChange}
  35. value={this.state.answer}/>
  36. <label for="answerbox-answer">Answer: {this.props.gameState.currentQuestionType}</label>
  37. </div>
  38. <a className="waves-effect waves-light btn"
  39. onClick={this.handleSubmit}>Submit</a>
  40. </div>
  41. </div>
  42. );
  43. }
  44. });
  45. var QuestionBox = React.createClass({
  46. getInitialState() {
  47. return {question: "", questionType: ""}
  48. },
  49. handleSubmit() {
  50. this.props.socket.emit("question:submit");
  51. },
  52. handleQuestionChange(e) {
  53. this.setState({question: e.target.value});
  54. this.props.socket.emit("question:update", e.target.value);
  55. },
  56. handleQuestionTypeChange(e) {
  57. this.setState({questionType: e.target.value});
  58. this.props.socket.emit("questiontype:update", e.target.value);
  59. },
  60. render() {
  61. return (
  62. <div className="QuestionBox">
  63. <div className="row">
  64. <div className="input-field">
  65. <input type="text" id="questionbox-question"
  66. onChange={this.handleQuestionChange}
  67. value={this.state.question}/>
  68. <label for="questionbox-question">Question</label>
  69. </div>
  70. <div className="input-field">
  71. <input type="text" id="questionbox-questiontype"
  72. onChange={this.handleQuestionTypeChange}
  73. value={this.state.questionType}/>
  74. <label for="questionbox-questiontype">Question Type</label>
  75. </div>
  76. <a className="waves-effect waves-light btn"
  77. onClick={this.handleSubmit}>Submit</a>
  78. </div>
  79. </div>
  80. );
  81. }
  82. });
  83. var JoinBox = React.createClass({
  84. getInitialState() {
  85. return {
  86. room: localStorage.lastRoom || ""
  87. };
  88. },
  89. handleRoomChange(e) {
  90. this.setState({room: e.target.value});
  91. },
  92. handleSubmit(e) {
  93. localStorage.lastRoom = this.state.room;
  94. console.log(this.state.room);
  95. this.props.socket.emit("join", this.state.room);
  96. },
  97. render() {
  98. return (
  99. <div className="JoinBox">
  100. <div className="row">
  101. <div className="input-field">
  102. <input type="text" id="joinbox-room"
  103. onChange={this.handleRoomChange}
  104. value={this.state.room}/>
  105. <label for="joinbox-room">Room</label>
  106. </div>
  107. <a className="waves-effect waves-light btn"
  108. onClick={this.handleSubmit}>Join</a>
  109. </div>
  110. </div>
  111. );
  112. }
  113. });
  114. var SpectateBox = React.createClass({
  115. render() {
  116. return (
  117. <div className="SpectateBox">
  118. <div className="row">
  119. <p>Current Question: {this.props.gameState.currentQuestion}</p>
  120. <p>Current Question Type: {this.props.gameState.currentQuestionType}</p>
  121. <p>Current Answer: {this.props.gameState.currentAnswer}</p>
  122. <p>Players: {this.props.gameState.players}</p>
  123. </div>
  124. </div>
  125. );
  126. }
  127. });
  128. var Game = React.createClass({
  129. getInitialState() {
  130. return {
  131. gameState: {
  132. showConnectingBox: true
  133. },
  134. socket: io()
  135. };
  136. },
  137. componentDidMount() {
  138. this.state.socket.on("connect", () => {
  139. console.log("Connected");
  140. });
  141. this.state.socket.on("disconnect", () => {
  142. console.log("Disconnected");
  143. this.setState({
  144. gameState: {
  145. showConnectingBox: true
  146. }
  147. });
  148. });
  149. this.state.socket.on("updateGameState", (gameState) => {
  150. this.setState({
  151. gameState: gameState
  152. });
  153. });
  154. },
  155. render() {
  156. return (
  157. <div className="Game">
  158. { this.state.gameState.showConnectingBox ? <div>
  159. <div className="progress">
  160. <div className="indeterminate"></div>
  161. </div>
  162. <p>Connecting to server</p>
  163. </div> : null}
  164. { this.state.gameState.showQuestionBox ? <QuestionBox
  165. socket={this.state.socket}
  166. gameState={this.state.gameState}
  167. /> : null}
  168. { this.state.gameState.showSpectateBox ? <SpectateBox
  169. gameState={this.state.gameState}
  170. /> : null}
  171. { this.state.gameState.showAnswerBox ? <AnswerBox
  172. socket={this.state.socket}
  173. gameState={this.state.gameState}
  174. /> : null}
  175. { this.state.gameState.showJoinBox ? <JoinBox
  176. socket={this.state.socket}
  177. /> : null}
  178. { this.state.gameState.showGameWaitBox ? <div>
  179. <div className="progress">
  180. <div className="indeterminate"></div>
  181. </div>
  182. <p>Waiting for round to start</p>
  183. </div> : null}
  184. { this.state.gameState.showYouAreUpBox ? <div>
  185. <div className="progress">
  186. <div className="indeterminate"></div>
  187. </div>
  188. <p>You are answering next!</p>
  189. </div> : null}
  190. </div>
  191. );
  192. }
  193. });
  194. ReactDOM.render(
  195. <Game/>,
  196. document.getElementById("game")
  197. );
  198. </script>
  199. </body>
  200. </html>