Skip to content

Commit

Permalink
Merge pull request #358 from AlmirKadric/feature/updateWhenStationary
Browse files Browse the repository at this point in the history
added auto updates when stationary feature
  • Loading branch information
h4v1nfun authored Aug 11, 2016
2 parents 4929b6e + 24ceebe commit d3d51c6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { observable } from 'mobx'
export default {
updateXcodeLocation: observable(false),
addJitterToMoves: observable(true),
stationaryUpdates: observable(true),
speedLimit: observable(4), // ~40-25 km/h
zoom: observable(17) // map zoom
}
36 changes: 34 additions & 2 deletions src/models/user-location.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint max-len: 0 */
import { throttle } from 'lodash'
import { random, throttle } from 'lodash'
import { observable } from 'mobx'
import Alert from 'react-s-alert'

Expand Down Expand Up @@ -40,8 +40,10 @@ const updateXcodeLocation = throttle(([ lat, lng ]) => {
// track location changes for total distance & average speed
stats.pushMove(lat, lng)

const jitter = settings.addJitterToMoves.get() ? random(-0.000009, 0.000009, true) : 0

const xcodeLocationData =
`<gpx creator="Xcode" version="1.1"><wpt lat="${lat.toFixed(6)}" lon="${lng.toFixed(6)}"><name>PokemonLocation</name></wpt></gpx>`
`<gpx creator="Xcode" version="1.1"><wpt lat="${(lat + jitter).toFixed(6)}" lon="${(lng + jitter).toFixed(6)}"><name>PokemonLocation</name></wpt></gpx>`

// write `pokemonLocation.gpx` file fro xcode spoof location
const filePath = resolve(remote.getGlobal('tmpProjectPath'), 'pokemonLocation.gpx')
Expand Down Expand Up @@ -78,4 +80,34 @@ userLocation.intercept(validateCoordinates)
// after update
userLocation.observe(() => updateXcodeLocation(userLocation))

// updated at random intervals to prevent reversion
let currentTimer = null
function scheduleUpdate() {
const randomWait = random(1000, 10000, true)
if (!settings.stationaryUpdates.get()) {
if (currentTimer) {
window.clearTimeout(currentTimer)
currentTimer = null
}
return
}

currentTimer = window.setTimeout(() => {
currentTimer = null

if (!settings.stationaryUpdates.get()) {
return
}

updateXcodeLocation(userLocation)
scheduleUpdate()
}, randomWait)
}

// watch settings for updates
settings.stationaryUpdates.observe(() => scheduleUpdate())

// initial trigger
scheduleUpdate()

export default userLocation
3 changes: 2 additions & 1 deletion src/views/map/boolean-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React from 'react'
import { observer } from 'mobx-react'
import cx from 'classnames'

import { addJitterToMoves, updateXcodeLocation } from '../../models/settings.js'
import { addJitterToMoves, stationaryUpdates, updateXcodeLocation } from '../../models/settings.js'

const settings = [
[ addJitterToMoves, 'Add randomness to moves' ],
[ stationaryUpdates, 'Update even when stationary' ],
[ updateXcodeLocation, 'Auto update Xcode location' ]
]

Expand Down
12 changes: 5 additions & 7 deletions src/views/map/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ const handleMove = action((direction) => {
random(0.0000300, 0.000070, true) / speedCoeff :
random(0.0000700, 0.000070, true) / speedCoeff

const jitter = settings.addJitterToMoves.get() ? random(-0.000009, 0.000009, true) : 0

let newLocation
switch (direction) {
case 'LEFT': { newLocation = [ userLocation[0] + jitter, userLocation[1] - move ]; break }
case 'RIGHT': { newLocation = [ userLocation[0] + jitter, userLocation[1] + move ]; break }
case 'DOWN': { newLocation = [ userLocation[0] - move, userLocation[1] + jitter ]; break }
case 'UP': { newLocation = [ userLocation[0] + move, userLocation[1] + jitter ]; break }
default: { newLocation = [ userLocation[0] + jitter, userLocation[1] + jitter ] }
case 'LEFT': { newLocation = [ userLocation[0], userLocation[1] - move ]; break }
case 'RIGHT': { newLocation = [ userLocation[0], userLocation[1] + move ]; break }
case 'DOWN': { newLocation = [ userLocation[0] - move, userLocation[1] ]; break }
case 'UP': { newLocation = [ userLocation[0] + move, userLocation[1] ]; break }
default: { newLocation = [ userLocation[0], userLocation[1] ] }
}

userLocation.replace(newLocation)
Expand Down

0 comments on commit d3d51c6

Please sign in to comment.