new
This commit is contained in:
parent
d2bb025723
commit
628c2b932e
@ -9,7 +9,8 @@ class Cell extends Component {
|
|||||||
this.state = {
|
this.state = {
|
||||||
h: props.h,
|
h: props.h,
|
||||||
v: props.v,
|
v: props.v,
|
||||||
shotState: props.shotState
|
shotState: props.shotState,
|
||||||
|
changeable: props.changeable
|
||||||
}
|
}
|
||||||
if (this.state.shotState === "x") {
|
if (this.state.shotState === "x") {
|
||||||
this.state.color = "red";
|
this.state.color = "red";
|
||||||
@ -18,7 +19,13 @@ class Cell extends Component {
|
|||||||
} else {
|
} else {
|
||||||
this.state.color = "white";
|
this.state.color = "white";
|
||||||
}
|
}
|
||||||
this.symb = " ";
|
}
|
||||||
|
|
||||||
|
componentWillReceiveProps(props) {
|
||||||
|
this.setState({
|
||||||
|
shotState: props.shotState,
|
||||||
|
changeable: props.changeable
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
changeColor() {
|
changeColor() {
|
||||||
@ -35,21 +42,30 @@ class Cell extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shoot() {
|
shoot() {
|
||||||
|
this.props.parent.setState({
|
||||||
|
changeable: false
|
||||||
|
})
|
||||||
request("shoot", {
|
request("shoot", {
|
||||||
game_id: localStorage.getItem("gameId"),
|
game_id: localStorage.getItem("gameId"),
|
||||||
token: localStorage.getItem("token"),
|
token: localStorage.getItem("token"),
|
||||||
h: this.props.h,
|
h: this.props.h,
|
||||||
v: this.props.v
|
v: this.props.v
|
||||||
}, (response) => {
|
}, (response) => {
|
||||||
this.setState({
|
if (response.shot) {
|
||||||
color: response.shoot ? "red" : "grey"
|
this.setState({
|
||||||
})
|
shotState: "x",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
this.setState({
|
||||||
|
shotState: "."
|
||||||
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
click() {
|
click() {
|
||||||
if (!this.props.changeable) return;
|
if (!this.state.changeable) return;
|
||||||
|
if (this.state.shotState !== " ") return;
|
||||||
if (this.props.parent.props.ready)
|
if (this.props.parent.props.ready)
|
||||||
this.shoot();
|
this.shoot();
|
||||||
else
|
else
|
||||||
@ -57,11 +73,16 @@ class Cell extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
if (!this.props.parent.cells.includes(this))
|
let color = "white";
|
||||||
this.props.parent.cells.push(this);
|
if (this.state.shotState === "x") {
|
||||||
this.props.parent.cells.sort((a, b) => a.key < b.key);
|
color = "red";
|
||||||
|
} else if (this.state.shotState === "o") {
|
||||||
|
color = "darkcyan";
|
||||||
|
} else if (this.state.shotState === ".") {
|
||||||
|
color = "gray";
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<button style={{backgroundColor: this.state.color}} onClick={() => this.click()} className="cell"></button>
|
<button style={{backgroundColor: color}} onClick={() => this.click()} className="cell"></button>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,18 +107,28 @@ class Field extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
cells: this.props.cells
|
cells: this.props.cells,
|
||||||
|
changeable: props.changeable
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentWillReceiveProps(props) {
|
||||||
|
this.setState({
|
||||||
|
changeable: props.changeable
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
this.cells = [];
|
|
||||||
let cells = [];
|
let cells = [];
|
||||||
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 shotState={this.state.cells !== undefined && this.state.cells !== null ? this.state.cells[i][j] : null} parent={this} changeable={this.props.changeable} key={(i * 10 + j)} pos={i * 10 + j} h={i} v={j} ready={this.props.ready} />;
|
let shotState = null;
|
||||||
|
if (this.props.cells !== undefined && this.props.cells !== null) {
|
||||||
|
shotState = this.props.cells[i][j]
|
||||||
|
}
|
||||||
|
const cell = <Cell shotState={shotState} parent={this} changeable={this.state.changeable} key={(i * 10 + j)} pos={i * 10 + j} h={i} v={j} ready={this.props.ready} />;
|
||||||
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>);
|
||||||
|
@ -16,7 +16,6 @@ class Game extends Component {
|
|||||||
doUpdate = () => {
|
doUpdate = () => {
|
||||||
|
|
||||||
const process = (response) => {
|
const process = (response) => {
|
||||||
console.log(response);
|
|
||||||
this.setState({
|
this.setState({
|
||||||
myField: response.my_field,
|
myField: response.my_field,
|
||||||
opponentField: response.opponent_field,
|
opponentField: response.opponent_field,
|
||||||
|
@ -62,7 +62,6 @@ class NewGame extends Component {
|
|||||||
game_id: game_id,
|
game_id: game_id,
|
||||||
token: localStorage.getItem("token")
|
token: localStorage.getItem("token")
|
||||||
}, function(response) {
|
}, function(response) {
|
||||||
console.log(response);
|
|
||||||
obj.setState({
|
obj.setState({
|
||||||
readyText: response.attend ? "Соперник ставит корабли" : "Ждем ответа соперника",
|
readyText: response.attend ? "Соперник ставит корабли" : "Ждем ответа соперника",
|
||||||
readyDisabled: true
|
readyDisabled: true
|
||||||
|
Loading…
Reference in New Issue
Block a user