From 29033af025ae1512a7c2d28dd0adbc980d7b23a3 Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Fri, 24 Aug 2018 20:51:37 -0700 Subject: [PATCH] Persist schedule blocks to the DB. --- .../20180824200145_CreateScheduleBlocks.sql | 11 +++ field/arena.go | 1 - model/database.go | 4 + model/match.go | 6 +- model/schedule_block.go | 38 +++++++++ model/schedule_block_test.go | 47 +++++++++++ model/sponsor_slide.go | 2 +- templates/setup_schedule.html | 19 +++-- tournament/schedule.go | 11 +-- tournament/schedule_test.go | 16 ++-- web/alliance_station_display_test.go | 2 - web/setup_schedule.go | 83 +++++++++++++------ web/setup_schedule_test.go | 6 +- 13 files changed, 187 insertions(+), 59 deletions(-) create mode 100644 db/migrations/20180824200145_CreateScheduleBlocks.sql create mode 100644 model/schedule_block.go create mode 100644 model/schedule_block_test.go diff --git a/db/migrations/20180824200145_CreateScheduleBlocks.sql b/db/migrations/20180824200145_CreateScheduleBlocks.sql new file mode 100644 index 0000000..07b95e8 --- /dev/null +++ b/db/migrations/20180824200145_CreateScheduleBlocks.sql @@ -0,0 +1,11 @@ +-- +goose Up +CREATE TABLE schedule_blocks ( + id INTEGER PRIMARY KEY, + matchtype VARCHAR(16), + starttime DATETIME, + nummatches int, + matchspacingsec int +); + +-- +goose Down +DROP TABLE schedule_blocks; diff --git a/field/arena.go b/field/arena.go index 0460d13..92c503e 100644 --- a/field/arena.go +++ b/field/arena.go @@ -531,7 +531,6 @@ func (arena *Arena) Update() { } // Handle field sensors/lights/motors. - //arena.Plc.SimulateInput(matchTimeSec) arena.handlePlcInput() arena.handlePlcOutput() arena.handleLeds() diff --git a/model/database.go b/model/database.go index f922d5e..16cbdd2 100644 --- a/model/database.go +++ b/model/database.go @@ -35,6 +35,7 @@ type Database struct { allianceTeamMap *modl.DbMap lowerThirdMap *modl.DbMap sponsorSlideMap *modl.DbMap + scheduleBlockMap *modl.DbMap } // Opens the SQLite database at the given path, creating it if it doesn't exist, and runs any pending @@ -120,6 +121,9 @@ func (database *Database) mapTables() { database.sponsorSlideMap = modl.NewDbMap(database.db, dialect) database.sponsorSlideMap.AddTableWithName(SponsorSlide{}, "sponsor_slides").SetKeys(true, "Id") + + database.scheduleBlockMap = modl.NewDbMap(database.db, dialect) + database.scheduleBlockMap.AddTableWithName(ScheduleBlock{}, "schedule_blocks").SetKeys(true, "Id") } func serializeHelper(target *string, source interface{}) error { diff --git a/model/match.go b/model/match.go index db3e556..b9fede6 100644 --- a/model/match.go +++ b/model/match.go @@ -69,7 +69,7 @@ func (database *Database) TruncateMatches() error { func (database *Database) GetMatchByName(matchType string, displayName string) (*Match, error) { var matches []Match - err := database.teamMap.Select(&matches, "SELECT * FROM matches WHERE type = ? AND displayname = ?", + err := database.matchMap.Select(&matches, "SELECT * FROM matches WHERE type = ? AND displayname = ?", matchType, displayName) if err != nil { return nil, err @@ -82,14 +82,14 @@ func (database *Database) GetMatchByName(matchType string, displayName string) ( func (database *Database) GetMatchesByElimRoundGroup(round int, group int) ([]Match, error) { var matches []Match - err := database.teamMap.Select(&matches, "SELECT * FROM matches WHERE type = 'elimination' AND "+ + err := database.matchMap.Select(&matches, "SELECT * FROM matches WHERE type = 'elimination' AND "+ "elimround = ? AND elimgroup = ? ORDER BY eliminstance", round, group) return matches, err } func (database *Database) GetMatchesByType(matchType string) ([]Match, error) { var matches []Match - err := database.teamMap.Select(&matches, + err := database.matchMap.Select(&matches, "SELECT * FROM matches WHERE type = ? ORDER BY elimround desc, eliminstance, elimgroup, id", matchType) return matches, err } diff --git a/model/schedule_block.go b/model/schedule_block.go new file mode 100644 index 0000000..c0bda84 --- /dev/null +++ b/model/schedule_block.go @@ -0,0 +1,38 @@ +// Copyright 2018 Team 254. All Rights Reserved. +// Author: pat@patfairbank.com (Patrick Fairbank) +// +// Model and datastore CRUD methods for a schedule block at an event. + +package model + +import ( + "time" +) + +type ScheduleBlock struct { + Id int + MatchType string + StartTime time.Time + NumMatches int + MatchSpacingSec int +} + +func (database *Database) CreateScheduleBlock(block *ScheduleBlock) error { + return database.scheduleBlockMap.Insert(block) +} + +func (database *Database) GetScheduleBlocksByMatchType(matchType string) ([]ScheduleBlock, error) { + var blocks []ScheduleBlock + err := database.scheduleBlockMap.Select(&blocks, "SELECT * FROM schedule_blocks WHERE matchtype = ? ORDER BY "+ + "starttime ", matchType) + return blocks, err +} + +func (database *Database) DeleteScheduleBlocksByMatchType(matchType string) error { + _, err := database.scheduleBlockMap.Exec("DELETE FROM schedule_blocks WHERE matchtype = ?", matchType) + return err +} + +func (database *Database) TruncateScheduleBlocks() error { + return database.scheduleBlockMap.TruncateTables() +} diff --git a/model/schedule_block_test.go b/model/schedule_block_test.go new file mode 100644 index 0000000..d5439c0 --- /dev/null +++ b/model/schedule_block_test.go @@ -0,0 +1,47 @@ +// Copyright 2018 Team 254. All Rights Reserved. +// Author: pat@patfairbank.com (Patrick Fairbank) + +package model + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestScheduleBlockCrud(t *testing.T) { + db := setupTestDb(t) + + scheduleBlock1 := ScheduleBlock{0, "practice", time.Now().UTC(), 10, 600} + assert.Nil(t, db.CreateScheduleBlock(&scheduleBlock1)) + scheduleBlock2 := ScheduleBlock{0, "qualification", time.Now().UTC(), 20, 480} + assert.Nil(t, db.CreateScheduleBlock(&scheduleBlock2)) + scheduleBlock3 := ScheduleBlock{0, "qualification", scheduleBlock2.StartTime.Add(time.Second * 20 * 480), 20, 480} + assert.Nil(t, db.CreateScheduleBlock(&scheduleBlock3)) + + // Test retrieval of all blocks by match type. + blocks, err := db.GetScheduleBlocksByMatchType("practice") + assert.Nil(t, err) + if assert.Equal(t, 1, len(blocks)) { + assert.Equal(t, scheduleBlock1, blocks[0]) + } + blocks, err = db.GetScheduleBlocksByMatchType("qualification") + assert.Nil(t, err) + if assert.Equal(t, 2, len(blocks)) { + assert.Equal(t, scheduleBlock2, blocks[0]) + assert.Equal(t, scheduleBlock3, blocks[1]) + } + + // Test deletion of blocks. + assert.Nil(t, db.DeleteScheduleBlocksByMatchType("practice")) + blocks, err = db.GetScheduleBlocksByMatchType("practice") + assert.Nil(t, err) + assert.Equal(t, 0, len(blocks)) + blocks, err = db.GetScheduleBlocksByMatchType("qualification") + assert.Nil(t, err) + assert.Equal(t, 2, len(blocks)) + assert.Nil(t, db.TruncateScheduleBlocks()) + blocks, err = db.GetScheduleBlocksByMatchType("qualification") + assert.Nil(t, err) + assert.Equal(t, 0, len(blocks)) +} diff --git a/model/sponsor_slide.go b/model/sponsor_slide.go index 9b027af..237e4e1 100644 --- a/model/sponsor_slide.go +++ b/model/sponsor_slide.go @@ -44,6 +44,6 @@ func (database *Database) TruncateSponsorSlides() error { func (database *Database) GetAllSponsorSlides() ([]SponsorSlide, error) { var sponsorSlides []SponsorSlide - err := database.teamMap.Select(&sponsorSlides, "SELECT * FROM sponsor_slides ORDER BY id") + err := database.sponsorSlideMap.Select(&sponsorSlides, "SELECT * FROM sponsor_slides ORDER BY id") return sponsorSlides, err } diff --git a/templates/setup_schedule.html b/templates/setup_schedule.html index 2bc1b6d..f209ab3 100644 --- a/templates/setup_schedule.html +++ b/templates/setup_schedule.html @@ -15,7 +15,8 @@ {{end}}
-
+
Schedule Parameters
@@ -24,14 +25,16 @@
@@ -46,11 +49,11 @@

- +

- + Generate Schedule/Save Blocks +

+

{{if .EventSettings.TbaPublishingEnabled}} @@ -156,7 +159,7 @@ existing matches and their data.