forked from TF2Stadium/Helen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_badge.go
48 lines (40 loc) · 988 Bytes
/
twitch_badge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package controllers
import (
"html/template"
"net/http"
"regexp"
"github.com/TF2Stadium/Helen/models/lobby"
"github.com/TF2Stadium/Helen/models/player"
)
var (
twitchBadge *template.Template
reValidPath = regexp.MustCompile(`badge/(\d+)`)
emptyPage = `
<html>
<head>
<meta http-equiv="refresh" content="5">
</head>
</html>
`
)
func TwitchBadge(w http.ResponseWriter, r *http.Request) {
if !reValidPath.MatchString(r.URL.Path) {
http.NotFound(w, r)
return
}
matches := reValidPath.FindStringSubmatch(r.URL.Path)
steamid := matches[1]
player, err := player.GetPlayerBySteamID(steamid)
if err != nil { //player not found
http.Error(w, "Player with given SteamID not found", http.StatusNotFound)
return
}
id, err := player.GetLobbyID(false)
if err != nil {
//player not in lobby right now, just serve a page that refreshes every 5 seconds
w.Write([]byte(emptyPage))
return
}
lobby, _ := lobby.GetLobbyByID(id)
err = twitchBadge.Execute(w, lobby)
}