moth/cmd/mothd/server.go

186 lines
4.2 KiB
Go
Raw Normal View History

2020-03-01 16:10:55 -07:00
package main
import (
"fmt"
2020-08-14 20:26:04 -06:00
"io"
2020-03-01 16:10:55 -07:00
"strconv"
2020-08-14 20:26:04 -06:00
"time"
2020-03-01 16:10:55 -07:00
)
type Category struct {
Name string
Puzzles []int
}
type ReadSeekCloser interface {
io.Reader
io.Seeker
io.Closer
}
type StateExport struct {
Config struct {
Devel bool
}
Messages string
TeamNames map[string]string
PointsLog []Award
2020-08-14 20:26:04 -06:00
Puzzles map[string][]int
2020-03-01 16:10:55 -07:00
}
type PuzzleProvider interface {
Open(cat string, points int, path string) (ReadSeekCloser, time.Time, error)
Inventory() []Category
CheckAnswer(cat string, points int, answer string) error
Component
}
type ThemeProvider interface {
Open(path string) (ReadSeekCloser, time.Time, error)
Component
}
type StateProvider interface {
Messages() string
PointsLog() []*Award
TeamName(teamId string) (string, error)
SetTeamName(teamId, teamName string) error
AwardPoints(teamId string, cat string, points int) error
2020-03-01 16:10:55 -07:00
Component
}
type Component interface {
Update()
}
type MothServer struct {
Puzzles PuzzleProvider
2020-08-14 20:26:04 -06:00
Theme ThemeProvider
State StateProvider
2020-03-01 16:10:55 -07:00
}
func NewMothServer(puzzles PuzzleProvider, theme ThemeProvider, state StateProvider) *MothServer {
return &MothServer{
Puzzles: puzzles,
2020-08-14 20:26:04 -06:00
Theme: theme,
State: state,
2020-03-01 16:10:55 -07:00
}
}
func (s *MothServer) NewHandler(teamId string) MothRequestHandler {
return MothRequestHandler{
MothServer: s,
2020-08-14 20:26:04 -06:00
teamId: teamId,
2020-03-01 16:10:55 -07:00
}
}
// XXX: Come up with a better name for this.
type MothRequestHandler struct {
*MothServer
teamId string
}
func (mh *MothRequestHandler) PuzzlesOpen(cat string, points int, path string) (ReadSeekCloser, time.Time, error) {
export := mh.ExportAllState()
fmt.Println(export.Puzzles)
for _, p := range export.Puzzles[cat] {
fmt.Println(points, p)
if p == points {
return mh.Puzzles.Open(cat, points, path)
}
}
2020-08-14 20:26:04 -06:00
return nil, time.Time{}, fmt.Errorf("Puzzle locked")
2020-03-01 16:10:55 -07:00
}
func (mh *MothRequestHandler) ThemeOpen(path string) (ReadSeekCloser, time.Time, error) {
return mh.Theme.Open(path)
}
func (mh *MothRequestHandler) Register(teamName string) error {
// XXX: Should we just return success if the team is already registered?
// XXX: Should this function be renamed to Login?
if teamName == "" {
return fmt.Errorf("Empty team name")
}
return mh.State.SetTeamName(mh.teamId, teamName)
}
func (mh *MothRequestHandler) CheckAnswer(cat string, points int, answer string) error {
if err := mh.Puzzles.CheckAnswer(cat, points, answer); err != nil {
return err
}
2020-08-14 20:26:04 -06:00
if err := mh.State.AwardPoints(mh.teamId, cat, points); err != nil {
return err
}
2020-08-14 20:26:04 -06:00
return nil
2020-03-01 16:10:55 -07:00
}
func (mh *MothRequestHandler) ExportAllState() *StateExport {
2020-03-01 16:10:55 -07:00
export := StateExport{}
teamName, _ := mh.State.TeamName(mh.teamId)
2020-08-14 20:26:04 -06:00
2020-03-01 16:10:55 -07:00
export.Messages = mh.State.Messages()
export.TeamNames = map[string]string{"self": teamName}
// Anonymize team IDs in points log, and write out team names
pointsLog := mh.State.PointsLog()
exportIds := map[string]string{mh.teamId: "self"}
maxSolved := map[string]int{}
export.PointsLog = make([]Award, len(pointsLog))
for logno, award := range pointsLog {
exportAward := *award
2020-08-14 20:26:04 -06:00
if id, ok := exportIds[award.TeamID]; ok {
exportAward.TeamID = id
2020-03-01 16:10:55 -07:00
} else {
exportId := strconv.Itoa(logno)
2020-08-14 20:26:04 -06:00
name, _ := mh.State.TeamName(award.TeamID)
exportAward.TeamID = exportId
exportIds[award.TeamID] = exportAward.TeamID
2020-03-01 16:10:55 -07:00
export.TeamNames[exportId] = name
}
export.PointsLog[logno] = exportAward
2020-08-14 20:26:04 -06:00
2020-03-01 16:10:55 -07:00
// Record the highest-value unlocked puzzle in each category
if award.Points > maxSolved[award.Category] {
maxSolved[award.Category] = award.Points
}
}
export.Puzzles = make(map[string][]int)
for _, category := range mh.Puzzles.Inventory() {
// Append sentry (end of puzzles)
allPuzzles := append(category.Puzzles, 0)
max := maxSolved[category.Name]
puzzles := make([]int, 0, len(allPuzzles))
for i, val := range allPuzzles {
puzzles = allPuzzles[:i+1]
if val > max {
break
}
}
export.Puzzles[category.Name] = puzzles
}
return &export
}
func (mh *MothRequestHandler) ExportState() *StateExport {
export := mh.ExportAllState()
2020-08-14 20:26:04 -06:00
// We don't give this out to just anybody,
// because back when we did,
// we got a bad reputation on some secretive blacklist,
// and now the Navy can't register for events.
if export.TeamNames["self"] == "" {
export.Puzzles = map[string][]int{}
}
2020-08-14 20:26:04 -06:00
return export
}