potential fix for resetting while game is running

This commit is contained in:
Nico 2021-04-10 19:20:21 +01:00
parent 999ec57197
commit 4afce3410b
1 changed files with 36 additions and 24 deletions

58
main.go
View File

@ -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.
@ -171,6 +196,7 @@ func countdown(conn *net.Conn, reset chan int) {
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, 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" {