import React from 'react'; import Grid from './grid/grid'; import GridControl from './grid/grid-control'; class MineSeeker extends React.Component { constructor(props) { super(props); var gameAssoc = props.gameId !== '' ? props.gameId : this.makeGameAssoc(50); var channel = "mineseeker/channel/" + gameAssoc; this.state = { env: props.env, gameInherited: props.gameId !== '', gameAssoc: gameAssoc, channel: channel, session: null, createGrid: false, stepCache: null } } 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; } /** 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: redPoints, blue: bluePoints}; } /** THE END */ makeGameEndIfItEnds(bluePoints, redPoints, resign = false) { var redWins = redPoints > 25, blueWins = bluePoints > 25; if (redWins || blueWins || resign) { this.refs.gridControl.state.sound.won.play(); if (false === resign) { this.refs.gridControl.setState({ overlay: true, overlayTitle: (redWins ? 'Red' : 'Blue') + " wins the game!", overlaySubTitle: "Play again!" }); } this.refs.gridControl.showLeftMines(); this.refs.gridControl.refs.userControl.setState({activePlayer: false}); this.refs.gridControl.refs.userControl.refs.red.setState({desc: ""}); this.refs.gridControl.refs.userControl.refs.blue.setState({desc: ""}); } } resignProcess(color) { this.refs.gridControl.setState({ overlay: true, overlayTitle: color === this.refs.gridControl.state.webPlayer ? "You have been give up" : "Your opponent has been resigned", overlaySubTitle: color === this.refs.gridControl.state.webPlayer ? "You LOSE!" : "You WIN!" }); this.makeGameEndIfItEnds(0, 0, true); } clickResign() { this.state.session .publish(this.state.channel, { 'resign': this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red' }); this.resignProcess(this.refs.gridControl.state.webPlayer); } clickResignCancel() { this.refs.gridControl.setState({ overlay: false }); } /** RESIGN */ resign() { let users = this.refs.gridControl.refs.userControl, activePlayer = users.state.activePlayer ? 'blue' : 'red'; if (this.refs.gridControl.state.webPlayer === activePlayer) { this.refs.gridControl.setState({ overlay: true, overlayTitle: "Are u sure u want to resign?!", overlaySubTitle:
}); } } /** after rendering */ componentDidMount() { /** Create Websocket w/ Bahnhof.js */ var websocket = WS.connect( this.state.env === 'dev' ? "ws://mine.dev:6450" : "ws://www.mineseeker.ninja:6450" ); /** * 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; /** * Connect - RPC * 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] ) .then( (gridServer) => { console.info("Grid has been created! Return w/ gameAssoc."); 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, channel: this.state.channel, desc: { buddy: