Advertisement
Qpel

4praktiniiiiiiss

Nov 21st, 2019
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app:
  2.  
  3.  
  4. import React, {Component} from 'react'
  5.  
  6. class App extends Component {
  7.     constructor(props) {
  8.         super(props)
  9.         this.inputSelect = React.createRef()
  10.         this.onDice = this.onDice.bind(this)
  11.         this.state = {
  12.             isLoaded: false,
  13.             //shipLocation: [x: 0, y: 0],
  14.             value: '',
  15.             coordinates: [],
  16.             random: 0,
  17.             activePath : 0
  18.     }
  19. }
  20.  
  21. onDice() {
  22.     fetch("http://localhost:3000/api/dice?activePath="+ this.state.activePath)
  23.     .then(res => res.json())
  24.     .then(result => {
  25.      
  26.         this.setState({
  27.             coordinates: result.coordinates,
  28.             dice: result.dice,
  29.             activePath: result.coordinates.id
  30.         })
  31.     })
  32.     .catch(e => console.log(e))
  33.  
  34. }
  35.  
  36. componentDidMount(){
  37.     //const result = {
  38.      //   actionType: 'start'
  39.     //}
  40.    /*
  41.     fetch('http://localhost:3000/api/action', {
  42.         method: 'POST',
  43.         body: JSON.stringify(result)
  44.     })
  45.     .then(res => res.json())
  46.     .then(result => {
  47.         this.setState({
  48.             currentCoordinate: result
  49.         })
  50.     })*/
  51.  
  52. }
  53.  
  54. _onHandleAction(actionType, bonus = {}){
  55.     const result = {
  56.         actionType,
  57.         bonus
  58.     }
  59. /*
  60.     fetch('http://localhost:3000/api/action', {
  61.         method: 'POST',
  62.         body: JSON.stringify(result)
  63.     })
  64.     .then(res => res.json())
  65.     .then(result => {
  66.         this.setState({
  67.             currentCoordinate: result
  68.         })
  69.     })*/
  70. }
  71.  
  72. showDice() {
  73.     let dicee = []
  74.     for (let i = 0; i < this.state.random; i++){
  75.         console.log(i);
  76.       dicee.push( <div className="grid-item"></div> )
  77.     }
  78.     return dicee
  79. }
  80.  
  81.  
  82.     render() {
  83.         console.log(this.state.coordinates)
  84.         const {currentCoordinate}=this.state
  85.        
  86.         return (
  87.             <div className="fatherblock">
  88.                 <div className="firstdiv">
  89.                     <img className="imgmap" id="mapas" src="./images/zemelapis.png"/>
  90.                     <img style={{left: 660, top:730, position:"absolute", height: 150, width: 150}} src="./images/laivas.png"/>
  91.                    
  92.                 </div>
  93.                 <button onClick={this.onDice }>Kauliukas</button>
  94.                 <div>Iskrito: {this.state.random} </div>
  95.                 <div className="grid-container">
  96.                
  97.                 </div>
  98.             </div>
  99.         )
  100.     }
  101.    
  102. }
  103. export default App
  104.  
  105.  
  106.  
  107.  
  108. pathfinderservice:
  109.  
  110.  
  111. class PathFinderServise {
  112.     constructor(dice, activePosition, amount, coordinates){
  113.         this._dice = dice
  114.         this._activePosition = activePosition
  115.         this._amount = amount
  116.         this._coordinates = coordinates
  117.     }
  118.  
  119.     getPath() {
  120.       const lastPosition = this._getLastPosition()
  121.       const activePath = this._getActivePath(lastPosition)
  122.       return activePath
  123.     }
  124.    
  125.     _getLastPosition() {
  126.         let lastPosition = this._activePosition + this._dice
  127.         if (lastPosition = 0 ) lastPosition = lastPosition
  128.         else if (lastPosition > this._amount) lastPosition = this._amount//sitas isijungia tik jei virsyja
  129.         else if(lastPosition < 1) lastPosition = 1 //kas neitu i minusa bet i pirma laukeli
  130.         return lastPosition//current 3 dice 2 = 5
  131.     }
  132.  
  133.     _getActivePath(lastPosition) {
  134.  
  135.      
  136.         let result = {}
  137.         for (let i = 0; i < this._amount; i++) {
  138.             if(!this._coordinates[i] ){
  139.                 break
  140.             }
  141.             let coordinate = this._coordinates[i]
  142.             if (coordinate.id == lastPosition) {
  143.                 result = coordinate
  144.                 break
  145.             }
  146.         }
  147.  
  148.         return result
  149.     }
  150.  
  151. }
  152. module.exports = PathFinderServise
  153.  
  154.  
  155. gamecontroler:
  156.  
  157. const PathFinderServise = require('../services/PathFinderService')
  158.  
  159. class GameControler {
  160.     constructor() {
  161.         this.findCoordinate = this.findCoordinate.bind(this)
  162.         this.getPath = this.getPath.bind(this)
  163.         this.getDices = this.getDices.bind(this)
  164.        
  165.     }
  166.  
  167.     _shipCoordinates() {
  168.         return [
  169.            
  170.             {id: 1, x: 660  , y: 730 ,entity:1, obsticle: false},
  171.             {id: 2, x: 530  , y: 730 , entity:1,obsticle: {bonus: {multiply: true}}}, // daugiklis x2
  172.             {id: 3, x: 440  , y: 700 , entity:1,obsticle: {penalty: {newgame: true}}}, // new game
  173.             {id: 4, x: 495  , y: 610 ,entity:1, obsticle: false},
  174.             {id: 5, x: 540  , y: 530 , entity:1,obsticle: {penalty: {test: true}}}, // testas prides +1
  175.             {id: 6, x: 440  , y: 510 , entity:1,obsticle: {bonus: {multiply: true }}},, // daugiklis x2
  176.             {id: 7, x: 345  , y: 520, entity:1, obsticle: false},
  177.             {id: 8, x: 250  , y: 490 ,entity:1, obsticle: false},
  178.             {id: 9, x: 230  , y: 405, entity:1, obsticle: false},
  179.             {id: 10, x: 150  , y: 340 ,entity:1, obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  180.             {id: 11, x: 190 , y:  260 , entity:2, obsticle: false},
  181.             {id: 12, x: 230 , y:  170  , entity:2, obsticle: false},
  182.             {id: 13, x: 280  ,y:  110 , entity:2, obsticle: false},
  183.             {id: 14, x: 270  , y: 120  , entity:2, obsticle: false},
  184.             {id: 15, x: 480  , y: 130  ,entity:2   , obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  185.             {id: 16, x: 565  , y: 100  ,entity:2   , obsticle: {penalty: {newgame: true}}},
  186.             {id: 17, x: 650  , y: 120 , entity:2 , obsticle: {bonus: {stepfurther: true }}}, // 2 zingsniai i prieki ( laimi zaidima )
  187.             {id: 18, x: 690  , y: 180  ,entity:2   , obsticle: {penalty: {test: true}}}, // testas
  188.             {id: 19, x: 740  , y: 240 , entity:2, obsticle: false},
  189.         ]
  190.     }
  191.  
  192.  
  193.     findCoordinate(req, res, next) {
  194.         const coordinateId = parseInt(req.body.coordinateId) || 0
  195.         const coordinates = this._shipCoordinates()
  196.         const amount = coordinates.length
  197.         let result = {}
  198.         for (let i = 0; i < amount; i++) {
  199.             let coordinate = coordinates[i]
  200.             if (coordinate.id == coordinateId) {
  201.                 result = coordinate
  202.                 break
  203.             }
  204.         }
  205.     }
  206.  
  207.     _onHandleBonus(actionType, bonus = {}) {
  208.         bonus = {steps: -3, activePosition: 3}
  209.         actionType = 'bonus'
  210.             const result = {
  211.                     actionType,
  212.                     bonus
  213.             }
  214.             fetch('http://localhost:3000/api/coordinate',  {
  215.         method: 'POST' ,
  216.         body: JSON.stringify(result)
  217.             })
  218.             .then(res => res.json() )
  219.             .then(result => {
  220.                 this.setState({
  221.                     currentCoordinate: result + (bonus)
  222.                 })
  223.               })
  224.         }
  225.  
  226.         componentDidUpdate(prevProps, prevState) {
  227.             const {newPosition} = this.props;
  228.             if (prevProps.activePosition !== newPosition) {
  229.                 const {coordinate} = prevState;
  230.               console.log('${prevProps.activePosition} keliaujate i ${newPosition}');
  231.            
  232.          
  233.          
  234.             }
  235.         }
  236.  
  237.         _dice(){
  238.             return  Math.floor(Math.random() * 6) + 1;
  239.         }
  240.        
  241.         getDices(req, res, next) {
  242.             const dice = this._dice()
  243.             return res.json(dice)
  244.         }
  245.  
  246.     getPath(req, res, next) {
  247.         let newPosition = 0
  248.         //let dice = this._dice()
  249.         let activePosition = req.query.activePath
  250.         let bonus = '-3'
  251.         let amount = 19
  252.        
  253.         activePosition = parseInt(activePosition)
  254.         bonus = parseInt(bonus)
  255.         const actionType = req.body.actionType
  256.         if(actionType == 'dice') {
  257.             //turi kviesti dice funckija ir ispausdinti kiek gavo back ende skaicius i front enda
  258.             newPosition = this._dice()
  259.         } else if (actionType == 'bonus') {
  260.             newPosition = activePosition + (bonus)
  261.         }
  262.         const pathFinderService = new PathFinderServise(newPosition, activePosition, amount, this._shipCoordinates())
  263.         const result = pathFinderService.getPath()
  264.        // console.log(result)
  265.         return res.json(
  266.             {
  267.                 coordinates: result,
  268.                 dice: newPosition
  269.             }
  270.  
  271.         )
  272.     }
  273. }
  274.  
  275. module.exports = GameControler
  276.  
  277. routes:
  278.  
  279. const GameControler = require('./controllers/GameControler')
  280. const gameControler = new GameControler()
  281.  
  282. module.exports = (app) => {
  283.     app.get('/', (req, res) => {
  284.         res.render('home')
  285.     })
  286.     //app.get('/', gameControler.getPath)
  287.     //app.get('/api/', gameControler.getPath)
  288.     //app.post('/api/', gameControler.getPath)
  289.    
  290.     app.get('/api/dice', gameControler.getPath)
  291.     return app
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement