-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
195 lines (159 loc) · 6.26 KB
/
main.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
const axios = require('axios')
const fs = require('fs')
const qs = require('qs')
const { URLSearchParams } = require('url')
const users = JSON.parse(fs.readFileSync('./login.json'))
const apiUrl = 'http://smkn5tangerangkota.sch.id:5680/api'
async function start (user) {
try {
var userPayload = {
username: user.NISN,
password: user.NAMA,
imei: generateRandomNumber(100, 900)
}
const login = await axios.post(apiUrl + '/login', userPayload)
// delay 1500ms
await delay(500)
if (login.data.success == "true") {
const siswa_id = login.data.siswa.id
const getJadwal = await axios.post(apiUrl + '/main', {
siswa_id: siswa_id
})
// delay 1500ms
await delay(500)
console.log('siswa_id: ' + siswa_id)
console.log('mencoba mencari jadwal ujian...\n')
if (getJadwal.data.success == "true") {
const filterJadwal = getJadwal.data.jadwal.filter(item => item.nilai == null || item.nilai == 0)
const mulaiUjian = await axios.post(apiUrl + '/mulaiujian', {
tipe: getJadwal.data.tipe,
siswa_id: siswa_id,
jadwal_id: filterJadwal[0].id,
pelajaran_id: filterJadwal[0].pelajaran_id,
urutan: filterJadwal[0].urutan
})
// delay 1500ms
await delay(500)
console.log('tipe: ' + getJadwal.data.tipe)
console.log('jadwal_id: ' + filterJadwal[0].id)
console.log('pelajaran_id: ' + filterJadwal[0].pelajaran_id)
console.log('urutan: ' + filterJadwal[0].urutan)
console.log('mencoba mendapatkan soal....\n')
if (mulaiUjian.data.success == true) {
console.log('ujian_id: ' + mulaiUjian.data.ujian_id)
console.log('mencari soal...\n')
const getSoal = await axios.post(apiUrl + '/getsoal', {
tipe: getJadwal.data.tipe,
jadwal_detail_id: filterJadwal[0].jadwal_detail_id
})
// delay 1500ms
await delay(500)
if (getSoal.data.success == "true") {
console.log('panjang soal: '+ getSoal.data.soal.length)
console.log('mencoba mengerjakan ujian...\n')
await kirimUjian(getJadwal.data.tipe, mulaiUjian.data.ujian_id, getSoal.data.soal, filterJadwal[0].urutan, siswa_id, generateRandomNumber(90, 100))
} else {
console.log('gagal mendapatkan soal!')
console.log(filterJadwal[0])
}
} else {
console.log("error ketika mencari jadwal ujian!")
}
} else {
console.log('nothing to do!')
console.log('no task here...')
}
} else {
console.log('failed to login because this account has been logged in!')
console.log('trying to logout account....')
await logout(userPayload.username)
console.log('success, restart program!')
}
} catch (e) {
console.log(e)
console.log('failed because api host was error!')
}
}
async function kirimUjian (tipe, ujian_id, soal, urutan, siswa_id, kkm) {
var payload = {
tipe: tipe,
ujian_id: ujian_id,
jawaban_pg: []
}
let nilai = 0
for (let i = 0; i < soal.length; i++) {
payload.jawaban_pg.push({
id: soal[i].id,
kunci: 'A',
jawaban: await generateRandomAnswer()
})
}
const payloadString = new URLSearchParams(payload).toString()
console.log(payload)
await axios.post(apiUrl + '/selesaiujian', payloadString)
.then(result => console.log(result.data))
// for (let i = 0; i < soal.length; i++) {
// payload.jawaban_pg.push({
// id: soal[i].id,
// kunci: '',
// jawaban: await generateRandomAnswer()
// })
// let send = true
// while (send) {
// console.log(payload)
// // payload.jawaban_pg[i].jawaban = await generateRandomAnswer()
// const payloadString = new URLSearchParams(payload).toString()
// const post = await axios.post(apiUrl + '/selesaiujian', payloadString, {
// headers: {
// 'content-type': 'application/x-www-form-urlencoded'
// }
// })
// if (post.data.success == "true") {
// await delay(500)
// const checkNilai = await getNilai(urutan, siswa_id)
// console.log(checkNilai)
// console.log(post.data)
// if (checkNilai > nilai) {
// nilai = checkNilai
// send = false
// }
// }
// // set delay 500 every while loop
// await delay(1000)
// }
// send = true
// if (nilai >= kkm) {
// console.log(`program telah selesai dijalankan dengan nilai kkm ditetapkan menjadi ${kkm} dan nilai anda adalah ${nilai}`)
// break
// }
// // set delay 1000
// await delay(1000)
// }
//console.log(payload)
}
function generateRandomAnswer() {
const answers = ['A', 'B', 'C', 'D', 'E']
const randomIndex = Math.floor(Math.random() * answers.length)
return answers[randomIndex]
}
async function logout (username) {
await axios.post(apiUrl + '/logout', {
username: username
})
return true
}
function generateRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function delay (ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function getNilai (urutan, siswa_id) {
const nilai = await axios.post(apiUrl + '/main', {
siswa_id: siswa_id
})
return nilai.data.jadwal.filter(item => item.urutan == urutan)[0].nilai
}
start(users)