5m4ck3r commited on
Commit
1784957
1 Parent(s): e5489f2

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. requirements.txt +2 -0
  3. server.py +87 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ EXPOSE 5000
12
+
13
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn
server.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
3
+ import uvicorn
4
+
5
+ app = FastAPI()
6
+
7
+ parties = {}
8
+ users = {}
9
+
10
+ class User:
11
+ def __init__(self, username, websocket, status):
12
+ self.username = username
13
+ self.websocket = websocket
14
+ self.status = status
15
+
16
+ def generate_party_code():
17
+ return ''.join([str(random.randint(0, 9)) for _ in range(6)])
18
+
19
+ async def broadcast_message(party_id, sender, message):
20
+ if party_id in parties:
21
+ if message.startswith("TMPN:") and sender.status == "owner":
22
+ formatted_message = message
23
+ else:
24
+ status = 'Owner' if sender.status == 'owner' else 'User'
25
+ formatted_message = f"{sender.username}:[{status}]:{message}"
26
+
27
+ for user_name in parties[party_id]['members']:
28
+ user = users[user_name]
29
+ if user != sender:
30
+ await user.websocket.send_text(formatted_message)
31
+
32
+ @app.websocket("/ws")
33
+ async def party_server(websocket: WebSocket):
34
+ await websocket.accept()
35
+ username = None
36
+ party_code = None
37
+ user_status = None
38
+
39
+ await websocket.send_text("[system]:Enter your name to start.")
40
+ data = await websocket.receive_text()
41
+ username = data
42
+
43
+ users[username] = User(username, websocket, "user")
44
+
45
+ await websocket.send_text("[system]:Would you like to create or join a party? Type 'create' or 'join'.")
46
+ action = await websocket.receive_text()
47
+
48
+ if action == "create":
49
+ party_code = generate_party_code()
50
+ parties[party_code] = {'owner': username, 'members': {username}}
51
+ users[username].status = "owner"
52
+ await websocket.send_text(f"[system]:Party created! Your party code is {party_code}.")
53
+ await broadcast_message(party_code, users[username], f'[system]:{username} has created the party.')
54
+
55
+ elif action == "join":
56
+ await websocket.send_text("[system]:Enter the party code to join.")
57
+ party_code = await websocket.receive_text()
58
+
59
+ if party_code in parties:
60
+ parties[party_code]['members'].add(username)
61
+ users[username].status = "user"
62
+ await websocket.send_text(f"[system]:You have joined the party with code {party_code}.")
63
+ await broadcast_message(party_code, users[username], f'[system]:{username} has joined the party.')
64
+ else:
65
+ await websocket.send_text("[system]:Invalid party code.")
66
+
67
+ try:
68
+ while True:
69
+ data = await websocket.receive_text()
70
+ if party_code:
71
+ await broadcast_message(party_code, users[username], data)
72
+ except WebSocketDisconnect:
73
+ if party_code and party_code in parties:
74
+ parties[party_code]['members'].remove(username)
75
+ if parties[party_code]['owner'] == username:
76
+ await broadcast_message(party_code, users[username], f'[system]:{username} (Owner) has left the party. All members are being kicked out.')
77
+ for member in list(parties[party_code]['members']):
78
+ member_user = users[member]
79
+ await member_user.websocket.send_text("[system]:The owner has left, and the party is deleted.")
80
+ parties[party_code]['members'].remove(member)
81
+ del parties[party_code]
82
+ else:
83
+ await broadcast_message(party_code, users[username], f'[system]:{username} has left the party.')
84
+ del users[username]
85
+
86
+ if __name__ == "__main__":
87
+ uvicorn.run(app, host="0.0.0.0", port=5000)