This repository was archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (68 loc) · 1.95 KB
/
Copy pathindex.js
File metadata and controls
81 lines (68 loc) · 1.95 KB
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
const User = require('./lib/user')
const Library = require('./lib/library')
const inquirer = require('inquirer')
async function importConfig() {
const { existsSync, readFileSync } = require('fs')
let info
if (existsSync('./config.json')) {
info = JSON.parse(readFileSync('./config.json', 'utf8'))
console.log('Detected configuration file')
} else {
info = await inquirer.prompt([
{
name: 'username',
message: 'Username:',
type: 'password'
},
{
name: 'password',
message: 'Password:',
type: 'password'
}
])
}
return info
}
async function main() {
try {
let user = new User(await importConfig())
let loginResult = await user.login()
if (loginResult.success) {
console.log('Login success')
console.log('Welcome!', user.name)
} else {
console.log(`Login failed (reason: ${loginResult.serverMessage})`)
return
}
let library = new Library()
let floor = await library.queryFloor()
let { selectedArea } = await inquirer.prompt({
type: 'list',
name: 'selectedArea',
message: 'Select an area:',
choices: floor.areas.map(area => {
return { name: `${area.name} (${area.vacancy})`, value: area }
})
})
let area = await library.queryArea(selectedArea)
let { selectedSeat } = await inquirer.prompt({
type: 'list',
name: 'selectedSeat',
message: 'Select an seat:',
choices: area.seats.map(seat => {
return { name: `${seat.name} (${seat.statusText})`, value: seat }
})
})
let bookResult = await library.bookSeat(selectedSeat, user)
if (bookResult.success) {
console.log(`Booking success (message: ${bookResult.serverMessage})`)
} else {
console.log(`Booking failed (reason: ${bookResult.serverMessage})`)
return
}
} catch (e) {
console.log('Error:', e.message)
console.log('Call stack:', e.stack)
}
}
main()