forked from Ganapati/Simple-Game-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
31 lines (26 loc) · 883 Bytes
/
player.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
import uuid
import json
import socket
class Player:
def __init__(self, addr, udp_port):
"""
Identify a remote player
"""
self.identifier = str(uuid.uuid4())
self.addr = addr
self.udp_addr = (addr[0], int(udp_port))
def send_tcp(self, success, data, sock):
"""
Send tcp packet to player for server interaction
"""
success_string = "False"
if success:
success_string = "True"
message = json.dumps({"success": success_string, "message": data})
sock.send(message.encode())
def send_udp(self, player_identifier, message):
"""
Send udp packet to player (game logic interaction)
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(json.dumps({player_identifier: message}).encode(), self.udp_addr)