Cleanup and attempt to not clobber config
This commit is contained in:
2
chess.go
2
chess.go
@ -59,6 +59,7 @@ func chessCommand(m chat1.MsgSummary) {
|
|||||||
game.Move = true
|
game.Move = true
|
||||||
game.StartTime = time.Now()
|
game.StartTime = time.Now()
|
||||||
config.Games[convID] = game
|
config.Games[convID] = game
|
||||||
|
saveConfig()
|
||||||
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("I have created a new game for this conversation @%+v is playing as White, @%+v is playing as Black.", game.White, game.Black))
|
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("I have created a new game for this conversation @%+v is playing as White, @%+v is playing as Black.", game.White, game.Black))
|
||||||
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("Possible commands are `@chessbot show` to show the board, or `@chessbot forfeit`. Otherwise I will be expecting a move in Algebraic Notation."))
|
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("Possible commands are `@chessbot show` to show the board, or `@chessbot forfeit`. Otherwise I will be expecting a move in Algebraic Notation."))
|
||||||
|
|
||||||
@ -103,6 +104,7 @@ func showBoard(m chat1.MsgSummary, g *Game) {
|
|||||||
|
|
||||||
func submitMove(m chat1.MsgSummary, g *Game) {
|
func submitMove(m chat1.MsgSummary, g *Game) {
|
||||||
defer log.PanicSafe()
|
defer log.PanicSafe()
|
||||||
|
defer saveConfig()
|
||||||
parts := strings.Split(m.Content.Text.Body, " ")
|
parts := strings.Split(m.Content.Text.Body, " ")
|
||||||
if g.Move {
|
if g.Move {
|
||||||
if m.Sender.Username != g.White {
|
if m.Sender.Username != g.White {
|
||||||
|
|||||||
12
config.go
12
config.go
@ -5,20 +5,16 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loadConfig() {
|
func loadConfig() Config {
|
||||||
var c Config
|
var c Config
|
||||||
confFile, _ := ioutil.ReadFile(configFile)
|
confFile, _ := ioutil.ReadFile(configFile)
|
||||||
err := json.Unmarshal([]byte(confFile), &c)
|
_ = json.Unmarshal([]byte(confFile), &c)
|
||||||
if err != nil {
|
return c
|
||||||
log.LogErrorType(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
config = c
|
|
||||||
log.LogInfo("Setup completed using config file.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveConfig() {
|
func saveConfig() {
|
||||||
defer log.PanicSafe()
|
defer log.PanicSafe()
|
||||||
|
log.LogInfo("Saving config.")
|
||||||
file, err := json.Marshal(config)
|
file, err := json.Marshal(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.LogErrorType(err)
|
log.LogErrorType(err)
|
||||||
|
|||||||
4
main.go
4
main.go
@ -19,14 +19,13 @@ var (
|
|||||||
k = keybase.NewKeybase()
|
k = keybase.NewKeybase()
|
||||||
|
|
||||||
configFile = "config.json"
|
configFile = "config.json"
|
||||||
config Config
|
config = loadConfig()
|
||||||
log = loggy.NewLogger(config.LogOpts)
|
log = loggy.NewLogger(config.LogOpts)
|
||||||
r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
)
|
)
|
||||||
|
|
||||||
func printChat(m chat1.MsgSummary) {
|
func printChat(m chat1.MsgSummary) {
|
||||||
defer log.PanicSafe()
|
defer log.PanicSafe()
|
||||||
defer saveConfig()
|
|
||||||
if m.Sender.Username == k.Username {
|
if m.Sender.Username == k.Username {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -38,7 +37,6 @@ func printChat(m chat1.MsgSummary) {
|
|||||||
}
|
}
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Loading config.")
|
fmt.Println("Loading config.")
|
||||||
loadConfig()
|
|
||||||
fmt.Println("Starting log")
|
fmt.Println("Starting log")
|
||||||
log = loggy.NewLogger(config.LogOpts)
|
log = loggy.NewLogger(config.LogOpts)
|
||||||
fmt.Println("Log configured")
|
fmt.Println("Log configured")
|
||||||
|
|||||||
Reference in New Issue
Block a user