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.StartTime = time.Now()
|
||||
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("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) {
|
||||
defer log.PanicSafe()
|
||||
defer saveConfig()
|
||||
parts := strings.Split(m.Content.Text.Body, " ")
|
||||
if g.Move {
|
||||
if m.Sender.Username != g.White {
|
||||
|
||||
12
config.go
12
config.go
@ -5,20 +5,16 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func loadConfig() {
|
||||
func loadConfig() Config {
|
||||
var c Config
|
||||
confFile, _ := ioutil.ReadFile(configFile)
|
||||
err := json.Unmarshal([]byte(confFile), &c)
|
||||
if err != nil {
|
||||
log.LogErrorType(err)
|
||||
return
|
||||
}
|
||||
config = c
|
||||
log.LogInfo("Setup completed using config file.")
|
||||
_ = json.Unmarshal([]byte(confFile), &c)
|
||||
return c
|
||||
}
|
||||
|
||||
func saveConfig() {
|
||||
defer log.PanicSafe()
|
||||
log.LogInfo("Saving config.")
|
||||
file, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
log.LogErrorType(err)
|
||||
|
||||
4
main.go
4
main.go
@ -19,14 +19,13 @@ var (
|
||||
k = keybase.NewKeybase()
|
||||
|
||||
configFile = "config.json"
|
||||
config Config
|
||||
config = loadConfig()
|
||||
log = loggy.NewLogger(config.LogOpts)
|
||||
r = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
)
|
||||
|
||||
func printChat(m chat1.MsgSummary) {
|
||||
defer log.PanicSafe()
|
||||
defer saveConfig()
|
||||
if m.Sender.Username == k.Username {
|
||||
return
|
||||
}
|
||||
@ -38,7 +37,6 @@ func printChat(m chat1.MsgSummary) {
|
||||
}
|
||||
func main() {
|
||||
fmt.Println("Loading config.")
|
||||
loadConfig()
|
||||
fmt.Println("Starting log")
|
||||
log = loggy.NewLogger(config.LogOpts)
|
||||
fmt.Println("Log configured")
|
||||
|
||||
Reference in New Issue
Block a user