Skip to content

Commit

Permalink
finish #128 es7 async and await
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnymac committed Aug 25, 2016
1 parent 04c1f5d commit cdb4c72
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 87 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
'everything-else'
]
}],
"no-loops/no-loops": 2
"no-loops/no-loops": 2,
# https://github.com/eslint/eslint/issues/6274
"generator-star-spacing": 0
},
"plugins": [
"import",
Expand Down
135 changes: 77 additions & 58 deletions main.development.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ if (process.env.NODE_ENV === 'development') {
require('electron-debug')() // eslint-disable-line global-require
}

function sleep(time) {
return new Promise(r => setTimeout(r, time))
}

function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
Expand Down Expand Up @@ -215,7 +219,7 @@ ipcMain.on('check-and-delete-credentials', () => {
}
})

ipcMain.on('pokemon-login', (event, method, username, password) => {
ipcMain.on('pokemon-login', async (event, method, username, password) => {
console.log('[+] Attempting to login')
let login
if (method === 'google') {
Expand All @@ -224,15 +228,17 @@ ipcMain.on('pokemon-login', (event, method, username, password) => {
login = new pogobuf.PTCLogin()
}

login.login(username, password).then(token => {
try {
const token = await login.login(username, password)

client.setAuthInfo(method, token)
client.init()

event.sender.send('pokemon-logged-in')
}).catch(error => {
} catch (error) {
console.error(error)
showErrorMessage(error.message)
})
}
})
// END OF LOGIN

Expand All @@ -241,9 +247,12 @@ ipcMain.on('table-did-mount', () => {
})

// POKEMON
ipcMain.on('get-player-info', (event) => {
ipcMain.on('get-player-info', async (event) => {
console.log('[+] Retrieving player info')
client.getPlayer().then(response => {

try {
const response = await client.getPlayer()

if (!response.success) {
event.returnValue = {
success: false
Expand All @@ -255,7 +264,9 @@ ipcMain.on('get-player-info', (event) => {
success: 'true',
player_data: response.player_data
}
})
} catch (error) {
console.error(error)
}
})

function generateEmptySpecies(candies) {
Expand Down Expand Up @@ -374,8 +385,10 @@ function parseInventory(inventory) {
}
}

function getPlayersPokemons(event, sync = 'sync') {
client.getInventory(0).then(inventory => {
async function getPlayersPokemons(event, sync = 'sync') {
try {
const inventory = await client.getInventory(0)

if (!inventory.success) {
const payload = { success: false }
if (sync !== 'sync') {
Expand All @@ -393,70 +406,76 @@ function getPlayersPokemons(event, sync = 'sync') {
} else {
event.sender.send('receive-players-pokemons', payload)
}
})
} catch (error) {
console.error(error)
}
}

ipcMain.on('get-players-pokemons', (event, sync) => {
console.log("[+] Retrieving player's Pokemons and Calculating Evolves")
getPlayersPokemons(event, sync)
})

ipcMain.on('power-up-pokemon', (event, id, nickname) => {
client.upgradePokemon(id)
.then(() => {
console.log(`[+] Upgraded Pokemon with id: ${id}`)
const message = `Upgraded ${nickname} succesfully!`
const title = `Power Up ${nickname}`
// TODO parse the response instead of retrieving all the new pokemon
// Requires replacing the main parsing with more functional code
getPlayersPokemons(event, 'async')
showInformationMessage(message, title)
})
.catch(console.error)
ipcMain.on('power-up-pokemon', async (event, id, nickname) => {
try {
await client.upgradePokemon(id)

console.log(`[+] Upgraded Pokemon with id: ${id}`)
const message = `Upgraded ${nickname} succesfully!`
const title = `Power Up ${nickname}`
// TODO parse the response instead of retrieving all the new pokemon
// Requires replacing the main parsing with more functional code
getPlayersPokemons(event, 'async')
showInformationMessage(message, title)
} catch (error) {
console.error(error)
}
})

ipcMain.on('transfer-pokemon', (event, pokemon, delay) => {
setTimeout(() => {
// The line below is useful right now for testing
// it'd be even more useful with mock data responses!
// (new Promise((resolve, reject) => { resolve() }))
client.releasePokemon(pokemon.id)
.then(() => {
console.log(`[+] Released Pokemon with id: ${pokemon.id}`)

event.sender.send('transfer-pokemon-complete', pokemon)
}).catch(console.error)
}, delay)
ipcMain.on('transfer-pokemon', async (event, pokemon, delay) => {
try {
await sleep(delay)
await client.releasePokemon(pokemon.id)
console.log(`[+] Released Pokemon with id: ${pokemon.id}`)

event.sender.send('transfer-pokemon-complete', pokemon)
} catch (error) {
console.error(error)
}
})

ipcMain.on('evolve-pokemon', (event, pokemon, delay) => {
setTimeout(() => {
// The line below is useful right now for testing
// it'd be even more useful with mock data responses!
// (new Promise((resolve, reject) => { resolve() }))
client.evolvePokemon(pokemon.id)
.then(() => {
console.log(`[+] Evolved Pokemon with id: ${pokemon.id}`)

event.sender.send('evolve-pokemon-complete', pokemon)
}).catch(console.error)
}, delay)
ipcMain.on('evolve-pokemon', async (event, pokemon, delay) => {
try {
await sleep(delay)
await client.evolvePokemon(pokemon.id)
console.log(`[+] Evolved Pokemon with id: ${pokemon.id}`)

event.sender.send('evolve-pokemon-complete', pokemon)
} catch (error) {
console.error(error)
}
})

ipcMain.on('favorite-pokemon', (event, id, isFavorite) => {
client.setFavoritePokemon(id, isFavorite)
.then(() => {
getPlayersPokemons(event, 'async')
}).catch(console.error)
console.log(`[+] Pokemon favorite status set to ${isFavorite}`)
ipcMain.on('favorite-pokemon', async (event, id, isFavorite) => {
try {
await client.setFavoritePokemon(id, isFavorite)

console.log(`[+] Pokemon favorite status set to ${isFavorite}`)
getPlayersPokemons(event, 'async')
} catch (error) {
console.error(error)
}
})

ipcMain.on('rename-pokemon', (event, id, nickname) => {
client.nicknamePokemon(id, nickname)
.then(() => {
console.log(`[+] Pokemon ${id} nicknamed ${nickname}`)
ipcMain.on('rename-pokemon', async (event, id, nickname) => {
try {
await client.nicknamePokemon(id, nickname)

event.sender.send('rename-pokemon-complete', id, nickname)
}).catch(console.error)
console.log(`[+] Pokemon ${id} nicknamed ${nickname}`)

event.sender.send('rename-pokemon-complete', id, nickname)
} catch (error) {
console.error(error)
}
})
// END OF POKEMON
55 changes: 28 additions & 27 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,36 @@ function build(cfg) {
})
}

function startPack() {
async function startPack() {
console.log('start pack...')
build(electronCfg)
.then(() => build(cfg))
.then(() => del('release'))
.then(paths => {
if (shouldBuildAll) {
// build for all platforms
const archs = ['ia32', 'x64']
const platforms = ['linux', 'win32', 'darwin']

platforms.forEach(plat => {
archs.forEach(arch => {
pack(plat, arch, log(plat, arch))
})
})
} else if (argv.platform || argv.arch) {
const arch = argv.arch || os.arch()
const platform = argv.platform || os.platform()

pack(platform, arch, log(platform, arch))
} else {
// build for current platform only
pack(os.platform(), os.arch(), log(os.platform(), os.arch()))

try {
await build(electronCfg)
await build(cfg)
const paths = await del('release')

if (shouldBuildAll) {
// build for all platforms
const archs = ['ia32', 'x64']
const platforms = ['linux', 'win32', 'darwin']

for (const plat of platforms) { // eslint-disable-line
for (const arch of archs) { // eslint-disable-line
await pack(plat, arch, log(plat, arch))
}
}
})
.catch(err => {
console.error(err)
})
} else if (argv.platform || argv.arch) {
const arch = argv.arch || os.arch()
const platform = argv.platform || os.platform()

await pack(platform, arch, log(platform, arch))
} else {
// build for current platform only
await pack(os.platform(), os.arch(), log(os.platform(), os.arch()))
}
} catch (error) {
console.error(error)
}
}

function pack(plat, arch, cb) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build-renderer": "cross-env NODE_ENV=production node -r babel-register ./node_modules/webpack/bin/webpack --config webpack.config.production.js --progress --profile --colors",
"build": "npm run build-main && npm run build-renderer",
"start-hot": "cross-env HOT=1 NODE_ENV=development electron -r babel-register -r babel-polyfill ./main.development.js",
"package": "cross-env NODE_ENV=production node -r babel-register package.js",
"package": "cross-env NODE_ENV=production node -r babel-register -r babel-polyfill package.js",
"package-all": "npm run package -- --all",
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
"dev": "concurrently --kill-others \"npm run hot-server\" \"npm run start-hot\""
Expand Down

0 comments on commit cdb4c72

Please sign in to comment.