dockerfile

This commit is contained in:
Administrator 2022-08-25 12:26:55 +03:00
parent b3448ec4c4
commit 8b48cddffe
4 changed files with 63 additions and 30 deletions

View File

@ -10,13 +10,19 @@ class Cell extends Component {
v: props.v, v: props.v,
color: "white", color: "white",
} }
this.symb = " ";
} }
changeColor() { changeColor() {
if (this.state.color === "white") if (this.state.color === "white") {
this.setState({color: "green"}); this.setState({color: "green"});
else this.symb = "o"
}
else {
this.setState({color: "white"}); this.setState({color: "white"});
this.symb = " "
}
this.props.parent.insert(this.symb, this.props.pos);
this.props.parent.props.parent.update(); this.props.parent.props.parent.update();
} }
@ -33,9 +39,9 @@ class Cell extends Component {
} }
render() { render() {
this.props.parent.cells.push(this); if (!this.props.parent.cells.includes(this))
this.props.parent.cells.push(this);
this.props.parent.cells.sort((a, b) => a.key < b.key); this.props.parent.cells.sort((a, b) => a.key < b.key);
console.log(this.props.parent.cells.length);
return ( return (
<button style={{backgroundColor: this.state.color}} onClick={() => this.click()} className="cell"></button> <button style={{backgroundColor: this.state.color}} onClick={() => this.click()} className="cell"></button>
) )
@ -49,12 +55,16 @@ class Field extends Component {
props.parent.field = this; props.parent.field = this;
this.line = " ";
this.getLine = () => { this.getLine = () => {
let line = ""; return this.line;
for (let i = 0; i < 100; i++) { }
line += this.cells.at(i).state.color === "white" ? " " : "o";
} this.insert = (symb, pos) => {
return line; const first = this.line.slice(0, pos);
const last = this.line.slice(pos + 1);
this.line = first + symb + last;
} }
} }
@ -65,7 +75,7 @@ class Field extends Component {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
let line = [] let line = []
for (let j = 0; j < 10; j++) { for (let j = 0; j < 10; j++) {
const cell = <Cell parent={this} changeable={this.props.changeable} key={(i * 10 + j)} h={i} v={j} />; const cell = <Cell parent={this} changeable={this.props.changeable} key={(i * 10 + j)} pos={i * 10 + j} h={i} v={j} />;
line.push(<td key={(i * 10 + j) * 100}>{cell}</td>); line.push(<td key={(i * 10 + j) * 100}>{cell}</td>);
} }
cells.push(<tr key={(i * 100000)}>{line}</tr>); cells.push(<tr key={(i * 100000)}>{line}</tr>);

View File

@ -1,32 +1,49 @@
import { Component } from "react"; import { Component } from "react";
import request from "../scripts/requests";
export default class Ready extends Component { class Ready extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.update();
this.state = { this.state = {
text: "Расставь корабли чтобы активировать", text: "Расставь корабли чтобы активировать",
disabled: true disabled: true,
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() {
if (!this.props.ok) if (!this.props.ok) {
this.setState({ this.setState({
text: "Расставь корабли чтобы активировать", text: "Расставь корабли чтобы активировать",
disabled: true disabled: true
}) })
else }
else {
this.setState({ this.setState({
text: "Я готов", text: "Я готов",
disabled: false disabled: false
}) })
this.props.parent.ready = this; }
} }
render() { render() {
return <button className='main' disabled={this.state.disabled}><p>{this.state.text}</p></button> return <button onClick={this.click} className='main' disabled={this.state.disabled}><p>{this.state.text}</p></button>
} }
} }
export default Ready;

View File

@ -8,8 +8,11 @@ import request, {host} from '../scripts/requests.js';
class NewGame extends Component { class NewGame extends Component {
getToken() { getToken() {
const queryParams = new URLSearchParams(window.location.search); return this.queryParams.get('playerToken');
return queryParams.get('playerToken'); }
getGameId() {
return this.queryParams.get('gameId');
} }
constructor(props) { constructor(props) {
@ -17,19 +20,29 @@ class NewGame extends Component {
this.state = { this.state = {
ok: false ok: false
} }
this.queryParams = new URLSearchParams(window.location.search);
} }
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}); this.setState({ok: response.correct});
if (response.correct) {
this.ready.setState({
text: "Я готов",
disabled: false
})
} else {
this.ready.setState({
text: "Расставь корабли чтобы активировать",
disabled: true
})
}
this.ready.setState({ok: this.state.ok}); this.ready.setState({ok: this.state.ok});
this.ready.update();
}); });
} }
render() { render() {
return ( return (
<div> <div>
<center><p className='logo'>Расставь корабли</p></center> <center><p className='logo'>Расставь корабли</p></center>

View File

@ -1,11 +1,4 @@
var h = null; export const host = "http://127.0.0.1:8000/"
if (process.env.HOST_URL !== undefined) {
h = process.env.HOST_URL;
} else {
h = "http://127.0.0.1:8000/";
}
export const host = h;
function request(method, body, callback) { function request(method, body, callback) {