def initPlay(roomCode): roomId = roomIdFromUserCookie() room = Room.query.filter_by(id=roomId).first() ct = 1 for x in room.users: user = User.query.filter_by(id=x.id).first() if user.play_order != 0: break else: playOrder = initPlayOrder(roomId) for x in playOrder: user = User.query.filter_by(id=x).first() user.play_order = ct if ct == 1: user.in_play = True ct += 1 db.session.commit() room.game_started = True room.roundNo = 'rnd1' db.session.commit() resp = {"success": True} resp["roomCode"] = sendRoomCode() return resp
def nextPlayer(*args): if not args: # Enables the optional passing of a roomId roomId = roomIdFromUserCookie() else: roomId = args[0] playerOne, playerTwo = getNextPlayer(roomId) User.query.get(playerOne.id).in_play = False User.query.get(playerTwo.id).in_play = True db.session.commit() return {"success": True, "newPlayer": True}
def startTimer(*args): roomId = roomIdFromUserCookie() room = Room.query.filter_by(id=roomId).first() secs = room.time_remaining resetTimer(roomId) timer = actualTimer() thr = threading.Thread(target=timer.run, args=( secs, roomId, )) thr.start() return {"Timer": "Started"}
def response(): roomId = roomIdFromUserCookie() room = Room.query.filter_by(id=roomId).first() resp = {} resp['roomCode'] = sendRoomCode() resp['rndNo'] = room.roundNo currentGuesser = User.query.filter_by(room_id=roomId, in_play=1).first() resp['currentPlayer'] = [currentGuesser.id, currentGuesser.username] resp['timerStarted'] = room.timer_started resp['Time'] = room.time_remaining if room.time_remaining == 0: resp['newPlayer'] = True if room.scorecard == True: resp['scorecard'] = True t1, t2 = rndScore(roomId, room.roundNo) resp['scores'] = [t1, t2] return jsonify(resp)
def getNewName(rndNo): roomId = roomIdFromUserCookie() namesToGuess = [] kwargs = {"in_room": roomId, rndNo: 0} names = Names.query.filter_by(**kwargs).all() resp = {} if len(names) <= 0: resp["newRound"] = True scorecard(roomId) return resp while len(namesToGuess) < 1: randName = random.choice(names) newNameId = randName.id newName = randName.name newNameAdded = User.query.filter_by(id=randName.added_by).first() if [newNameId, newName, newNameAdded.username] not in namesToGuess: namesToGuess.append([newNameId, newName, newNameAdded.username]) resp["newRound"] = False resp["namesToGuess"] = namesToGuess return resp
def poll(): roomId = roomIdFromUserCookie() room = Room.query.filter_by(id=roomId).first() if room.game_started == "1": # gameStartTimer(roomId) resp = {} resp["current_state"] = "starting" resp["timer"] = room.game_started if room.game_started == "1": resp["current_state"] = "go" resp["timer"] = "0" else: usersInRoom = User.query.filter_by(room_id=roomId).all() noTeam = [] team1 = [] team2 = [] ct = 0 for x in usersInRoom: if usersInRoom[ct].team == 1: team1.append(usersInRoom[ct].username) elif usersInRoom[ct].team == 2: team2.append(usersInRoom[ct].username) else: noTeam.append(usersInRoom[ct].username) ct += 1 resp = { "no_team": noTeam, "team_one": team1, "team_two": team2, "current_state": "ready" } room = Room.query.filter_by(id=roomId).first() resp['roomCode'] = sendRoomCode() for x in usersInRoom: if x.team == 0: resp["current_state"] = "not-ready" return jsonify(resp)
def gameOver(roomCode): room = roomFromCode(roomCode) resetTimer(room.id) scores = totalScore(room.id) roomId = roomIdFromUserCookie() return render_template('gameover.html', scores=scores)