diff --git a/main.go b/main.go index f237538..2367433 100644 --- a/main.go +++ b/main.go @@ -125,6 +125,31 @@ func parseMessage(s string) IRCMessage { } +/* + function scoreString formats the score list and returns a string. +*/ +func scoreString() string { + // Make an empty starting string + s := "" + // Initialise a pairlist for names and fill it + names := make(PairList, len(scores)) + i := 0 + for k, v := range scores { + names[i] = Pair{Key: k, Value: v} + i++ + } + // sort and format for sending + sort.Reverse(names) + for i, v := range names { + // append all the scores to it + s += fmt.Sprintf("%s: %d", v.Key, v.Value) + if i != len(names)-1 { + s += "," + } + } + return s +} + /* function sendMessage sends a message using connection conn on IRC channel c with content s. It first locks the connection mutex, and unlocks it afterwards, to make the function concurrency-safe. @@ -170,7 +195,8 @@ func countdown(conn *net.Conn, reset chan int) { setCounter(c - 1) if c-1 == 0 { locked = true - sendMessage(conn, channel, "Ker-Chunk! The timer ticked down to the final position. You're all dead.") + sendMessage(conn, channel, "Ker-Chunk! The timer ticked down to the final position. You're all dead.") + sendMessage(conn, channel, scoreString()) } else { sendMessage(conn, channel, fmt.Sprintf("*tick, tick, tick* The timer ticks down. It now reads %d", c-1)) } @@ -258,31 +284,13 @@ func main() { if msgContent == ",ping" { sendMessage(&conn, channel, "Pong!\r\n") } else if msgContent == ",scores" { - // Make an empty starting string - s := "" - // Initialise a pairlist for names and fill it - names := make(PairList, len(scores)) - i := 0 - for k, v := range scores { - names[i] = Pair{Key: k, Value: v} - i++ - } - // sort and format for sending - sort.Reverse(names) - for i, v := range names { - // append all the scores to it - s += fmt.Sprintf("%s: %d", v.Key, v.Value) - if i != len(names)-1 { - s += "," - } - } - sendMessage(&conn, channel, s) + sendMessage(&conn, channel, scoreString()) } else if msgContent == ",press" { // TODO score these c := getCounter() if c != 0 { reset <- 0 - points := resetCount - c + points := (resetCount - c)*(resetCount - c) scores[msgSender] += points sendMessage(&conn, channel, fmt.Sprintf("%s presses the button for %d points. The countdown resets to %d", msgSender, points, resetCount)) } else { @@ -290,9 +298,13 @@ func main() { } } else if msgContent == ",reset" { // TODO maybe check some kind of auth on doing this - reset <- 0 - scores = map[string]int{} // empty the scoreboard - sendMessage(&conn, channel, "Timer reset!") + if getCounter() == 0 { + reset <- 0 + scores = map[string]int{} // empty the scoreboard + sendMessage(&conn, channel, "Timer reset!") + } else { + sendMessage(&conn, channel, "You can't reset the timer while the game is running!") + } } else if msgContent == ",count" { sendMessage(&conn, channel, fmt.Sprintf("You look up at the ominous countdown. It reads %d", getCounter())) } else if msgContent == ",help" {