-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
100 lines (86 loc) · 2.66 KB
/
index.ts
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
// provide a cli-style interface
// 1. login 2. info 3. exit
// login: ask for username and password => print welcome message => print "press any key to continue" => back to the main menu
// info: print "This is the info command" => print "press any key to continue" => back to the main menu
// exit: print "Goodbye!" => close the interface
// every time the user enter without words, flush the input and ask again
// when type password in the terminal, it should be hidden
console.log("Hello via Bun!");
import { question } from "./utils";
import { updateUser, getUser } from "./sqlite";
// console.log(db.query("select 'Hello world' as message;").get()); // => { message: "Hello world" }
updateUser({ name: "aaa" });
// console.log(db.query("select * from users limit 1;").get());
let exitFlag = false;
const login = async () => {
const url = new URL("https://spt-games-split.zeabur.app/api/v1/users");
console.log(`You are about to login to ${url}`);
const name = await question("Enter name: ");
const password = await question("Enter password: ", { hidden: true });
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, password }),
});
if (response.ok) {
const { token } = await response.json();
updateUser({ name, token, is_login: 1 });
} else {
console.error(`${response.status} ${response.statusText}`);
try {
const { error } = await response.json();
console.error(error);
} catch (error) {
console.error("No additional information available");
}
}
};
// const pressAnyKeyToContinue = () => {
// console.log("Press any key to continue...");
// console.error("(currently not working)");
// console.log();
// };
const info = () => {
console.log("This is the info command");
};
const exit = () => {
console.log("Goodbye!");
// break the loop
exitFlag = true;
};
const main = async () => {
const guestPrompt = "1. login 2. info 3. exit\n";
const userPrompt = "1. logout 2. info 3. exit\n";
let count = 0;
while (count < 10) {
const { is_login } = getUser();
let input;
if (is_login) {
console.log("You are logged in");
input = await question(`${userPrompt}Enter your choice: `);
} else {
input = await question(`${guestPrompt}Enter your choice: `);
}
switch (input) {
case "1":
console.clear();
await login();
break;
case "2":
console.clear();
info();
break;
case "3":
exit();
break;
default:
console.clear();
break;
}
if (exitFlag) {
break;
}
count++;
}
};
main();