Skip to content

Instantly share code, notes, and snippets.

@Nishisonic
Created May 15, 2019 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nishisonic/78dd1421a8de5ad517f2f01934a837ff to your computer and use it in GitHub Desktop.
Save Nishisonic/78dd1421a8de5ad517f2f01934a837ff to your computer and use it in GitHub Desktop.
// Loading of general data/functions.
const fs = require('fs'),
{
Client
} = require('pg')
const semver = require('semver')
global.currentDir = __dirname
const FILE_NAME = 'aaci_tsun.csv'
if (!fs.existsSync(`${global.currentDir}/config/dblogin.json`)) {
console.error(`Missing database login information, 'config/dblogin.json' doesn't exist!
Set contents to:
{
"user": "xxx",
"host": "xxx",
"database": "xxx",
"password": "xxx",
"port": 1234
}`)
return
}
global.dblogin = require(`${global.currentDir}/config/dblogin.json`)
global.idtobasename = require(`${global.currentDir}/config/idtobasename.json`)
global.historicalFleets = require(`${global.currentDir}/config/historicals.json`)
const client = new Client(dblogin)
client.connect()
client.query(`SELECT * FROM aaci WHERE badaaci = false AND (ship->>'damage')::int > 1 ORDER BY id`, (err, data) => {
if (err) {
console.log(err)
client.end()
return
}
const entries = data.rows
const accumulated = entries.filter(element => semver.gte(element.version.replace(/[a-zA-Z]/g, ''), '32.5.0')).reduce((accumulator, element, _index) => {
const currentValue = accumulator[element.shippossibleaaci]
if (currentValue === undefined) {
accumulator[element.shippossibleaaci] = [...new Array(element.shippossibleaaci.length + 2)].fill(0)
}
const index = element.shippossibleaaci.indexOf(element.triggeredaaci)
if (index >= 0) {
accumulator[element.shippossibleaaci][index + 2] += 1
} else {
accumulator[element.shippossibleaaci][1] += 1
}
accumulator[element.shippossibleaaci][0] += 1
if (_index % 10000 === 0) {
console.log(`${_index}行終了`)
console.log(accumulator)
}
return accumulator
}, {})
console.log('処理終了')
console.log(accumulated)
const maxLength = Math.max(...Object.values(accumulated).map(v => v.length))
write('可能種別,総回数', true)
for (let i = 0; i < maxLength - 2; i++) {
write(`,種別${i + 1},種別${i + 1}回数,種別${i + 1}発動率`)
}
writeln()
Object.keys(accumulated).sort((a, b) => {
const aa = isNaN(a) ? a.split(",") : [a]
const bb = isNaN(b) ? b.split(",") : [b]
const aaa = aa.map((v, i) => v * Math.pow(100, maxLength - i)).reduce((p, c) => p + c, 0)
const bbb = bb.map((v, i) => v * Math.pow(100, maxLength - i)).reduce((p, c) => p + c, 0)
return aaa > bbb ? 1 : aaa < bbb ? -1 : 0
}).forEach(key => {
const total = accumulated[key][0]
write(`"[${key}]",${total}`)
const keys = isNaN(key) ? key.split(",") : [key]
accumulated[key].filter((_, i) => i > 1).forEach((v, i) => {
write(`,${keys[i]}種,${v},${(v / total * 100).toFixed(1)}%`)
})
writeln()
})
client.end()
})
function write(str = "", newFile = false) {
if (newFile) {
fs.writeFileSync(`${global.currentDir}/${FILE_NAME}`, str)
} else {
fs.appendFileSync(`${global.currentDir}/${FILE_NAME}`, str)
}
}
function writeln(str = "") {
write(`${str}\n`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment