-
Notifications
You must be signed in to change notification settings - Fork 10
/
mjsoul.js
166 lines (159 loc) · 5.28 KB
/
mjsoul.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict"
const EventEmitter = require("events")
const WebSocket = require("ws")
const pb = require("protobufjs")
const crypto = require("crypto")
const msgType = {notify: 1, req: 2, res: 3}
const FastTest = [
"authGame", "broadcastInGame", "checkNetworkDelay", "confirmNewRound", "enterGame", "fetchGamePlayerState",
"finishSyncGame", "inputChiPengGang", "inputGameGMCommand", "inputOperation", "syncGame", //"terminateGame"
]
const hash = (password)=>{
return crypto.createHmac("sha256", "lailai").update(password, "utf8").digest("hex")
}
class MJSoul extends EventEmitter {
constructor(config = {}) {
super()
this.status = "closed"
this.msgIndex = 0
this.msgQueue = []
this.ws = null
this.wsOption = {}
this.service = ".lq.Lobby."
this.root = pb.Root.fromJSON(require("./liqi.json"))
this.wrapper = this.root.lookupType("Wrapper")
this.url = "wss://gateway-cdn.maj-soul.com/gateway"
this.timeout = 5000
this._onOpen = ()=>{}
for (let k in config) {
this[k] = config[k]
}
}
open(onOpen = ()=>{}) {
this.status = "connecting"
this._onOpen = onOpen
this.ws = new WebSocket(this.url, this.wsOption)
this.ws.on("open", ()=>{
this.status = "connected"
onOpen()
})
this.ws.on("message", this._onMessage.bind(this))
this.ws.on("close", ()=>{
this.status = "closed"
this.ws = null
this.msgIndex = 0
this.msgQueue = []
this.emit("close")
})
this.ws.on("error", err=>this.emit("error", err))
}
close() {
try {
this.status = "closing"
this.ws.terminate()
} catch(e) {}
}
_onMessage(data) {
if (data[0] == msgType.notify) {
data = this.wrapper.decode(data.slice(1))
try {
data.data = this.root.lookupType(data.name).decode(data.data)
} catch (e) {
return
}
data.name = data.name.substr(4)
if (data.name === "ActionPrototype") {
data.data.data = this.root.lookupType(data.data.name).decode(data.data.data)
}
this.emit(data.name, data.data)
}
if (data[0] == msgType.res) {
let index = (data[2] << 8 ) + data[1]
if (this.msgQueue[index] !== undefined) {
let dataTpye = this.root.lookupType(this.msgQueue[index].name)
this.msgQueue[index].cb(dataTpye.decode(this.wrapper.decode(data.slice(3)).data))
delete this.msgQueue[index]
}
}
}
send(name, data = {}, callback = ()=>{}) {
if (typeof data === "function") {
let tmp = callback
callback = data
data = typeof tmp === "function" ? {} : tmp
}
if (this.status !== "connected") {
if (this.status === "closed") this.open(this._onOpen)
callback({
"error": {
"code": 9999,
"message": "We are connecting to mjsoul. Please try again."
}
})
return
}
if (FastTest.includes(name))
name = ".lq.FastTest." + name
else
name = this.service + name
try {
var service = this.root.lookup(name).toJSON()
} catch(e) {
callback({
"error": {
"code": 9998,
"message": "Wrong api name."
}
})
return
}
let reqName = service.requestType
let resName = service.responseType
let reqType = this.root.lookupType(reqName)
data = {
name: name,
data: reqType.encode(reqType.create(data)).finish()
}
this.msgIndex %= 60007
this.msgQueue[this.msgIndex] = {name:resName, cb:callback}
data = Buffer.concat([Buffer.from([msgType.req, this.msgIndex - (this.msgIndex >> 8 << 8), this.msgIndex >> 8]), this.wrapper.encode(this.wrapper.create(data)).finish()])
this.ws.send(data)
this.msgIndex++
}
async sendAsync(name, data = {}) {
return new Promise((resolve, reject)=>{
let id = setTimeout(()=>{
reject({
"error": {
"code": 9997,
"message": "Timeout " + this.timeout + "ms exceeded."
}
})
}, this.timeout)
this.send(name, data, (data)=>{
clearTimeout(id)
if (data.hasOwnProperty("error"))
reject(data)
else
resolve(data)
})
})
}
hash(password) {
return hash(password)
}
}
class DHS extends MJSoul {
constructor(config = {}) {
super()
this.service = ".lq.CustomizedContestManagerApi."
this.root = pb.Root.fromJSON(require("./dhs.json"))
this.wrapper = this.root.lookupType("Wrapper")
this.url = "wss://gateway-v2.maj-soul.com:6001"
for (let k in config) {
this[k] = config[k]
}
}
}
MJSoul.DHS = DHS
module.exports = MJSoul