From: Brendan Hansen Date: Tue, 17 Sep 2019 20:21:12 +0000 (-0500) Subject: Competition time limits are enforced X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=fa755b2d327e4f962b0196814f23117f17ad25f9;p=codebox.git Competition time limits are enforced --- diff --git a/codebox/controllers/problem/problem.moon b/codebox/controllers/problem/problem.moon index edb6856..a38f71a 100644 --- a/codebox/controllers/problem/problem.moon +++ b/codebox/controllers/problem/problem.moon @@ -8,7 +8,7 @@ make_controller inject: queries: 'queries' - middleware: { 'logged_in' } + middleware: { 'logged_in', 'competition_started' } scripts: { "pie_chart" } get: capture_errors_json => diff --git a/codebox/controllers/problem/submit.moon b/codebox/controllers/problem/submit.moon index 6c2a10f..a26efe9 100644 --- a/codebox/controllers/problem/submit.moon +++ b/codebox/controllers/problem/submit.moon @@ -9,7 +9,7 @@ make_controller queries: 'queries' executer: 'executer' - middleware: { 'logged_in' } + middleware: { 'logged_in', 'during_competition' } scripts: { 'vendor/ace/ace', 'problem_submit' } get: capture_errors_json => @@ -35,9 +35,8 @@ make_controller return json: { status: 'problem not found' } test_cases = problem\get_test_cases! - competition = Competitions\find active: true - id = @executer\request @params.lang, @params.code, @user.id, problem.id, competition.id, test_cases, problem.time_limit + id = @executer\request @params.lang, @params.code, @user.id, problem.id, @competition.id, test_cases, problem.time_limit json: id diff --git a/codebox/middleware/competition_started.moon b/codebox/middleware/competition_started.moon new file mode 100644 index 0000000..b04ed9d --- /dev/null +++ b/codebox/middleware/competition_started.moon @@ -0,0 +1,14 @@ +import Competitions from require 'models' + +{:time_to_number} = (require 'utils.time')! + +=> + @competition = Competitions\find active: true + unless @competition + @write json: 'No active competition' + + current_time = os.time() + start_time = time_to_number @competition.start + + unless start_time <= current_time + @write '

Competition has not begun

' \ No newline at end of file diff --git a/codebox/middleware/during_competition.moon b/codebox/middleware/during_competition.moon new file mode 100644 index 0000000..a5a2303 --- /dev/null +++ b/codebox/middleware/during_competition.moon @@ -0,0 +1,17 @@ +import Competitions from require 'models' + +{:time_to_number} = (require 'utils.time')! + +=> + @competition = Competitions\find active: true + unless @competition + @write json: 'No active competition' + + current_time = os.time() + start_time = time_to_number @competition.start + end_time = time_to_number @competition.end + + unless start_time <= current_time + @write '

Competition has not begun

' + unless current_time <= end_time + @write '

Competition has ended

' \ No newline at end of file diff --git a/codebox/utils/string.moon b/codebox/utils/string.moon new file mode 100644 index 0000000..318d6f0 --- /dev/null +++ b/codebox/utils/string.moon @@ -0,0 +1,5 @@ +string.split = (pattern) => + t = {} + for s in string.gmatch @, "([^#{pattern}]+)" + table.insert t, s + t \ No newline at end of file diff --git a/codebox/utils/time.moon b/codebox/utils/time.moon new file mode 100644 index 0000000..9d24f83 --- /dev/null +++ b/codebox/utils/time.moon @@ -0,0 +1,19 @@ +require 'utils.string' + +-> { + -- Assumes time is formmated like: + -- YYYY-MM-DD HH:MM:SS + time_to_number: (timestamp) -> + {date, time} = string.split timestamp, " " + + {year, month, day} = string.split date, "-" + {hour, minute, second} = string.split time, ":" + + return os.time + year: tonumber(year) + month: tonumber(month) + day: tonumber(day) + hour: tonumber(hour) + minute: tonumber(minute) + second: tonumber(second) +} \ No newline at end of file