Advertisement
Qpel

4praktinisssss

Nov 20th, 2019
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. gamecontroler:
  2.  
  3. const PathFinderServise = require('../services/PathFinderService')
  4.  
  5. class GameControler {
  6.     constructor() {
  7.         this.findCoordinate = this.findCoordinate.bind(this)
  8.         this.getPath = this.getPath.bind(this)
  9.  
  10.        
  11.  
  12.     }
  13.  
  14.     _shipCoordinates() {
  15.         return [
  16.             {id: 1, x: 671  , y: 855 ,entity:1, obsticle: false},
  17.             {id: 2, x: 580  , y: 834 , entity:1,obsticle: {bonus: {multiply: true}}}, // daugiklis x2
  18.             {id: 3, x: 494  , y: 784 , entity:1,obsticle: {penalty: {newgame: true}}}, // new game
  19.             {id: 4, x: 547  , y: 707 ,entity:1, obsticle: false},
  20.             {id: 5, x: 602  , y: 625 , entity:1,obsticle: {penalty: {test: true}}}, // testas prides +1
  21.             {id: 6, x: 499  , y: 601 , entity:1,obsticle: {bonus: {multiply: true }}},, // daugiklis x2
  22.             {id: 7, x: 400  , y: 623, entity:1, obsticle: false},
  23.             {id: 8, x: 315  , y: 583 ,entity:1, obsticle: false},
  24.             {id: 9, x: 285  , y: 501, entity:1, obsticle: false},
  25.             {id: 10, x:207  , y: 434 ,entity:1, obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  26.             {id: 11, x: 248 , y: 346 , entity:2, obsticle: false},
  27.             {id: 12, x: 281 , y: 256  , entity:2, obsticle: false},
  28.             {id: 13, x: 343  ,y: 196 , entity:2, obsticle: false},
  29.             {id: 14, x: 436  , y:208   , entity:2, obsticle: false},
  30.             {id: 15, x: 540  , y:  216  ,entity:2   , obsticle: {penalty: {backstep: true}}}, //  3 zingsniai atgal
  31.             {id: 16, x: 630  , y: 193  ,entity:2   , obsticle: {penalty: {newgame: true}}},
  32.             {id: 17, x: 708  , y: 207 , entity:2 , obsticle: {bonus: {stepfurther: true }}}, // 2 zingsniai i prieki ( laimi zaidima )
  33.             {id: 18, x: 751  , y: 275  ,entity:2   , obsticle: {penalty: {test: true}}}, // testas
  34.             {id: 19, x: 806  , y: 323 , entity:2, obsticle: false},
  35.         ]
  36.     }
  37.  
  38.  
  39.     findCoordinate(req, res, next) {
  40.         const coordinateId = parseInt(req.body.coordinateId) || 0
  41.         const coordinates = this._shipCoordinates()
  42.         const amount = coordinates.length
  43.         let result = {}
  44.         for (let i = 0; i < amount; i++) {
  45.             let coordinate = coordinates[i]
  46.             if (coordinate.id == coordinateId) {
  47.                 result = coordinate
  48.                 break
  49.             }
  50.         }
  51.     }
  52.  
  53.  
  54.    
  55.     _dice(){
  56.         return  Math.floor(Math.random() * 6) + 1;
  57.     }
  58.     getPath(req, res, next) {
  59.         let newPosition = 0
  60.         let dice = 2
  61.         let activePosition = '5'
  62.         let bonus = '-5'
  63.         let amount = 19
  64.        
  65.         activePosition = parseInt(activePosition)
  66.         bonus = parseInt(bonus)
  67.         const actionType = 'dice'
  68.         //const actionType = req.body.actionType
  69.         if(actionType == 'dice') {
  70.             //turi kviesti dice funckija ir ispausdinti kiek gavo back ende skaicius i front enda
  71.             newPosition = dice
  72.             console.log(newPosition)
  73.         } else if (actionType == 'bonus') {
  74.             newPosition = activePosition + (bonus)
  75.            // console.log(newPosition)
  76.         }
  77.         const pathFinderService = new PathFinderServise(dice, activePosition, amount, this._shipCoordinates())
  78.         const result = pathFinderService.getPath()
  79.         return res.json(result)
  80.     }
  81. }
  82.  
  83. module.exports = GameControler
  84.  
  85.  
  86.  
  87. pathfinderservice:
  88.  
  89.  
  90. class PathFinderServise {
  91.     constructor(dice, activePosition, amount, coordinates){
  92.         this._dice = dice
  93.         this._activePosition = activePosition
  94.         this._amount = amount
  95.         this._coordinates = coordinates
  96.     }
  97.  
  98.     getPath() {
  99.       const lastPosition = this._getLastPosition()
  100.       const activePath = this._getActivePath(lastPosition)
  101.       return activePath
  102.     }
  103.    
  104.     _getLastPosition() {
  105.         let lastPosition = this._activePosition + this._dice
  106.         if (lastPosition > this._amount) lastPosition = this._amount//sitas isijungia tik jei virsyja
  107.         else if(lastPosition < 1) lastPosition = 1 //kas neitu i minusa bet i pirma laukeli
  108.         return lastPosition//current 3 dice 2 = 5
  109.     }
  110.  
  111.     _getActivePath(req, res, next) {
  112.  
  113.        console.log(this._coordinates)
  114.         let result = {}
  115.         for (let i = 0; i < this._amount; i++) {
  116.             let coordinate = this._coordinates[i]
  117.             if (coordinate.id == this._activePosition) {
  118.                 result = coordinate
  119.                 break
  120.             }
  121.         }
  122.         console.log(result)
  123.         return result
  124.     }
  125.  
  126. }
  127. module.exports = PathFinderServise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement