Added function to split teams

main
noah metz 2024-01-28 22:39:22 -07:00
parent 3a1820bea7
commit 24e4ae69e0
1 changed files with 14 additions and 4 deletions

@ -43,7 +43,7 @@ init_db(Database) ->
{blue_2, text, [not_null]}],
[{primary_key, [division, type, number]},
{check, "type IN ('practice', 'qualification', 'elimination')"},
{foreign_key, {[division], divisions, [division], ""}},
{foreign_key, {[division], divisions, [division], "ON DELETE CASCADE"}},
{foreign_key, {[blue_1], teams, [team], ""}},
{foreign_key, {[blue_2], teams, [team], ""}},
{foreign_key, {[red_1], teams, [team], ""}},
@ -55,7 +55,7 @@ init_db(Database) ->
{instance, integer, [not_null]},
{score, blob, [not_null]}],
[{primary_key, [division, type, number, instance]},
{foreign_key, {[division, type, number], matches, [division, type, number], ""}}]),
{foreign_key, {[division, type, number], matches, [division, type, number], "ON DELETE CASCADE"}}]),
ok = sqlite3:create_table(Database, match_states, [{division, integer, [not_null]},
{type, text, [not_null]},
@ -63,7 +63,7 @@ init_db(Database) ->
{time, integer, [not_null]},
{state, blob, [not_null]}],
[{primary_key, [division, type, number, time]},
{foreign_key, {[division, type, number], matches, [division, type, number], ""}}]),
{foreign_key, {[division, type, number], matches, [division, type, number], "ON DELETE CASCADE"}}]),
ok = sqlite3:create_table(Database, finals, [{number, integer, [not_null]},
{red_1, text, [not_null]},
@ -183,6 +183,12 @@ handle_call({add_teams, Teams}, _, State) ->
handle_call({assign_divisions, Teams}, _, State) ->
ok = assign_divisions(State#state.database, Teams),
{reply, ok, State};
handle_call(split_teams, _, State) ->
Teams = [X||{_,X} <- lists:sort([{rand:uniform(), N} || N <- get_teams(State#state.database)])],
[{columns, _}, {rows, Divisions}] = sqlite3:sql_exec(State#state.database, "SELECT division FROM divisions;"),
Assignments = [{Team, (I rem length(Divisions)) + 1} || {I, Team} <- lists:enumerate(Teams)],
ok = assign_divisions(State#state.database, Assignments),
{reply, ok, State};
handle_call({delete_teams, Removed}, _, State) ->
ok = delete_teams(State#state.database, Removed),
{reply, ok, State};
@ -202,7 +208,7 @@ handle_call({edit_division, Division, PSize, QSize, ESize}, _, State) ->
[PSize, QSize, ESize, Division])),
{reply, ok, State};
handle_call({delete_division, Division}, _, State) ->
ok = sqlite3:sql_exec(State#state.database, io_lib:format("DELETE FROM divisions WHERE division = ~p;", [Division])),
ok = first_error(sqlite3:sql_exec_script(State#state.database, io_lib:format("UPDATE teams SET division = NULL WHERE division = ~p; DELETE FROM divisions WHERE division = ~p;", [Division, Division]))),
{reply, ok, State};
handle_call({generate_division, Division, Round, Seed}, _, State) ->
Size = get_div_size(State#state.database, Division, Round),
@ -224,5 +230,9 @@ get_div_teams(Database, Division) ->
[{columns, Columns}, {rows, Rows}] = sqlite3:sql_exec(Database, io_lib:format("SELECT team FROM teams WHERE division = ~p;", [Division])),
get_column("team", Columns, Rows).
get_teams(Database) ->
[{columns, Columns}, {rows, Rows}] = sqlite3:sql_exec(Database, "SELECT team FROM teams;"),
get_column("team", Columns, Rows).
handle_cast(_, State) ->
{noreply, State}.