Skip to content

Commit 1f2c565

Browse files
authored
calculate player with best average count lag (#147)
* only consider player leader if they lead the entire time * more
1 parent 80e37aa commit 1f2c565

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

internal/gameServer/server.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ func (g *GameServer) averageBufferHealth(playerNumber int) (float32, error) {
107107
}
108108
}
109109

110+
func (g *GameServer) averageCountLag(playerNumber int) (float32, error) {
111+
defer func() { g.gameData.countLag[playerNumber] = g.gameData.countLag[playerNumber][:0] }()
112+
113+
if len(g.gameData.countLag[playerNumber]) > 0 {
114+
var total float32
115+
for _, value := range g.gameData.countLag[playerNumber] {
116+
total += float32(value)
117+
}
118+
return total / float32(len(g.gameData.countLag[playerNumber])), nil
119+
} else {
120+
return 0, fmt.Errorf("no count lag data for player %d", playerNumber)
121+
}
122+
}
123+
110124
func (g *GameServer) ManageBuffer() {
111125
for {
112126
if !g.Running {
@@ -115,14 +129,18 @@ func (g *GameServer) ManageBuffer() {
115129
}
116130

117131
var bufferHealth float32 = -1.0
132+
var countLag float32 = 255
118133
var leadPlayer int
119134
g.gameDataMutex.Lock() // BufferHealth can be modified by processUDP in a different thread
120135
for i := range 4 {
121-
var err error
122-
g.gameData.averageBufferHealth[i], err = g.averageBufferHealth(i)
123-
if err == nil && g.gameData.countLag[i] == 0 {
136+
var errBufferHeatlh error
137+
var errCountLag error
138+
g.gameData.averageBufferHealth[i], errBufferHeatlh = g.averageBufferHealth(i)
139+
g.gameData.averageCountLag[i], errCountLag = g.averageCountLag(i)
140+
if errBufferHeatlh == nil && errCountLag == nil {
141+
// if leadPlayer == 0 {
124142
// if g.gameData.averageBufferHealth[i] > bufferHealth {
125-
if leadPlayer == 0 {
143+
if g.gameData.averageCountLag[i] < countLag {
126144
bufferHealth = g.gameData.averageBufferHealth[i]
127145
leadPlayer = i + 1
128146
}
@@ -155,7 +173,7 @@ func (g *GameServer) ManagePlayers() {
155173
_, ok := g.registrations[i]
156174
if ok {
157175
if g.gameData.playerAlive[i] {
158-
g.Logger.Info("player status", "player", i, "regID", g.registrations[i].regID, "bufferHealth", g.gameData.averageBufferHealth[i], "bufferSize", g.gameData.bufferSize, "countLag", g.gameData.countLag[i], "address", g.gameData.playerAddresses[i])
176+
g.Logger.Info("player status", "player", i, "regID", g.registrations[i].regID, "bufferHealth", g.gameData.averageBufferHealth[i], "bufferSize", g.gameData.bufferSize, "countLag", g.gameData.averageCountLag[i], "address", g.gameData.playerAddresses[i])
159177
playersActive = true
160178
} else {
161179
g.Logger.Info("player disconnected UDP", "player", i, "regID", g.registrations[i].regID, "address", g.gameData.playerAddresses[i])

internal/gameServer/udp.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ type GameData struct {
2323
playerAddresses [4]*net.UDPAddr
2424
bufferSize uint32
2525
averageBufferHealth [4]float32
26+
averageCountLag [4]float32
2627
bufferHealth [4][]byte
2728
inputs [4]*lru.Cache[uint32, InputData]
2829
pendingInput [4]InputData
29-
countLag [4]uint32
30+
countLag [4][]uint32
3031
sendBuffer []byte
3132
recvBuffer []byte
3233
playerAlive [4]bool
@@ -149,8 +150,8 @@ func (g *GameServer) processUDP(addr *net.UDPAddr) {
149150
g.Logger.Error(err, "could not process request", "regID", regID)
150151
return
151152
}
152-
g.gameData.countLag[sendingPlayerNumber] = g.sendUDPInput(count, addr, playerNumber, spectator != 0, sendingPlayerNumber)
153-
g.gameDataMutex.Lock() // PlayerAlive and BufferHealth can be modified in different threads
153+
g.gameDataMutex.Lock() // playerAlive, countLag and bufferHealth can be modified in different threads
154+
g.gameData.countLag[sendingPlayerNumber] = append(g.gameData.countLag[sendingPlayerNumber], g.sendUDPInput(count, addr, playerNumber, spectator != 0, sendingPlayerNumber))
154155
g.gameData.bufferHealth[sendingPlayerNumber] = append(g.gameData.bufferHealth[sendingPlayerNumber], g.gameData.recvBuffer[11])
155156
g.gameData.playerAlive[sendingPlayerNumber] = true
156157
g.gameDataMutex.Unlock()
@@ -214,6 +215,7 @@ func (g *GameServer) createUDPServer() error {
214215
for i := range 4 {
215216
g.gameData.inputs[i], _ = lru.New[uint32, InputData](InputDataMax)
216217
g.gameData.bufferHealth[i] = make([]byte, 0, InputDataMax)
218+
g.gameData.countLag[i] = make([]uint32, 0, InputDataMax)
217219
}
218220
g.gameData.syncValues, _ = lru.New[uint32, []byte](100) // Store up to 100 sync values
219221
g.gameData.sendBuffer = make([]byte, 508)

0 commit comments

Comments
 (0)