Fix bug in race results (maybe)

main
DustyP 9 months ago
parent 29993a4d56
commit f33bb7e9a3

@ -186,7 +186,6 @@ func (dc *DerbyClock) readLoop() {
if len(buffer) >= 3 && string(buffer[len(buffer)-3:]) == "C\r\n" {
dc.mu.Lock()
dc.status = StatusRunning
dc.results = nil
dc.mu.Unlock()
// Send race start event
@ -204,14 +203,6 @@ func (dc *DerbyClock) readLoop() {
// Try to extract a result from the buffer
result := dc.tryExtractResult(buffer)
if result != nil {
dc.mu.Lock()
// Add result to our stored results
dc.results = append(dc.results, result)
// Determine finish place based on how many results we have
result.FinishPlace = len(dc.results)
dc.mu.Unlock()
// Send lane finish event
dc.eventChan <- Event{
Type: EventLaneFinish,
@ -282,11 +273,24 @@ func (dc *DerbyClock) tryExtractResult(buffer []byte) *Result {
continue
}
finishPlace := 0
switch finishChar {
case "!":
finishPlace = 1
case "\"":
finishPlace = 2
case "#":
finishPlace = 3
case "$":
finishPlace = 4
}
// Create and return the result
return &Result{
Lane: lane,
Time: timeVal,
FinishPlace: 0, // Will be set by the caller
FinishPlace: finishPlace,
FinishSymbol: finishChar,
}
}

@ -47,6 +47,7 @@ func main() {
// Process events from the clock
go func() {
raceResults := make([]*derby.Result, 0)
for event := range clock.Events() {
switch event.Type {
case derby.EventRaceStart:
@ -56,14 +57,16 @@ func main() {
result := event.Result
fmt.Printf("🚗 Lane %d finished in place %d with time %.4f seconds\n",
result.Lane, result.FinishPlace, result.Time)
raceResults = append(raceResults, result)
case derby.EventRaceComplete:
fmt.Println("\n🏆 Race complete! Final results:")
for _, result := range clock.Results() {
for _, result := range raceResults {
fmt.Printf("Place %d: Lane %d - %.4f seconds\n",
result.FinishPlace, result.Lane, result.Time)
}
fmt.Println("\nEnter command (r/f/q/?):")
raceResults = nil
}
}
}()

Loading…
Cancel
Save