Refactor schedule JS to be less buggy (fixes #12).

This commit is contained in:
Patrick Fairbank
2017-09-02 19:18:15 -07:00
parent 4e418434da
commit 11b6b5f856
4 changed files with 29 additions and 14 deletions

View File

@@ -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;
};

View File

@@ -171,7 +171,10 @@
<script src="/static/js/setup_schedule.js"></script>
<script>
{{range $block := .ScheduleBlocks}}
addBlock(moment(Date.parse({{$block.StartTime}})), {{$block.NumMatches}}, {{$block.MatchSpacingSec}});
addBlock(moment({{$block.StartTime.Unix}} * 1000), {{$block.NumMatches}}, {{$block.MatchSpacingSec}});
{{end}}
{{if not .ScheduleBlocks}}
addBlock();
{{end}}
</script>
{{end}}

View File

@@ -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, "")

View File

@@ -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&" +