mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 21:56:50 -04:00
Add button to force publishing of alliances to TBA.
This commit is contained in:
@@ -39,6 +39,13 @@
|
||||
Finalize Alliance Selection
|
||||
</button>
|
||||
</div>
|
||||
{{if .EventSettings.TbaPublishingEnabled}}
|
||||
<div class="form-group">
|
||||
<button type="button" class="btn btn-info" onclick="$('#confirmPublishAlliances').modal('show');">
|
||||
Publish Alliances to TBA
|
||||
</button>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="col-lg-5">
|
||||
<table class="table table-striped table-hover">
|
||||
@@ -149,6 +156,26 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="confirmPublishAlliances" class="modal" style="top: 20%;">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title">Confirm</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>Are you sure you want to publish the alliances to The Blue Alliance? This will overwrite any
|
||||
existing alliance data.</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<form class="form-horizontal" action="/setup/alliance_selection/publish" method="POST">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary">Publish Alliances</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{define "script"}}
|
||||
<script>
|
||||
|
||||
@@ -21,7 +21,7 @@ type RankedTeam struct {
|
||||
}
|
||||
|
||||
// Global vars to hold the alliances that are in the process of being selected.
|
||||
var cachedAlliances [][]*model.AllianceTeam
|
||||
var cachedAlliances [][]model.AllianceTeam
|
||||
var cachedRankedTeams []*RankedTeam
|
||||
|
||||
// Shows the alliance selection page.
|
||||
@@ -52,10 +52,10 @@ func (web *Web) allianceSelectionPostHandler(w http.ResponseWriter, r *http.Requ
|
||||
|
||||
// Iterate through all selections and update the alliances.
|
||||
for i, alliance := range cachedAlliances {
|
||||
for j, spot := range alliance {
|
||||
for j := range alliance {
|
||||
teamString := r.PostFormValue(fmt.Sprintf("selection%d_%d", i, j))
|
||||
if teamString == "" {
|
||||
spot.TeamId = 0
|
||||
cachedAlliances[i][j].TeamId = 0
|
||||
} else {
|
||||
teamId, err := strconv.Atoi(teamString)
|
||||
if err != nil {
|
||||
@@ -71,7 +71,7 @@ func (web *Web) allianceSelectionPostHandler(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
found = true
|
||||
team.Picked = true
|
||||
spot.TeamId = teamId
|
||||
cachedAlliances[i][j].TeamId = teamId
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -104,15 +104,15 @@ func (web *Web) allianceSelectionStartHandler(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
// Create a blank alliance set matching the event configuration.
|
||||
cachedAlliances = make([][]*model.AllianceTeam, web.arena.EventSettings.NumElimAlliances)
|
||||
cachedAlliances = make([][]model.AllianceTeam, web.arena.EventSettings.NumElimAlliances)
|
||||
teamsPerAlliance := 3
|
||||
if web.arena.EventSettings.SelectionRound3Order != "" {
|
||||
teamsPerAlliance = 4
|
||||
}
|
||||
for i := 0; i < web.arena.EventSettings.NumElimAlliances; i++ {
|
||||
cachedAlliances[i] = make([]*model.AllianceTeam, teamsPerAlliance)
|
||||
cachedAlliances[i] = make([]model.AllianceTeam, teamsPerAlliance)
|
||||
for j := 0; j < teamsPerAlliance; j++ {
|
||||
cachedAlliances[i][j] = &model.AllianceTeam{AllianceId: i + 1, PickPosition: j}
|
||||
cachedAlliances[i][j] = model.AllianceTeam{AllianceId: i + 1, PickPosition: j}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func (web *Web) allianceSelectionResetHandler(w http.ResponseWriter, r *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
cachedAlliances = [][]*model.AllianceTeam{}
|
||||
cachedAlliances = [][]model.AllianceTeam{}
|
||||
cachedRankedTeams = []*RankedTeam{}
|
||||
web.arena.AllianceSelectionNotifier.Notify(nil)
|
||||
http.Redirect(w, r, "/setup/alliance_selection", 303)
|
||||
@@ -179,7 +179,7 @@ func (web *Web) allianceSelectionFinalizeHandler(w http.ResponseWriter, r *http.
|
||||
// Save alliances to the database.
|
||||
for _, alliance := range cachedAlliances {
|
||||
for _, team := range alliance {
|
||||
err := web.arena.Database.CreateAllianceTeam(team)
|
||||
err := web.arena.Database.CreateAllianceTeam(&team)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
@@ -225,7 +225,31 @@ func (web *Web) allianceSelectionFinalizeHandler(w http.ResponseWriter, r *http.
|
||||
http.Redirect(w, r, "/setup/alliance_selection", 303)
|
||||
}
|
||||
|
||||
// Publishes the alliances to the web.
|
||||
func (web *Web) allianceSelectionPublishHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !web.userIsAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
err := web.arena.TbaClient.PublishAlliances(web.arena.Database)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to publish alliances: "+err.Error(), 500)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/setup/teams", 303)
|
||||
}
|
||||
|
||||
func (web *Web) renderAllianceSelection(w http.ResponseWriter, r *http.Request, errorMessage string) {
|
||||
if len(cachedAlliances) == 0 && !web.canModifyAllianceSelection() {
|
||||
// The application was restarted since the alliance selection was conducted; reload the alliances from the DB.
|
||||
var err error
|
||||
cachedAlliances, err = web.arena.Database.GetAllAlliances()
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
template, err := web.parseFiles("templates/setup_alliance_selection.html", "templates/base.html")
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
@@ -234,7 +258,7 @@ func (web *Web) renderAllianceSelection(w http.ResponseWriter, r *http.Request,
|
||||
nextRow, nextCol := web.determineNextCell()
|
||||
data := struct {
|
||||
*model.EventSettings
|
||||
Alliances [][]*model.AllianceTeam
|
||||
Alliances [][]model.AllianceTeam
|
||||
RankedTeams []*RankedTeam
|
||||
NextRow int
|
||||
NextCol int
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
func TestSetupAllianceSelection(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
cachedAlliances = [][]*model.AllianceTeam{}
|
||||
cachedAlliances = [][]model.AllianceTeam{}
|
||||
cachedRankedTeams = []*RankedTeam{}
|
||||
web.arena.EventSettings.NumElimAlliances = 15
|
||||
web.arena.EventSettings.SelectionRound3Order = "L"
|
||||
@@ -96,7 +96,7 @@ func TestSetupAllianceSelection(t *testing.T) {
|
||||
func TestSetupAllianceSelectionErrors(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
cachedAlliances = [][]*model.AllianceTeam{}
|
||||
cachedAlliances = [][]model.AllianceTeam{}
|
||||
cachedRankedTeams = []*RankedTeam{}
|
||||
web.arena.EventSettings.NumElimAlliances = 2
|
||||
for i := 1; i <= 6; i++ {
|
||||
@@ -150,7 +150,7 @@ func TestSetupAllianceSelectionErrors(t *testing.T) {
|
||||
recorder = web.postHttpResponse("/setup/alliance_selection", "selection0_0=asdf")
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
assert.Contains(t, recorder.Body.String(), "already been finalized")
|
||||
cachedAlliances = [][]*model.AllianceTeam{}
|
||||
cachedAlliances = [][]model.AllianceTeam{}
|
||||
cachedRankedTeams = []*RankedTeam{}
|
||||
recorder = web.postHttpResponse("/setup/alliance_selection/start", "")
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
@@ -160,7 +160,7 @@ func TestSetupAllianceSelectionErrors(t *testing.T) {
|
||||
func TestSetupAllianceSelectionAutofocus(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
cachedAlliances = [][]*model.AllianceTeam{}
|
||||
cachedAlliances = [][]model.AllianceTeam{}
|
||||
cachedRankedTeams = []*RankedTeam{}
|
||||
web.arena.EventSettings.NumElimAlliances = 2
|
||||
|
||||
@@ -248,3 +248,14 @@ func TestSetupAllianceSelectionAutofocus(t *testing.T) {
|
||||
assert.Equal(t, -1, i)
|
||||
assert.Equal(t, -1, j)
|
||||
}
|
||||
|
||||
func TestSetupAllianceSelectionPublish(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
web.arena.TbaClient.BaseUrl = "fakeurl"
|
||||
web.arena.EventSettings.TbaPublishingEnabled = true
|
||||
|
||||
recorder := web.postHttpResponse("/setup/alliance_selection/publish", "")
|
||||
assert.Equal(t, 500, recorder.Code)
|
||||
assert.Contains(t, recorder.Body.String(), "Failed to publish alliances")
|
||||
}
|
||||
|
||||
@@ -147,6 +147,7 @@ func (web *Web) newHandler() http.Handler {
|
||||
router.HandleFunc("/setup/alliance_selection/start", web.allianceSelectionStartHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/alliance_selection/reset", web.allianceSelectionResetHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/alliance_selection/finalize", web.allianceSelectionFinalizeHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/alliance_selection/publish", web.allianceSelectionPublishHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/field", web.fieldGetHandler).Methods("GET")
|
||||
router.HandleFunc("/setup/field", web.fieldPostHandler).Methods("POST")
|
||||
router.HandleFunc("/setup/field/reload_displays", web.fieldReloadDisplaysHandler).Methods("GET")
|
||||
|
||||
Reference in New Issue
Block a user