From d2bb025723bb9b77b220962daef5d057446093cd Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 26 Aug 2022 21:32:43 +0300 Subject: [PATCH] new --- src/App.js | 4 +++ src/components/Field.js | 28 ++++++++++++++++++--- src/screens/Connect.js | 33 +++++++++++++++++++++++- src/screens/Game.js | 56 +++++++++++++++++++++++++++++++++++++++++ src/screens/Home.js | 1 + src/screens/NewGame.js | 53 +++++++++++++++++++++++++++++++------- 6 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 src/screens/Game.js diff --git a/src/App.js b/src/App.js index 4294c59..7f232a0 100644 --- a/src/App.js +++ b/src/App.js @@ -1,6 +1,8 @@ import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Home from './screens/Home.js'; import NewGame from './screens/NewGame.js'; +import Connect from './screens/Connect.js'; +import Game from './screens/Game.js'; import './App.css'; function App() { @@ -9,6 +11,8 @@ function App() { } /> } /> + } /> + } /> ); diff --git a/src/components/Field.js b/src/components/Field.js index 71cc760..d468850 100644 --- a/src/components/Field.js +++ b/src/components/Field.js @@ -1,4 +1,5 @@ import { Component } from "react"; +import request from "../scripts/requests"; import '../styles/styles.css' class Cell extends Component { @@ -8,7 +9,14 @@ class Cell extends Component { this.state = { h: props.h, v: props.v, - color: "white", + shotState: props.shotState + } + if (this.state.shotState === "x") { + this.state.color = "red"; + } else if (this.state.shotState === ".") { + this.state.color = "grey"; + } else { + this.state.color = "white"; } this.symb = " "; } @@ -27,7 +35,17 @@ class Cell extends Component { } shoot() { - + + request("shoot", { + game_id: localStorage.getItem("gameId"), + token: localStorage.getItem("token"), + h: this.props.h, + v: this.props.v + }, (response) => { + this.setState({ + color: response.shoot ? "red" : "grey" + }) + }) } click() { @@ -67,6 +85,10 @@ class Field extends Component { this.line = first + symb + last; } + this.state = { + cells: this.props.cells + } + } render() { @@ -75,7 +97,7 @@ class Field extends Component { for (let i = 0; i < 10; i++) { let line = [] for (let j = 0; j < 10; j++) { - const cell = ; + const cell = ; line.push({cell}); } cells.push({line}); diff --git a/src/screens/Connect.js b/src/screens/Connect.js index 365e8c0..a0e8ac1 100644 --- a/src/screens/Connect.js +++ b/src/screens/Connect.js @@ -1,5 +1,36 @@ import {Component} from 'react'; +import request from '../scripts/requests'; class Connect extends Component { -} \ No newline at end of file + constructor(props) { + super(props); + this.queryParams = new URLSearchParams(window.location.search); + const game_id = this.queryParams.get('gameId'); + localStorage.setItem('gameId', game_id); + } + + getToken() { + return this.queryParams.get('token'); + } + + getGameId() { + return localStorage.getItem('gameId'); + } + + componentDidMount() { + request("attend_game", { + game_id: this.getGameId(), + attend_token: this.getToken() + }, (response) => { + localStorage.setItem("token", response.token); + window.location.href = "/new_game?gameId=" + this.getGameId() + }) + } + + render() { + return
+ } +} + +export default Connect; \ No newline at end of file diff --git a/src/screens/Game.js b/src/screens/Game.js new file mode 100644 index 0000000..eea3293 --- /dev/null +++ b/src/screens/Game.js @@ -0,0 +1,56 @@ +import { Component } from "react"; +import Field from '../components/Field.js'; +import request from "../scripts/requests.js"; + +class Game extends Component { + + constructor(props) { + super(props); + this.state = { + myField: null, + opponentField: null, + myTurn: false + } + } + + doUpdate = () => { + + const process = (response) => { + console.log(response); + this.setState({ + myField: response.my_field, + opponentField: response.opponent_field, + myTurn: response.my_turn + }) + } + request("get_fields", { + game_id: localStorage.getItem("gameId"), + token: localStorage.getItem("token") + }, process) + } + + componentDidMount() { + this.timer = setInterval(() => this.doUpdate(), 2000); + } + + render() { + return ( +
+

Let's play!

+
+
+

Player1

+ +
+
+
+

Player2

+ +
+
+
+ ) + } +} + +export default Game; \ No newline at end of file diff --git a/src/screens/Home.js b/src/screens/Home.js index 05eb805..e554d68 100644 --- a/src/screens/Home.js +++ b/src/screens/Home.js @@ -7,6 +7,7 @@ class Home extends Component { goNewGame() { function go(response) { localStorage.setItem('token', response.my_token); + localStorage.setItem('gameId', response.game_id); window.location.href='/new_game?gameId=' + response.game_id.toString() + "&playerToken=" + response.player_token; } request("new_game", {}, go); diff --git a/src/screens/NewGame.js b/src/screens/NewGame.js index 06f4424..4c6c1d6 100644 --- a/src/screens/NewGame.js +++ b/src/screens/NewGame.js @@ -5,6 +5,19 @@ import Field from '../components/Field.js'; import Ready from '../components/Ready.js'; import request, {host} from '../scripts/requests.js'; +class AttendLink extends Component { + render() { + if (this.props.token !== null) + return ( +
+

Ссылка для подключения

+ this.setState({ - readyText: "Ждем ответа соперника", - readyDisabled: true - })) + }, (response) => { + this.setState({ + readyText: "Ждем ответа соперника", + readyDisabled: true + }) + }); + this.timer = setInterval(() => this.doUpdate(this), 2000); } render() { @@ -74,10 +112,7 @@ class NewGame extends Component { -
-

Ссылка для подключения

-