Move missing avatar logic from JavaScript to server.

This commit is contained in:
Patrick Fairbank
2019-09-14 14:00:51 -07:00
parent 969f0f1c80
commit b9e061904f
6 changed files with 31 additions and 15 deletions

View File

@@ -20,7 +20,7 @@ import (
const (
tbaBaseUrl = "https://www.thebluealliance.com"
tbaAuthKey = "MAApv9MCuKY9MSFkXLuzTSYBCdosboxDq8Q3ujUE2Mn8PD3Nmv64uczu5Lvy0NQ3"
avatarsDir = "static/img/avatars"
AvatarsDir = "static/img/avatars"
)
type TbaClient struct {
@@ -242,7 +242,7 @@ func (client *TbaClient) DownloadTeamAvatar(teamNumber, year int) error {
}
// Store the avatar to disk as a PNG file.
avatarPath := fmt.Sprintf("%s/%d.png", avatarsDir, teamNumber)
avatarPath := fmt.Sprintf("%s/%d.png", AvatarsDir, teamNumber)
ioutil.WriteFile(avatarPath, avatarBytes, 0644)
return nil
}

View File

@@ -450,7 +450,7 @@ var initializeSponsorDisplay = function() {
};
var getAvatarUrl = function(teamId) {
return "/static/img/avatars/" + teamId + ".png";
return "/api/teams/" + teamId + "/avatar";
};
$(function() {
@@ -476,9 +476,6 @@ $(function() {
overlayCenteringShowParams = overlayCenteringBottomShowParams;
}
// Fall back to a blank avatar if one doesn't exist for the team.
$(".avatar, .final-avatar").attr("onerror", "this.src='" + getAvatarUrl(0) + "';");
// Set up the websocket back to the server.
websocket = new CheesyWebsocket("/displays/audience/websocket", {
allianceSelection: function(event) { handleAllianceSelection(event.data); },

View File

@@ -29,9 +29,6 @@ var handleMatchTime = function(data) {
};
$(function() {
// Fall back to a blank avatar if one doesn't exist for the team.
$(".avatar").attr("onerror", "this.src='/static/img/avatars/0.png';");
// Set up the websocket back to the server.
websocket = new CheesyWebsocket("/displays/queueing/websocket", {
matchLoad: function(event) { handleMatchLoad(event.data); },

View File

@@ -53,9 +53,9 @@
{{end}}
</div>
<div class="col-lg-1 avatars text-right">
<img class="avatar" src="/static/img/avatars/{{$match.Red1}}.png" /><br />
<img class="avatar" src="/static/img/avatars/{{$match.Red2}}.png" /><br />
<img class="avatar" src="/static/img/avatars/{{$match.Red3}}.png" />
<img class="avatar" src="/api/teams/{{$match.Red1}}/avatar" /><br />
<img class="avatar" src="/api/teams/{{$match.Red2}}/avatar" /><br />
<img class="avatar" src="/api/teams/{{$match.Red3}}/avatar" />
</div>
<div class="col-lg-2 red-teams">
{{$match.Red1}}<br />{{$match.Red2}}<br />{{$match.Red3}}
@@ -64,9 +64,9 @@
{{$match.Blue1}}<br />{{$match.Blue2}}<br />{{$match.Blue3}}
</div>
<div class="col-lg-1 avatars">
<img class="avatar" src="/static/img/avatars/{{$match.Blue1}}.png" /><br />
<img class="avatar" src="/static/img/avatars/{{$match.Blue2}}.png" /><br />
<img class="avatar" src="/static/img/avatars/{{$match.Blue3}}.png" />
<img class="avatar" src="/api/teams/{{$match.Blue1}}/avatar" /><br />
<img class="avatar" src="/api/teams/{{$match.Blue2}}/avatar" /><br />
<img class="avatar" src="/api/teams/{{$match.Blue3}}/avatar" />
</div>
</div>
{{end}}

View File

@@ -7,11 +7,15 @@ package web
import (
"encoding/json"
"fmt"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/Team254/cheesy-arena/partner"
"github.com/Team254/cheesy-arena/websocket"
"github.com/gorilla/mux"
"net/http"
"os"
"strconv"
)
type MatchResultWithSummary struct {
@@ -194,3 +198,20 @@ func (web *Web) arenaWebsocketApiHandler(w http.ResponseWriter, r *http.Request)
// Subscribe the websocket to the notifiers whose messages will be passed on to the client.
ws.HandleNotifiers(web.arena.MatchTimingNotifier, web.arena.MatchLoadNotifier, web.arena.MatchTimeNotifier)
}
// Serves the avatar for a given team, or a default if none exists.
func (web *Web) teamAvatarsApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
teamId, err := strconv.Atoi(vars["teamId"])
if err != nil {
handleWebErr(w, err)
return
}
avatarPath := fmt.Sprintf("%s/%d.png", partner.AvatarsDir, teamId)
if _, err := os.Stat(avatarPath); os.IsNotExist(err) {
avatarPath = fmt.Sprintf("%s/0.png", partner.AvatarsDir)
}
http.ServeFile(w, r, avatarPath)
}

View File

@@ -106,6 +106,7 @@ func (web *Web) newHandler() http.Handler {
router.HandleFunc("/api/matches/{type}", web.matchesApiHandler).Methods("GET")
router.HandleFunc("/api/rankings", web.rankingsApiHandler).Methods("GET")
router.HandleFunc("/api/sponsor_slides", web.sponsorSlidesApiHandler).Methods("GET")
router.HandleFunc("/api/teams/{teamId}/avatar", web.teamAvatarsApiHandler).Methods("GET")
router.HandleFunc("/display", web.placeholderDisplayHandler).Methods("GET")
router.HandleFunc("/display/websocket", web.placeholderDisplayWebsocketHandler).Methods("GET")
router.HandleFunc("/displays/alliance_station", web.allianceStationDisplayHandler).Methods("GET")