-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (67 loc) · 1.93 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"github.com/JCGrant/twitch-paints/canvas"
"github.com/JCGrant/twitch-paints/database"
"github.com/JCGrant/twitch-paints/pixels"
"github.com/JCGrant/twitch-paints/twitch"
)
var configPath = flag.String("config", "config.json", "path to config.json")
func main() {
flag.Parse()
if *configPath == "" {
log.Fatalln("no path to config.json supplied")
}
twitchConfig, err := twitch.ReadConfig(*configPath)
if err != nil {
log.Fatalln(err)
}
windowWidth := mustGetIntEnvVar("WIDTH")
windowHeight := mustGetIntEnvVar("HEIGHT")
twitchMessages := make(chan twitch.Message)
canvasPixels := make(chan pixels.Pixel)
backupPixels := make(chan pixels.Pixel)
dbPath := "pixels.json"
db := database.New(windowWidth, windowHeight)
err = db.LoadPixels(dbPath)
if err != nil {
log.Printf("loading pixels failed: %s\n", err)
log.Printf("recreating %s\n", dbPath)
db.SavePixels(dbPath)
err = db.LoadPixels(dbPath)
if err != nil {
log.Fatalln("loading databse failed: ", err)
}
}
go twitch.Run(twitchConfig, nil, twitchMessages)
// Get twitch message, parse them to pixels, and pass pixels to other channels
go func() {
for msg := range twitchMessages {
if p, err := parseMessage(msg.Text); err == nil && isValidPixel(p, windowWidth, windowHeight) {
log.Println(fmt.Sprintf("%s: %s", msg.Nickname, msg.Text))
canvasPixels <- p
backupPixels <- p
}
}
}()
go database.Run(backupPixels, dbPath, db)
canvas.Run(canvasPixels, windowWidth, windowHeight, db.Pixels())
}
func parseMessage(msg string) (pixels.Pixel, error) {
return pixels.FromString(msg)
}
func isValidPixel(p pixels.Pixel, windowWidth, windowHeight int) bool {
return p.X >= 0 && p.X < windowWidth && p.Y >= 0 && p.Y < windowHeight
}
func mustGetIntEnvVar(name string) int {
valueStr := os.Getenv(name)
value, err := strconv.Atoi(valueStr)
if err != nil {
log.Fatalln(err)
}
return value
}