@@ -107,6 +107,20 @@ func (g *GameServer) averageBufferHealth(playerNumber int) (float32, error) {
107
107
}
108
108
}
109
109
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
+
110
124
func (g * GameServer ) ManageBuffer () {
111
125
for {
112
126
if ! g .Running {
@@ -115,14 +129,18 @@ func (g *GameServer) ManageBuffer() {
115
129
}
116
130
117
131
var bufferHealth float32 = - 1.0
132
+ var countLag float32 = 255
118
133
var leadPlayer int
119
134
g .gameDataMutex .Lock () // BufferHealth can be modified by processUDP in a different thread
120
135
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 {
124
142
// if g.gameData.averageBufferHealth[i] > bufferHealth {
125
- if leadPlayer == 0 {
143
+ if g . gameData . averageCountLag [ i ] < countLag {
126
144
bufferHealth = g .gameData .averageBufferHealth [i ]
127
145
leadPlayer = i + 1
128
146
}
@@ -155,7 +173,7 @@ func (g *GameServer) ManagePlayers() {
155
173
_ , ok := g .registrations [i ]
156
174
if ok {
157
175
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 ])
159
177
playersActive = true
160
178
} else {
161
179
g .Logger .Info ("player disconnected UDP" , "player" , i , "regID" , g .registrations [i ].regID , "address" , g .gameData .playerAddresses [i ])
0 commit comments