From 11b6b5f85609a542506e736ff25b6203cda4c0f0 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sat, 2 Sep 2017 19:18:15 -0700 Subject: [PATCH] Refactor schedule JS to be less buggy (fixes #12). --- static/js/setup_schedule.js | 32 ++++++++++++++++++++++++-------- templates/setup_schedule.html | 5 ++++- web/setup_schedule.go | 4 ---- web/setup_schedule_test.go | 2 +- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/static/js/setup_schedule.js b/static/js/setup_schedule.js index 6950b9a..2faebf1 100644 --- a/static/js/setup_schedule.js +++ b/static/js/setup_schedule.js @@ -4,17 +4,22 @@ // Client-side methods for the schedule generation page. var blockTemplate = Handlebars.compile($("#blockTemplate").html()); -var lastBlockNumber = 0; var blockMatches = {}; // Adds a new scheduling block to the page. var addBlock = function(startTime, numMatches, matchSpacingSec) { + var lastBlockNumber = getLastBlockNumber(); if (!startTime) { - // Start the next block where the last one left off and use the same spacing. - var lastStartTime = moment(Date.parse($("#startTime" + lastBlockNumber).val())); - var lastNumMatches = blockMatches[lastBlockNumber]; - matchSpacingSec = getMatchSpacingSec(lastBlockNumber); - startTime = moment(lastStartTime + lastNumMatches * matchSpacingSec * 1000); + if ($.isEmptyObject(blockMatches)) { + matchSpacingSec = 360; + startTime = moment().add(1, "hour").startOf("hour"); + } else { + // Start the next block where the last one left off and use the same spacing. + var lastStartTime = moment($("#startTime" + lastBlockNumber).val(), "YYYY-MM-DD hh:mm:ss A"); + var lastNumMatches = blockMatches[lastBlockNumber]; + matchSpacingSec = getMatchSpacingSec(lastBlockNumber); + startTime = moment(lastStartTime + lastNumMatches * matchSpacingSec * 1000); + } numMatches = 10; } var endTime = moment(startTime + numMatches * matchSpacingSec * 1000); @@ -31,8 +36,8 @@ var addBlock = function(startTime, numMatches, matchSpacingSec) { // Updates the per-block and global schedule statistics. var updateBlock = function(blockNumber) { - var startTime = moment(Date.parse($("#startTime" + blockNumber).val())); - var endTime = moment(Date.parse($("#endTime" + blockNumber).val())); + var startTime = moment($("#startTime" + blockNumber).val(), "YYYY-MM-DD hh:mm:ss A"); + var endTime = moment($("#endTime" + blockNumber).val(), "YYYY-MM-DD hh:mm:ss A"); var matchSpacingSec = getMatchSpacingSec(blockNumber); var numMatches = Math.floor((endTime - startTime) / matchSpacingSec / 1000); var actualEndTime = moment(startTime + numMatches * matchSpacingSec * 1000).format("hh:mm:ss A"); @@ -97,3 +102,14 @@ var getMatchSpacingSec = function(blockNumber) { var matchSpacingMinSec = $("#matchSpacingMinSec" + blockNumber).val().split(":"); return parseInt(matchSpacingMinSec[0]) * 60 + parseInt(matchSpacingMinSec[1]); }; + +var getLastBlockNumber = function() { + var max = 0; + $.each(blockMatches, function(k, v) { + var number = parseInt(k); + if (number > max) { + max = number; + } + }); + return max; +}; diff --git a/templates/setup_schedule.html b/templates/setup_schedule.html index cd638cb..2bc1b6d 100644 --- a/templates/setup_schedule.html +++ b/templates/setup_schedule.html @@ -171,7 +171,10 @@ {{end}} diff --git a/web/setup_schedule.go b/web/setup_schedule.go index 46a6767..19e1b13 100644 --- a/web/setup_schedule.go +++ b/web/setup_schedule.go @@ -27,10 +27,6 @@ func (web *Web) scheduleGetHandler(w http.ResponseWriter, r *http.Request) { } if len(cachedScheduleBlocks) == 0 { - tomorrow := time.Now().AddDate(0, 0, 1) - location, _ := time.LoadLocation("Local") - startTime := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 9, 0, 0, 0, location) - cachedScheduleBlocks = append(cachedScheduleBlocks, tournament.ScheduleBlock{startTime, 10, 360}) cachedMatchType = "practice" } web.renderSchedule(w, r, "") diff --git a/web/setup_schedule_test.go b/web/setup_schedule_test.go index a8d9e0e..999d632 100644 --- a/web/setup_schedule_test.go +++ b/web/setup_schedule_test.go @@ -20,7 +20,7 @@ func TestSetupSchedule(t *testing.T) { // Check the default setting values. recorder := web.getHttpResponse("/setup/schedule") assert.Equal(t, 200, recorder.Code) - assert.Contains(t, recorder.Body.String(), "360") // The default match spacing. + assert.Contains(t, recorder.Body.String(), "addBlock();") // Submit a schedule for generation. postData := "numScheduleBlocks=3&startTime0=2014-01-01 09:00:00 AM&numMatches0=7&matchSpacingSec0=480&" +