-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from hunterjm/develop
v0.4.0
- Loading branch information
Showing
13 changed files
with
437 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import _ from 'lodash'; | ||
import * as types from './watchTypes'; | ||
import { getApi } from '../utils/ApiUtil'; | ||
import { setCredits } from './account'; | ||
|
||
const filter = { | ||
type: 'player', | ||
num: 50, | ||
}; | ||
|
||
let cycleTimeout; | ||
|
||
const search = async (api, player, page = 0) => { | ||
const searchFilter = _.merge({}, filter, { | ||
definitionId: player.id, | ||
start: page | ||
}); | ||
let searchResults = 0; | ||
let last20Min = []; | ||
try { | ||
const searchResponse = await api.search(searchFilter); | ||
searchResults = searchResponse.auctionInfo.length; | ||
last20Min = _.filter(searchResponse.auctionInfo, trade => trade.expires <= 1200); | ||
} catch (e) { | ||
console.error(`Error watching auctions for ${player.name}`, e); // eslint-disable-line | ||
} | ||
if (searchResults > 0 && last20Min.length === searchResults) { | ||
// Increment page and search again | ||
last20Min = last20Min.concat(await search(api, player, page + 51)); | ||
} | ||
return last20Min; | ||
}; | ||
|
||
export function start(player) { | ||
return async (dispatch, getState) => { | ||
dispatch({ type: types.START_WATCHING }); | ||
dispatch(clearWatched(player.id)); | ||
const state = getState(); | ||
const api = getApi(state.account.email); | ||
const watched = await search(api, player); | ||
await dispatch(setWatched(player.id, _.keyBy(watched, 'tradeId'))); | ||
await dispatch(cycle(player)); | ||
}; | ||
} | ||
|
||
export function clearWatched(id) { | ||
return { type: types.CLEAR_WATCH, id }; | ||
} | ||
|
||
export function setWatched(id, trades) { | ||
return { type: types.SET_WATCH, id, trades }; | ||
} | ||
|
||
export function cycle(player) { | ||
return async (dispatch, getState) => { | ||
const state = getState(); | ||
const api = getApi(state.account.email); | ||
const tradeIds = Object.keys(_.get(state.watch, `trades[${player.id}]`, {})); | ||
if (state.watch.watching && tradeIds.length) { | ||
let statuses; | ||
try { | ||
statuses = await api.getStatus(tradeIds); | ||
dispatch(setCredits(statuses.credits)); | ||
} catch (e) { | ||
console.error(`Error getting trade statuses: ${JSON.stringify(tradeIds)}`, e); // eslint-disable-line | ||
statuses = { auctionInfo: [] }; | ||
} | ||
dispatch(setWatched(player.id, _.keyBy(statuses.auctionInfo, 'tradeId'))); | ||
const active = _.filter(statuses.auctionInfo, trade => trade.expires > 0); | ||
if (active.length) { | ||
cycleTimeout = window.setTimeout(() => { | ||
dispatch(cycle(player)); | ||
}, 10000); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export function stop() { | ||
if (cycleTimeout) { | ||
window.clearTimeout(cycleTimeout); | ||
cycleTimeout = undefined; | ||
} | ||
return { type: types.STOP_WATCHING }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const START_WATCHING = 'watch/start'; | ||
export const STOP_WATCHING = 'watch/stop'; | ||
export const CLEAR_WATCH = 'watch/clear'; | ||
export const SET_WATCH = 'watch/set'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.