From c837a826c0775d72ba3ce24b17318ff601361134 Mon Sep 17 00:00:00 2001 From: Elijah Steres Date: Thu, 14 Jan 2021 01:30:31 -0500 Subject: [PATCH] connect league create page to backend --- league_storage.py | 1 + leagues.py | 2 +- main_controller.py | 38 +++++++++++++++++++-- simmadome/src/CreateLeague.css | 11 +++++- simmadome/src/CreateLeague.tsx | 62 ++++++++++++++++++++++++---------- 5 files changed, 92 insertions(+), 22 deletions(-) diff --git a/league_storage.py b/league_storage.py index 63a7fe7..5029740 100644 --- a/league_storage.py +++ b/league_storage.py @@ -64,6 +64,7 @@ def init_league_db(league): c.execute(teams_table_check_string) for team in league.teams_in_league(): + print(team) c.execute("INSERT INTO teams (name) VALUES (?)", (team.name,)) player_string = "INSERT INTO stats (name, team_name) VALUES (?,?)" diff --git a/leagues.py b/leagues.py index b685d8a..2d92e7a 100644 --- a/leagues.py +++ b/leagues.py @@ -14,7 +14,7 @@ class league_structure(object): self.owner = None def setup(self, league_dic, division_games = 1, inter_division_games = 1, inter_league_games = 1, games_per_hour = 2): - self.league = league_dic #key: subleague, value: {division : team_name} + self.league = league_dic # { subleague name : { division name : [team object] } } self.constraints = { "division_games" : division_games, "inter_div_games" : inter_division_games, diff --git a/main_controller.py b/main_controller.py index 0b968e7..8627250 100644 --- a/main_controller.py +++ b/main_controller.py @@ -1,4 +1,6 @@ -import asyncio, time, datetime, games, json, threading, jinja2, leagues, os +import asyncio, time, datetime, games, json, threading, jinja2, leagues, os, leagues +from leagues import league_structure +from league_storage import league_exists from flask import Flask, url_for, Response, render_template, request, jsonify, send_from_directory, abort from flask_socketio import SocketIO, emit import database as db @@ -20,7 +22,7 @@ def serve(path): ### API @app.route('/api/teams/search') -def searchteams(): +def search_teams(): query = request.args.get('query') page_len = int(request.args.get('page_len')) page_num = int(request.args.get('page_num')) @@ -29,7 +31,7 @@ def searchteams(): abort(400, "A query term is required") result = db.search_teams(query) - if page_len is not None: #pagination should probably be doen in the sqlite query but this will do for now + if page_len is not None: #pagination should probably be done in the sqlite query but this will do for now if page_num is None: abort(400, "A page_len argument must be accompanied by a page_num argument") result = result[page_num*page_len : (page_num + 1)*page_len] @@ -37,6 +39,36 @@ def searchteams(): return jsonify([json.loads(x[0])['name'] for x in result]) #currently all we need is the name but that can change +@app.route('/api/leagues', methods=['POST']) +def create_league(): + config = json.loads(request.data) + + if (league_exists(config['name'])): + abort(400, "A league by that name already exists") + + print(config) + league_dic = { + subleague['name'] : { + division['name'] : [games.get_team(team_name) for team_name in division['teams']] + for division in subleague['divisions'] + } + for subleague in config['structure']['subleagues'] + } + + new_league = league_structure(config['name']) + new_league.setup( + league_dic, + division_games=config['division_series'], + inter_division_games=config['inter_division_series'], + inter_league_games=config['inter_league_series'], + ) + new_league.generate_schedule() + leagues.save_league(new_league) + + return "League created successfully" + + + ### SOCKETS thread2 = threading.Thread(target=socketio.run,args=(app,'0.0.0.0')) diff --git a/simmadome/src/CreateLeague.css b/simmadome/src/CreateLeague.css index 58c3e36..90c680b 100644 --- a/simmadome/src/CreateLeague.css +++ b/simmadome/src/CreateLeague.css @@ -49,7 +49,7 @@ input[type=number]::-webkit-outer-spin-button { margin-top: 1rem; } -.cl_league_options, .cl_league_structure { +.cl_league_options, .cl_league_structure, .cl_confirm_box { display: flex; background: var(--background-tertiary); flex-direction: column; @@ -58,6 +58,15 @@ input[type=number]::-webkit-outer-spin-button { padding-top: 1.5rem; } +.cl_confirm_box { + min-width: 55rem; + padding: 2.5rem; + display: flex; + align-items: center; + justify-content: center; + font-size: 20pt; +} + .cl_league_options { align-items: center; } diff --git a/simmadome/src/CreateLeague.tsx b/simmadome/src/CreateLeague.tsx index b64ce1e..3a4dbe4 100644 --- a/simmadome/src/CreateLeague.tsx +++ b/simmadome/src/CreateLeague.tsx @@ -210,6 +210,8 @@ let initLeagueStructure = { function CreateLeague() { let [name, setName] = useState(""); let [showError, setShowError] = useState(false); + let [nameExists, setNameExists] = useState(false); + let [createSuccess, setCreateSuccess] = useState(false); let [structure, structureDispatch] = useReducer(leagueStructureReducer, initLeagueStructure); let [options, optionsDispatch] = useReducer(LeagueOptionsReducer, new LeagueOptionsState()); @@ -221,18 +223,52 @@ function CreateLeague() { } }) + if (createSuccess) { + return( +
+
+ League created succesfully! +
+
+ ); + } + return (
- setName(e.target.value)}/> -
{name === "" && showError ? "A name is required." : ""}
+ { + setName(e.target.value); + setNameExists(false); + }}/> +
{ + name === "" && showError ? "A name is required." : + nameExists && showError ? "A league by that name already exists" : + "" + }
{ @@ -246,19 +282,14 @@ function CreateLeague() { } function makeRequest(name:string, structure: LeagueStructureState, options:LeagueOptionsState) { - - if (!validRequest(name, structure, options)) { - return null - } - - return ({ + return JSON.stringify({ + name: name, structure: { - name: name, subleagues: structure.subleagues.map(subleague => ({ name: subleague.name, divisions: subleague.divisions.map(division => ({ name: division.name, - teams: division.teams + teams: division.teams.map(team => team.name) })) })) }, @@ -272,7 +303,6 @@ function makeRequest(name:string, structure: LeagueStructureState, options:Leagu } function validRequest(name:string, structure: LeagueStructureState, options:LeagueOptionsState) { - return ( name !== "" && validNumber(options.games_series) && @@ -280,7 +310,7 @@ function validRequest(name:string, structure: LeagueStructureState, options:Leag validNumber(options.inter_division_series) && validNumber(options.inter_league_series) && validNumber(options.top_postseason) && - validNumber(options.wildcards) && + validNumber(options.wildcards, 0) && structure.subleagues.length % 2 === 0 && structure.subleagues.every(subleague => subleague.name !== "" && @@ -292,8 +322,8 @@ function validRequest(name:string, structure: LeagueStructureState, options:Leag ) } -function validNumber(value: string) { - return Number(value) !== NaN && Number(value) > 0 +function validNumber(value: string, min = 1) { + return Number(value) !== NaN && Number(value) >= min } // LEAGUE STRUCUTRE @@ -426,8 +456,6 @@ function Division(props: {state: DivisionState, dispatch:(action: DistributiveOm // LEAGUE OPTIONS -type StateBundle = [T, React.Dispatch>] - function LeagueOptions(props: {state: LeagueOptionsState, dispatch: React.Dispatch, showError: boolean}) { return (