Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Switched to ETA for travel #68

Merged
merged 4 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/components/TravelTimer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const customState = {
ship: {
...defaultState.ship,
destination: {
eta: '10000',
name: 'Test Planet 2',
value: 50
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ exports[`<TravelTimer /> Should render the <TravelTimer /> component. 1`] = `
>
Travel Timer
</h3>
<span>
8 minutes 20 seconds
</span>
</div>
</DocumentFragment>
`;
10 changes: 7 additions & 3 deletions src/components/PlanetDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Box, Button, Heading } from 'grommet'
import { Target } from 'grommet-icons'
import { setShipTraveling, setDestination } from '../redux/actions/ship'
import { setShipTraveling, setDestination, setETA } from '../redux/actions/ship'
import ItemDisplay from './ItemDisplay'
import { createETA } from '../util'

const PlanetDisplay = ({ handleShipTravel, planet, ship }) => {
const { isHomePlanet, items, location, name } = planet
Expand All @@ -21,7 +22,7 @@ const PlanetDisplay = ({ handleShipTravel, planet, ship }) => {
data-testid={`travel-button-${name}`}
hoverIndicator
icon={<Target />}
onClick={() => handleShipTravel({ name, value: location })}
onClick={() => handleShipTravel({ name, value: location }, ship)}
plain
/>
)}
Expand All @@ -43,11 +44,14 @@ PlanetDisplay.propTypes = {
const mapStateToProps = ({ ship }) => ({ ship })

const mapDispatchToProps = dispatch => ({
handleShipTravel: destination => {
handleShipTravel: (destination, ship) => {
// * set isShipTraveling to true
dispatch(setShipTraveling(true))
// * set destination
dispatch(setDestination(destination))
// * set ETA
const eta = createETA(destination, ship)
dispatch(setETA(eta.format('x')))
}
})

Expand Down
54 changes: 18 additions & 36 deletions src/components/TravelTimer.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
import React, { useEffect, useState } from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { createTravelDuration } from '../util'
import { Box, Heading } from 'grommet'
import { addCash } from '../redux/actions/user'
import {
removeCargo,
setShipLocation,
setDestination,
setShipTraveling,
setTravelDuration
setShipTraveling
} from '../redux/actions/ship'
import moment from 'moment'

const TravelTimer = ({ handleSetTravelDuration, handleTimerStopped, ship }) => {
const [minutes, setMinutes] = useState(0)
const [seconds, setSeconds] = useState(0)
const TravelTimer = ({ handleTimerStopped, ship }) => {
const [timeLeft, setTimeLeft] = useState(null)

const timerLogic = () => {
if (ship.isShipTraveling) {
let timerDuration
if (ship.travelDuration) {
// * There is already a travel durataion, use it
timerDuration = moment.duration({
minutes: ship.travelDuration.minutes,
seconds: ship.travelDuration.seconds
})
} else {
timerDuration = createTravelDuration(ship.destination, ship)
}
setMinutes(timerDuration.minutes())
setSeconds(timerDuration.seconds())

const travelTimer = setInterval(() => {
timerDuration.subtract(1, 'second')
setMinutes(timerDuration.minutes())
setSeconds(timerDuration.seconds())
handleSetTravelDuration(timerDuration)
const now = moment()
now.millisecond(0)
const differenceMill = moment(ship.destination.eta, 'x').diff(now)

const diffDuration = moment.duration({ milliseconds: differenceMill })

diffDuration.subtract(1, 'second')

if (timerDuration.asMilliseconds() === 0) {
if (diffDuration.asMilliseconds() === 0) {
clearInterval(travelTimer)
handleTimerStopped(ship)
}

setTimeLeft(diffDuration)
}, 1000)
}
}
Expand All @@ -51,15 +40,16 @@ const TravelTimer = ({ handleSetTravelDuration, handleTimerStopped, ship }) => {
return ship.isShipTraveling ? (
<Box>
<Heading level="3">Travel Timer</Heading>
<span>
{minutes} minutes {seconds} seconds
</span>
{timeLeft && (
<span>
{timeLeft.minutes()} minutes {timeLeft.seconds()} seconds
</span>
)}
</Box>
) : null
}

TravelTimer.propTypes = {
handleSetTravelDuration: PropTypes.func.isRequired,
handleTimerStopped: PropTypes.func.isRequired,
ship: PropTypes.object.isRequired
}
Expand Down Expand Up @@ -87,14 +77,6 @@ const mapDispatchToProps = dispatch => ({
)
dispatch(setDestination(null))
dispatch(setShipTraveling(false))
},
handleSetTravelDuration: travelDuration => {
dispatch(
setTravelDuration({
minutes: travelDuration.minutes(),
seconds: travelDuration.seconds()
})
)
}
})

Expand Down
12 changes: 6 additions & 6 deletions src/redux/actions/ship.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// * ACTION TYPES
const REMOVE_CARGO = 'REMOVE_CARGO'
const SET_DESTINATION = 'SET_DESTINATION'
const SET_ETA = 'SET_ETA'
const SET_SHIP_LOCATION = 'SET_SHIP_LOCATION'
const SET_SHIP_TRAVELING = 'SET_SHIP_TRAVELING'
const SET_TRAVEL_DURATION = 'SET_TRAVEL_DURATION'
const STORE_CARGO = 'STORE_CARGO'

// * ACTION GENERATORS
Expand All @@ -19,6 +19,11 @@ export const setDestination = destination => ({
payload: { destination }
})

export const setETA = eta => ({
type: SET_ETA,
payload: { eta }
})

export const setShipLocation = location => ({
type: SET_SHIP_LOCATION,
payload: { location }
Expand All @@ -29,11 +34,6 @@ export const setShipTraveling = isShipTraveling => ({
payload: { isShipTraveling }
})

export const setTravelDuration = travelDuration => ({
type: SET_TRAVEL_DURATION,
payload: { travelDuration }
})

export const storeCargo = (item, quantity) => ({
type: STORE_CARGO,
payload: {
Expand Down
8 changes: 6 additions & 2 deletions src/redux/reducers/ship.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const shipDefaultState = {
location: {
name: null,
value: null
},
travelDuration: null
}
}

export default (state = shipDefaultState, action) => {
Expand All @@ -28,6 +27,11 @@ export default (state = shipDefaultState, action) => {
}
case 'SET_DESTINATION':
return { ...state, destination: action.payload.destination }
case 'SET_ETA':
return {
...state,
destination: { ...state.destination, eta: action.payload.eta }
}
case 'SET_SHIP_LOCATION':
return {
...state,
Expand Down
7 changes: 5 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ export const createDuration = () => {
return moment.duration({ minutes: minutesLeft, seconds: secondsLeft })
}

export const createTravelDuration = (destination, ship) => {
export const createETA = (destination, ship) => {
const distance = Math.abs(destination.value - ship.location.value)
const seconds = distance * 10
const eta = moment()
eta.add(seconds, 'seconds')
eta.millisecond(0)

return moment.duration({ seconds })
return eta
}

export const generateContracts = () => {
Expand Down