2016-10-01 21:49:15 +02:00
|
|
|
import React from 'react';
|
2016-10-25 11:19:50 +02:00
|
|
|
import Grid from './grid/grid';
|
2016-10-01 21:49:15 +02:00
|
|
|
import GridControl from './grid/grid-control';
|
|
|
|
|
|
|
|
|
|
class MineSeeker extends React.Component {
|
2016-10-25 11:19:50 +02:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
var gameAssoc = props.gameId !== '' ? props.gameId : this.makeGameAssoc(50);
|
|
|
|
|
var channel = "acme/channel/" + gameAssoc;
|
2016-10-31 18:07:37 +01:00
|
|
|
var userName = props.userName;
|
2016-10-25 11:19:50 +02:00
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
gameInherited: props.gameId !== '',
|
|
|
|
|
gameAssoc: gameAssoc,
|
|
|
|
|
channel: channel,
|
2016-10-31 18:07:37 +01:00
|
|
|
userName: userName,
|
2016-10-28 16:16:27 +02:00
|
|
|
session: null,
|
2016-10-25 11:19:50 +02:00
|
|
|
createGrid: false,
|
2016-10-28 16:16:27 +02:00
|
|
|
stepCache: null
|
2016-10-25 11:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeGameAssoc(len) {
|
|
|
|
|
var text = "";
|
|
|
|
|
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
|
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 15:58:53 +02:00
|
|
|
/** after rendering */
|
|
|
|
|
componentDidMount() {
|
2016-10-28 16:16:27 +02:00
|
|
|
/** Create Websocket w/ Bahnhof.js */
|
|
|
|
|
var websocket = WS.connect("ws://mine.dev:6450");
|
2016-10-25 11:19:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Connect
|
|
|
|
|
* Session is an Autobahn JS WAMP session.
|
|
|
|
|
*/
|
2016-10-28 16:16:27 +02:00
|
|
|
websocket.on("socket/connect", (session) => {
|
|
|
|
|
console.info("Successfully connected to the Server!");
|
|
|
|
|
|
|
|
|
|
if (!this.state.gameInherited) {
|
|
|
|
|
var gridClient = new Grid().state.grid;
|
|
|
|
|
}
|
2016-10-25 11:19:50 +02:00
|
|
|
|
|
|
|
|
/**
|
2016-10-28 16:16:27 +02:00
|
|
|
* Connect - RPC
|
2016-10-25 11:19:50 +02:00
|
|
|
* Send grid information to the server
|
|
|
|
|
*/
|
|
|
|
|
session
|
|
|
|
|
.call(
|
|
|
|
|
this.state.gameInherited ? "sample/connectGame" : "sample/startGame",
|
2016-10-31 19:44:20 +01:00
|
|
|
this.state.gameInherited ? this.state.gameAssoc : [Base64.encode(JSON.stringify(gridClient)), this.state.gameAssoc]
|
2016-10-25 11:19:50 +02:00
|
|
|
)
|
|
|
|
|
.then(
|
2016-10-28 16:16:27 +02:00
|
|
|
(gridServer) => {
|
|
|
|
|
console.info("Grid has been created! Return w/ gameAssoc.");
|
|
|
|
|
|
|
|
|
|
this.state.session = session;
|
|
|
|
|
|
2016-10-31 15:35:29 +01:00
|
|
|
/** save session to GridControl */
|
2016-10-28 16:16:27 +02:00
|
|
|
this.refs.gridControl.setState({
|
2016-10-31 19:44:20 +01:00
|
|
|
grid: this.state.gameInherited ? JSON.parse(Base64.decode(gridServer)) : gridClient,
|
2016-10-28 16:16:27 +02:00
|
|
|
session: this.state.session,
|
|
|
|
|
channel: this.state.channel
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-31 18:07:37 +01:00
|
|
|
/** setup the web player && save player name */
|
2016-10-28 16:16:27 +02:00
|
|
|
if (this.refs.gridControl.state.webPlayer === null) {
|
|
|
|
|
if (this.state.gameInherited) {
|
|
|
|
|
this.refs.gridControl.state.webPlayer = 'blue';
|
2016-10-31 18:07:37 +01:00
|
|
|
this.refs.gridControl.refs.userControl.refs.blue.setState({name: this.state.userName});
|
2016-10-28 16:16:27 +02:00
|
|
|
} else {
|
|
|
|
|
this.refs.gridControl.state.webPlayer = 'red';
|
2016-10-31 18:07:37 +01:00
|
|
|
this.refs.gridControl.refs.userControl.refs.red.setState({name: this.state.userName});
|
2016-10-28 16:16:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-25 11:19:50 +02:00
|
|
|
|
2016-10-31 15:35:29 +01:00
|
|
|
/** Connect - Subscribe */
|
2016-10-28 16:16:27 +02:00
|
|
|
this.state.session.subscribe(
|
2016-10-25 11:19:50 +02:00
|
|
|
this.state.channel,
|
|
|
|
|
(uri, payload, log) => {
|
|
|
|
|
|
2016-10-28 16:16:27 +02:00
|
|
|
if (typeof payload.data !== 'undefined') {
|
|
|
|
|
console.warn(payload.user + " has been stepped to coords: " + payload.data.coords[0] + ', ' + payload.data.coords[1]);
|
|
|
|
|
|
|
|
|
|
/** Auto-Step if this player is not the current user */
|
|
|
|
|
if (this.refs.gridControl.state.webPlayer !== payload.data.player) {
|
|
|
|
|
console.warn('Opponent stepped: Auto-Step process');
|
2016-10-31 18:07:37 +01:00
|
|
|
|
2016-10-31 15:35:29 +01:00
|
|
|
this.refs.gridControl.refs.userControl.state.bombSelected = payload.data.bomb;
|
2016-10-28 16:16:27 +02:00
|
|
|
this.refs.gridControl.stepEvent(payload.data.coords);
|
|
|
|
|
}
|
2016-10-25 11:19:50 +02:00
|
|
|
} else {
|
2016-10-31 18:07:37 +01:00
|
|
|
console.info(
|
|
|
|
|
(typeof payload.user !== 'undefined' ? payload.user : 'user') + " has been subscribed to the channel!"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// /** Save the opponent's player name */
|
|
|
|
|
// if (this.state.gameInherited) {
|
|
|
|
|
// this.refs.gridControl.refs.userControl.refs.blue.setState({name: payload.user});
|
|
|
|
|
// }
|
2016-10-25 11:19:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
},
|
2016-10-28 16:16:27 +02:00
|
|
|
(error, desc) => console.log(["RPC Error", error, desc])
|
2016-10-25 11:19:50 +02:00
|
|
|
);
|
2016-10-18 15:58:53 +02:00
|
|
|
});
|
|
|
|
|
|
2016-10-25 11:19:50 +02:00
|
|
|
/**
|
2016-10-28 16:16:27 +02:00
|
|
|
* DisConnect
|
2016-10-25 11:19:50 +02:00
|
|
|
* Error provides us with some insight into the disconnection: error.reason and error.code
|
|
|
|
|
*/
|
2016-10-28 16:16:27 +02:00
|
|
|
websocket.on("socket/disconnect", (error) => console.log("Disconnected for " + error.reason + " with code " + error.code));
|
2016-10-18 15:58:53 +02:00
|
|
|
}
|
2016-10-01 21:49:15 +02:00
|
|
|
|
2016-10-28 16:16:27 +02:00
|
|
|
onClick(coords) {
|
|
|
|
|
var activePlayer = this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red';
|
|
|
|
|
|
2016-10-31 15:13:01 +01:00
|
|
|
/** Player step and it is the current player */
|
2016-10-28 16:16:27 +02:00
|
|
|
if (activePlayer === this.refs.gridControl.state.webPlayer) {
|
|
|
|
|
this.refs.gridControl.stepEvent(coords);
|
|
|
|
|
|
|
|
|
|
this.state.session
|
|
|
|
|
.publish(this.state.channel, {
|
|
|
|
|
'coords': coords,
|
2016-10-31 15:35:29 +01:00
|
|
|
'player': activePlayer,
|
|
|
|
|
'bomb': this.refs.gridControl.refs.userControl.state.bombSelected
|
2016-10-28 16:16:27 +02:00
|
|
|
});
|
|
|
|
|
}
|
2016-10-25 11:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** FOR DEVELOPMENT */
|
|
|
|
|
createLink() {
|
|
|
|
|
return this.state.gameAssoc
|
|
|
|
|
? <a href={"/play/" + this.state.gameAssoc} target="_blank">Play w/ me!</a>
|
|
|
|
|
: '';
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-01 21:49:15 +02:00
|
|
|
render() {
|
|
|
|
|
return (
|
2016-10-25 11:19:50 +02:00
|
|
|
<div>
|
|
|
|
|
<div>
|
|
|
|
|
{this.createLink()}
|
|
|
|
|
</div>
|
2016-10-28 16:16:27 +02:00
|
|
|
<GridControl ref="gridControl"
|
|
|
|
|
env={this.props.env === 'dev'}
|
|
|
|
|
onClick={this.onClick.bind(this)}/>
|
2016-10-25 11:19:50 +02:00
|
|
|
</div>
|
2016-10-01 21:49:15 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MineSeeker;
|