This commit is contained in:
Administrator 2022-08-25 20:23:46 +03:00
parent 9983f7b212
commit 3d71f5dee6
3 changed files with 23 additions and 26 deletions

View File

@ -1,5 +1,4 @@
import { Component } from "react"; import { Component } from "react";
import request from "../scripts/requests";
class Ready extends Component { class Ready extends Component {
@ -10,19 +9,6 @@ class Ready extends Component {
disabled: true, disabled: true,
parent: props.parent parent: props.parent
} }
this.state.parent.ready = this;
this.click = this.click.bind(this);
}
click() {
request("place_ships", {
game_id: this.state.parent.getGameId(),
token: localStorage.getItem('token'),
field: this.state.parent.field.getLine()
}, () => this.setState({
text: "Ждем ответа соперника",
disabled: true
}))
} }
update() { update() {
@ -41,7 +27,7 @@ class Ready extends Component {
} }
render() { render() {
return <button onClick={this.click} className='main' disabled={this.state.disabled}><p>{this.state.text}</p></button> return <button onClick={this.props.onClick} className='main' disabled={this.props.disabled}><p>{this.props.text}</p></button>
} }
} }

View File

@ -18,7 +18,9 @@ class NewGame extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
ok: false ok: false,
readyText: "Заполни поле чтобы начать",
readyDisabled: true
} }
this.queryParams = new URLSearchParams(window.location.search); this.queryParams = new URLSearchParams(window.location.search);
} }
@ -26,22 +28,31 @@ class NewGame extends Component {
update() { update() {
const line = this.field.getLine(); const line = this.field.getLine();
request("check_field_correct", {field: line}, (response) => { request("check_field_correct", {field: line}, (response) => {
this.setState({ok: response.correct});
if (response.correct) { if (response.correct) {
this.ready.setState({ this.setState({
text: "Я готов", readyText: "Я готов",
disabled: false readyDisabled: false
}) })
} else { } else {
this.ready.setState({ this.setState({
text: "Расставь корабли чтобы активировать", readyText: "Заполни поле чтобы начать",
disabled: true readyDisabled: true
}) })
} }
this.ready.setState({ok: this.state.ok});
}); });
} }
clickReady() {
request("place_ships", {
game_id: this.getGameId(),
token: localStorage.getItem('token'),
field: this.field.getLine()
}, (response) => this.setState({
readyText: "Ждем ответа соперника",
readyDisabled: true
}))
}
render() { render() {
return ( return (
<div> <div>
@ -67,7 +78,7 @@ class NewGame extends Component {
<p>Ссылка для подключения</p> <p>Ссылка для подключения</p>
<input className='borders' value={host + "connect?token=" + this.getToken()} /> <input className='borders' value={host + "connect?token=" + this.getToken()} />
</center> </center>
<Ready ok={this.state.ok} parent={this} /> <Ready onClick={() => this.clickReady()} text={this.state.readyText} disabled={this.state.readyDisabled} />
</div> </div>
); );
} }

View File

@ -1,4 +1,4 @@
export const host = "http://battleship.develop.sprinthub.ru/" export const host = "http://127.0.0.1:8000/"
function request(method, body, callback) { function request(method, body, callback) {