-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
208 lines (162 loc) · 6.08 KB
/
main.py
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
import os
import users
import keys
import db
from user_manager import UserManager
from key_manager import KeysManager
from constants import API, VERSION, DATABASE
print(" " * 2 + "-" * 72 + " " * 2)
print("OpenSkyCity Users and Keys Manager (Console Edition)".center(78))
print(" " * 2 + "-" * 72 + " " * 2)
print(f"Version: {VERSION}")
print(f"Database: {DATABASE}")
print(f"API: {API}")
print(" " * 2 + "-" * 72 + " " * 2)
dm = db.DatabaseManager.from_file("keys.db")
dm.generate_tables()
um = UserManager()
km = KeysManager()
dev_mode = False
dev = False
logged_in = False
def print_menu():
print("Main Menu")
print("1. Register")
print(f"2. {'Login' if not logged_in else 'Logout'}")
if logged_in:
print("3. Buy a key")
print("4. List all owned keys")
print("5. Sell a key")
print("6. Print your account details")
if dev:
print("7. List all users")
print("8. List all keys")
print("9. Print info about a specific user")
print("*: Exit")
print("0: Print this menu")
def save():
um._save()
km._save()
print_menu()
menu_option = ""
try:
while menu_option != "*":
logged_in = bool(um.logged_user)
logged_user = um.logged_user
if logged_user:
dev = logged_user.type == users.UserType.DEVELOPER or dev_mode
save()
menu_option = input(f"{('(DEV CONSOLE) ' if dev else '')}Enter an option: ")
if menu_option == "*":
continue
if menu_option == "DEV ACTIVATE":
dev_mode = True
print("Dev Mode activated, GG!")
continue
if menu_option != "*":
try:
if not logged_in and int(menu_option) > 2:
print("This option does not exist")
continue
if not dev and logged_in and int(menu_option) > 6:
print("This option does not exist")
continue
if dev and int(menu_option) > 9:
print("This option does not exist")
continue
except Exception as e:
print(e)
continue
match menu_option:
case "1":
print("Register")
name = input("Name: ")
password = input("Password: ")
um.create_user(name, users.UserType.BASIC, password)
print("User created successfully!")
case "2":
if logged_in:
if um.logout():
print("Logged out successfully")
else:
print("Failed to logout... are you sure you logged in?")
continue
print("Login")
name = input("Name: ")
pw = input("Password: ")
if um.login(name, pw):
print("Logged in successfully!")
else:
print("Failed to login, check the credentials!")
case "3":
print("Buy a key")
if not logged_user:
print("You are not logged in!")
continue
print("Key Menu")
print("1. Premium")
print("2. Special Sandbox")
key_type = keys.KeyType(
int(input("Which key would you like to buy?: "))
)
key = km.create_key(key_type)
print(f"The alotted key: {key}")
logged_user.add_key(key)
case "4":
print("Listing all owned keys...")
if not logged_user:
print("Please login to view your owned keys")
continue
for key in logged_user.keys:
print(key.value, key.type)
case "5":
if not logged_user:
print("You cannot sell a key if you are not logged in.")
continue
for idx, key in enumerate(logged_user.keys, 1):
print(str(idx) + ".", key.value)
sell_option = input("Enter the option which you want to sell: ")
if int(sell_option) > len(logged_user.keys):
print("The key you want to sell does not exist.")
continue
key = logged_user.keys[int(sell_option)-1]
confirm = input("Are you sure you want to sell this key?: ")
if not confirm.lower().startswith("y"):
print("Cancelled the action!")
continue
logged_user.remove_key(key)
print("Key sold successfully! You won't be able to buy this key again!")
case "6":
if not logged_user:
print("This should not happen, crashing!")
continue
print("Printing your account details")
print(logged_user.name, logged_user.type)
case "7":
print("Listing all users...")
from pprint import pprint
all_users = um.list_all_users()
pprint([user.to_tuple() for user in all_users])
case "8":
print("Listing all keys...")
from pprint import pprint
all_keys = km.list_all_keys()
pprint([key.to_tuple() for key in all_keys])
case "9":
from pprint import pprint
print("Printing user details...")
name = input("Enter the user name: ")
pprint(um.get_user(name))
case "0":
print_menu()
case _:
print("Not implemented!")
except Exception as e:
import traceback
traceback.print_exception(e)
os.system("pause")
quit()
print("Saving all changes")
save()
print("Thank you for using the system!")
os.system("pause")