diff options
Diffstat (limited to 'src/Actions')
-rw-r--r-- | src/Actions/actions.ts | 9 | ||||
-rw-r--r-- | src/Actions/auth.ts | 44 | ||||
-rw-r--r-- | src/Actions/entity.ts | 105 |
3 files changed, 158 insertions, 0 deletions
diff --git a/src/Actions/actions.ts b/src/Actions/actions.ts new file mode 100644 index 0000000..988ba65 --- /dev/null +++ b/src/Actions/actions.ts @@ -0,0 +1,9 @@ +import auth from './auth' +import entity from './entity' + +export const actions = { + ...auth, + ...entity, +} + +export default actions
\ No newline at end of file diff --git a/src/Actions/auth.ts b/src/Actions/auth.ts new file mode 100644 index 0000000..fb26c98 --- /dev/null +++ b/src/Actions/auth.ts @@ -0,0 +1,44 @@ +import { Dispatch } from 'redux'; + +const auth = { + 'login': () => (dispatch: Dispatch) => { + dispatch({ + type: 'authLoad', + loading: true, + }) + fetch("https://www.ingress.com/intel", { "credentials": "include", "referrer": "https://intel.ingress.com/" }) + .then(r => r.text()) + .then(t => { + const s1 = t.match(/gen_dashboard_(.+?)\.js/) + if (s1 == null) { + throw new Error("V not found") + } + const v = s1[1] + const s2 = t.match(/var PLAYER = (.+?)\;\n/) + if (s2 == null) { + throw new Error("user not found") + } + const user = JSON.parse(s2[1]) + const s3 = t.match(/\<input type='hidden' name='csrfmiddlewaretoken' value='(.+?)' \/\>/) + if (s3 == null) { + throw new Error("csrf not found") + } + const csrf = s3[1] + return { v, user, csrf } + // }) + // .then(({ v, user }) => { + // return CookieManager.get("https://intel.ingress.com") + // .then(cookies => { + // const csrf = cookies['csrftoken'] + // return { v, csrf, user } + // }) + }).then(({ v, csrf, user }) => + dispatch({ + type: 'authSet', + v, csrf, user + }) + ) + } +} + +export default auth
\ No newline at end of file diff --git a/src/Actions/entity.ts b/src/Actions/entity.ts new file mode 100644 index 0000000..632b505 --- /dev/null +++ b/src/Actions/entity.ts @@ -0,0 +1,105 @@ + +import { Dispatch, Store } from 'redux'; +import { Region } from 'react-native-maps'; +import { getTilesToLoad, loadTiles, getPortalDetails } from '../Api/api'; +import { Portal, Link, Field } from '../Api/types'; +import { LoadedResult } from '../Api/interfaces'; +import { decodePortal } from '../Api/entityDecoder'; + +const entity = { + 'update': (region: Region, width: number, refresh: boolean) => async (dispatch, getStore) => { + const store = getStore() + let tiles = getTilesToLoad(region, width) + if (!refresh) { + const loadedAlready = Object.keys(store.entities.portals) + tiles = tiles.filter(t => loadedAlready.indexOf(t) == -1) + } + const display = store.entities.display + dispatch(entity.loadTiles(tiles, display)) + }, + 'loadTiles': (tiles: string[], display: any) => async (dispatch, getStore) => { + const store = getStore() + const { v, csrf } = store.auth + const params = { v, csrf } + dispatch(entity.entitiesLoad(tiles.length)) + setImmediate(() => loadTiles(tiles, params) + .then((loadedData: LoadedResult) => dispatch(entity.receiveTiles(loadedData, display)))) + }, + 'receiveTiles': (loadedData: LoadedResult, display: any) => async (dispatch) => { + dispatch(entity.portalsSet(loadedData.portals)) + dispatch(entity.linksSet(loadedData.links)) + dispatch(entity.fieldsSet(loadedData.fields)) + Object.keys(loadedData.portalsByTile).forEach(tile => { + loadedData.portalsByTile[tile].forEach(p => { + if (display.portals.indexOf(p) == -1) { + display.portals.push(p) + } + }) + }) + Object.keys(loadedData.linksByTile).forEach(tile => { + loadedData.linksByTile[tile].forEach(p => { + if (display.links.indexOf(p) == -1) { + display.links.push(p) + } + }) + }) + Object.keys(loadedData.fieldsByTile).forEach(tile => { + loadedData.fieldsByTile[tile].forEach(p => { + if (display.fields.indexOf(p) == -1) { + display.fields.push(p) + } + }) + }) + dispatch(entity.entitiesDisplay(display)) + if (loadedData.failedTiles.length > 0) { + setImmediate(() => { + dispatch(entity.loadTiles(loadedData.failedTiles, display)) + }) + } else { + dispatch(entity.entitiesLoad(0)) + } + }, + 'getPortalDetails': (guid: string) => async (dispatch, getStore) => { + const store = getStore() + const { v, csrf } = store.auth + const params = { v, csrf } + getPortalDetails(guid, params).then(j => { + const portal = decodePortal(j.result) + portal.fullLoad = true + dispatch(entity.portalSet(guid, portal)) + }) + }, + 'entitiesDisplay': (display: any) => ({ + type: 'entitiesDisplay', + display + }), + 'entitiesLoad': (loading: number) => ({ + type: 'entitiesLoad', + loading + }), + 'portalsSet': (portals: { [guid: string]: Portal }) => ({ + type: 'portalsSet', + portals, + }), + 'linksSet': (links: { [guid: string]: Link }) => ({ + type: 'linksSet', + links, + }), + 'fieldsSet': (fields: { [guid: string]: Field }) => ({ + type: 'fieldsSet', + fields, + }), + 'portalSet': (guid: string, portal: Portal) => ({ + type: 'portalSet', + guid, portal, + }), + 'linkSet': (guid: string, link: Link) => ({ + type: 'linksSet', + guid, link, + }), + 'fieldSet': (guid: string, field: Field) => ({ + type: 'fieldsSet', + guid, field, + }) +} +export default entity
\ No newline at end of file |