Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
main ▾

Modern-Apps.git / scripts / unblockjam / level_tools / src / cmd / convert_set / main.go

main.go89 lines · 2.0 KiBRaw
1package main
2
3import (
4 "bufio"
5 "encoding/json"
6 "fmt"
7 "os"
8 "strconv"
9 "strings"
10
11 "rush_conv"
12)
13
14type Colors struct {
15 Primary string `json:"primary"`
16 Secondary string `json:"secondary"`
17 Tertiary string `json:"tertiary"`
18 Background string `json:"background"`
19 Surface string `json:"surface"`
20 PrimaryContainer string `json:"primaryContainer"`
21 SecondaryContainer string `json:"secondaryContainer"`
22}
23
24type Pack struct {
25 Name string `json:"name"`
26 Colors Colors `json:"colors"`
27 Levels []rush_conv.Level `json:"levels"`
28}
29
30func main() {
31 if err := run(); err != nil {
32 fmt.Fprintln(os.Stderr, err)
33 os.Exit(1)
34 }
35}
36
37func run() error {
38 var levels []rush_conv.Level
39
40 scanner := bufio.NewScanner(os.Stdin)
41 for scanner.Scan() {
42 line := scanner.Text()
43 parts := strings.Fields(line)
44 if len(parts) != 3 {
45 return fmt.Errorf("expected exactly 3 parts in line %q, got %d", line, len(parts))
46 }
47
48 // Fogleman's database columns are: number of moves, board description, cluster size.
49 optimal, err := strconv.Atoi(parts[0])
50 if err != nil {
51 return fmt.Errorf("converting optimal value to integer in line %q: %w", line, err)
52 }
53
54 puzzle := parts[1]
55 board := rush_conv.Convert(puzzle)
56
57 level := rush_conv.Level{
58 Board: board,
59 ID: rush_conv.BoardID(board),
60 Moves: optimal,
61 }
62
63 levels = append(levels, level)
64 }
65 if err := scanner.Err(); err != nil {
66 return fmt.Errorf("reading stdin: %w", err)
67 }
68
69 result := Pack{
70 Name: "Example Pack",
71 Colors: Colors{
72 Primary: "0x00000000",
73 Secondary: "0x00000000",
74 Tertiary: "0x00000000",
75 Background: "0x00000000",
76 Surface: "0x00000000",
77 PrimaryContainer: "0x00000000",
78 SecondaryContainer: "0x00000000",
79 },
80 Levels: levels,
81 }
82
83 encoder := json.NewEncoder(os.Stdout)
84 encoder.SetIndent("", " ")
85 if err := encoder.Encode(result); err != nil {
86 return fmt.Errorf("encoding JSON: %w", err)
87 }
88 return nil
89}