Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1: Changed refrences to player to team, added team owner field #3

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions app/models/player.py → app/models/team.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from mongoengine import fields, Document


class Player(Document):
class Team(Document):

name = fields.StringField()
team = fields.ListField()
owner = fields.StringField()
team_members = fields.ListField()
draft_position = fields.IntField()

def to_json(self):
return {
"_id": str(self.pk),
"name": self.name,
"team": [contestant.to_json() for contestant in self.team],
"owner": self.owner,
"team_members": [contestant.to_json() for contestant in self.team_members],
"draft_position": self.draft_position,
}
77 changes: 0 additions & 77 deletions app/routes/player_routes.py

This file was deleted.

78 changes: 78 additions & 0 deletions app/routes/team_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from http import HTTPStatus
from flask import Blueprint, request, make_response, jsonify
from app.services import team_services
import logging

TEAM_BP = Blueprint("team_bp", __name__, url_prefix="/team")


@TEAM_BP.route("/", methods=["GET"])
def get_teams():
try:
teams = team_services.get_all_teams()
team_dicts = [team.to_json() for team in teams]
return make_response({"data": team_dicts}, HTTPStatus.OK)
except Exception as exception:
logging.error(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST


@TEAM_BP.route("/<team_id>", methods=["GET"])
def get_team(team_id):
try:
team = team_services.get_team(team_id)
team_dict = team.to_json()
return team_dict, HTTPStatus.OK
except Exception as exception:
logging.error(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST


@TEAM_BP.route("/new", methods=["POST"])
def new_team():
try:
team_name = request.json.get("name", None)
owner = request.json.get("owner", None)
if not team_name:
return jsonify({"msg": "Missing team name"}), HTTPStatus.BAD_REQUEST
if not owner:
return jsonify({"msg": "Missing team owner"}), HTTPStatus.BAD_REQUEST
new_team = team_services.create_team(team_name, owner)
new_team_dict = new_team.to_json()
return new_team_dict, HTTPStatus.OK
except Exception as exception:
logging.error(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST


@TEAM_BP.route("/shuffle", methods=["PUT"])
def shuffle_teams():
try:
team_services.shuffle_teams()
return "Success", HTTPStatus.OK
except Exception as exception:
logging(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST


@TEAM_BP.route("/draft/<team_id>", methods=["PUT"])
def draft(team_id):
try:
contestant_id = request.json.get("contestant_id", None)
if not contestant_id:
return jsonify({"message": "Missing contestant id"}), HTTPStatus.BAD_REQUEST
team_services.draft_contestant(team_id, contestant_id)
return "Success", HTTPStatus.OK
except Exception as exception:
logging.error(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST


@TEAM_BP.route("/remove/<team_id>", methods=["DELETE"])
def remove_team(team_id):
try:
team_services.remove_team(team_id)
return "Success", HTTPStatus.OK
except Exception as exception:
logging.error(str(exception))
return str(exception), HTTPStatus.BAD_REQUEST
64 changes: 0 additions & 64 deletions app/services/player_services.py

This file was deleted.

64 changes: 64 additions & 0 deletions app/services/team_services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import logging
import random
import copy
from app.models.team import Team
from app.services import contestant_services


def get_all_teams():
"""Get all Teams."""
teams = Team.objects
return sorted(teams, key=lambda x: x.draft_position)


def get_team(team_id):
"""Get single team from id."""
return Team.objects.get(pk=team_id)


def create_team(team_name: str, owner: str):
"""Create new team."""
new_team = Team(name=team_name, owner=owner, team=[], draft_position=0)
new_team.save()
return new_team


def remove_team(team_id):
"""Remove single team."""
team = get_team(team_id)
team.delete()


def draft_contestant(team_id, contestant_id):
"""Draft a contestant to a team."""
team = get_team(team_id)
contestant = contestant_services.get_contestant(contestant_id)
contestant.drafted = True
contestant.save()
team.team.append(contestant)
team.save()


def remove_drafted_contestants(team_id):
"""Remove all contestants from team's team."""
team = get_team(team_id)
team.team_members = []
team.save()


def remove_all_drafted_contestants():
"""Remove all contestants from all teams."""
teams = get_all_teams()
for team in teams:
remove_drafted_contestants(team.pk)


def shuffle_teams():
"""Set random draft positions for each team."""
teams = get_all_teams()
available_teams = copy.deepcopy(teams)
for x in range(len(teams)):
team = random.choice(available_teams)
available_teams.remove(team)
team.draft_position = x
team.save()