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

60
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. 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. 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) setCounter(c - 1)
if c-1 == 0 { if c-1 == 0 {
locked = true 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 { } else {
sendMessage(conn, channel, fmt.Sprintf("*tick, tick, tick* The timer ticks down. It now reads %d", c-1)) 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" { if msgContent == ",ping" {
sendMessage(&conn, channel, "Pong!\r\n") sendMessage(&conn, channel, "Pong!\r\n")
} else if msgContent == ",scores" { } else if msgContent == ",scores" {
// Make an empty starting string sendMessage(&conn, channel, scoreString())
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)
} else if msgContent == ",press" { } else if msgContent == ",press" {
// TODO score these // TODO score these
c := getCounter() c := getCounter()
if c != 0 { if c != 0 {
reset <- 0 reset <- 0
points := resetCount - c points := (resetCount - c)*(resetCount - c)
scores[msgSender] += points scores[msgSender] += points
sendMessage(&conn, channel, fmt.Sprintf("%s presses the button for %d points. The countdown resets to %d", msgSender, points, resetCount)) sendMessage(&conn, channel, fmt.Sprintf("%s presses the button for %d points. The countdown resets to %d", msgSender, points, resetCount))
} else { } else {
@ -290,9 +298,13 @@ func main() {
} }
} else if msgContent == ",reset" { } else if msgContent == ",reset" {
// TODO maybe check some kind of auth on doing this // TODO maybe check some kind of auth on doing this
reset <- 0 if getCounter() == 0 {
scores = map[string]int{} // empty the scoreboard reset <- 0
sendMessage(&conn, channel, "Timer reset!") 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" { } else if msgContent == ",count" {
sendMessage(&conn, channel, fmt.Sprintf("You look up at the ominous countdown. It reads %d", getCounter())) sendMessage(&conn, channel, fmt.Sprintf("You look up at the ominous countdown. It reads %d", getCounter()))
} else if msgContent == ",help" { } else if msgContent == ",help" {