mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -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) {
|
||||
|
||||
@@ -17,7 +17,7 @@ func TestAllianceStationDisplay(t *testing.T) {
|
||||
|
||||
recorder := web.getHttpResponse("/displays/alliance_station")
|
||||
assert.Equal(t, 302, recorder.Code)
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=874")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=100")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "station=R1")
|
||||
|
||||
recorder = web.getHttpResponse("/displays/alliance_station?displayId=1&station=B1")
|
||||
|
||||
@@ -15,7 +15,7 @@ func TestAudienceDisplay(t *testing.T) {
|
||||
|
||||
recorder := web.getHttpResponse("/displays/audience")
|
||||
assert.Equal(t, 302, recorder.Code)
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=874")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=100")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "background=%230f0")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "reversed=false")
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestPlaceholderDisplay(t *testing.T) {
|
||||
|
||||
recorder := web.getHttpResponse("/displays/audience")
|
||||
assert.Equal(t, 302, recorder.Code)
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=874")
|
||||
assert.Contains(t, recorder.Header().Get("Location"), "displayId=100")
|
||||
|
||||
recorder = web.getHttpResponse("/display?displayId=1")
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
|
||||
Reference in New Issue
Block a user