From e37708cac3d7240a21dafb4bd946ca85b2603649 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Mon, 29 Jan 2024 00:07:52 -0700 Subject: [PATCH] Added functions for finals state/score --- src/database.erl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/database.erl b/src/database.erl index 3c09edc..da9875a 100644 --- a/src/database.erl +++ b/src/database.erl @@ -241,6 +241,25 @@ handle_call({match_state, Division, Round, Number, MatchState}, _, State) -> [Division, atom_to_list(Round), Number, get_tick(State), MatchState])), {reply, ok, State}; +handle_call({finals_score, Number, Red, Blue, Score}, _, State) -> + [{columns, Columns}, {rows, Rows}] = sqlite3:sql_exec(State#state.database, + io_lib:format( + "SELECT instance FROM finals_scores WHERE number = ~p;", + [Number])), + Instances = get_column("instance", Columns, Rows), + NextInstance = if length(Instances) == 0 -> 1; + true -> lists:max(Instances) + 1 + end, + ok = first_error(sqlite3:sql_exec_script(State#state.database, lists:append( + [io_lib:format("INSERT INTO finals_scores(number, instance, red, blue, score) VALUES(~p, ~p, ~p, ~p, '~s');", [Number, NextInstance, Red, Blue, Score]), + io_lib:format("INSERT INTO finals_states(number, tick, state) VALUES(~p, ~p, '~s');", [Number, get_tick(State), "scored"])]))), + {reply, ok, State}; +handle_call({finals_state, Number, MatchState}, _, State) -> + {rowid, _} = sqlite3:sql_exec(State#state.database, + io_lib:format("INSERT INTO finals_states(number, tick, state) VALUES(~p, ~p, '~s');", + [Number, get_tick(State), MatchState])), + {reply, ok, State}; + handle_call({add_division, PSize, QSize, ESize}, _, State) -> [{columns, Columns}, {rows, Rows}] = sqlite3:sql_exec(State#state.database, "SELECT division FROM divisions;"), Divisions = lists:sort(get_column("division", Columns, Rows)),