forked from erbridge/botcommentator
/
main.go
144 lines (116 loc) · 3.97 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/ChimeraCoder/anaconda"
"github.com/erbridge/gotwit"
"github.com/erbridge/gotwit/callback"
"github.com/erbridge/gotwit/twitter"
"github.com/erbridge/botcommentator/connect4"
)
const (
Blank = `⬜`
Sun = `🌞`
Moon = `🌚`
)
var (
boardRegexp = regexp.MustCompile("(?sm)\n\n(.*)\n1⃣")
pieceRegexp = regexp.MustCompile(fmt.Sprintf("%s|(%s)|(%s)", Blank, Sun, Moon))
turnCountRegexp = regexp.MustCompile("^Move[[:space:]]*([[:digit:]]+):[[:space:]]*")
sunStartsRegexp = regexp.MustCompile("Sun[[:space:]]*to[[:space:]]*Play[[:space:]]*$")
moonStartsRegexp = regexp.MustCompile("Moon[[:space:]]*to[[:space:]]*Play[[:space:]]*$")
sunTurnRegexp = regexp.MustCompile("Sun's[[:space:]]*Turn[[:space:]]*$")
moonTurnRegexp = regexp.MustCompile("Moon's[[:space:]]*Turn[[:space:]]*$")
sunWinRegexp = regexp.MustCompile("Sun[[:space:]]*Wins!*[[:space:]]*$")
moonWinRegexp = regexp.MustCompile("Moon[[:space:]]*Wins!*[[:space:]]*$")
nullGameRegexp = regexp.MustCompile("Null[[:space:]]*Game[[:space:]]*$")
)
func createMassConnect4Callback(b *gotwit.Bot) func(anaconda.Tweet) {
return func(t anaconda.Tweet) {
if t.User.ScreenName != "massconnect4" {
return
}
text := ""
if sunStartsRegexp.MatchString(t.Text) {
text += "Good morning, folks! Time for another gripping game of Mass Connect 4. Sun goes first this time."
} else if moonStartsRegexp.MatchString(t.Text) {
text += "Good evening, folks! Time for another gripping game of Mass Connect 4. Moon goes first this time."
} else if sunWinRegexp.MatchString(t.Text) {
text += "Looks like the sun's shining today. A great game from two great teams."
} else if moonWinRegexp.MatchString(t.Text) {
text += "It's been a long night, but the moon's as bright as ever. Well played by both teams."
} else if nullGameRegexp.MatchString(t.Text) {
text += "They're calling the match off. Looks like they just couldn't take the pressure."
} else if boardStrings := boardRegexp.FindStringSubmatch(t.Text); boardStrings != nil {
var turnCount uint64 = 0
if turnCountStrings := turnCountRegexp.FindStringSubmatch(t.Text); turnCountStrings != nil {
turnCount, _ = strconv.ParseUint(turnCountStrings[1], 10, 0)
}
if turnCount == 1 {
text += "And we're off!"
} else if turnCount > 10 {
nextPiece := 0
nextTeam := ""
lastTeam := ""
if sunTurnRegexp.MatchString(t.Text) {
nextPiece = 1
nextTeam = "Sun"
lastTeam = "Moon"
} else if moonTurnRegexp.MatchString(t.Text) {
nextPiece = -1
nextTeam = "Moon"
lastTeam = "Sun"
}
weightedCount, count := countWins(boardStrings[1], nextPiece)
if prop := float32(nextPiece) * float32(weightedCount) / float32(count); prop > 0.9 {
text += fmt.Sprintf("Things are looking good for %s.", nextTeam)
} else if prop < -0.9 {
text += fmt.Sprintf("%s's pulling away. Can %s come back from this?", lastTeam, nextTeam)
}
}
}
if text == "" {
return
}
text += fmt.Sprintf(" https://twitter.com/%s/status/%s", t.User.ScreenName, t.IdStr)
b.Post(text, false)
}
}
func countWins(boardString string, piece int) (weightedCount, count int) {
board := connect4.NewBoard(6, 7)
rows := strings.Split(boardString, "\n")
for rowIdx := len(rows) - 1; rowIdx >= 0; rowIdx-- {
matches := pieceRegexp.FindAllStringSubmatch(rows[rowIdx], -1)
for colIdx, m := range matches {
piece := 0
if m[1] != "" {
piece = 1
} else if m[2] != "" {
piece = -1
} else {
continue
}
board.Play(piece, colIdx)
}
}
return board.CountWins(piece, 8)
}
func main() {
var (
con twitter.ConsumerConfig
acc twitter.AccessConfig
)
f := "secrets.json"
if _, err := os.Stat(f); err == nil {
con, acc, _ = twitter.LoadConfigFile(f)
} else {
con, acc, _ = twitter.LoadConfigEnv()
}
b := gotwit.NewBot("BotCommentator", con, acc)
b.RegisterCallback(callback.Post, createMassConnect4Callback(&b))
b.Start()
b.Stop()
}