def get(self, id, board_no, ns_pair, ew_pair): ''' Returns the complete change log for a hand to tournament owners. ''' tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckUserOwnsTournamentAndMaybeReturnStatus(self.response, users.get_current_user(), tourney): return if not CheckValidHandPlayersCombinationAndMaybeSetStatus( self.response, tourney, board_no, ns_pair, ew_pair): return change_logs = ChangeLog._query( ancestor=HandScore.CreateKey(tourney, board_no, ns_pair, ew_pair)).order( -ChangeLog.key).fetch() change_dict = { 'changes' : [] } for cl in change_logs: change_dict['changes'].append(cl.to_dict()) self.response.headers['Content-Type'] = 'application/json' self.response.set_status(200) self.response.out.write(json.dumps(change_dict, indent=2))
def get(self, id, board_no, ns_pair, ew_pair): ''' Gets the information about a particular hand in the tournament. Args: id: String. Tournament id. board_no: Integer. Hand number. ns_pair: Integer. Pair number of team playing North/South. ew_pair: Integer. Pair number of team playing East/West. See api for request and response documentation. ''' tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckValidHandPlayersCombinationAndMaybeSetStatus( self.response, tourney, board_no, ns_pair, ew_pair): return hand_score = HandScore.GetByHandParams(tourney, board_no, ns_pair, ew_pair) if hand_score: response = { 'calls' : hand_score.calls_dict(), 'ns_score' : hand_score.get_ns_score(), 'ew_score' : hand_score.get_ew_score(), 'notes' : hand_score.notes, } self.response.headers['Content-Type'] = 'application/json' self.response.set_status(200) self.response.out.write(json.dumps(response, indent=2)) else: self.response.set_status(204)
def delete(self, id, board_no, ns_pair, ew_pair): ''' Delete hand with these hand number and opponents from this tournament. Args: id: String. Tournament id. board_no: Integer. Hand number. ns_pair: Integer. Pair number of team playing North/South. ew_pair: Integer. Pair number of team playing East/West. See api for request and response documentation. ''' tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckUserOwnsTournamentAndMaybeReturnStatus(self.response, users.get_current_user(), tourney): return if not CheckValidHandPlayersCombinationAndMaybeSetStatus( self.response, tourney, board_no, ns_pair, ew_pair): return hand_score = HandScore.GetByHandParams(tourney, board_no, ns_pair, ew_pair) if not hand_score: SetErrorStatus(self.response, 404, "Invalid Request", "Hand {} between pairs {} and {} is not set".format( board_no, ns_pair, ew_pair)) return hand_score.Delete() self.response.set_status(204)
def head(self, id, board_no, ns_pair, ew_pair): ''' Check if a hand with this configuration is present in tournament id. Args: id: String. Tournament id. board_no: Integer. Hand number. ns_pair: Integer. Pair number of team playing North/South. ew_pair: Integer. Pair number of team playing East/West. See api for request and response documentation. ''' tourney = GetTourneyWithIdAndMaybeReturnStatus(self.response, id) if not tourney: return if not CheckValidHandPlayersCombinationAndMaybeSetStatus( self.response, tourney, board_no, ns_pair, ew_pair): return hand_score = HandScore.GetByHandParams(tourney, board_no, ns_pair, ew_pair) if hand_score: self.response.set_status(200) return self.response.set_status(204)
def _GetJsonRoundWithScore(self, round, tourney, pair_no): ''' Converts round information to a json interpretable string adding scored hands if any exist. Args: round: MovementRound. Contains all player/hand information about a specific matchup from the point of view of Pair pair_no. tourney: Tournament. Tournament in which this is happening. pair_no: Pair from whose point of view this movement is seen. Returns: Dict as expected by api. Includes any scores that have already been added. ''' hands = round.hands round_str = round.to_dict() opp = round.opponent if opp: opp_pp = PlayerPair.GetByPairNo(tourney, opp) if opp_pp: round_str["opponent_names"] = [x.get("name") for x in opp_pp.player_list()] if hands: del round_str['hands'] for h in hands: if round.is_north: hand_score = HandScore.GetByHandParams(tourney, h, pair_no, round.opponent) else: hand_score = HandScore.GetByHandParams(tourney, h, round.opponent, pair_no) if hand_score: round_str.setdefault('hands', []).append({ 'hand_no' : h, 'score': { 'calls' : hand_score.calls_dict(), 'ns_score' : hand_score.get_ns_score(), 'ew_score' : hand_score.get_ew_score(), 'notes' : hand_score.notes, }}) else: round_str.setdefault('hands', []).append({ 'hand_no' : h }) return round_str
def _GetAllPlayedScores(self, tourney, board_no, matchups): '''Returns all played scores for given matchups for a board. Args: tourney: Tournament. Parent tournament of the hands. board_no: Integer. Number of the hand whose scores we're looking up. matchups: List of Integer pairs. Matchups of pairs for a hand. Returns: HandScores from the matchup set that are already scored. ''' hand_request = [] for ns_pair, ew_pair in matchups: hand_request.append((board_no, ns_pair, ew_pair)) return HandScore.GetByMultipleHands(tourney, hand_request)
def _CanPutHandAndMaybeSetResponse(self, tourney, board_no, ns_pair, ew_pair): ''' Tests whether the tournament lock stats allows this user to write a hand. The owner is always allowed to write a hand. Pair code players can write a hand if the tournament is UNLOCKED or if it is LOCKABLE and no hand is currently written. Args: tourney: Tournament. Current tournament. board_no: Integer. Hand number. ns_pair: Integer. Pair number of team playing North/South. ew_pair: Integer. Pair number of team playing East/West. Returns: Boolean. True iff the put call is allowed. ''' user = users.get_current_user() if (user and tourney.owner_id == user.user_id()) or tourney.IsUnlocked(): return True if tourney.IsLocked(): SetErrorStatus(self.response, 405, "Forbidden by Tournament Status", "This tournament is locked. No hands can be edited by non-directors") return False hand_score = HandScore.GetByHandParams(tourney, board_no, ns_pair, ew_pair) if not hand_score: return True self.response.headers['Content-Type'] = 'application/json' response = { 'calls' : hand_score.calls_dict(), 'ns_score' : hand_score.get_ns_score(), 'ew_score' : hand_score.get_ew_score(), 'notes' : hand_score.notes, } self.response.set_status(405) self.response.out.write(json.dumps(response, indent=2)) return False
def _GetMovementHandsAsync(self, tourney, movement, pair_no): ''' Converts movement information to a json interpretable string adding scored hands if any exist. Args: movement: Movement. Movement for this pair. tourney: Tournament. Tournament in which this is happening. pair_no: Pair from whose point of view this movement is seen. Returns: List as expected by api. Includes any scores that have already been added. ''' # Dict from round number to list of futures hand_futures_dict = defaultdict(list) players_futures_dict = {} movement_list = [] for round in movement: hands = round.hands if not hands: continue opp = round.opponent players_futures_dict[round.round] = PlayerPair.GetByPairNoAsync( tourney, opp) for h in hands: if round.is_north: hand_futures_dict[round.round].append( HandScore.GetByHandParamsAsync(tourney, h, pair_no, round.opponent)) else: hand_futures_dict[round.round].append( HandScore.GetByHandParamsAsync(tourney, h, round.opponent, pair_no)) for round in movement: hands = round.hands round_str = round.to_dict() opp = round.opponent if opp: opp_pp = players_futures_dict[round.round].get_result() if opp_pp: round_str["opponent_names"] = [ x.get("name") for x in opp_pp.player_list() ] if hands: del round_str['hands'] for i in xrange(len(hands)): hand_score = hand_futures_dict[round.round][i].get_result() if hand_score and not hand_score.deleted: round_str.setdefault('hands', []).append({ 'hand_no': hands[i], 'score': { 'calls': hand_score.calls_dict(), 'ns_score': hand_score.get_ns_score(), 'ew_score': hand_score.get_ew_score(), 'notes': hand_score.notes, } }) else: round_str.setdefault('hands', []).append({'hand_no': hands[i]}) movement_list.append(round_str) return movement_list