App.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import React from 'react';
  2. import Board from "./Board";
  3. import {gameService} from "./services/GameService";
  4. import Logs from "./Logs";
  5. import Video from "./Video";
  6. import Players from './Players';
  7. import SelectPlayerDialog from "./SelectPlayerDialog";
  8. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  9. import {faTimesCircle} from "@fortawesome/free-solid-svg-icons";
  10. import {rtcService} from "./services/webrtc";
  11. import Settings from "./Settings";
  12. import HelpDialog from "./HelpDialog";
  13. const MAX_LOGS = 200;
  14. export default class App extends React.Component {
  15. constructor(props) {
  16. super(props)
  17. this.state = {
  18. logs: [],
  19. chat: [],
  20. cardToShow: null,
  21. showHelp: true,
  22. lostConnection: false
  23. };
  24. }
  25. componentDidMount() {
  26. this.connectToGame();
  27. }
  28. connectToGame = () => {
  29. this.setState({game: null}, () => {
  30. gameService.currentPlayer = null;
  31. const location = window.location;
  32. let url = location.protocol.replace("http", "ws") + '//' + window.location.hostname + (location.port.length > 0 ? ':' + location.port : "") + location.pathname;
  33. console.log('connecting to websocket', location, url);
  34. const wsConnection = new WebSocket(url);
  35. wsConnection.onclose = () => {
  36. this.setState({lostConnection: true}, () => {
  37. setTimeout(this.connectToGame, 1000);
  38. });
  39. }
  40. wsConnection.onmessage = this.updateGame;
  41. gameService.ws = wsConnection;
  42. rtcService.serverConnection = wsConnection;
  43. })
  44. }
  45. updateGame = (message) => {
  46. const data = JSON.parse(message.data);
  47. if (data.type === 'game') {
  48. const game = data.game;
  49. gameService.game = game;
  50. this.setState({game: game});
  51. } else if (data.type === 'log') {
  52. this.setState({logs: data.message});
  53. } else if (data.type === 'chat') {
  54. this.setState({chat: data.message});
  55. } else if (data.type === 'cardDrawn') {
  56. this.setState({
  57. cardToShow: {
  58. card: data.card,
  59. player: data.player,
  60. type: data.cardType,
  61. }
  62. });
  63. } else if (data.type === 'newGame') {
  64. gameService.currentPlayer = null;
  65. } else {
  66. rtcService.gotMessageFromServer(message);
  67. }
  68. }
  69. showHelp = () => this.setState({showHelp: true});
  70. hideHelp = () => this.setState({showHelp: false});
  71. render() {
  72. if (this.state.game) {
  73. const showPlayerDialog = gameService.currentPlayer === null && !this.state.showHelp;
  74. const card = this.state.cardToShow;
  75. return (<div>
  76. <Settings game={this.state.game} logs={this.state.logs} chat={this.state.chat}
  77. showHelp={this.showHelp}/>
  78. <div className="game">
  79. <Board game={this.state.game}/>
  80. <Logs logs={this.state.logs}/>
  81. <Video game={this.state.game} chat={this.state.chat}/>
  82. <Players game={this.state.game}/>
  83. </div>
  84. {this.state.showHelp && <HelpDialog dismiss={this.hideHelp}/>}
  85. {showPlayerDialog && <SelectPlayerDialog game={this.state.game}/>}
  86. {card && <div class="card-overlay">
  87. <div className={"card-picked " + card.type}>
  88. {<a className="close" onClick={(e) => {
  89. this.setState({cardToShow: null});
  90. }}><FontAwesomeIcon icon={faTimesCircle}/></a>}
  91. <span className="card-type">{card.type}</span>
  92. <span className="card-text">{card.card}</span>
  93. <span className="card-player">TO: {card.player.name}</span>
  94. </div>
  95. </div>}
  96. </div>);
  97. } else {
  98. return (<div className="game-loading">
  99. {this.state.lostConnection && <span>Lost connection to server, reconnecting...</span>}
  100. {!this.state.lostConnection && <span>Loading game...</span>}
  101. </div>);
  102. }
  103. }
  104. }