-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrecord.js
47 lines (47 loc) · 1.37 KB
/
record.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict"
const fs = require("fs")
const https = require("https")
const pb = require("protobufjs")
const root = pb.Root.fromJSON(require("./liqi.json"))
const wrapper = root.lookupType("Wrapper")
const parse = (data)=>{
try {
let GameDetailRecords = root.lookupType("GameDetailRecords").decode(wrapper.decode(data).data)
GameDetailRecords.records.forEach((value, index)=>{
let data = wrapper.decode(value)
GameDetailRecords.records[index] = {
"name": data.name.substr(4),
"data": root.lookupType(data.name).decode(data.data)
}
})
return GameDetailRecords.records
} catch(e) {
return {"error": "parse error"}
}
}
const parseFile = (filepath)=>{
return parse(fs.readFileSync(filepath))
}
const parseByUrl = (url, cb, option = {})=>{
https.get(url, option, (res)=>{
let raw = Buffer.from([])
res.on("data", (data)=>{
raw = Buffer.concat([raw, Buffer.from(data)])
})
res.on("end", ()=>{
cb(parse(raw))
})
}).on("error", err=>{
cb({"error": "connection error"})
})
}
const parseById = (id, cb, option = {})=>{
let url = "https://record-v2.maj-soul.com:5333/majsoul/game_record/"+id
parseByUrl(url, cb, option)
}
module.exports = {
parse,
parseFile,
parseByUrl,
parseById
}