mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 21:56:50 -04:00
Use sequential new display IDs to preserve sorting on setup page.
This commit is contained in:
@@ -7,7 +7,6 @@ package field
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"sort"
|
||||
@@ -18,7 +17,6 @@ import (
|
||||
|
||||
const (
|
||||
minDisplayId = 100
|
||||
maxDisplayId = 999
|
||||
)
|
||||
|
||||
type DisplayType int
|
||||
@@ -136,11 +134,12 @@ func (arena *Arena) NextDisplayId() string {
|
||||
|
||||
// Loop until we get an ID that isn't already used. This is inefficient if there is a large number of displays, but
|
||||
// that should never be the case.
|
||||
candidateId := minDisplayId
|
||||
for {
|
||||
candidateId := strconv.Itoa(rand.Intn(maxDisplayId+1-minDisplayId) + minDisplayId)
|
||||
if _, ok := arena.Displays[candidateId]; !ok {
|
||||
return candidateId
|
||||
if _, ok := arena.Displays[strconv.Itoa(candidateId)]; !ok {
|
||||
return strconv.Itoa(candidateId)
|
||||
}
|
||||
candidateId++
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,8 +193,7 @@ func (arena *Arena) MarkDisplayDisconnected(display *Display) {
|
||||
delete(arena.Displays, existingDisplay.Id)
|
||||
} else {
|
||||
existingDisplay.ConnectionCount -= 1
|
||||
arena.DisplayConfigurationNotifier.Notify()
|
||||
|
||||
}
|
||||
arena.DisplayConfigurationNotifier.Notify()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,12 +59,11 @@ func TestDisplayToUrl(t *testing.T) {
|
||||
func TestNextDisplayId(t *testing.T) {
|
||||
arena := setupTestArena(t)
|
||||
|
||||
assert.Equal(t, "874", arena.NextDisplayId())
|
||||
assert.Equal(t, "100", arena.NextDisplayId())
|
||||
|
||||
// The next random numbers for the test seed are 514 and 653; check that a number is skipped if already used.
|
||||
display := &Display{Id: "514"}
|
||||
display := &Display{Id: "100"}
|
||||
arena.RegisterDisplay(display)
|
||||
assert.Equal(t, "653", arena.NextDisplayId())
|
||||
assert.Equal(t, "101", arena.NextDisplayId())
|
||||
}
|
||||
|
||||
func TestDisplayRegisterUnregister(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user