Skip to content

Commit 6b817db

Browse files
committed
players: fix weapon attribution (#148)
1 parent 690bda0 commit 6b817db

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

datatables.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,14 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
276276
}
277277
cache[i2] = entityID
278278

279-
// Attribute weapon to player
280279
wep := &p.weapons[entityID]
280+
281+
// Clear previous owner
282+
if wep.Owner != nil {
283+
delete(wep.Owner.RawWeapons, wep.EntityID)
284+
}
285+
286+
// Attribute weapon to player
281287
wep.Owner = pl
282288
pl.RawWeapons[entityID] = wep
283289
} else {
@@ -411,8 +417,10 @@ func (p *Parser) nadeProjectileDestroyed(proj *common.GrenadeProjectile) {
411417

412418
func (p *Parser) bindWeapon(entity *st.Entity, wepType common.EquipmentElement) {
413419
entityID := entity.ID()
420+
currentOwner := p.weapons[entityID].Owner
414421
p.weapons[entityID] = common.NewEquipment(wepType)
415422
eq := &p.weapons[entityID]
423+
eq.Owner = currentOwner
416424
eq.EntityID = entityID
417425
eq.AmmoInMagazine = -1
418426

game_events.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,7 @@ func mapGameEventData(d *msg.CSVCMsg_GameEventListDescriptorT, e *msg.CSVCMsg_Ga
545545

546546
// Returns the players instance of the weapon if applicable or a new instance otherwise.
547547
func getPlayerWeapon(player *common.Player, wepType common.EquipmentElement) *common.Equipment {
548-
class := wepType.Class()
549-
isSpecialWeapon := class == common.EqClassGrenade || (class == common.EqClassEquipment && wepType != common.EqKnife)
550-
if !isSpecialWeapon && player != nil {
548+
if player != nil {
551549
for _, wep := range player.Weapons() {
552550
if wep.Weapon == wepType {
553551
return wep

game_events_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,36 @@ func TestRoundEnd_LoserState_Score(t *testing.T) {
5353

5454
assert.Equal(t, 1, eventOccurred)
5555
}
56+
57+
func TestGetPlayerWeapon_NilPlayer(t *testing.T) {
58+
wep := getPlayerWeapon(nil, common.EqAK47)
59+
60+
assert.NotNil(t, wep)
61+
assert.Equal(t, common.EqAK47, wep.Weapon)
62+
}
63+
64+
func TestGetPlayerWeapon_Found(t *testing.T) {
65+
ak := &common.Equipment{Weapon: common.EqAK47}
66+
pl := &common.Player{
67+
RawWeapons: map[int]*common.Equipment{
68+
1: ak,
69+
},
70+
}
71+
72+
wep := getPlayerWeapon(pl, common.EqAK47)
73+
74+
assert.True(t, wep == ak)
75+
}
76+
77+
func TestGetPlayerWeapon_NotFound(t *testing.T) {
78+
ak := &common.Equipment{Weapon: common.EqAK47}
79+
pl := &common.Player{
80+
RawWeapons: map[int]*common.Equipment{
81+
1: ak,
82+
},
83+
}
84+
85+
wep := getPlayerWeapon(pl, common.EqM4A1)
86+
87+
assert.Equal(t, common.EqM4A1, wep.Weapon)
88+
}

0 commit comments

Comments
 (0)