5m4ck3r commited on
Commit
4982eb6
1 Parent(s): 4c560e4

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +51 -6
server.py CHANGED
@@ -30,8 +30,37 @@ async def broadcast_message(party_id, sender, message: str):
30
  for user_name in parties[party_id]['members']:
31
  user = users[user_name]
32
  if user != sender:
 
33
  await user.websocket.send_text(formatted_message)
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  @app.websocket("/ws")
36
  async def party_server(websocket: WebSocket):
37
  await websocket.accept()
@@ -43,17 +72,19 @@ async def party_server(websocket: WebSocket):
43
  await websocket.send_text("[system]:[001]:ENTER DATA:")
44
  data = await websocket.receive_text()
45
  data = data.lower()
46
- action = data.split("|")[0] # Get the action
47
 
48
  if not action.lower() in ["create", "join"]:
49
  await websocket.send_text("[system]:[004]:Invalid action")
50
  await websocket.close(1000, reason="Invalid action")
51
  return
 
52
  if action.lower() == "create":
53
  if data.count("|") != 5:
54
  await websocket.send_text("[system]:[004]:Invalid input data")
55
  await websocket.close(1000, "Invalid input data")
56
  return
 
57
  else:
58
  if data.count("|") != 3:
59
  await websocket.send_text("[system]:[004]:Invalid input data")
@@ -68,7 +99,8 @@ async def party_server(websocket: WebSocket):
68
  username = data.split("|")[-2]
69
  device_id = data.split("|")[-1]
70
 
71
- users[username] = User(username, websocket, "user")
 
72
 
73
  if action == "create":
74
  link = data.split("|")[2]
@@ -82,24 +114,27 @@ async def party_server(websocket: WebSocket):
82
  party_code = str(party_code)
83
  parties[party_code] = {
84
  'owner': username,
85
- 'members': {username},
86
  'password' : password,
87
  'title' : name,
88
  'link' : link,
89
  'type' : 'Private'
90
  }
 
91
  else:
92
  party_code = generate_party_code()
93
  parties[party_code] = {
94
  'owner': username,
95
- 'members': {username},
96
  'password' : None,
97
  'title' : name,
98
  'link' : link,
99
  'type' : 'Public'
100
  }
 
101
  users[username].status = "owner"
102
  users[username].device_id = device_id
 
103
  await websocket.send_text(f"[system]:[006]:Party created! Your party code is :{party_code}.")
104
  await broadcast_message(party_code, users[username], f'[system]::{username} has created the party.')
105
 
@@ -116,12 +151,22 @@ async def party_server(websocket: WebSocket):
116
  await websocket.send_text(f"[system]:[004]: Invalid password")
117
  await websocket.close(1000, "Invalid password")
118
  return
119
- parties[party_code]['members'].add(username)
120
- users[username].status = "user"
 
 
 
 
 
 
 
121
  users[username].device_id = device_id
 
 
122
  await websocket.send_text(f"[system]:[006]:You have joined the party with code {party_code}.")
123
  await websocket.send_text(f"[info]:[006]:{parties[party_code]['title']}|{parties[party_code]['link']}|{len(parties[party_code]['members'])}")
124
  await broadcast_message(party_code, users[username], f'[006]:{username} has joined the party.')
 
125
  else:
126
  await websocket.send_text("[system]:[004]:Invalid party code.")
127
  await websocket.close(code=1000, reason="Invalid party code")
 
30
  for user_name in parties[party_id]['members']:
31
  user = users[user_name]
32
  if user != sender:
33
+ # if user.websocket:
34
  await user.websocket.send_text(formatted_message)
35
 
36
+ @app.get("/")
37
+ async def root_data():
38
+ return {
39
+ "status" : True,
40
+ "message" : "Working"
41
+ }
42
+
43
+ @app.get("/newroom")
44
+ async def create_new_room(status: str, link: str, name: str, username: str, device_id: str, password: str=None):
45
+ party_code = generate_party_code()
46
+ parties[party_code] = {
47
+ 'owner': username,
48
+ 'members': [],
49
+ 'password' : password,
50
+ 'title' : name,
51
+ 'link' : link,
52
+ 'type' : 'Private'
53
+ }
54
+
55
+ users[username] = User(username.lower(), None, "owner")
56
+ users[username].device_id = device_id
57
+
58
+ return {
59
+ "status" : True,
60
+ "party" : party_code,
61
+ "message" : "Created success"
62
+ }
63
+
64
  @app.websocket("/ws")
65
  async def party_server(websocket: WebSocket):
66
  await websocket.accept()
 
72
  await websocket.send_text("[system]:[001]:ENTER DATA:")
73
  data = await websocket.receive_text()
74
  data = data.lower()
75
+ action = data.split("|")[0]
76
 
77
  if not action.lower() in ["create", "join"]:
78
  await websocket.send_text("[system]:[004]:Invalid action")
79
  await websocket.close(1000, reason="Invalid action")
80
  return
81
+
82
  if action.lower() == "create":
83
  if data.count("|") != 5:
84
  await websocket.send_text("[system]:[004]:Invalid input data")
85
  await websocket.close(1000, "Invalid input data")
86
  return
87
+
88
  else:
89
  if data.count("|") != 3:
90
  await websocket.send_text("[system]:[004]:Invalid input data")
 
99
  username = data.split("|")[-2]
100
  device_id = data.split("|")[-1]
101
 
102
+ if not username in users:
103
+ users[username] = User(username, websocket, "user")
104
 
105
  if action == "create":
106
  link = data.split("|")[2]
 
114
  party_code = str(party_code)
115
  parties[party_code] = {
116
  'owner': username,
117
+ 'members': [username],
118
  'password' : password,
119
  'title' : name,
120
  'link' : link,
121
  'type' : 'Private'
122
  }
123
+
124
  else:
125
  party_code = generate_party_code()
126
  parties[party_code] = {
127
  'owner': username,
128
+ 'members': [username],
129
  'password' : None,
130
  'title' : name,
131
  'link' : link,
132
  'type' : 'Public'
133
  }
134
+
135
  users[username].status = "owner"
136
  users[username].device_id = device_id
137
+ users[username].websocket = websocket
138
  await websocket.send_text(f"[system]:[006]:Party created! Your party code is :{party_code}.")
139
  await broadcast_message(party_code, users[username], f'[system]::{username} has created the party.')
140
 
 
151
  await websocket.send_text(f"[system]:[004]: Invalid password")
152
  await websocket.close(1000, "Invalid password")
153
  return
154
+ # if device_id == parties[party_code]['owner']
155
+ if device_id == users[parties[party_code]['owner']].device_id:
156
+ parties[party_code]['owner'] = username
157
+ users[username].status = "owner"
158
+
159
+ else:
160
+ users[username].status = "user"
161
+
162
+ parties[party_code]['members'].append(username)
163
  users[username].device_id = device_id
164
+ users[username].websocket = websocket
165
+
166
  await websocket.send_text(f"[system]:[006]:You have joined the party with code {party_code}.")
167
  await websocket.send_text(f"[info]:[006]:{parties[party_code]['title']}|{parties[party_code]['link']}|{len(parties[party_code]['members'])}")
168
  await broadcast_message(party_code, users[username], f'[006]:{username} has joined the party.')
169
+ print(f"Sending brodcast message to {party_code}, {username}")
170
  else:
171
  await websocket.send_text("[system]:[004]:Invalid party code.")
172
  await websocket.close(code=1000, reason="Invalid party code")