def joinCharacterModal(request): if "user" in request.session: userId = request.session["user"]["localId"] if request.method == "POST": data = json.loads(request.body) print(data) characterName = data["characterName"] characterMove = data["characterMove"] print(characterName) print(characterMove) gameId = data["gameId"] users = gameServices.getProperty(gameId, "Users") if len(users.keys()) < 4: gameServices.addUserToGame(gameId, userId) gameName = gameServices.getProperty(gameId, "Name") gameServices.addCharacterUserToGame(gameId, userId, characterName, characterMove) userServices.addGameToUser(userId, gameId, gameName) isMaximumUsersReached = False else: isMaximumUsersReached = True dataResponse = {"gameId": gameId, "isMaximumUsersReached": isMaximumUsersReached} return HttpResponse(json.dumps(dataResponse), content_type="application/json; charset=utf-8") else: return HttpResponseRedirect('../') else: return HttpResponseRedirect('../')
def gameView(request, gameId): if "user" in request.session: users = gameServices.getProperty(gameId, "Users") userId = request.session["user"]["localId"] if userId in users.keys(): gameCode = gameServices.getProperty(gameId, "Code") gameName = gameServices.getProperty(gameId, "Name") chatMessages = [] if request.method == "POST" and "newMessage" in request.POST.keys(): chatMessages.append(request.POST.get("newMessage")) characters = gameServices.getProperty(gameId, "Characters") isMaster = gameServices.isUserMaster(gameId, userId) ############################ gameMap = gameServices.getProperty(gameId, "Map") maxVersion = versionServices.getLastVersion(gameMap) maxOrder = maxVersion["Order"] mapItemList = versionServices.getListsFromVersion(gameMap, maxOrder) mapName = mapServices.getProperty(gameMap, "Name") response = { 'gameId': gameId, 'maxOrder': maxOrder, 'mapId': gameMap, 'map': mapName, 'mapTiles': mapItemList[0], 'mapChips': mapItemList[1], 'mapCharacters': mapItemList[2], 'menuTiles': tileServices.getAllTiles(), 'menuChips': chipServices.getAllChips(), 'menuCharacters': characterServices.getAllCharacters(), "chatMessages": chatMessages, "gameCode": gameCode, "gameName": gameName, "characters": characters, "isMaster": isMaster, } ############################ if isMaster: return render(request, "game.html", response) else: return render(request, "gameUser.html", response) else: return HttpResponseRedirect('../') else: return HttpResponseRedirect('../')
def joinGamePost(request): if "user" in request.session: userId = request.session["user"]["localId"] if request.method == "POST": characterName = request.POST.get("characterName") moveStat = request.POST.get("moveStat") print(characterName) print(moveStat) gameId = request.POST.get("gameId") users = gameServices.getProperty(gameId, "Users") if len(users.keys()) < 4: gameServices.addUserToGame(gameId, userId) gameName = gameServices.getProperty(gameId, "Name") userServices.addGameToUser(userId, gameId, gameName) return redirect("gameView", gameId=gameId) else: messages.error(request, "La partida ha superado el nº máximo de participantes") return HttpResponseRedirect('../') else: return HttpResponseRedirect('../') else: return HttpResponseRedirect('../')
async def websocket_receive(self, event): print("received", event) front_text = event.get('text', None) ##prueba = json.loads(front_text) if front_text is not None: loaded_dict_data = json.loads(front_text) if loaded_dict_data.get("canvasArray") is not None \ or loaded_dict_data.get("chipsArray") is not None \ or loaded_dict_data.get("charactersArray") is not None: userId = self.scope["session"]["user"]["localId"] canvasArray = loaded_dict_data.get("canvasArray") chipsArray = loaded_dict_data.get("chipsArray") charactersArray = loaded_dict_data.get("charactersArray") myResponse = { "canvasArray": canvasArray, "chipsArray": chipsArray, "charactersArray": charactersArray, "sender": userId } elif loaded_dict_data.get("pythonUsersTurns"): myResponse = { "pythonUsersTurns": loaded_dict_data.get("pythonUsersTurns"), "userTurn": loaded_dict_data.get("userTurn"), "endTurn": loaded_dict_data.get("endTurn"), } else: msg = loaded_dict_data.get("message") userId = self.scope["session"]["user"]["localId"] print(self.scope["session"]["user"]) usersId = list( gameServices.getProperty(self.game_id, "Users").keys()) users = userServices.getIdAndNamesFromUsersList(usersId) myResponse = { "message": msg, "username": users[userId], "senderID": userId } #broadcasts the message event to be sent await self.channel_layer.group_send(self.game_id, { "type": "chat_message_event", "text": json.dumps(myResponse) })
def saveGame(request): data = json.loads(request.body) gameId = data["gameId"] mapId = data["mapId"] name = data["mapName"] order = data["order"] tiles = data["tiles"] chips = data["chips"] characters = data["characters"] userCharacters = data["userCharacters"] members = len(gameServices.getProperty(gameId, "Users").keys())-1 gameServices.updateUserCharacterPosition(gameId, userCharacters, members) version = versionServices.createVersionFromMap(name, mapId, order, tiles, chips, characters) mapServices.addVersion(version, mapId) response = { 'newOrder': order + 1 } return JsonResponse(response, status=200)
def joinGame(request): if "user" in request.session: userId = request.session["user"]["localId"] if request.method == "POST": gameCode = request.POST.get("gameCode") joinGameId = gameServices.getGameIdFromCode(gameCode) if joinGameId is not None: users = gameServices.getProperty(joinGameId, "Users") if userId not in users.keys(): return render(request, "joinGame.html", {"gameId": joinGameId}) else: return redirect("gameView", gameId=joinGameId) else: messages.error(request, "The game with code " + gameCode + " doesn't exist.") return HttpResponseRedirect('../') else: return HttpResponseRedirect('../') else: return HttpResponseRedirect('../')
def joinGameModal(request): if "user" in request.session: userId = request.session["user"]["localId"] if request.method == "POST": data = json.loads(request.body) gameCode = data["gameCode"] joinGameId = gameServices.getGameIdFromCode(gameCode) isNewUser = False gameExist = False if joinGameId is not None: users = gameServices.getProperty(joinGameId, "Users") gameExist = True if userId not in users.keys(): isNewUser = True else: isNewUser = False dataResponse = {"gameCode": gameCode, "isNewUser": isNewUser, "gameExist": gameExist, "gameId": joinGameId} return HttpResponse(json.dumps(dataResponse), content_type="application/json; charset=utf-8") else: HttpResponseRedirect('../') else: return HttpResponseRedirect('../')
def demoChat(request, gameId): users = list(gameServices.getProperty(gameId, "Users").values()) chatMessages = [] if request.method == "POST": chatMessages.append(request.POST.get("newMessage")) return render(request, "demoChat.html", {"chatMessages": chatMessages})