def played_card(roomId, cardId): infoLogger(f'playing card: {cardId}') cardId = int(cardId) room = Room(roomId) if not room.gameType: return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) if not room.is_player_valid(current_user.userId): flash('you are not member of room.') redirect(url_for('ui_blueprint.index')) if not room.is_gameStarted(): return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) # update gameStatus with this. (value and starter) # remove the card from roomStatus #check if user actually has the card. if (current_user.userId == room.get_firstPlayer() and room.get_teamMate(current_user.userId) == room.currentPlayer): if cardId in room.cards.get(room.get_teamMate(current_user.userId)): infoLogger(f'playing on behalf of team mate') room.play_aCard(room.get_teamMate(current_user.userId), cardId) else: flash('your team-mate doesn\'t have this card.') elif not cardId in room.cards.get(current_user.userId): flash('you dont have this card.') elif not current_user.userId == room.currentPlayer: flash('it is not your turn to Play.') elif room.get_teamMate(room.get_firstPlayer()) == room.currentPlayer: flash('Your Team-mate have to play behaulf of you.') else: room.play_aCard(current_user.userId, cardId) if room.check_ifCardsFinished(): room = Room(roomId) room.update_finishRound() return redirect(url_for('ui_blueprint.play', roomId=room.roomId))
def update_finishRound(self): """called when all cards are finished""" pointsTable = self.get_pointsTable() otherTeam = [ self.get_previousPlayer(self.host), self.get_nextPlayer(self.host) ] hostTeamPoints = (pointsTable[self.host] + pointsTable[self.get_teamMate(self.host)]) otherTeamPoints = pointsTable[otherTeam[0]] + pointsTable[otherTeam[1]] card = Card() if hostTeamPoints > otherTeamPoints: winnerTeam = "hostTeamScore" winMargin = ((hostTeamPoints - 6) * card.pointMapper[self.gameType]) else: winnerTeam = "otherTeamScore" winMargin = ((otherTeamPoints - 6) * card.pointMapper[self.gameType]) allTeamScores = self.get_teamScores() updatedScore = allTeamScores.get(winnerTeam, 0) + winMargin infoLogger( f'winnerTeam={winnerTeam} margin={winMargin} updatedScore={updatedScore}' ) self.update_teamScore(winnerTeam, updatedScore) # do things which are done at starting nextStartPlayer = self.get_nextPlayer(self.get_firstPlayer()) self.update_gameSelector(nextStartPlayer) self.update_firstPlayer(nextStartPlayer) self.reset_gameType() self.distribute_cards() self.clear_db_play_aCard(nextStartPlayer) self.reset_allPlayersPoint()
def room(roomId): room = Room(roomId) isHost = room.is_userHost(current_user.userId) gameStarted = room.is_gameStarted() if request.method == "POST": x = dict(request.form) if not x or not x.get("playerChosen"): flash('chose your team-mate.') return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) if not isHost: flash('you can\'t choose team-mate') return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) if gameStarted: flash('this game is already started') return redirect(url_for('ui_blueprint.play', roomId=room.roomId)) if x.get("playerChosen") == room.host: flash('you can\'t choose yourself') return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) if not len(room.players) == 4: flash('No of players is not 4.') return redirect(url_for('ui_blueprint.room', roomId=room.roomId)) room.add_playersToGame(x.get("playerChosen")) room.set_gameStarted() room.reset_gameType() room.distribute_cards() if room.get_currentBufferCards(): infoLogger('clearing old cards') room.clear_db_play_aCard(current_user.userId) return redirect(url_for('ui_blueprint.play', roomId=room.roomId)) # if not isinstance(roomId,str) or not roomId.isnumeric(): # return jsonify("Invalid room"),400 if not current_user.userId in room.players: flash(f'You have not joined room: {roomId}') return redirect(url_for('ui_blueprint.joinroom')) if gameStarted: infoLogger(f'game is already started. redirecting') return redirect(url_for('ui_blueprint.play', roomId=room.roomId)) flash('current room is' f': {room.roomId}') if isHost: flash('current room\'s code is' f': {room.get_roomCode()}') return render_template( 'room.html', roomId=room.roomId, isHost=isHost, players=room.players, gameStarted=gameStarted, )
def play_aCard(self, userId, cardId): """when someone plays a card""" infoLogger(f'playing card: {cardId}') self.update_db_play_aCard(userId, cardId) playStatusDict = self.get_currentBufferCards() if not len(playStatusDict) < 4: #this is last player of the round winner = self.decide_winnerOfRound( playStatusDict, (self.reversePlayerMappings[userId] % 4) + 1, ) infoLogger(f'winner of this round is {winner}') self.clear_db_play_aCard(self.playersMapping[winner]) self.give_PlayerAPoint(self.playersMapping[winner])
def createroom(): user = current_user user.get_roomId() infoLogger(user.roomId) if user.roomId: flash(f'Already a room is created. Room ID: {user.roomId}') # if not user.roomCode: # user.get_roomCode() return redirect(url_for('ui_blueprint.room', roomId=user.roomId)) else: roomCode = random.randint(1000, 9999) user.set_roomCode(roomCode) insert_row( **{ "userId": user.userId, "columns": { "roomState": "N", "roomCode": roomCode, "host": user.userId, "starter": user.userId, "hostTeamScore": 0, "otherTeamScore": 0, }, "table_name": "roomInfo" }) room_info = select_query_dict( **{ "columns": ["roomId"], "filters": { "host": user.userId, "roomCode": roomCode, }, "table_name": "roomInfo", "userId": user.userId, }) user.set_roomId(room_info.get("roomId")) room = Room(room_info.get("roomId")) room.add_host(current_user.userId) room.add_roomToGameStatus() flash('created a new room' f' code: {roomCode}') return redirect(url_for('ui_blueprint.room', roomId=user.roomId))
def index(): user = current_user user.get_roomId() infoLogger(user.roomId) return render_template('index.html', roomId=user.roomId)