-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
218 lines (211 loc) · 6.8 KB
/
admin.py
File metadata and controls
218 lines (211 loc) · 6.8 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
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
import socket
SERVER = "127.0.0.1"
PORT = 49091
admin = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
admin.connect((SERVER, PORT))
role = 'admin'
prompt = ''
# menjelaskan cara menggunakan command-command yang ada
def hint():
print('client ——— akun client')
print('admin ———— akun admin')
print('soal ————— soal')
print('game ————— game')
print('status ——— melihat berapa admin dan client yang connect ke server')
print('hint ————— menampilkan list command')
print('bye —————— disconnect dari server')
def iudHint():
print('insert —— Menambahkan')
print('update —— Mengupdate')
print('delete —— Menghapus')
print('back ———— Kembali')
def hintGame():
print('add ———— menambahkan game')
print('start —— memulai game')
print('hint ——— menampilkan list command')
print('back ——— Kembali')
# Untuk mengetahui sebuah string apakah bisa dijadikan int atau tidak
def is_int(val):
try:
int(val)
except ValueError:
return False
return True
def iudUser(mainCommand,subcommand):
if subcommand=='hint':
iudHint()
elif subcommand=='insert'or subcommand=='update' or subcommand=='delete':
admin.sendall(mainCommand.encode('UTF-8'))
admin.sendall(subcommand.encode('UTF-8'))
if subcommand=='update' or subcommand=='delete':
uname = input('username : ')
admin.sendall(uname.encode('UTF-8'))
if subcommand=='update':
pwd = input('password : ')
admin.sendall(pwd.encode('UTF-8'))
print(mainCommand+' '+admin.recv(2222).decode('UTF-8'))
elif subcommand=='delete':
print(uname + ' ' + admin.recv(2222).decode('UTF-8'))
else:
print('Error filtering subcommand!!!')
elif subcommand=='insert':
uname = input('username : ')
pwd = input('password : ')
admin.sendall(uname.encode('UTF-8'))
admin.sendall(pwd.encode('UTF-8'))
print(admin.recv(2222).decode('UTF-8'))
elif subcommand=='back':
print('going back')
else:
print('command tidak tersedia')
def iudSoal(mainCommand,subcommand):
if subcommand=='hint':
iudHint()
elif subcommand=='insert'or subcommand=='update' or subcommand=='delete':
# admin.sendall(mainCommand.encode('UTF-8'))
# admin.sendall(subcommand.encode('UTF-8'))
if subcommand=='update' or subcommand=='delete':
no = ''
while not is_int(no):
if no!='':
print('Input harus angka')
no = input('no : ')
admin.sendall(str(no).encode('UTF-8'))
if subcommand=='update':
soal = input('soal : ')
admin.sendall(soal.encode('UTF-8'))
nilai = ''
while not is_int(nilai):
if nilai!='':
print('input harus angka!!!')
nilai = input('nilai : ')
admin.sendall(nilai.encode('UTF-8'))
A = input('A : ')
admin.sendall(A.encode('UTF-8'))
B = input('B : ')
admin.sendall(B.encode('UTF-8'))
C = input('C : ')
admin.sendall(C.encode('UTF-8'))
D = input('D : ')
admin.sendall(D.encode('UTF-8'))
kunjaw = input('kunci jawaban(A/B/C/D) : ').upper()
while kunjaw!='A' and kunjaw!='B' and kunjaw!='C' and kunjaw!='D':
print('input harus A/B/C/D')
kunjaw = input('kunci jawaban(A/B/C/D) : ').upper()
admin.sendall(kunjaw.encode('UTF-8'))
print(mainCommand+' '+admin.recv(2222).decode('UTF-8'))
elif subcommand=='delete':
print('soal nomor '+str(no)+' has been removed')
else:
print('Error filtering subcommand!!!')
elif subcommand=='insert':
soal = input('soal : ')
admin.sendall(soal.encode('UTF-8'))
nilai = input('nilai : ')
while not is_int(nilai):
if nilai!='':
print('input harus angka!!!')
nilai = input('nilai : ')
admin.sendall(nilai.encode('UTF-8'))
A = input('A : ')
admin.sendall(A.encode('UTF-8'))
B = input('B : ')
admin.sendall(B.encode('UTF-8'))
C = input('C : ')
admin.sendall(C.encode('UTF-8'))
D = input('D : ')
admin.sendall(D.encode('UTF-8'))
kunjaw = ''
while kunjaw!='A' and kunjaw!='B' and kunjaw!='C' and kunjaw!='D':
if kunjaw!='':
print('input harus A/B/C/D')
kunjaw = input('kunci jawaban(A/B/C/D) : ').upper()
admin.sendall(kunjaw.encode('UTF-8'))
print(admin.recv(2222).decode('UTF-8'))
elif subcommand=='back':
print('going back')
else:
print('command tidak tersedia')
def doCommand(command):
prompt = str(role)+'@'+str(username)+'——>'+str(command)+'——>'
if command=='hint':
hint()
elif command=='client' or command=='admin':
iudHint()
subcommand = input(prompt).lower()
prompt = str(role)+'@'+str(username)+'——>'
iudUser(command,subcommand)
# di sini beres
elif command=='game':
admin.sendall(command.encode('UTF-8'))
hintGame()
subcommand = input(prompt).lower()
admin.sendall(subcommand.encode('UTF-8'))
if subcommand=='hint':
hintGame()
elif subcommand=='add':
namaGame = input('nama game : ').lower()
admin.sendall(namaGame.encode('UTF-8'))
jmlClient = ''
while not is_int(jmlClient):
if jmlClient!='':
print('input harus angka!!!')
jmlClient = input('jumlah client : ')
admin.sendall(jmlClient.encode('UTF-8'))
jmlPertanyaan = ''
while not is_int(jmlPertanyaan):
if jmlPertanyaan!='':
print('input harus angka!!!')
jmlPertanyaan = input('jumlah pertanyaan : ')
admin.sendall(jmlPertanyaan.encode('UTF-8'))
elif subcommand=='start':
print('masukkan nama game yang ingin dimulai')
print('game yang sudah ada:')
print(admin.recv(2222).decode('UTF-8'))
namaGame = input('nama game : ')
admin.sendall(namaGame.encode('UTF-8'))
print(admin.recv(2222).decode('UTF-8'))
elif subcommand=='back':
print('going back')
elif command=='soal':
admin.sendall(command.encode('UTF-8'))
print('show —— tampilkan jumlah soal yang sudah ada')
iudHint()
subcommand = input(prompt).lower()
admin.sendall(subcommand.encode('UTF-8'))
if subcommand=='show':
# menunjukkan jumlah soal yang ada
jmlSoal = admin.recv(2222).decode('UTF-8')
print('Jumlah soal : '+jmlSoal)
semuaSoal = admin.recv(2222).decode('UTF-8')
print(semuaSoal)
else:
iudSoal(command,subcommand)
elif command=='status':
admin.sendall(command.encode('UTF-8'))
admStat = admin.recv(2222).decode('UTF-8')
print(admStat)
cliStat = admin.recv(2222).decode('UTF-8')
print(cliStat)
else:
print('command tidak tersedia')
while True:
lstatus = 'Login required'
admin.sendall(role.encode('UTF-8'))
while lstatus!='Logged in':
username = input('username : ')
admin.sendall(username.encode('UTF-8'))
password = input('password : ')
admin.sendall(password.encode('UTF-8'))
lstatus = admin.recv(2222).decode('UTF-8')
print(lstatus)
hint()
prompt = str(role)+'@'+str(username)+'——>'
command = input(prompt).lower()
while command!='bye':
doCommand(command)
command = input(prompt).lower()
break
admin.sendall('bye'.encode('UTF-8'))
print('logged out')
admin.close()