Skip to content

Instantly share code, notes, and snippets.

@Javran
Last active February 12, 2018 16:38
Show Gist options
  • Save Javran/cc670caac1d6ca3b9ab507efd19bf2ef to your computer and use it in GitHub Desktop.
Save Javran/cc670caac1d6ca3b9ab507efd19bf2ef to your computer and use it in GitHub Desktop.
update_server_list
/*
Deps:
- npm i lodash request request-promise-native fs-extra localeval
Usage:
- cd </path/to/poi/repo>
- node update_server_list.js assets/data/server.json
- server.json should now be updated, use "git diff" to verify differences if any.
*/
const _ = require('lodash')
const request = require('request-promise-native')
const localeval = require('localeval')
const { readJsonSync, writeJsonSync } = require('fs-extra')
const [_ignore1, _ignore2, serverJsonPath] = process.argv
if (!serverJsonPath) {
console.error(`node <script> <server.json>`)
process.exit(1)
}
const serverJson = readJsonSync(serverJsonPath)
request(
'http://203.104.209.7/gadget/js/kcs_const.js'
).then(data => {
let updated = false
const sandbox = {}
const raw = localeval(data, sandbox)
const indexedServerJson = _.keyBy(_.toPairs(serverJson), x => x[1].num)
_.entries(sandbox.ConstServerInfo).map(([serverKey, serverIpRaw]) => {
const reResult = /^World_(\d+)$/.exec(serverKey)
if (!reResult)
return
const [_ignore3, serverNumStr] = reResult
const [_ignore4, serverIp] = /^http:\/\/((?:\d|\.)+)\/$/.exec(serverIpRaw)
const oldServerIp = indexedServerJson[serverNumStr][0]
if (oldServerIp !== serverIp) {
indexedServerJson[serverNumStr][0] = serverIp
updated = true
console.log(`Server #${serverNumStr}: ${oldServerIp} => ${serverIp}`)
}
})
if (updated) {
const reconstructed =
_.fromPairs(
// sort then reconstruct, in hope that this ends up
// enumerating properties in the expected order.
_.toPairs(indexedServerJson).sort(
(x,y) => Number(x[0]) - Number(y[0])
).map(x => x[1])
)
writeJsonSync(serverJsonPath, reconstructed, {spaces: '\t'})
console.log(`Updated ${serverJsonPath}`)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment