-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (290 loc) · 7.74 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
const {
getPHPSESSID,
login,
selectClass,
selectSeat,
checkSeatNum,
getSetInfo,
getRoomInfo,
checkSeat,
SignOutSeat
} = require('./api')
class Run {
constructor({
PHPSESSID = '',
username = '',
passwd = '',
roomId = '',
seatNum = ''
}) {
this.PHPSESSID = PHPSESSID,
this.username = username,
this.passwd = passwd,
this.roomId = roomId - 1,
this.seatNum = String(seatNum)
// 其他属性
this.roomState = []
this.trueNum = null
this.SateInfo = []
this.notWrite = false
this.run()
}
async run() {
this.PHPSESSID = await this._getCookie()
if (await this._login() !== 'success') {
console.log('登陆失败')
return
}
let state = await this._checkSeat()
state = JSON.parse(state)
if (!(state.label === "fail")) {
console.log('你已经处于约座状态了')
console.log(state)
return state
}
let roomState = await this._roomState()
roomState = JSON.parse(roomState)
await roomState.rows.forEach(item => {
this.roomState.push({
id: item.id,
open_status: item.open_status,
bespeak_status: item.bespeak_status
})
});
while (this.roomState[Number(this.roomId)].open_status !== '1' &&
this.roomState[Number(this.roomId)].bespeak_status !== '1' &&
this.roomState.length > 0) {
console.log(`当前房间号${this.roomId}`)
console.log('你选择的房间关闭, 我们将为你更换可用房间')
this.roomState.splice(Number(this.roomId), 1)
let roomId = await this._selectCanUseRoom()
if (roomId === 'null') {
console.log('所有房间都是关闭的')
return
}
}
console.log(`当前房间号${this.roomId}`)
await this._selectRoom(this.roomId + 1)
while (await this._checkCanUseRoomNum(this) <= 0 && this.roomState.length >= 0) {
console.log('座位已经被选完了, 我将为你在换换房间')
await this._selectCanUseRoom(this.roomId)
if (this.roomState.length <= 0) {
console.log('没有可以使用的房间了')
return
}
await this._selectRoom(this.roomState[this.roomId].id)
}
console.log('有多的位置, 那是你的')
let SateInfo = await this._getSateInfo()
SateInfo = JSON.parse(SateInfo)
this.trueNum = SateInfo.total
if (!(this.seatNum > SateInfo.rows.length)) {
if (this.checkState(SateInfo.rows[Number(this.seatNum)])) {
let result = await this._selectSeat()
let flag = await this.stateResultCheck(result)
if (flag === 0 || flag === 1) return
}
}
console.log('多于的位置被占了或者不能用')
await this._deaWithState(SateInfo.rows)
if (this.SateInfo.length <= 0) {
console.log('该教室现在不能约座')
return
}
console.log('位置已经被约了, 好气哦,那我将并发去约其他座位了')
let flagArray = await this._randomTen()
let stateResult = await this._concurrency(flagArray)
let i = 1
console.log(flagArray.length)
if (flagArray.length < 10) {
console.log(`第${flagArray.length}请求尝试`)
} else {
console.log(`第${i * 10}请求尝试`)
}
while (stateResult && this.SateInfo.length >= 10) {
i++
console.log(`第${i * 10}请求尝试`)
flagArray = await this._randomTen()
stateResult = await this._concurrency(flagArray)
}
console.log('完成')
}
_randomTen() {
let flag = []
let flagArray = []
if (this.SateInfo.length < 10) {
flagArray = new Array(this.SateInfo.length).fill(0)
} else {
flagArray =new Array(10).fill(0)
}
flagArray.forEach(() => {
let less = this.SateInfo.splice((Math.floor(Math.random() * this.SateInfo.length)), 1)[0]
flag.push(less)
})
return flag
}
async _concurrency(array) {
let flag = false
const promises = array.map((item) => {
return this._selectSeat(item.seat_sn)
})
await Promise.all(promises).then(async (result) => {
let flagArray = []
await result.forEach(async (item) => {
flagArray.push(await this.stateResultCheck(item))
})
if (flagArray.every((item) => item === '2')) {
flag = true
}
})
return flag
}
stateResultCheck(result) {
result = JSON.parse(result)
if (result.label === 'bespeak_success') {
console.log('约座成功')
return 1
} else if (result.label === 'overtime' ||
result.label === 'all_bespeak_stop' ||
result.label === 'violations_times' ||
result.label === 'room_close' ||
result.label === 'bespeak_close' ||
result.label === '') {
console.log(`${result.label} 现在不能约座`)
return 0
} else if (result.label === 'already_bespeaked') {
console.log('已经预约过了')
return 0
} else if (result.label === 'seat_used') {
console.log('座位被占了, 我帮你约约其他的')
return 2
}
}
checkState(less) {
let {
use_status,
bespeak0,
bespeak1,
bespeak2,
bespeak3,
bespeak4,
bespeak5,
bespeak6
} = less
if (!Number(use_status) && !Number(bespeak0) &&
!Number(bespeak1) && !Number(bespeak2) &&
!Number(bespeak3) && !Number(bespeak4) &&
!Number(bespeak5) && !Number(bespeak6)) {
return true
}
return false
}
async _deaWithState(less) {
less.forEach((item, i) => {
if (i > this.trueNum) return
if (this.checkState(item)) {
this.SateInfo.push(item)
}
})
console.log(this.trueNum)
console.log(this.SateInfo.length)
}
async _checkCanUseRoomNum(that) {
let checkRoomNum = await that._checkRoomNum()
checkRoomNum = JSON.parse(checkRoomNum)
return parseInt(checkRoomNum.allNoNum)
}
_selectCanUseRoom(u) {
if (u || u === 0) this.roomState.splice(u, 1)
let remove = []
this.roomState.forEach((item, i) => {
if (item.open_status !== '1' || item.bespeak_status !== '1') {
remove.push(i)
}
})
let flag = remove.sort((x, y) => y - x).shift()
while (flag) {
this.roomState.splice(flag, 1)
flag = remove.shift()
}
if (this.roomState.length <= 0) return 'null'
this.roomId = Math.floor(Math.random() * this.roomState.length)
return 1
}
// 获取Cookie
async _getCookie() {
return await getPHPSESSID()
}
// 登陆
async _login() {
const {
username,
passwd
} = this
return await login({
username,
passwd,
PHPSESSID: this.PHPSESSID
})
}
// 获取房间状态
async _roomState() {
return await getRoomInfo({
PHPSESSID: this.PHPSESSID
})
}
// 选择房间
async _selectRoom(roomId) {
if (!roomId) roomId = this.roomId
return await selectClass({
roomId,
PHPSESSID: this.PHPSESSID
})
}
// 查看房间座位可用数量
async _checkRoomNum() {
return await checkSeatNum({
PHPSESSID: this.PHPSESSID
})
}
// 获取房间座位的状态和数目
async _getSateInfo() {
return await getSetInfo({
PHPSESSID: this.PHPSESSID
})
}
// 选择座位
async _selectSeat(seatNum) {
if (!seatNum) seatNum = this.seatNum
return await selectSeat({
seatNum,
PHPSESSID: this.PHPSESSID
})
}
// 查看选择座位后的情况
async _checkSeat() {
return await checkSeat({
PHPSESSID: this.PHPSESSID
})
}
// 登出
async _signOutSeat() {
return await SignOutSeat({
PHPSESSID: this.PHPSESSID
})
}
}
let argv = process.argv
argv.shift()
argv.shift()
if (argv.length < 4) {
console.log('参数不够')
process.exit(1)
}
new Run({
PHPSESSID: '',
username: argv[0],
passwd: argv[1],
roomId: argv[2],
seatNum: argv[3]
})