Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- style.css:
- * {
- font-family: sans-serif;
- margin: 0;
- }
- button {
- height: 40px;
- width: 200px;
- margin: 10px;
- }
- .warning {
- background-color: red;
- text-align: center;
- width: 100%;
- padding: 10px;
- font-size: 14pt;
- color: white;
- }
- .hide{
- width: 30px;
- height: 30px;
- float: right;
- }
- body, html {
- height: 100%;
- width: 100%;
- }
- .img {
- background-image: url(../images/zemelapis.png);
- background-repeat: no-repeat;
- background-size: 1000px 1000px;
- background-position: center;
- height: 1000px;
- justify-content: center;
- position:relative;
- }
- .text {
- font-family: Impact, Charcoal, sans-serif;
- font-size: 22px;
- letter-spacing: 1px;
- word-spacing: 1.4px;
- color: #E5B86C;
- font-weight: normal;
- text-decoration: none;
- font-style: normal;
- font-variant: normal;
- text-transform: none;
- }
- .laivas {
- content: url(../images/laivas.png);
- position: absolute;
- top: 671px;
- left: 855px;
- width:200px;
- height: 200px;
- }
- app:
- import React, {Component} from 'react'
- class App extends Component {
- constructor(props) {
- super(props)
- this.inputSelect = React.createRef()
- this.onHandleCurrentCoordinate = this.onHandleCurrentCoordinate.bind(this)
- this.state = {
- isLoaded: false,
- //shipLocation: [x: 0, y: 0],
- value: '',
- coordinates: []
- }
- }
- 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
- })
- })
- }
- onHandleCurrentCoordinate() {
- const coordinateId = this.inputSelect.current.value//passiima values kelintas
- const data = {coordinateId: coordinateId}
- fetch("http://localhost:3000/api/coordinate", {
- method: 'POST',
- headers: {"Content-type": "application/json" },
- body: JSON.stringify(data)
- })
- .then(res => res.json())
- .then(result => {
- this.setState({
- isLoaded: false,
- currentCoordinate: result
- })
- })
- .catch(e => console.log(e))
- }
- render() {
- const {currentCoordinate} =this.state
- return (
- <div>
- <div className="img">
- <div className="laivas"> </div>
- <div className="text"> Žemėlapis </div>
- </div>
- </div>
- )
- }
- }
- export default App
- 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 = this._dice()
- let activePosition = dice
- 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
- 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)
- return app
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement