def listRooms(self): request = {"state": "main menu", "ask": "list games"} self.skt.sendto(json.dumps(request).encode(), self.server) response = self.skt.recvfrom(512)[0].decode() response = json.loads(response) if not response: logf("Client: (FAIL) not recing date.", True) else: print(" -Rooms List-") for gameName in response["value"]["result"]: print(gameName + " (?/?)") print(" - " + str(len(response["value"]["result"])) + " rooms found -") print('')
def updateFromServer(self): while len(self.serverData): response = self.serverData.pop(0) if response["state"] != "gaming": logf("Client: Locks like you are not inside a game") self.crashed = True else: value = response["value"] p = self.getPlayer(value["addr"]) if not p: logf( "Client: There is IP that is not gaming, but a recive data about he. Lets create he" ) p = Player(WHITE) p.prepare() p.pos = value["pos"]
def run(self): global requestQueue global clients global sktSend global portSend sktSend.bind((host, portSend)) while True: existsRequest.acquire() if not requestQueue: existsRequest.wait() client, request = requestQueue.pop(0) logf("Handling " + str(client.addr), True) if request["state"] == "main menu": self.mainMenuHandle(client, request) elif request["state"] == "gaming": self.gamingHandle(client, request) existsRequest.release()
def run(self): global sktRecive global host global portRecive global clients sktRecive = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sktRecive.bind((host, portRecive)) global clients global requestQueue while True: data, addr = sktRecive.recvfrom(1024) data = data.decode() logf("Server: Request Reciver: Recive new Data from " + str(addr), True) existsRequest.acquire() if not data: logf("Server: Client " + str(addr) + " is not sending information.", True) else: client = getClient(addr[0]) if not client: client = Client(addr[0], addr[1]) clients.append(client) requestQueue.append((client, json.loads(data))) logf("Server: Appended new request.", True) existsRequest.notify() existsRequest.release()
def getInGame(self): name = input("Tell me the room name (press ENTER and chose 3 to list all rooms): ") request = {"state":"main menu", "ask":"get in game", "value": {"name": name}} self.skt.sendto(json.dumps(request).encode(), self.server) response = json.loads(self.skt.recvfrom(1024)[0].decode()) logf("Client: Response recived for get in game " + str(response), True) if not response: logf("Client: (FAIL) not recing date.", True) elif response["value"]["result"] == "Fail": logf("Client: (Fail) server msg = " + str(response["value"]["reason"])) elif response["value"]["result"] == "Success": self.play(name, 0, response["value"]["id"]) else: logf("Client: Some thing wrong in getInGame", True)
def gamingHandle(self, client, request): global games logf("Server: Hendling " + str(request), True) data = request["value"] game = games.getById(data["gameId"]) p = game.getPlayer(client) value = {"addr": p.client.addr} for act in data["actions"]: if act["name"] == "exit game": game.removePlayer(client) return # other IF cases, to implement if "pos" in data: p.pos = data["pos"] value["pos"] = p.pos logf("Recive from client " + str(client.addr) + " pos " + str(data["pos"])) response = {"state": "gaming", "value": value} global sktSend for player in game.players: if player.client.addr != p.client.addr: sktSend.sentto(json.dumps(response).encode(), (player.client.addr, player.client.port))
def createGame(self): name = input("Tell me the room name: ") map = input("Tell me the map number (betwen 1 and 1): ") request = {"state": "main menu", "ask": "create game", "value": {"name": name, "map": map}} self.skt.sendto(json.dumps(request).encode(), self.server) response = json.loads(self.skt.recvfrom(1024)[0].decode()) if not response: logf("Client: (FAIL) not recing date.", True) elif response["value"]["result"] == "Fail": logf("Client: (Fail) server msg = " + str(response["value"]["reason"])) elif response["value"]["result"] == "Success": self.play(name, 0, response["value"]["id"]) else: logf("Client: Some thing wrong in createGame", True)
def mainMenuHandle(self, client, request): global games logf("Server: Request " + str(request), True) if request["ask"] == "get in game": # GET IN GAME logf("Server: User request to get in game", True) name = request["value"]["name"] logf('Server: Adding player of client ' + str(client.addr) + " to game " + name, True) player = Player(client) game = games.getByName(name) if not game: logf("Server: FAIL: There is not game with name " + name, True) response = {"state": "main menu", "value": {"result": "Fail", "reason": "Chose game dosen't exists"}} else: game.addPlayer(player) logf("Server: Sucess", True) response = {"state": "main menu", "value": {"result": "Success", "name": game.name, "map": game.map, "id": game.id}} elif request["ask"] == "create game": # CREATE GAME logf("Server: User request to create game", True) if "maxNumPlayers" in request["value"]: g = Game(request["value"]["name"], request["value"]["map"], request["value"]["maxNumPlayers"]) else: g = Game(request["value"]["name"], request["value"]["map"]) gameId = games.newGame(g) if gameId != -1: logf('Server: Created new game with id ' + str(gameId), True) logf('Server: Adding player of client ' + str(client.addr) + " to game " + str(gameId), True) player = Player(client) games.getById(gameId).addPlayer(player) response = {"state": "main menu", "value": {"result": "Success", "name": games.getById(gameId).name, "map": games.getById(gameId).map, "id": gameId}} else: response = {"state": "main menu", "value": {"result": "Fail", "reason": "Can't create game"}} elif request["ask"] == "list games": # LIST GAMES logf("Server: User request to list all games", True) response = {"state": "main menu", "value": {"result": games.listAll()}} else: # EXIT logf("Server: User exit", True) removeClient(client) return # response = {"state": "main menu", "value": {"result": "Success"}} global sktSend logf("Server: Responding client", True) sktSend.sendto(json.dumps(response).encode(), (client.addr, 5002))
from serverSide.Server import RequestHandler, RequestReciver from utils.Utils import logf rr = RequestReciver() rh = RequestHandler() logf("Start Handler", True) rh.start() logf("Start Reciver", True) rr.start() rr.join() rh.join()