Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gamecontroler:
- const PathFinderServise = require('../services/PathFinderService')
- class GameControler {
- constructor() {
- this.findCoordinate = this.findCoordinate.bind(this)
- this.getPath = this.getPath.bind(this)
- }
- _shipCoordinates() {
- return [
- {id: 1, x: 671 , y: 855 ,entity:1, obsticle: false},
- {id: 2, x: 580 , y: 834 , entity:1,obsticle: {bonus: {multiply: true}}}, // daugiklis x2
- {id: 3, x: 494 , y: 784 , entity:1,obsticle: {penalty: {newgame: true}}}, // new game
- {id: 4, x: 547 , y: 707 ,entity:1, obsticle: false},
- {id: 5, x: 602 , y: 625 , entity:1,obsticle: {penalty: {test: true}}}, // testas prides +1
- {id: 6, x: 499 , y: 601 , entity:1,obsticle: {bonus: {multiply: true }}},, // daugiklis x2
- {id: 7, x: 400 , y: 623, entity:1, obsticle: false},
- {id: 8, x: 315 , y: 583 ,entity:1, obsticle: false},
- {id: 9, x: 285 , y: 501, entity:1, obsticle: false},
- {id: 10, x:207 , y: 434 ,entity:1, obsticle: {penalty: {backstep: true}}}, // 3 zingsniai atgal
- {id: 11, x: 248 , y: 346 , entity:2, obsticle: false},
- {id: 12, x: 281 , y: 256 , entity:2, obsticle: false},
- {id: 13, x: 343 ,y: 196 , entity:2, obsticle: false},
- {id: 14, x: 436 , y:208 , entity:2, obsticle: false},
- {id: 15, x: 540 , y: 216 ,entity:2 , obsticle: {penalty: {backstep: true}}}, // 3 zingsniai atgal
- {id: 16, x: 630 , y: 193 ,entity:2 , obsticle: {penalty: {newgame: true}}},
- {id: 17, x: 708 , y: 207 , entity:2 , obsticle: {bonus: {stepfurther: true }}}, // 2 zingsniai i prieki ( laimi zaidima )
- {id: 18, x: 751 , y: 275 ,entity:2 , obsticle: {penalty: {test: true}}}, // testas
- {id: 19, x: 806 , y: 323 , 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
- }
- }
- }
- _dice(){
- return Math.floor(Math.random() * 6) + 1;
- }
- getPath(req, res, next) {
- let newPosition = 0
- let dice = 2
- let activePosition = '5'
- let bonus = '-5'
- let amount = 19
- activePosition = parseInt(activePosition)
- bonus = parseInt(bonus)
- const actionType = 'dice'
- //const actionType = req.body.actionType
- if(actionType == 'dice') {
- //turi kviesti dice funckija ir ispausdinti kiek gavo back ende skaicius i front enda
- newPosition = dice
- console.log(newPosition)
- } else if (actionType == 'bonus') {
- newPosition = activePosition + (bonus)
- // console.log(newPosition)
- }
- const pathFinderService = new PathFinderServise(dice, activePosition, amount, this._shipCoordinates())
- const result = pathFinderService.getPath()
- return res.json(result)
- }
- }
- module.exports = GameControler
- 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 > 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(req, res, next) {
- console.log(this._coordinates)
- let result = {}
- for (let i = 0; i < this._amount; i++) {
- let coordinate = this._coordinates[i]
- if (coordinate.id == this._activePosition) {
- result = coordinate
- break
- }
- }
- console.log(result)
- return result
- }
- }
- module.exports = PathFinderServise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement