-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
117 lines (94 loc) · 2.99 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"math/rand"
"os"
"strings"
"github.com/josephnaberhaus/gauthordle/internal/commit"
"github.com/josephnaberhaus/gauthordle/internal/config"
"github.com/josephnaberhaus/gauthordle/internal/game"
"github.com/josephnaberhaus/gauthordle/internal/git"
"github.com/josephnaberhaus/gauthordle/internal/output"
)
const helpBody = "A daily game where you try to guess the author of some Git commits.\n\nTo play, simply \"git checkout\" the main development branch of your repository\nand run this program with no arguments.\n\nNew games start at midnight Central Time."
var (
dumpCommits = flag.String("debugDumpCommits", "", "File to dump JSON containing all commits considered when generating the game.")
help = flag.Bool("help", false, "Print the help message.")
random = flag.Bool("random", false, "If true, play a random game instead of the daily game.")
team = flag.String("team", "", "Team to build the game for. This must mach a team defined in your config.")
)
func main() {
flag.Parse()
if len(flag.Args()) > 0 {
exit(fmt.Errorf("unsupported arguments %q\n", strings.Join(flag.Args(), ",")))
}
if *help {
showUsage()
}
if !git.IsGitInstalled() {
exit(errors.New("git must be installed"))
}
if !git.IsInGitRepo() {
exit(errors.New("must be in a git repository"))
}
fmt.Println("Building game...")
startTime, endTime := game.PuzzleTimeRange()
cfg, err := config.Load()
exitIfError(err)
// Get the commits for this game.
filterOptions := []commit.FilterOption{
commit.WithConfig(cfg),
commit.WithStartTime(startTime),
commit.WithEndTime(endTime),
}
if *team != "" {
if _, ok := cfg.Teams[*team]; !ok {
exit(fmt.Errorf("team %q doesn't exist in your config file", *team))
}
filterOptions = append(filterOptions, commit.WithTeam(*team))
}
filter, err := commit.BuildFilter(filterOptions...)
exitIfError(err)
commits, err := filter.GetCommits()
exitIfError(err)
if *dumpCommits != "" {
serializedCommits, err := json.MarshalIndent(commits, "", " ")
exitIfError(err)
err = os.WriteFile(*dumpCommits, serializedCommits, os.ModePerm)
exitIfError(err)
}
// Build and run the game.
gameOptions := []game.Option{
game.WithCommits(commits),
}
if !*random {
// For non-random games, use the startTime as the random source so that it's stable throughout the day.
gameOptions = append(gameOptions, game.WithRandomSource(rand.NewSource(startTime.Unix())))
}
if cfg.AuthorBias != nil {
gameOptions = append(gameOptions, game.WithAuthorBias(*cfg.AuthorBias))
} else {
gameOptions = append(gameOptions, game.WithAuthorBias(3.5))
}
puzzle, err := game.BuildPuzzle(gameOptions...)
exitIfError(err)
err = puzzle.Run()
exitIfError(err)
}
func showUsage() {
fmt.Println(helpBody)
flag.Usage()
os.Exit(0)
}
func exitIfError(err error) {
if err != nil {
exit(err)
}
}
func exit(err error) {
output.FprintColor(os.Stderr, fmt.Sprintf("ERROR: %s\n", err.Error()), output.Red)
os.Exit(1)
}