Private
Public Access
1
0
Files
MineSeeker/src/Mine/SeekerBundle/Resources/public/js/mine-seeker/app.js

254 lines
11 KiB
JavaScript
Raw Normal View History

import React from 'react';
2016-10-25 11:19:50 +02:00
import Grid from './grid/grid';
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 = "mineseeker/channel/" + gameAssoc;
2016-10-25 11:19:50 +02:00
this.state = {
gameInherited: props.gameId !== '',
gameAssoc: gameAssoc,
channel: channel,
session: null,
2016-10-25 11:19:50 +02:00
createGrid: false,
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-11-20 11:53:41 +01:00
/** STEP */
makePointsCalcAndStep(coords) {
let users = this.refs.gridControl.refs.userControl,
activePlayer = users.state.activePlayer ? 'blue' : 'red',
inactivePlayer = users.state.activePlayer ? 'red' : 'blue',
redPoints = activePlayer === 'red'
? users.refs[activePlayer].state.mines
: users.refs[inactivePlayer].state.mines,
bluePoints = activePlayer === 'blue'
? users.refs[activePlayer].state.mines
: users.refs[inactivePlayer].state.mines;
this.refs.gridControl.stepEvent(coords);
let mineCache = this.refs.gridControl.state.foundUserMineCache;
redPoints += activePlayer === 'red' ? mineCache : 0;
bluePoints += activePlayer === 'blue' ? mineCache : 0;
return {red: bluePoints, blue: redPoints};
}
/** THE END */
2016-11-18 19:28:52 +01:00
makeGameEndIfItEnds(bluePoints, redPoints) {
2016-11-18 20:03:54 +01:00
var redWins = redPoints > 2,
blueWins = bluePoints > 2;
2016-11-18 19:28:52 +01:00
if (redWins || blueWins) {
this.refs.gridControl.state.sound.won.play();
2016-11-18 19:28:52 +01:00
this.refs.gridControl.setState({
overlay: true,
overlayTitle: (redWins ? 'Red' : 'Blue') + " wins the game!",
overlaySubTitle: "Play again!"
});
this.refs.gridControl.showLeftMines();
2016-11-19 15:45:52 +01:00
this.refs.gridControl.refs.userControl.setState({activePlayer: false});
2016-11-20 11:06:25 +01:00
this.refs.gridControl.refs.userControl.refs.red.setState({desc: ""});
this.refs.gridControl.refs.userControl.refs.blue.setState({desc: ""});
2016-11-18 19:28:52 +01:00
}
}
/** after rendering */
componentDidMount() {
/** 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.
*/
websocket.on("socket/connect", (session) => {
console.info("Successfully connected to the Server!");
var gridClient = this.state.gameInherited || new Grid().state.grid;
2016-10-25 11:19:50 +02:00
/**
* Connect - RPC
2016-10-25 11:19:50 +02:00
* Send grid information to the server
*/
session
.call(
this.state.gameInherited ? "mineseeker-rpc/connectGame" : "mineseeker-rpc/startGame",
this.state.gameInherited ? this.state.gameAssoc : [Base64.encode(JSON.stringify(gridClient)), this.state.gameAssoc]
2016-10-25 11:19:50 +02:00
)
.then(
(gridServer) => {
console.info("Grid has been created! Return w/ gameAssoc.");
2016-11-18 20:42:29 +01:00
this.setState({session: session});
2016-10-31 15:35:29 +01:00
/** save session to GridControl */
2016-11-18 20:03:54 +01:00
/** render grid fields - #12 */
this.refs.gridControl.setState({
grid: this.state.gameInherited ? JSON.parse(Base64.decode(gridServer)) : gridClient,
channel: this.state.channel,
2016-11-20 11:06:25 +01:00
desc: {
buddy: <div>
Your buddy is <br/>
making a <br/>
move.
</div>,
you: <div>
It is your turn! <br/>
Make a move.
</div>
},
overlay: true,
overlayTitle: "We are waiting for your opponent...",
overlaySubTitle: this.state.gameAssoc
2016-11-19 16:37:08 +01:00
?
<div>
<div className="clippy">
<input id="foo"
defaultValue={window.location.href + '/' + this.state.gameAssoc}/>
<button className="btn">
<img src="/bundles/mineseeker/images/clippy.svg" alt="Copy to clipboard"/>
</button>
</div>
<a href={"/play/" + this.state.gameAssoc} target="_blank">Play w/ me!</a>
</div>
2016-11-18 20:03:54 +01:00
: '',
renderGridFields: this.state.gameAssoc
});
2016-10-31 15:35:29 +01:00
/** Connect - Subscribe */
this.state.session.subscribe(
2016-10-25 11:19:50 +02:00
this.state.channel,
(uri, payload, log) => {
2016-11-02 18:05:10 +01:00
var isTopicEvent = typeof payload.data !== 'undefined',
isNotUnsubscribe = typeof payload.msg === 'undefined';
2016-11-02 18:05:10 +01:00
if (isTopicEvent) {
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-11-18 20:42:29 +01:00
this.refs.gridControl.refs.userControl.setState({bombSelected: payload.data.bomb});
2016-11-20 11:53:41 +01:00
/** STEP */
let points = this.makePointsCalcAndStep(payload.data.coords);
2016-11-18 20:03:54 +01:00
/** THE END */
2016-11-20 11:53:41 +01:00
this.makeGameEndIfItEnds(points.blue, points.red);
}
2016-10-25 11:19:50 +02:00
} else {
2016-11-01 10:52:39 +01:00
/** It is subscribe or unsubscribe */
if (isNotUnsubscribe) {
2016-11-01 10:52:39 +01:00
console.info(
(typeof payload.user !== 'undefined' ? payload.user : 'user') + " has been subscribed to the channel!"
);
2016-11-19 15:45:52 +01:00
/** 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'
2016-11-01 10:52:39 +01:00
});
2016-11-19 15:45:52 +01:00
/** every user has been came */
if (payload.userCnt === 2) {
/** 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({
2016-11-20 11:06:25 +01:00
name: payload.users.red !== '' ? payload.users.red : payload.users.redAnon,
2016-11-19 15:45:52 +01:00
});
this.refs.gridControl.refs.userControl.refs.blue.setState({
2016-11-20 11:06:25 +01:00
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,
2016-11-19 15:45:52 +01:00
});
this.refs.gridControl.setState({overlay: false});
}
2016-11-01 10:52:39 +01:00
} else {
console.info(payload.msg);
this.refs.gridControl.setState({
overlay: true,
overlayTitle: "The connection has been lost...",
overlaySubTitle: "Please, restart the game!"
});
2016-11-01 10:52:39 +01:00
}
2016-10-25 11:19:50 +02:00
}
}
);
},
(error, desc) => console.error(["RPC Error", error, desc])
2016-10-25 11:19:50 +02:00
);
});
2016-10-25 11:19:50 +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
*/
websocket.on("socket/disconnect", (error) => console.error("Disconnected for " + error.reason + " with code " + error.code));
}
onClick(coords) {
var activePlayer = this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red';
/** if the clicked field is NEVER CLICKED */
if (this.refs.gridControl.checkFieldHasBeenNeverClicked(coords[0], coords[1])) {
/** Player step and it is the current player */
if (activePlayer === this.refs.gridControl.state.webPlayer) {
2016-11-20 11:53:41 +01:00
/** STEP */
let points = this.makePointsCalcAndStep(coords);
/** THE END */
2016-11-20 11:53:41 +01:00
this.makeGameEndIfItEnds(points.blue, points.red);
this.state.session
.publish(this.state.channel, {
'coords': coords,
'player': activePlayer,
'bomb': this.refs.gridControl.refs.userControl.state.bombSelected,
2016-11-20 11:53:41 +01:00
'redPoints': points.red,
'bluePoints': points.blue
});
}
}
2016-10-25 11:19:50 +02:00
}
render() {
return (
2016-11-14 20:04:24 +01:00
<GridControl ref="gridControl"
env={this.props.env === 'dev'}
onClick={this.onClick.bind(this)}/>
);
}
}
export default MineSeeker;