def delete_party(request): """ DELETE -- Deletes the party that the member is in, if any. We are allowing "POST" here because django tests don't allow DELETE :param request: :return: """ session_email = request.user.username if not request.user.is_authenticated: return http_response({}, message="You are not logged in", code=302) try: user = Member.objects.get(email=session_email) except Member.DoesNotExist: return http_response( {}, message="You do not have the required permissions", code=403) party_id = user.party_id if party_id is None: return http_response(message='You are not in a party', code=400) run_connection("DELETE FROM api_party WHERE id = %s", party_id) # Make sure any members associated with this party are updated return run_connection( "UPDATE api_member SET party_id=NULL WHERE party_id=%s", party_id)
def create_queue(type): """ :param type: :return: """ return run_connection("INSERT INTO api_queue (type) VALUES (%s)", type)
def delete_queue(id): """ Deletes the queue with id :param id: :return: """ return run_connection("DELETE FROM api_queue WHERE id=%s", id)
def edit_queue(id, type): """ Modifies queue with id to a different type :param id: :param type: :return: """ if get_queue_by_id(id): return run_connection("UPDATE api_queue SET type=%s WHERE id=%s", type, id) else: return http_response({}, message='No queue exists with this id!', code=400)
def add_member(request): """ POST -- Adds a member to the logged in user's party Required Keys: id, delete Optional Keys: queue_id, add_members, remove_members NOTE: `add_members` and `remove_members` are comma separated lists of member id's to add or remove from the party respectively :param request: :return: """ post_dict = dict(request.POST.items()) if not validate_keys(['member_id'], post_dict): return http_response({}, message="Keys not found", code=400) member_id = post_dict['member_id'] my_id = id_for_member(request.user.email) party = party_for_member(my_id) party_id = party.id rawquery = Member.objects.raw( "SELECT * FROM api_member WHERE interested_ptr_id=%s AND party_id NOT NULL", [member_id]) member_is_in_party = len(list(rawquery)) > 0 if member_is_in_party: return http_response(message="Member is already in a party", code=400) members = Member.objects.raw( "SELECT * FROM api_member WHERE party_id NOT NULL AND party_id = %s AND interested_ptr_id = %s", [party_id, member_id]) if len(list(members)) > 0: return http_response(message="Member is already part of party") response = run_connection( "UPDATE api_member SET party_id = %s WHERE interested_ptr_id = %s", party_id, member_id) return response
def join_party(request): session_email = request.user.username if not request.user.is_authenticated: return http_response({}, message="You are not logged in", code=302) try: user = Member.objects.get(email=session_email) except Member.DoesNotExist: return http_response( {}, message="You do not have the required permissions", code=403) member_id = user.id curr_party_id = user.party_id if curr_party_id is not None: return http_response(message="You are already in a party", code=400) post_dict = dict(request.POST.items()) if not validate_keys(['party_id'], post_dict): return http_response({}, message="Keys not found", code=400) party_id = post_dict["party_id"] return run_connection( "UPDATE api_member SET party_id=%s WHERE interested_ptr_id=%s", party_id, member_id)
def create_party(request): """ POST -- Creates a party. Anyone with Member permission or above can do this. Required Keys: queue_type, member_ids NOTE: member_ids is a comma separated list of ids to add to the party (EXCLUDES the user themselves) :param request: :return: """ post_dict = dict(request.POST.items()) print(post_dict) if not validate_keys(['queue_id', 'member_ids'], post_dict): return http_response({}, message="Keys not found", code=400) queue_id = post_dict['queue_id'] queues = Queue.objects.raw("SELECT * FROM api_queue WHERE id = %s", [queue_id]) if len(list(queues)) <= 0: return http_response(message='Queue does not exist', code=400) queue = queues[0] queue_id = queue.id user_id = id_for_member(request.user.email) member_ids = post_dict['member_ids'] member_ids = member_ids.split(",") # list of ids to add to the party if user_id not in member_ids: member_ids.append( str(user_id)) # add self to the party, if not already specified # Check if the user is already in a party rawquery = Member.objects.raw( "SELECT * FROM api_member WHERE interested_ptr_id=%s AND party_id NOT NULL", [user_id]) member_is_in_party = len(list(rawquery)) > 0 if member_is_in_party: return http_response(message="User is already in a party", code=400) parties_with_max_id = Party.objects.raw( "SELECT * FROM api_party WHERE id = (SELECT MAX(id) FROM api_party)") if len(list(parties_with_max_id)) == 0: new_id = 0 else: party_with_max_id = parties_with_max_id[0] new_id = party_with_max_id.id + 1 # Check if there are other parties on this queue. If not, for each court associated with # this queue, check if there are matches with NULL endDateTime with the court id. If for a court_id, there # are no matches with a NULL endDateTime, that means there is no ongoing match on that court. In that case, # skip creating a party and just throw this group into a match on that empty court. open_court_id = queue_is_empty_with_open_court(queue_id) if open_court_id: # Create match # Randomize the member_id's into two teams (preferably equal numbers on each team) a_players = [] b_players = [] num_players = len(member_ids) for member_id in member_ids: choice = random.choice([True, False]) if choice and len(a_players) < num_players / 2: # Add to a_players, if half of players aren't already on there a_players.append(member_id) else: if len(b_players) < num_players / 2: b_players.append(member_id) else: a_players.append(member_id) create_match(a_players=a_players, b_players=b_players, court_id=open_court_id) return http_response(message="OK", code=200) else: # Create party on the queue response = run_connection( "INSERT INTO api_party(id, queue_id) VALUES (%s, %s)", new_id, queue_id) if response.status_code != 200: return response # response = run_connection("UPDATE api_member SET party_id = %s WHERE interested_ptr_id = %s", new_id, member_id) # if response.status_code != 200: # return response add_members_to_party(new_id, member_ids) return response
def dequeue_party_to_court_call(queue_type): response = get_parties_by_playtime(queue_type) my_json = json.loads(response.content.decode()) parties = my_json['parties'] if len(parties) == 0: return http_response({}, message='No parties on this queue', code=400) party_to_dequeue = parties[0] party_id = party_to_dequeue['party_id'] queues = Queue.objects.raw("SELECT * FROM api_queue WHERE type = %s", [queue_type]) if len(list(queues)) == 0: return http_response({}, message='No such queue found', code=400) queue = queues[0] queue_courts = Court.objects.raw( "SELECT * FROM api_court WHERE queue_id = %s", [queue.id]) if len(list(queue_courts)) == 0: return http_response({}, message='No courts available for this queue', code=400) found_available_court = False for court in queue_courts: if court.match is None: # Found an empty court found_available_court = True # Get the members from the party members_query = Member.objects.raw( "SELECT * FROM api_member WHERE party_id = %s", [party_id]) members = [] for member in members_query: member.party = None member.save() members.append(member) # Remove party from queue response = run_connection("DELETE FROM api_party WHERE id = %s", party_id) if response.status_code != 200: # Error return response # Create match on court all_matches = Match.objects.raw("SELECT * FROM api_match") largest_id = max([match.id for match in all_matches ]) if len(list(all_matches)) > 0 else -1 id_of_new_match = largest_id + 1 now = datetime.datetime.now(tz=api.datetime_extension.cst) response = run_connection( "INSERT INTO api_match(id, startDateTime, scoreA, scoreB) VALUES (%s, %s, 0, 0)", id_of_new_match, serializeDateTime(now)) if response.status_code != 200: # Error return response # Assign teams num_members = len(members) for i in range(num_members): team = "A" if i % 2 == 0 else "B" member = members[i] response = run_connection( "INSERT INTO api_playedin(team, match_id, member_id) VALUES (%s, %s, %s)", team, id_of_new_match, member.id) if response.status_code != 200: # Error return response # Add the match to the court response = run_connection( "UPDATE api_court SET match_id = %s WHERE id = %s", id_of_new_match, court.id) break if found_available_court: return response else: return http_response(message="No available courts", code=400)