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

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