Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app:
- import React, {Component} from 'react'
- class App extends Component {
- constructor(props) {
- super(props)
- this.inputSelect = React.createRef()
- this.onDice = this.onDice.bind(this)
- this.state = {
- isLoaded: false,
- //shipLocation: [x: 0, y: 0],
- value: '',
- coordinates: [],
- random: 0,
- activePath : 0
- }
- }
- onDice() {
- fetch("http://localhost:3000/api/dice?activePath="+ this.state.activePath)
- .then(res => res.json())
- .then(result => {
- this.setState({
- coordinates: result.coordinates,
- dice: result.dice,
- activePath: result.coordinates.id
- })
- })
- .catch(e => console.log(e))
- }
- componentDidMount(){
- //const result = {
- // actionType: 'start'
- //}
- /*
- fetch('http://localhost:3000/api/action', {
- method: 'POST',
- body: JSON.stringify(result)
- })
- .then(res => res.json())
- .then(result => {
- this.setState({
- currentCoordinate: result
- })
- })*/
- }
- _onHandleAction(actionType, bonus = {}){
- const result = {
- actionType,
- bonus
- }
- /*
- fetch('http://localhost:3000/api/action', {
- method: 'POST',
- body: JSON.stringify(result)
- })
- .then(res => res.json())
- .then(result => {
- this.setState({
- currentCoordinate: result
- })
- })*/
- }
- showDice() {
- let dicee = []
- for (let i = 0; i < this.state.random; i++){
- console.log(i);
- dicee.push( <div className="grid-item"></div> )
- }
- return dicee
- }
- render() {
- console.log(this.state.coordinates)
- const {currentCoordinate}=this.state
- return (
- <div className="fatherblock">
- <div className="firstdiv">
- <img className="imgmap" id="mapas" src="./images/zemelapis.png"/>
- <img style={{left: 660, top:730, position:"absolute", height: 150, width: 150}} src="./images/laivas.png"/>
- </div>
- <button onClick={this.onDice }>Kauliukas</button>
- <div>Iskrito: {this.state.random} </div>
- <div className="grid-container">
- </div>
- </div>
- )
- }
- }
- export default App
- pathfinderservice:
- class PathFinderServise {
- constructor(dice, activePosition, amount, coordinates){
- this._dice = dice
- this._activePosition = activePosition
- this._amount = amount
- this._coordinates = coordinates
- }
- getPath() {
- const lastPosition = this._getLastPosition()
- const activePath = this._getActivePath(lastPosition)
- return activePath
- }
- _getLastPosition() {
- let lastPosition = this._activePosition + this._dice
- if (lastPosition = 0 ) lastPosition = lastPosition
- else if (lastPosition > this._amount) lastPosition = this._amount//sitas isijungia tik jei virsyja
- else if(lastPosition < 1) lastPosition = 1 //kas neitu i minusa bet i pirma laukeli
- return lastPosition//current 3 dice 2 = 5
- }
- _getActivePath(lastPosition) {
- let result = {}
- for (let i = 0; i < this._amount; i++) {
- if(!this._coordinates[i] ){
- break
- }
- let coordinate = this._coordinates[i]
- if (coordinate.id == lastPosition) {
- result = coordinate
- break
- }
- }
- return result
- }
- }
- module.exports = PathFinderServise
- gamecontroler:
- const PathFinderServise = require('../services/PathFinderService')
- class GameControler {
- constructor() {
- this.findCoordinate = this.findCoordinate.bind(this)
- this.getPath = this.getPath.bind(this)
- this.getDices = this.getDices.bind(this)
- }
- _shipCoordinates() {
- return [
- {id: 1, x: 660 , y: 730 ,entity:1, obsticle: false},
- {id: 2, x: 530 , y: 730 , entity:1,obsticle: {bonus: {multiply: true}}}, // daugiklis x2
- {id: 3, x: 440 , y: 700 , entity:1,obsticle: {penalty: {newgame: true}}}, // new game
- {id: 4, x: 495 , y: 610 ,entity:1, obsticle: false},
- {id: 5, x: 540 , y: 530 , entity:1,obsticle: {penalty: {test: true}}}, // testas prides +1
- {id: 6, x: 440 , y: 510 , entity:1,obsticle: {bonus: {multiply: true }}},, // daugiklis x2
- {id: 7, x: 345 , y: 520, entity:1, obsticle: false},
- {id: 8, x: 250 , y: 490 ,entity:1, obsticle: false},
- {id: 9, x: 230 , y: 405, entity:1, obsticle: false},
- {id: 10, x: 150 , y: 340 ,entity:1, obsticle: {penalty: {backstep: true}}}, // 3 zingsniai atgal
- {id: 11, x: 190 , y: 260 , entity:2, obsticle: false},
- {id: 12, x: 230 , y: 170 , entity:2, obsticle: false},
- {id: 13, x: 280 ,y: 110 , entity:2, obsticle: false},
- {id: 14, x: 270 , y: 120 , entity:2, obsticle: false},
- {id: 15, x: 480 , y: 130 ,entity:2 , obsticle: {penalty: {backstep: true}}}, // 3 zingsniai atgal
- {id: 16, x: 565 , y: 100 ,entity:2 , obsticle: {penalty: {newgame: true}}},
- {id: 17, x: 650 , y: 120 , entity:2 , obsticle: {bonus: {stepfurther: true }}}, // 2 zingsniai i prieki ( laimi zaidima )
- {id: 18, x: 690 , y: 180 ,entity:2 , obsticle: {penalty: {test: true}}}, // testas
- {id: 19, x: 740 , y: 240 , entity:2, obsticle: false},
- ]
- }
- findCoordinate(req, res, next) {
- const coordinateId = parseInt(req.body.coordinateId) || 0
- const coordinates = this._shipCoordinates()
- const amount = coordinates.length
- let result = {}
- for (let i = 0; i < amount; i++) {
- let coordinate = coordinates[i]
- if (coordinate.id == coordinateId) {
- result = coordinate
- break
- }
- }
- }
- _onHandleBonus(actionType, bonus = {}) {
- bonus = {steps: -3, activePosition: 3}
- actionType = 'bonus'
- const result = {
- actionType,
- bonus
- }
- fetch('http://localhost:3000/api/coordinate', {
- method: 'POST' ,
- body: JSON.stringify(result)
- })
- .then(res => res.json() )
- .then(result => {
- this.setState({
- currentCoordinate: result + (bonus)
- })
- })
- }
- componentDidUpdate(prevProps, prevState) {
- const {newPosition} = this.props;
- if (prevProps.activePosition !== newPosition) {
- const {coordinate} = prevState;
- console.log('${prevProps.activePosition} keliaujate i ${newPosition}');
- }
- }
- _dice(){
- return Math.floor(Math.random() * 6) + 1;
- }
- getDices(req, res, next) {
- const dice = this._dice()
- return res.json(dice)
- }
- getPath(req, res, next) {
- let newPosition = 0
- //let dice = this._dice()
- let activePosition = req.query.activePath
- let bonus = '-3'
- let amount = 19
- activePosition = parseInt(activePosition)
- bonus = parseInt(bonus)
- const actionType = req.body.actionType
- if(actionType == 'dice') {
- //turi kviesti dice funckija ir ispausdinti kiek gavo back ende skaicius i front enda
- newPosition = this._dice()
- } else if (actionType == 'bonus') {
- newPosition = activePosition + (bonus)
- }
- const pathFinderService = new PathFinderServise(newPosition, activePosition, amount, this._shipCoordinates())
- const result = pathFinderService.getPath()
- // console.log(result)
- return res.json(
- {
- coordinates: result,
- dice: newPosition
- }
- )
- }
- }
- module.exports = GameControler
- routes:
- const GameControler = require('./controllers/GameControler')
- const gameControler = new GameControler()
- module.exports = (app) => {
- app.get('/', (req, res) => {
- res.render('home')
- })
- //app.get('/', gameControler.getPath)
- //app.get('/api/', gameControler.getPath)
- //app.post('/api/', gameControler.getPath)
- app.get('/api/dice', gameControler.getPath)
- return app
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement