def guess(): guessed_name = request.args.get('guessed_name').strip() if guessed_name: guessed_user, fans = None, False try: guessed_user = g.api.get_user(id = guessed_name) except: return render_template('guess.html', error=u"纳尼!查无此人)^O^( 你们真的认识吗") try: fans = g.api.exists_friendship(user_a = guessed_user.id, user_b = g.weibo.id).friends except: return render_template('guess.html', error=u"获取好友关系失败,请稍候再试") else: return render_template('guess.html', error=u"微博昵称不能为空") if guessed_user: constellation_id, rate, error = worker.guess(id=guessed_user.id) if rate <= 30: constellation_id, rate, error = worker.guess(id=guessed_user.id) constellation = constellations[constellation_id][1] if error: return render_template('guess.html', error=error) Guess.set(guesser_id=g.user.id, guessed_id=guessed_user.id, constellation_id=constellation_id, rate=rate) Weibo.set(weibo=guessed_user) guessed_weibo = Weibo.get(id=guessed_user.id)[0] message = weibo_txt(fans=fans, screen_name=guessed_weibo.screen_name, constellation=constellation, rate=rate) rate = str(rate) + "%" return render_template('guess.html',constellation=constellation, rate=rate, message=message, error=None, guessed_weibo=guessed_weibo) else: return render_template('guess.html', error=u'无法获取该用户的微博信息')
def guess(request): word_id = request.POST['wordId'] unique_id = request.POST['game_id'] player = request.POST['player'] clue_number = int(request.POST['clueNumber']) game = get_object_or_404(Game, unique_id=unique_id) user = get_object_or_404(User, username=player) check_double_post(game, user) if word_id: word = get_object_or_404(Word, id=word_id.replace('word', '')) card = get_object_or_404(Card, word=word, game=game) card.chosen = True card.save() else: # User passed card = None team_color = request.POST['teamColor'].split('_')[0] guess = Guess(user=user, guesser_team=team_color, game=game, card=card) guess.save() if not word_id: # User passed game.current_turn = find_next_turn(game) game.current_guess_number = 0 game.save() return Response({"message": "OK"}) else: game.current_guess_number += 1 if card.color == 'black': # Assassinated! game.active = False team_choices = {'red', 'blue'} other_team = (team_choices - set(team_color)).pop() game.winning_team = other_team elif (clue_number > 0 and game.current_guess_number >= clue_number + 1) \ or guess.is_wrong(): # Note: this greater than also works for '0' unlimited clues game.current_turn = find_next_turn(game) game.current_guess_number = 0 decrement_card_counter(game, card.color) check_game_over(game) game.save() return Response({"message": "OK"})
def save_guess(guess: int, id_game: str, id_player: int) -> None: """ Save the user guess :param guess: The guess :param id_game: The id of the game :param id_player: The id of the player """ session = session_factory() game = session.query(Game).filter(Game.id == id_game).first() if not game: create_game(id_game, id_player) last_guess = get_last_guess_of_game(id_game) if not last_guess: guess_count = 1 else: guess_count = last_guess.guess_count + 1 won = is_winning_guess(id_game, guess) guess = Guess(guess=guess, guess_count=guess_count, is_winning_guess=won, id_game=id_game) session.add(guess) session.commit() session.close()
async def take_turn(self, message): player = getattr(self.game, f"player_{message.body['player_id']}") player.drawing_block() await self.pick_block(player=player, index=message.body['block_index']) request = await player.ws.recv() request = Request.deserialize(value=request) guess = Guess(**request.body) await self.guess_block(player, guess=guess)
def guess(): guessed_name = request.args.get('guessed_name').strip() if guessed_name: guessed_user, fans = None, False try: guessed_user = g.api.get_user(id=guessed_name) except: return render_template('guess.html', error=u"纳尼!查无此人)^O^( 你们真的认识吗") try: fans = g.api.exists_friendship(user_a=guessed_user.id, user_b=g.weibo.id).friends except: return render_template('guess.html', error=u"获取好友关系失败,请稍候再试") else: return render_template('guess.html', error=u"微博昵称不能为空") if guessed_user: constellation_id, rate, error = worker.guess(id=guessed_user.id) if rate <= 30: constellation_id, rate, error = worker.guess(id=guessed_user.id) constellation = constellations[constellation_id][1] if error: return render_template('guess.html', error=error) Guess.set(guesser_id=g.user.id, guessed_id=guessed_user.id, constellation_id=constellation_id, rate=rate) Weibo.set(weibo=guessed_user) guessed_weibo = Weibo.get(id=guessed_user.id)[0] message = weibo_txt(fans=fans, screen_name=guessed_weibo.screen_name, constellation=constellation, rate=rate) rate = str(rate) + "%" return render_template('guess.html', constellation=constellation, rate=rate, message=message, error=None, guessed_weibo=guessed_weibo) else: return render_template('guess.html', error=u'无法获取该用户的微博信息')
def answer(): # Prevent user from guessing twice on the same movie try: guess = Guess.save( request.form['guess'], g.user.id, request.form['movie'] ) except IntegrityError, e: return 'Du har redan gissat på den här filmen', 403
async def guess_block(self, player, guess: Guess): from_player = player # type: Player to_player = getattr(self.game, f'player_{guess.to_player_id}') # type: Player success = from_player.guess_block(target_player=to_player, target_block_index=guess.target, guess=guess.guess) await self.distribute_game() if success: response = Response(action=Actions.GUESS_SUCCESS.value, message="Guess Succeeded!", body="") from_player.guessing_more() await self.distribute_response(response=response) await self.distribute_game() next_action_message = await player.ws.recv() request = Request.deserialize(value=next_action_message) if request.action == Actions.MAKE_GUESS.value: guess = Guess(**request.body) await self.guess_block(player, guess) elif request.action == Actions.YIELD_TURN.value: from_player.get_ready() self.game.swap_turn() next_player = getattr(self.game, f'player_{self.game.turn}') next_player.drawing_block() else: raise TypeError('Invalid Action type.') else: response = Response(action=Actions.GUESS_FAIL.value, message="Guess Failed!", body="") await self.distribute_response(response=response) from_player.get_ready() self.game.swap_turn() next_player = getattr(self.game, f'player_{self.game.turn}') next_player.drawing_block() return self.game
def make_move(self, request): """endpoint to guess a letter or the word""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # make guess lower case guess = request.guess.lower() guess_obj = Guess(guess=guess, msg="") if game.game_over: raise endpoints.ForbiddenException('Illegal action: Game is already over.') guess_obj.guess_num = 1 + len(game.guess_history) guess_obj.word_state = ' '.join(game.guess_state) # check if isalpha if guess.isalpha() == False: raise endpoints.ForbiddenException('Illegal action: Letters only.') if guess == game.target: game.end_game(True) msg = 'You guessed the word, you win!' guess_obj.msg = msg guess_obj.word_state = game.target game.guess_hist_obj.append(guess_obj) game.guess_state = [letter for letter in game.target] game.put() return game.to_form(msg) if guess in game.guess_history: msg = "You already tried that, guess again. Lose a turn for being foolish." game.attempts_remaining -= 1 guess_obj.msg = msg game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg) # check if guess is a single letter if len(guess) == 1: game.guess_history.append(guess) if guess in game.target: msg = 'The word contains your letter!' game.update_guess_state(guess) guess_obj.word_state = ' '.join(game.guess_state) else: msg = 'Nope! Guess Again!' game.attempts_remaining -= 1 if game.target == ''.join(game.guess_state): game.end_game(True) msg = 'You guessed all the letters, you win!' guess_obj.msg = msg game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg) # otherwise, it's not a single letter or the word guess is wrong. else: game.attempts_remaining -= 1 msg = 'Incorrect, try again.' if game.attempts_remaining < 1: game.end_game(False) guess_obj.msg = msg + ' Game over!' game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg + ' Game over!') else: guess_obj.msg = msg game.guess_history.append(guess) game.update_guess_state(guess) game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg)
def make_move(self, request): """endpoint to guess a letter or the word""" game = get_by_urlsafe(request.urlsafe_game_key, Game) # make guess lower case guess = request.guess.lower() guess_obj = Guess(guess=guess, msg="") if game.game_over: raise endpoints.ForbiddenException( 'Illegal action: Game is already over.') guess_obj.guess_num = 1 + len(game.guess_history) guess_obj.word_state = ' '.join(game.guess_state) # check if isalpha if guess.isalpha() == False: raise endpoints.ForbiddenException('Illegal action: Letters only.') if guess == game.target: game.end_game(True) msg = 'You guessed the word, you win!' guess_obj.msg = msg guess_obj.word_state = game.target game.guess_hist_obj.append(guess_obj) game.guess_state = [letter for letter in game.target] game.put() return game.to_form(msg) if guess in game.guess_history: msg = "You already tried that, guess again. Lose a turn for being foolish." game.attempts_remaining -= 1 guess_obj.msg = msg game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg) # check if guess is a single letter if len(guess) == 1: game.guess_history.append(guess) if guess in game.target: msg = 'The word contains your letter!' game.update_guess_state(guess) guess_obj.word_state = ' '.join(game.guess_state) else: msg = 'Nope! Guess Again!' game.attempts_remaining -= 1 if game.target == ''.join(game.guess_state): game.end_game(True) msg = 'You guessed all the letters, you win!' guess_obj.msg = msg game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg) # otherwise, it's not a single letter or the word guess is wrong. else: game.attempts_remaining -= 1 msg = 'Incorrect, try again.' if game.attempts_remaining < 1: game.end_game(False) guess_obj.msg = msg + ' Game over!' game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg + ' Game over!') else: guess_obj.msg = msg game.guess_history.append(guess) game.update_guess_state(guess) game.guess_hist_obj.append(guess_obj) game.put() return game.to_form(msg)