-
Notifications
You must be signed in to change notification settings - Fork 0
/
serwer.py
101 lines (89 loc) · 2.33 KB
/
serwer.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
import socket
import json
iid_tasks = 0
class Task: #klasa zadan
iid = iid_tasks
priority = 0
name = "Nowe zadanie"
def __init__(self, newPriority, newName):
self.priority = newPriority
self.name = newName
self.iid = iid_tasks + 1
Tasks = [] # tablica zadan
Tasks = list(map(Task, Tasks)) #mapowanie tablicy
def SaveToFile():
with open('TODO.json', 'w+') as file:
for i in range(len(Tasks)):
json.dump({'\nID zadania: ': Tasks[i].iid, '\nZadanie: ': Tasks[i].name, '\nPriorytet: ': Tasks[i].priority}, file)
return
def ShowTasks():
print("Twoje zadania do wykonania: \n")
for i in range(len(Tasks)):
print("\n" + str(Tasks[i].iid))
print("\n" + Tasks[i].name)
print("\n" + "Priorytet: " + str(Tasks[i].priority))
SaveToFile()
return
def AddNewTask():
mes = klient.recv(1028).decode()
tab = mes.split("/")
newName = tab[0]
newPriority = int(tab[1])
NewTask = Task(newPriority, newName)
Tasks.append(NewTask)
print("Dodano nowe zadanie")
klient.send("Dodano nowe zadanie".encode())
SaveToFile()
return
def ShowTasksWithPriority():
choice_tmp = klient.recv(2).decode()
choice = int(choice_tmp)
counter = 0
for i in range(len(Tasks)):
if choice == Tasks[i].priority:
print("\nID: " + str(Tasks[i].iid))
print("\n" + Tasks[i].name)
counter = counter + 1
if counter == 0:
print("Nie ma zadania o danym priorytecie")
return
def DeleteTask():
id_tmp = klient.recv(2).decode()
id = int(id_tmp)
counter = 0
for i in range(len(Tasks)):
if id == Tasks[i].iid:
Tasks.remove(Tasks[i])
counter = counter + 1
if counter == 0:
print("Nie ma takiego zadania")
SaveToFile()
return
def Fun(wybor_user):
if wybor_user == 1:
ShowTasks()
elif wybor_user == 2:
ShowTasksWithPriority()
elif wybor_user == 3:
AddNewTask()
elif wybor_user == 4:
DeleteTask()
else:
klient.send("0").encode()
pass
serwer = socket.socket()
host = socket.gethostname()
port = 8888
serwer.bind((host, port))
serwer.listen(20)
klient, adres = serwer.accept()
print("Jest polaczenie", adres)
make = "1"
klient.send("1".encode())
while (make == "1"):
klient.send("\nWitaj w aplikacji do tworzenia listy TODO".encode())
wybor = klient.recv(2).decode()
choice = int(wybor)
Fun(choice)
make = klient.recv(2).decode()
klient.close()