-
Notifications
You must be signed in to change notification settings - Fork 1
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 #30 from MoT3rror/feature/interop-performance-boost
Feature/interop performance boost
- Loading branch information
Showing
11 changed files
with
6,452 additions
and
3,252 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
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
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,94 @@ | ||
const b = require('base64-arraybuffer') | ||
const axios = require('axios') | ||
const protobuf = require("protobufjs") | ||
const root = protobuf.loadSync('js/Proto/egginc.proto'); | ||
|
||
const ei_request = (path, payload, requestPB, responsePB) => { | ||
return new Promise((resolve, reject) => { | ||
var errMsg = requestPB.verify(payload); | ||
if (errMsg) | ||
throw Error(errMsg) | ||
|
||
var buffer = requestPB.encode(requestPB.create(payload)).finish() | ||
|
||
let options = { | ||
url: `http://afx-2-dot-auxbrainhome.appspot.com/${path}`, | ||
method: 'post', | ||
data: 'data=' + b.encode(buffer), | ||
} | ||
|
||
axios(options).then((response) => { | ||
let byteArray = new Array(0) | ||
protobuf.util.base64.decode(response.data, byteArray, 0) | ||
|
||
resolve(responsePB.decode(byteArray)) | ||
}).catch(err => { | ||
reject(err); | ||
}) | ||
}) | ||
} | ||
|
||
class EggIncApi { | ||
static getCoopStatus(contract, coop) { | ||
const payload = { | ||
contractId: contract, | ||
code: coop | ||
} | ||
|
||
return ei_request( | ||
'ei/coop_status', | ||
payload, | ||
root.lookupType('CoopStatusRequestPayload'), | ||
root.lookupType('CoopStatusResponsePayload') | ||
); | ||
} | ||
|
||
static getPeriodicals() { | ||
const payload = { | ||
userId: 'EI6411720689451008', | ||
currentClientVersion: 30, | ||
rinfo: { | ||
eiUserId: 'EI6411720689451008', | ||
clientVersion: 30, | ||
version: '1.20.7', | ||
platform: 'ANDROID' | ||
} | ||
} | ||
|
||
return ei_request( | ||
'ei/get_periodicals', | ||
payload, | ||
root.lookupType('GetPeriodicalsRequestPayload'), | ||
root.lookupType('GetPeriodicalsResponsePayload') | ||
); | ||
} | ||
|
||
static getPlayerInfo(playerId) { | ||
var payload = { | ||
clientVersion: 30, | ||
platform: 2, | ||
eiUserId: playerId, | ||
username: '', | ||
rinfo: { | ||
eiUserId: playerId, | ||
clientVersion: 30, | ||
version: '1.20.7', | ||
platform: 'ANDROID' | ||
} | ||
} | ||
|
||
return ei_request( | ||
'ei/first_contact', | ||
payload, | ||
root.lookupType('FirstContactRequestPayload'), | ||
root.lookupType('FirstContactResponsePayload') | ||
); | ||
} | ||
|
||
static getPlayerInfos(playerIds) { | ||
const tasks = playerIds.map(playerId => this.getPlayerInfo(playerId)); | ||
return Promise.all(tasks); | ||
} | ||
} | ||
|
||
module.exports = EggIncApi; |
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,52 @@ | ||
const EggIncApi = require('./egg-inc-api'); | ||
|
||
// cli around the API | ||
require('yargs') | ||
.scriptName('Egg Inc API') | ||
.usage('$0 <cmd> [args]') | ||
.command('getAllActiveContracts', 'Get All Active Contracts', (yargs) => {}, (argv) => { | ||
EggIncApi.getPeriodicals().then((data) => { | ||
console.log(JSON.stringify(data.periodicals.contracts)) | ||
}) | ||
}) | ||
.command('getCoopStatus', 'Get Coop Status', (yargs) => { | ||
yargs | ||
.positional('contract', {type: 'string'}) | ||
.positional('coop', {type: 'string'}) | ||
}, (argv) => { | ||
EggIncApi.getCoopStatus(argv.contract, argv.coop).then((data) => { | ||
console.log(JSON.stringify(data.status)) | ||
}) | ||
}) | ||
.command('getPlayerInfo', 'Get Player Info', (yargs) => { | ||
yargs | ||
.positional('playerId', {type: 'string'}) | ||
}, (argv) => { | ||
EggIncApi.getPlayerInfo(argv.playerId).then((data) => { | ||
console.log(JSON.stringify(data.payload.data)) | ||
}) | ||
}) | ||
.command('getPlayerInfos', 'Get Player Infos', (yargs) => { | ||
yargs | ||
.positional('playerIds', {type: 'string'}) // comma separated list of ids | ||
}, (argv) => { | ||
EggIncApi.getPlayerInfos(argv.playerIds.split(',')).then((data) => { | ||
console.log(JSON.stringify(data)) | ||
}) | ||
}) | ||
.command('events', 'Get Current Events', (yargs) => {}, (argv) => { | ||
return EggIncApi.getPeriodicals().then((data) => { | ||
console.log(JSON.stringify(data.periodicals.events)) | ||
}) | ||
}) | ||
.help() | ||
.argv | ||
|
||
/* | ||
Usage examples: | ||
node js/egg-inc-cli.js events | ||
node js/egg-inc-cli.js getAllActiveContracts | ||
node js/egg-inc-cli.js getCoopStatus --contract new-moon --coop dmv | ||
node js/egg-inc-cli.js getPlayerInfo --playerId EI6411720689451008 | ||
node js/egg-inc-cli.js getPlayerInfos --playerIds EI6411720689451008,EI6411720689451008 | ||
*/ |
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,91 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const createError = require('http-errors'); | ||
|
||
require('dotenv').config(); | ||
|
||
const EggIncApi = require('./egg-inc-api'); | ||
|
||
// web server around the API | ||
// setup express | ||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
// define end points | ||
app.get('/Periodicals', (req, res, next) => { | ||
EggIncApi.getPeriodicals().then((data) => { | ||
res.send(data.periodicals); | ||
}).catch((error) => { | ||
console.error(error); | ||
return next(new createError.InternalServerError(error)); | ||
}); | ||
}); | ||
|
||
app.get('/getCoopStatus', (req, res, next) => { | ||
EggIncApi.getCoopStatus(req.query.contract, req.query.coop).then((data) => { | ||
res.send(data.status); | ||
}).catch((error) => { | ||
console.error(error); | ||
return next(new createError.InternalServerError(error)); | ||
}); | ||
}); | ||
|
||
app.get('/getPlayerInfo', (req, res, next) => { | ||
EggIncApi.getPlayerInfo(req.query.playerId).then((data) => { | ||
res.send(data.payload.data); | ||
}).catch((error) => { | ||
console.error(error); | ||
return next(new createError.InternalServerError(error)); | ||
}); | ||
}); | ||
|
||
app.get('/getPlayerInfos', (req, res, next) => { | ||
// read input in various forms | ||
let ids = []; | ||
const { playerIds, playerIdsJoined} = req.query; | ||
if (playerIds) { // individual ids | ||
if (Array.isArray(playerIds)) { | ||
ids = ids.concat(playerIds); | ||
} else { | ||
ids.push(playerIds); | ||
} | ||
} | ||
if (playerIdsJoined) { // comma separated ids | ||
ids = ids.concat(playerIdsJoined.split(',')); | ||
} | ||
// run | ||
EggIncApi.getPlayerInfos(ids).then((data) => { | ||
res.send(data); | ||
}).catch((error) => { | ||
console.error(error); | ||
//return next(error); | ||
return next(new createError.InternalServerError(error)); | ||
}); | ||
}); | ||
|
||
app.get('/notFound', (/*req, res, next*/) => { | ||
throw new createError.NotFound(); | ||
}); | ||
|
||
(async function () { | ||
console.log('Starting!'); | ||
|
||
// start web server | ||
app.listen(process.env.EI_API_PORT, () => { | ||
console.log(`App started, port: ${process.env.EI_API_PORT}, running as: ${process.env.NODE_ENV}`); | ||
}); | ||
})() | ||
|
||
module.exports = app; | ||
|
||
/* | ||
Usage examples: | ||
npm run start-ei-web | ||
http://localhost:6001/Periodicals | ||
http://localhost:6001/getCoopStatus?contract=new-moon&coop=dmv | ||
http://localhost:6001/getPlayerInfo?playerId=EI6411720689451008 | ||
http://localhost:6001/getPlayerInfos?playerIdsJoined=EI6411720689451008,EI6411720689451008 | ||
http://localhost:6001/getPlayerInfos?playerIds[]=EI6411720689451008&playerIds[]=EI6411720689451008 | ||
http://localhost:6001/getPlayerInfos?playerIdsJoined=EI5426893308821504,EI6006465308917760,EI6622592762380288,EI6243749694275584,EI6670048183189504,EI5293412581900288,EI5889385330900992,EI4950801673355264 | ||
*/ |
Oops, something went wrong.