|
|
|
|
@@ -11,13 +11,15 @@ class MineSeeker extends React.Component {
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
env: props.env,
|
|
|
|
|
ssl: props.ssl,
|
|
|
|
|
gameInherited: props.gameId !== '',
|
|
|
|
|
gameAssoc: gameAssoc,
|
|
|
|
|
channel: channel,
|
|
|
|
|
session: null,
|
|
|
|
|
createGrid: false,
|
|
|
|
|
stepCache: [],
|
|
|
|
|
disconnect: false
|
|
|
|
|
connectionLost: false,
|
|
|
|
|
end: false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -30,7 +32,12 @@ class MineSeeker extends React.Component {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** STEP */
|
|
|
|
|
/**
|
|
|
|
|
* STEP
|
|
|
|
|
*
|
|
|
|
|
* @param coords
|
|
|
|
|
* @returns {{red: *, blue: *}}
|
|
|
|
|
*/
|
|
|
|
|
makePointsCalcAndStep(coords) {
|
|
|
|
|
let users = this.refs.gridControl.refs.userControl,
|
|
|
|
|
activePlayer = users.state.activePlayer ? 'blue' : 'red',
|
|
|
|
|
@@ -51,7 +58,38 @@ class MineSeeker extends React.Component {
|
|
|
|
|
return {red: redPoints, blue: bluePoints};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** THE END */
|
|
|
|
|
/**
|
|
|
|
|
* START
|
|
|
|
|
*
|
|
|
|
|
* @param payload
|
|
|
|
|
*/
|
|
|
|
|
makeGameStart(payload) {
|
|
|
|
|
/** every time the blue starts */
|
|
|
|
|
this.refs.gridControl.refs.userControl.setState({activePlayer: 1});
|
|
|
|
|
|
|
|
|
|
/** Set up player names w/ server data */
|
|
|
|
|
this.refs.gridControl.refs.userControl.refs.red.setState({
|
|
|
|
|
name: payload.users.red !== '' ? payload.users.red : payload.users.redAnon,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.refs.gridControl.refs.userControl.refs.blue.setState({
|
|
|
|
|
name: payload.users.blue !== '' ? payload.users.blue : payload.users.blueAnon,
|
|
|
|
|
desc: this.refs.gridControl.state.webPlayer === 'blue'
|
|
|
|
|
? this.refs.gridControl.state.desc.you
|
|
|
|
|
: this.refs.gridControl.state.desc.buddy,
|
|
|
|
|
active: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.refs.gridControl.setState({overlay: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* THE END
|
|
|
|
|
*
|
|
|
|
|
* @param bluePoints
|
|
|
|
|
* @param redPoints
|
|
|
|
|
* @param resign
|
|
|
|
|
*/
|
|
|
|
|
makeGameEndIfItEnds(bluePoints, redPoints, resign = false) {
|
|
|
|
|
let redWins = redPoints > 25,
|
|
|
|
|
blueWins = bluePoints > 25;
|
|
|
|
|
@@ -86,10 +124,13 @@ class MineSeeker extends React.Component {
|
|
|
|
|
: "You WIN!"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.setState({end: true});
|
|
|
|
|
|
|
|
|
|
this.makeGameEndIfItEnds(0, 0, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clickResign() {
|
|
|
|
|
/** PUBLISH */
|
|
|
|
|
this.state.session.publish(this.state.channel, {
|
|
|
|
|
'resign': this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red'
|
|
|
|
|
});
|
|
|
|
|
@@ -119,13 +160,13 @@ class MineSeeker extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wInit(session, gridServer, gridClient) {
|
|
|
|
|
wInit(session, data, gridClient) {
|
|
|
|
|
this.setState({session: session});
|
|
|
|
|
|
|
|
|
|
/** save session to GridControl */
|
|
|
|
|
/** render grid fields - #12 */
|
|
|
|
|
this.refs.gridControl.setState({
|
|
|
|
|
grid: this.state.gameInherited ? JSON.parse(Base64.decode(gridServer)) : gridClient,
|
|
|
|
|
grid: this.state.gameInherited ? JSON.parse(Base64.decode(data))['grid'] : gridClient,
|
|
|
|
|
channel: this.state.channel,
|
|
|
|
|
desc: {
|
|
|
|
|
buddy: <div>
|
|
|
|
|
@@ -157,39 +198,31 @@ class MineSeeker extends React.Component {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wSubscribe(payload) {
|
|
|
|
|
wSubscribe(payload, rpcUsers = null) {
|
|
|
|
|
this.state.env === 'dev' && console.info(
|
|
|
|
|
(typeof payload.user !== 'undefined' ? payload.user : 'user') + " has been subscribed to the channel!"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!this.state.disconnect) {
|
|
|
|
|
/** setup the web player */
|
|
|
|
|
null === this.refs.gridControl.state.webPlayer && this.refs.gridControl.setState({
|
|
|
|
|
webPlayer: payload.user === payload.users.blue || payload.user === payload.users.blueAnon
|
|
|
|
|
? 'blue'
|
|
|
|
|
: 'red'
|
|
|
|
|
});
|
|
|
|
|
let firstUser = !rpcUsers;
|
|
|
|
|
|
|
|
|
|
/** every user has been came */
|
|
|
|
|
if (payload.userCnt === 2) {
|
|
|
|
|
/** every time the blue starts */
|
|
|
|
|
this.refs.gridControl.refs.userControl.setState({activePlayer: 1});
|
|
|
|
|
this.refs.gridControl.state.webPlayer === null && this.refs.gridControl.setState({
|
|
|
|
|
webPlayer: payload.user === payload.users.blue ||
|
|
|
|
|
(
|
|
|
|
|
firstUser && payload.users.blueAnon !== '' ||
|
|
|
|
|
!firstUser && (rpcUsers.blueAnon === '' && rpcUsers.blue === '')
|
|
|
|
|
)
|
|
|
|
|
? 'blue' : 'red'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** Set up player names w/ server data */
|
|
|
|
|
this.refs.gridControl.refs.userControl.refs.red.setState({
|
|
|
|
|
name: payload.users.red !== '' ? payload.users.red : payload.users.redAnon,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.refs.gridControl.refs.userControl.refs.blue.setState({
|
|
|
|
|
name: payload.users.blue !== '' ? payload.users.blue : payload.users.blueAnon,
|
|
|
|
|
desc: this.refs.gridControl.state.webPlayer === 'blue'
|
|
|
|
|
? this.refs.gridControl.state.desc.you
|
|
|
|
|
: this.refs.gridControl.state.desc.buddy,
|
|
|
|
|
active: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.refs.gridControl.setState({overlay: false});
|
|
|
|
|
}
|
|
|
|
|
/** every user has been came */
|
|
|
|
|
if (
|
|
|
|
|
payload.userCnt === 2 &&
|
|
|
|
|
(
|
|
|
|
|
!this.state.connectionLost ||
|
|
|
|
|
this.state.connectionLost && false === this.refs.gridControl.refs.userControl.state.activePlayer && !this.state.end
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
this.makeGameStart(payload);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -226,7 +259,7 @@ class MineSeeker extends React.Component {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Connect - Subscribe */
|
|
|
|
|
subscribe() {
|
|
|
|
|
subscribe(rpcUsers = null) {
|
|
|
|
|
this.state.session.subscribe(
|
|
|
|
|
this.state.channel,
|
|
|
|
|
(uri, payload, log) => {
|
|
|
|
|
@@ -238,30 +271,31 @@ class MineSeeker extends React.Component {
|
|
|
|
|
this.wTopic(payload);
|
|
|
|
|
} else {
|
|
|
|
|
if (isNotUnsubscribe) {
|
|
|
|
|
this.wSubscribe(payload);
|
|
|
|
|
this.wSubscribe(payload, rpcUsers);
|
|
|
|
|
} else {
|
|
|
|
|
this.wUnsubscribe(payload);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** RECONNECTION */
|
|
|
|
|
if (payload.userCnt === 2 && this.state.disconnect) {
|
|
|
|
|
if (payload.userCnt === 2 && this.state.connectionLost) {
|
|
|
|
|
this.state.env === 'dev' && console.info('Reconnection process');
|
|
|
|
|
|
|
|
|
|
/** PUBLISH */
|
|
|
|
|
let cache = this.state.stepCache;
|
|
|
|
|
cache.forEach((item) => this.state.session.publish(this.state.channel, item));
|
|
|
|
|
this.setState({disconnect: false, stepCache: []});
|
|
|
|
|
this.setState({connectionLost: false, stepCache: []});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** after rendering */
|
|
|
|
|
/** After rendering */
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
/** Create Websocket w/ Bahnhof.js */
|
|
|
|
|
let websocket = WS.connect(
|
|
|
|
|
this.state.env === 'dev'
|
|
|
|
|
? "ws://mine.dev:6450"
|
|
|
|
|
: "ws://www.mineseeker.ninja:6450"
|
|
|
|
|
: (this.state.ssl === 'true' ? "wss" : "ws") + "://www.mineseeker.ninja:6450"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -271,7 +305,7 @@ class MineSeeker extends React.Component {
|
|
|
|
|
websocket.on("socket/connect", (session) => {
|
|
|
|
|
this.state.env === 'dev' && console.info("Successfully connected to the Server!");
|
|
|
|
|
|
|
|
|
|
if (!this.state.disconnect) {
|
|
|
|
|
if (!this.state.connectionLost) {
|
|
|
|
|
let gridClient = this.state.gameInherited || new Grid().state.grid;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -284,11 +318,11 @@ class MineSeeker extends React.Component {
|
|
|
|
|
this.state.gameInherited ? this.state.gameAssoc : [Base64.encode(JSON.stringify(gridClient)), this.state.gameAssoc]
|
|
|
|
|
)
|
|
|
|
|
.then(
|
|
|
|
|
(gridServer) => {
|
|
|
|
|
this.state.env === 'dev' && console.info("Grid has been created! Return w/ gameAssoc.");
|
|
|
|
|
(data) => {
|
|
|
|
|
this.state.env === 'dev' && console.info('RPC has been called');
|
|
|
|
|
|
|
|
|
|
this.wInit(session, gridServer, gridClient);
|
|
|
|
|
this.subscribe();
|
|
|
|
|
this.wInit(session, data, gridClient);
|
|
|
|
|
this.subscribe(this.state.gameInherited && JSON.parse(Base64.decode(data))['users']);
|
|
|
|
|
},
|
|
|
|
|
(error, desc) => this.state.env === 'dev' && console.error(["RPC Error", error, desc])
|
|
|
|
|
);
|
|
|
|
|
@@ -304,7 +338,11 @@ class MineSeeker extends React.Component {
|
|
|
|
|
*/
|
|
|
|
|
websocket.on("socket/disconnect", (error) => {
|
|
|
|
|
this.state.env === 'dev' && console.error("Disconnected for " + error.reason + " with code " + error.code);
|
|
|
|
|
this.setState({disconnect: true});
|
|
|
|
|
|
|
|
|
|
error.code === 6 && this.setState({connectionLost: true});
|
|
|
|
|
error.code === 3 && setTimeout(function () {
|
|
|
|
|
this.componentDidMount();
|
|
|
|
|
}.bind(this), 500);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -344,7 +382,7 @@ class MineSeeker extends React.Component {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** PUBLISH */
|
|
|
|
|
!this.state.disconnect
|
|
|
|
|
!this.state.connectionLost
|
|
|
|
|
? this.state.session.publish(this.state.channel, dataPack)
|
|
|
|
|
: this.cachePublish(dataPack);
|
|
|
|
|
}
|
|
|
|
|
|