Skip to content

Commit 94b3361

Browse files
authored
fix various linting issues (#204)
1 parent 73494bd commit 94b3361

File tree

18 files changed

+90
-60
lines changed

18 files changed

+90
-60
lines changed

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ linters:
1414
- gochecknoglobals
1515
- lll
1616
- typecheck
17+
- gomnd
18+
issues:
19+
exclude-rules:
20+
# Exclude some linters from running on tests files.
21+
- path: _test\.go
22+
linters:
23+
- wsl
24+
- funlen
1725
linters-settings:
1826
gocritic:
1927
disabled-checks:

internal/constants/constants.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package constants
2+
3+
const (
4+
MaxEdictBits = 11
5+
EntityHandleIndexMask = (1 << MaxEdictBits) - 1
6+
EntityHandleSerialNumberBits = 10
7+
EntityHandleBits = MaxEdictBits + EntityHandleSerialNumberBits
8+
InvalidEntityHandle = (1 << EntityHandleBits) - 1
9+
)

pkg/demoinfocs/common/equipment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func init() {
122122
initEqElementToName()
123123
}
124124

125+
//nolint:funlen
125126
func initEqNameToWeapon() {
126127
eqNameToWeapon = make(map[string]EquipmentType)
127128

@@ -198,6 +199,7 @@ func initEqNameToWeapon() {
198199
eqNameToWeapon["worldspawn"] = EqWorld
199200
}
200201

202+
//nolint:funlen
201203
func initEqElementToName() {
202204
eqElementToName = make(map[EquipmentType]string)
203205
eqElementToName[EqAK47] = "AK-47"
@@ -333,7 +335,9 @@ func (e Equipment) ZoomLevel() ZoomLevel {
333335
return 0
334336
}
335337

338+
// if the property doesn't exist we return 0 by default
336339
val, _ := e.Entity.PropertyValue("m_zoomLevel")
340+
337341
return ZoomLevel(val.IntVal)
338342
}
339343

@@ -350,7 +354,9 @@ func (e Equipment) AmmoReserve() int {
350354
return 0
351355
}
352356

357+
// if the property doesn't exist we return 0 by default
353358
val, _ := e.Entity.PropertyValue("m_iPrimaryReserveAmmoCount")
359+
354360
return val.IntVal
355361
}
356362

pkg/demoinfocs/common/inferno.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ func (inf Inferno) UniqueID() int64 {
4949
func (inf Inferno) Fires() Fires {
5050
entity := inf.Entity
5151
origin := entity.Position()
52-
53-
var fires []Fire
5452
nFires := entity.PropertyValueMust("m_fireCount").IntVal
53+
fires := make([]Fire, 0, nFires)
5554

5655
for i := 0; i < nFires; i++ {
5756
iStr := fmt.Sprintf("%03d", i)

pkg/demoinfocs/common/player.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ import (
66

77
"github.com/golang/geo/r3"
88

9+
constants "github.com/markus-wa/demoinfocs-golang/v2/internal/constants"
910
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
1011
)
1112

12-
const (
13-
maxEdictBits = 11
14-
entityHandleIndexMask = (1 << maxEdictBits) - 1
15-
entityHandleSerialNumberBits = 10
16-
entityHandleBits = maxEdictBits + entityHandleSerialNumberBits
17-
invalidEntityHandle = (1 << entityHandleBits) - 1
18-
)
19-
2013
// Player contains mostly game-relevant player information.
2114
type Player struct {
2215
demoInfoProvider demoInfoProvider // provider for demo info such as tick-rate or current tick
@@ -76,7 +69,7 @@ func (p *Player) IsAirborne() bool {
7669

7770
groundEntityHandle := p.Entity.Property("m_hGroundEntity").Value().IntVal
7871

79-
return groundEntityHandle == invalidEntityHandle
72+
return groundEntityHandle == constants.InvalidEntityHandle
8073
}
8174

8275
// FlashDurationTime returns the duration of the blinding effect as time.Duration instead of float32 in seconds.
@@ -129,7 +122,7 @@ This isn't very conclusive but it looks like IsFlashed isn't super reliable curr
129122

130123
// Used internally to set the active weapon, see ActiveWeapon()
131124
func (p *Player) activeWeaponID() int {
132-
return getInt(p.Entity, "m_hActiveWeapon") & entityHandleIndexMask
125+
return getInt(p.Entity, "m_hActiveWeapon") & constants.EntityHandleIndexMask
133126
}
134127

135128
// ActiveWeapon returns the currently active / equipped weapon of the player.

pkg/demoinfocs/datatables.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@ import (
77
"github.com/golang/geo/r3"
88
"github.com/markus-wa/go-unassert"
99

10+
constants "github.com/markus-wa/demoinfocs-golang/v2/internal/constants"
1011
common "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
1112
events "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/events"
1213
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
1314
)
1415

15-
const (
16-
maxEdictBits = 11
17-
entityHandleIndexMask = (1 << maxEdictBits) - 1
18-
entityHandleSerialNumberBits = 10
19-
entityHandleBits = maxEdictBits + entityHandleSerialNumberBits
20-
invalidEntityHandle = (1 << entityHandleBits) - 1
21-
maxEntities = 1 << maxEdictBits
22-
maxPlayers = 64
23-
maxWeapons = 64
24-
)
25-
2616
func (p *parser) mapEquipment() {
2717
for _, sc := range p.stParser.ServerClasses() {
2818
switch sc.Name() {
@@ -215,6 +205,7 @@ func (p *parser) getOrCreatePlayer(entityID int, rp *playerInfo) (isNew bool, pl
215205
return isNew, player
216206
}
217207

208+
//nolint:funlen
218209
func (p *parser) bindNewPlayer(playerEntity st.Entity) {
219210
entityID := playerEntity.ID()
220211
rp := p.rawPlayers[entityID-1]
@@ -289,6 +280,8 @@ func (p *parser) bindNewPlayer(playerEntity st.Entity) {
289280
}
290281
}
291282

283+
const maxWeapons = 64
284+
292285
func (p *parser) bindPlayerWeapons(playerEntity st.Entity, pl *common.Player) {
293286
// Some demos have an additional prefix for player weapons weapon
294287
var wepPrefix string
@@ -303,8 +296,8 @@ func (p *parser) bindPlayerWeapons(playerEntity st.Entity, pl *common.Player) {
303296
for i := range cache {
304297
i2 := i // Copy for passing to handler
305298
playerEntity.Property(wepPrefix + fmt.Sprintf("%03d", i)).OnUpdate(func(val st.PropertyValue) {
306-
entityID := val.IntVal & entityHandleIndexMask
307-
if entityID != entityHandleIndexMask {
299+
entityID := val.IntVal & constants.EntityHandleIndexMask
300+
if entityID != constants.EntityHandleIndexMask {
308301
if cache[i2] != 0 {
309302
// Player already has a weapon in this slot.
310303
delete(pl.Inventory, cache[i2])
@@ -351,7 +344,7 @@ func (p *parser) bindWeapons() {
351344
case "CBaseCSGrenade":
352345
// @micvbang TODO: handle grenades dropped by dead player.
353346
// Grenades that were dropped by a dead player (and can be picked up by other players).
354-
}
347+
} //nolint:wsl
355348
}
356349
}
357350

@@ -368,7 +361,7 @@ func (p *parser) bindGrenadeProjectiles(entity st.Entity) {
368361
p.gameState.grenadeProjectiles[entityID] = proj
369362

370363
var wep common.EquipmentType
371-
entity.OnCreateFinished(func() {
364+
entity.OnCreateFinished(func() { //nolint:wsl
372365
// copy the weapon so it doesn't get overwritten by a new entity in parser.weapons
373366
wepCopy := *(getPlayerWeapon(proj.Thrower, wep))
374367
proj.WeaponInstance = &wepCopy

pkg/demoinfocs/debug_on.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func debugGameEvent(d *msg.CSVCMsg_GameEventListDescriptorT, ge *msg.CSVCMsg_Gam
3838
if debugGameEvents == yes {
3939
// Map only the relevant data for each type
4040
data := make(map[string]interface{})
41+
4142
for k, v := range mapGameEventData(d, ge) {
4243
switch v.Type {
4344
case typeStr:

pkg/demoinfocs/fake/parser.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,12 @@ func (p *Parser) ParseNextFrame() (b bool, err error) {
211211
}
212212

213213
func max(numbers map[int][]interface{}) (maxNumber int) {
214-
for maxNumber = range numbers {
215-
break
216-
}
217214
for n := range numbers {
218215
if n > maxNumber {
219216
maxNumber = n
220217
}
221218
}
219+
222220
return
223221
}
224222

pkg/demoinfocs/game_events.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package demoinfocs
33
import (
44
"errors"
55
"fmt"
6+
67
"github.com/golang/geo/r3"
78
"github.com/markus-wa/go-unassert"
89

@@ -22,6 +23,7 @@ func (p *parser) handleGameEvent(ge *msg.CSVCMsg_GameEvent) {
2223
if p.gameEventDescs == nil {
2324
p.eventDispatcher.Dispatch(events.ParserWarn{Message: "received GameEvent but event descriptors are missing"})
2425
unassert.Error("received GameEvent but event descriptors are missing")
26+
2527
return
2628
}
2729

@@ -71,6 +73,7 @@ func (geh gameEventHandler) playerByUserID32(userID int32) *common.Player {
7173

7274
type gameEventHandlerFunc func(map[string]*msg.CSVCMsg_GameEventKeyT)
7375

76+
//nolint:funlen
7477
func newGameEventHandler(parser *parser) gameEventHandler {
7578
geh := gameEventHandler{parser: parser}
7679

@@ -192,15 +195,15 @@ func (geh gameEventHandler) roundStart(data map[string]*msg.CSVCMsg_GameEventKey
192195
})
193196
}
194197

195-
func (geh gameEventHandler) csWinPanelMatch(data map[string]*msg.CSVCMsg_GameEventKeyT) {
198+
func (geh gameEventHandler) csWinPanelMatch(map[string]*msg.CSVCMsg_GameEventKeyT) {
196199
geh.dispatch(events.AnnouncementWinPanelMatch{})
197200
}
198201

199-
func (geh gameEventHandler) roundAnnounceFinal(data map[string]*msg.CSVCMsg_GameEventKeyT) {
202+
func (geh gameEventHandler) roundAnnounceFinal(map[string]*msg.CSVCMsg_GameEventKeyT) {
200203
geh.dispatch(events.AnnouncementFinalRound{})
201204
}
202205

203-
func (geh gameEventHandler) roundAnnounceLastRoundHalf(data map[string]*msg.CSVCMsg_GameEventKeyT) {
206+
func (geh gameEventHandler) roundAnnounceLastRoundHalf(map[string]*msg.CSVCMsg_GameEventKeyT) {
204207
geh.dispatch(events.AnnouncementLastRoundHalf{})
205208
}
206209

@@ -222,7 +225,7 @@ func (geh gameEventHandler) roundEnd(data map[string]*msg.CSVCMsg_GameEventKeyT)
222225
})
223226
}
224227

225-
func (geh gameEventHandler) roundOfficiallyEnded(data map[string]*msg.CSVCMsg_GameEventKeyT) {
228+
func (geh gameEventHandler) roundOfficiallyEnded(map[string]*msg.CSVCMsg_GameEventKeyT) {
226229
// Issue #42
227230
// Sometimes grenades & infernos aren't deleted / destroyed via entity-updates at the end of the round,
228231
// so we need to do it here for those that weren't.
@@ -263,11 +266,11 @@ func (geh gameEventHandler) botTakeover(data map[string]*msg.CSVCMsg_GameEventKe
263266
})
264267
}
265268

266-
func (geh gameEventHandler) beginNewMatch(data map[string]*msg.CSVCMsg_GameEventKeyT) {
269+
func (geh gameEventHandler) beginNewMatch(map[string]*msg.CSVCMsg_GameEventKeyT) {
267270
geh.dispatch(events.MatchStart{})
268271
}
269272

270-
func (geh gameEventHandler) roundFreezeEnd(data map[string]*msg.CSVCMsg_GameEventKeyT) {
273+
func (geh gameEventHandler) roundFreezeEnd(map[string]*msg.CSVCMsg_GameEventKeyT) {
271274
geh.dispatch(events.RoundFreezetimeEnd{})
272275
}
273276

pkg/demoinfocs/game_state.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package demoinfocs
22

33
import (
4+
constants "github.com/markus-wa/demoinfocs-golang/v2/internal/constants"
45
common "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/common"
56
st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
67
)
@@ -180,6 +181,7 @@ type participants struct {
180181
// Includes spectators.
181182
func (ptcp participants) ByUserID() map[int]*common.Player {
182183
res := make(map[int]*common.Player)
184+
183185
for k, v := range ptcp.playersByUserID {
184186
// We need to check if the player entity hasn't been destroyed yet
185187
// See https://github.com/markus-wa/demoinfocs-golang/issues/98
@@ -269,11 +271,11 @@ func (ptcp participants) TeamMembers(team common.Team) []*common.Player {
269271
//
270272
// Returns nil if not found or if handle == invalidEntityHandle (used when referencing no entity).
271273
func (ptcp participants) FindByHandle(handle int) *common.Player {
272-
if handle == invalidEntityHandle {
274+
if handle == constants.InvalidEntityHandle {
273275
return nil
274276
}
275277

276-
entityID := handle & entityHandleIndexMask
278+
entityID := handle & constants.EntityHandleIndexMask
277279
player := ptcp.playersByEntityID[entityID]
278280

279281
if player == nil {

0 commit comments

Comments
 (0)