Skip to content

Commit

Permalink
add landscape mode
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed May 29, 2024
1 parent 262bbf3 commit 86db217
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
slog.Info("at least one process exited, shutting down")
}

err = shiftNotifier.Stop()
err := shiftNotifier.Stop()
if err != nil {
slog.Error("failed stopping notifier", "error", err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/shiftnotifier/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func New(config *Config, angelAPI angelapi.Service, messenger matrixmessenger.Me
}

http.HandleFunc("/data", s.serveJSONData)
http.HandleFunc("/", s.serveHumanData)
http.HandleFunc("/", s.serveHumanPortrait)
http.HandleFunc("/landscape", s.serveHumanLandscape)

return s
}
Expand Down
104 changes: 104 additions & 0 deletions internal/shiftnotifier/template/landscape.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<html>

<head>
{{ if .refresh_seconds }}
<meta http-equiv="refresh" content="{{ .refresh_seconds }}">
{{ end }}

<style>
html {
background: black;
color: darkgrey;
font-family: sans-serif;
min-height: 100%;
min-width: 100%;
font-size: 130%;
}

h1 {
text-align: center;
}

.flexcontainer {
display: flex;
justify-content: space-around;
}

.flexcontainer .flexchild {
padding: 4em;
background: #111;
margin: 0.5em;
}

.textbig {
font-size: 120%;
}

.badge {
background-color: #CCC;
color: #111;
padding: 0.3em;
border-radius: 0.3em;
line-height: 200%;
}
</style>
</head>

<body>
<h1>Upcoming Troll Changes for {{ .shift_time }}</h1>

<div class="flexcontainer">
{{ range $location, $diffs := .data.DiffsInLocations }}
<div class="flexchild">
<span class="textbig">📍 <b>{{ $location }}</b></span><br><br>
Arriving Trolls 🔜:<br>
{{ if $diffs.UsersArriving }}
<ul>
{{ range $diffs.UsersArriving }}
<li>{{ .Nickname }} <i>({{ .ShiftName }})</i></li>
{{ end }}
</ul>
{{ else }}
&nbsp;&nbsp;<i>none</i><br>
{{ end }}
<br>

Staying Trolls 🔄:<br>
{{ if $diffs.UsersWorking }}
<ul>
{{ range $diffs.UsersWorking }}
<li>{{ .Nickname }} <i>({{ .ShiftName }})</i></li>
{{ end }}
</ul>
{{ else }}
&nbsp;&nbsp;<i>none</i><br>
{{ end }}
<br>

Leaving Trolls 🔚:<br>
{{ if $diffs.UsersLeaving }}
<ul>
{{ range $diffs.UsersLeaving }}
<li>{{ .Nickname }} <i>({{ .ShiftName }})</i></li>
{{ end }}
</ul>
{{ else }}
&nbsp;&nbsp;<i>none</i><br>
{{ end }}
<br><br>
<span class="badge">{{ $diffs.ExpectedUsers }}</span> Trolls expected.<br><br>
{{ if $diffs.OpenUsers }}
🚨 Open positions:<br>
<ul>
{{ range $shiftType, $amount := $diffs.OpenUsers }}
<li><span class="badge">{{ $amount }}</span> {{ $shiftType }}</li>
{{ end }}
</ul>
{{ end }}

</div>
{{ end }}
</div>
</body>

</html>
54 changes: 53 additions & 1 deletion internal/shiftnotifier/webview.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"log/slog"
"net/http"
"strings"
"text/template"

_ "embed"
)

//go:embed template/landscape.html
var landscapeTemplate string

func (service *service) requireToken(r *http.Request) error {
t := r.URL.Query().Get("token")
if strings.TrimSpace(t) != service.config.Token {
Expand All @@ -34,7 +40,7 @@ func (service *service) serveJSONData(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(data)
}

func (service *service) serveHumanData(w http.ResponseWriter, r *http.Request) {
func (service *service) serveHumanPortrait(w http.ResponseWriter, r *http.Request) {
err := service.requireToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
Expand Down Expand Up @@ -65,3 +71,49 @@ body {

_, _ = w.Write([]byte(html))
}

func (service *service) serveHumanLandscape(w http.ResponseWriter, r *http.Request) {
err := service.requireToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("unauthorized"))
return
}

if service.latestDiffs == nil {
_, _ = w.Write([]byte("no data"))
return
}

service.latestDiffs.DiffsInLocations["test"] = shiftDiff{
ExpectedUsers: 200,
OpenUsers: map[string]int64{"Gulasch": 3, "Drucker": 7},
UsersLeaving: []shiftUser{
{
Nickname: "cubic",
ShiftName: "Tschunk",
},
{
Nickname: "2222222",
ShiftName: "Tschunkfsdfadd",
},
},
}

tmpl, err := template.New("landscape").Parse(landscapeTemplate)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
err = tmpl.Execute(w, map[string]any{
"data": service.latestDiffs,
"refresh_seconds": r.URL.Query().Get("refresh_seconds"),
"shift_time": service.latestDiffs.ReferenceTime.Format("Mon, 15:04"),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
}

0 comments on commit 86db217

Please sign in to comment.