Advertisement
Qpel

Untitled

Nov 21st, 2019
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const PathFinderServise = require('../services/PathFinderService')
  2.  
  3. class GameControler {
  4.     constructor() {
  5.         this.findCoordinate = this.findCoordinate.bind(this)
  6.         this.getPath = this.getPath.bind(this)
  7.  
  8.     }
  9.  
  10.     _shipCoordinates() {
  11.         return [
  12.             {id: 1, x: 660  , y: 730 ,entity:1, obsticle: false},
  13.             {id: 2, x: 530  , y: 730 , entity:1,obsticle: {bonus: {multiply: true}}}, // daugiklis x2
  14.             {id: 3, x: 440  , y: 700 , entity:1,obsticle: {penalty: {newgame: true}}}, // new game
  15.             {id: 4, x: 495  , y: 610 ,entity:1, obsticle: false},
  16.             {id: 5, x: 540  , y: 530 , entity:1,obsticle: {penalty: {test: true}}}, // testas prides +1
  17.             {id: 6, x: 440  , y: 510 , entity:1,obsticle: {bonus: {multiply: true }}},, // daugiklis x2
  18.             {id: 7, x: 345  , y: 520, entity:1, obsticle: false},
  19.             {id: 8, x: 250  , y: 490 ,entity:1, obsticle: false},
  20.             {id: 9, x: 230  , y: 405, entity:1, obsticle: false},
  21.             {id: 10, x: 150  , y: 340 ,entity:1, obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  22.             {id: 11, x: 190 , y:  260 , entity:2, obsticle: false},
  23.             {id: 12, x: 230 , y:  170  , entity:2, obsticle: false},
  24.             {id: 13, x: 280  ,y:  110 , entity:2, obsticle: false},
  25.             {id: 14, x: 270  , y: 120  , entity:2, obsticle: false},
  26.             {id: 15, x: 480  , y: 130  ,entity:2   , obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  27.             {id: 16, x: 565  , y: 100  ,entity:2   , obsticle: {penalty: {newgame: true}}},
  28.             {id: 17, x: 650  , y: 120 , entity:2 , obsticle: {bonus: {stepfurther: true }}}, // 2 zingsniai i prieki ( laimi zaidima )
  29.             {id: 18, x: 690  , y: 180  ,entity:2   , obsticle: {penalty: {test: true}}}, // testas
  30.             {id: 19, x: 740  , y: 240 , entity:2, obsticle: false},
  31.         ]
  32.     }
  33.  
  34.  
  35.     findCoordinate(req, res, next) {
  36.         const coordinateId = parseInt(req.body.coordinateId) || 0
  37.         const coordinates = this._shipCoordinates()
  38.         const amount = coordinates.length
  39.         let result = {}
  40.         for (let i = 0; i < amount; i++) {
  41.             let coordinate = coordinates[i]
  42.             if (coordinate.id == coordinateId) {
  43.                 result = coordinate
  44.                 break
  45.             }
  46.         }
  47.     }
  48.  
  49.     _onHandleBonus(actionType, bonus = {}) {
  50.         bonus = {steps: -3, activePosition: 3}
  51.         actionType = 'bonus'
  52.             const result = {
  53.                     actionType,
  54.                     bonus
  55.             }
  56.             fetch('http://localhost:3000/api/coordinate',  {
  57.         method: 'POST' ,
  58.         body: JSON.stringify(result)
  59.             })
  60.             .then(res => res.json() )
  61.             .then(result => {
  62.                 this.setState({
  63.                     currentCoordinate: result + (bonus)
  64.                 })
  65.               })
  66.         }
  67.  
  68.         componentDidUpdate(prevProps, prevState) {
  69.             const {newPosition} = this.props;
  70.             if (prevProps.activePosition !== newPosition) {
  71.                 const {coordinate} = prevState;
  72.               console.log('${prevProps.activePosition} keliaujate i ${newPosition}');
  73.            
  74.          
  75.          
  76.             }
  77.         }
  78.  
  79.         _dice(){
  80.             return  Math.floor(Math.random() * 6) + 1;
  81.         }
  82.  
  83.  
  84.  
  85.    
  86.     getPath(req, res, next) {
  87.         let newPosition = 0
  88.         let dice = 2
  89.         let activePosition = '3'
  90.         let bonus = '-3'
  91.         let amount = 19
  92.        
  93.         activePosition = parseInt(activePosition)
  94.         bonus = parseInt(bonus)
  95.         const actionType = req.body.actionType
  96.         if(actionType == 'dice') {
  97.             //turi kviesti dice funckija ir ispausdinti kiek gavo back ende skaicius i front enda
  98.             newPosition = this._dice()
  99.         } else if (actionType == 'bonus') {
  100.             newPosition = activePosition + (bonus)
  101.         }
  102.         const pathFinderService = new PathFinderServise(dice, activePosition, amount)
  103.         const result = pathFinderService.getPath()
  104.         console.log(result)
  105.         return res.json(result)
  106.     }
  107. }
  108.  
  109. module.exports = GameControler
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement