server/kill_experience_outcome_test.go (9276B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "sync" 6 "testing" 7 "time" 8 ) 9 10 func TestKillExperienceKeepsCumulativeProgressAcrossThresholds(t *testing.T) { 11 for _, scenario := range []struct { 12 name string 13 level, nextLevel uint16 14 progress, reward, nextProgress, points uint32 15 total, delta uint32 16 }{ 17 {"same level", 2, 2, 50, 100, 150, 1500, 1600, 100}, 18 {"multiple levels", 1, 3, 1400, 5000, 2350, 9500, 6400, 5000}, 19 {"last level", 199, 200, 73393850, 100, 0, 151500, 2061453150, 50}, 20 } { 21 t.Run(scenario.name, func(t *testing.T) { 22 world, output := newKillExperienceWorld(t, "unarmed") 23 player, target := world.characters[1], world.characters[1000000] 24 player.level, player.experience = scenario.level, scenario.progress 25 target.level = scenario.level 26 profile := *target.combatProfile 27 profile.Experience = scenario.reward 28 target.combatProfile = &profile 29 if err := world.saveUser(player); err != nil { 30 t.Fatal(err) 31 } 32 33 attackForExperience(t, world, "unarmed") 34 restored := newTestInventoryServer(t).characters[1] 35 36 if player.level != scenario.nextLevel || player.experience != scenario.nextProgress || player.stats.ImprovementPoints != scenario.points || 37 restored.level != scenario.nextLevel || restored.experience != scenario.nextProgress || restored.stats.ImprovementPoints != scenario.points { 38 t.Fatalf("progression: level=%d XP=%d IP=%d", player.level, player.experience, player.stats.ImprovementPoints) 39 } 40 awards := 0 41 for _, packet := range output.packets { 42 switch binary.BigEndian.Uint32(packet[16:20]) { 43 case 0x7f405a16: 44 awards++ 45 if scenario.nextLevel == scenario.level || binary.BigEndian.Uint32(packet[37:41]) != scenario.total || 46 binary.BigEndian.Uint32(packet[49:53]) != 0 || binary.BigEndian.Uint32(packet[57:61]) != scenario.delta { 47 t.Fatal("level event used the wrong cumulative XP or net gain") 48 } 49 case 0x2b333d6e: 50 if binary.BigEndian.Uint32(packet[29:33]) == 52 { 51 awards++ 52 if scenario.nextLevel != scenario.level || binary.BigEndian.Uint32(packet[33:37]) != scenario.total { 53 t.Fatal("same-level award lost its cumulative XP baseline") 54 } 55 } 56 } 57 } 58 if awards != 1 { 59 t.Fatalf("native award events: %d", awards) 60 } 61 }) 62 } 63 } 64 65 func TestKillExperienceRejectsAnUnrepresentableIPGrant(t *testing.T) { 66 world, _ := newKillExperienceWorld(t, "unarmed") 67 player, target := world.characters[1], world.characters[1000000] 68 player.experience, player.stats.ImprovementPoints = 1400, 4294967295 69 if err := world.saveUser(player); err != nil { 70 t.Fatal(err) 71 } 72 73 attackForExperience(t, world, "unarmed") 74 restored := newTestInventoryServer(t) 75 76 if player.experience != 1400 || player.level != 1 || player.stats.ImprovementPoints != 4294967295 || target.health != 1 || target.corpse != nil || 77 restored.characters[1].experience != 1400 || restored.characters[1].level != 1 || restored.characters[1].stats.ImprovementPoints != 4294967295 || 78 restored.characters[1000000].health != 1 { 79 t.Fatal("IP overflow wrapped a counter or committed part of the killing action") 80 } 81 } 82 83 func TestKillExperienceConcurrentCompletionsAwardOnce(t *testing.T) { 84 for _, attack := range []string{"unarmed", "fling", "fast", "nano"} { 85 t.Run(attack, func(t *testing.T) { 86 world, output := newKillExperienceWorld(t, attack) 87 player := world.characters[1] 88 player.experience = 1400 89 if err := world.saveUser(player); err != nil { 90 t.Fatal(err) 91 } 92 at := time.Now() 93 if attack == "unarmed" { 94 if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil { 95 t.Fatal(err) 96 } 97 at = player.nextAttack 98 } else if attack == "nano" { 99 if err := world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29618})[20:]); err != nil { 100 t.Fatal(err) 101 } 102 at = world.clients[1].nanoCast.readyAt.Add(time.Nanosecond) 103 } 104 var completions sync.WaitGroup 105 errors := make(chan error, 2) 106 107 var command zoneCommand 108 switch attack { 109 case "unarmed": 110 command = zoneTestCommand(func(state *server) error { state.advanceCombat(at); return nil }) 111 case "fling": 112 command = zonePacket{world.clients[1], flingShotRequest(1, 1000000)[20:]} 113 case "fast": 114 command = zonePacket{world.clients[1], fastAttackRequest(1, 1000000)[20:]} 115 case "nano": 116 command = zoneTestCommand(func(state *server) error { state.advanceNanos(at); return nil }) 117 } 118 zone, stop := startTestZone(t, world) 119 for range 2 { 120 completions.Go(func() { errors <- zone.call(command) }) 121 } 122 completions.Wait() 123 if err := stop(); err != nil { 124 t.Fatal(err) 125 } 126 close(errors) 127 restored := newTestInventoryServer(t) 128 129 for err := range errors { 130 if err != nil { 131 t.Fatal(err) 132 } 133 } 134 if player.level != 2 || player.experience != 50 || player.stats.ImprovementPoints != 5500 || 135 restored.characters[1].experience != 50 || restored.characters[1].level != 2 || restored.characters[1].stats.ImprovementPoints != 5500 || 136 restored.characters[1000000].health != 0 { 137 t.Fatal("concurrent completion lost or duplicated the committed reward") 138 } 139 levels := 0 140 for _, packet := range output.packets { 141 if binary.BigEndian.Uint32(packet[16:20]) == 0x7f405a16 { 142 levels++ 143 } 144 } 145 if levels != 1 { 146 t.Fatalf("concurrent level events: %d", levels) 147 } 148 }) 149 } 150 } 151 152 func TestKillExperienceCreditsTheEncounterWinnerRatherThanTheLastHit(t *testing.T) { 153 for _, scenario := range []struct { 154 name string 155 contribution, winner uint32 156 }{ 157 {"majority", 99, 1}, {"tie retains first", 1, 1}, {"finisher leads", 0, 2}, 158 } { 159 t.Run(scenario.name, func(t *testing.T) { 160 world, firstOutput := newKillExperienceWorld(t, "unarmed") 161 target := world.characters[1000000] 162 world.nextCharacter = 1 163 finisher := world.newCharacter() 164 finisher.playfield, finisher.position = 950, [3]float32{} 165 if err := world.saveUser(finisher); err != nil { 166 t.Fatal(err) 167 } 168 lastOutput := &combatTestConn{} 169 world.clients[2] = &worldClient{id: 2, conn: lastOutput, target: true, ready: true, npcs: map[uint32]bool{1000000: true}} 170 target.damageContributors = map[damageContributor]uint32{{player: 1}: scenario.contribution} 171 target.leadingContributor = damageContributor{player: 1} 172 173 if err := world.handleCombat(world.clients[2], attackPacket(2, 1000000)[20:]); err != nil { 174 t.Fatal(err) 175 } 176 world.advanceCombat(finisher.nextAttack) 177 restored := newTestInventoryServer(t) 178 179 if target.corpse == nil || target.corpse.looter != scenario.winner || restored.characters[scenario.winner].experience != 100 { 180 t.Fatal("reward and corpse did not retain the winning contributor") 181 } 182 for index, output := range []*combatTestConn{firstOutput, lastOutput} { 183 owner := uint32(index + 1) 184 experience, notifications := uint32(0), 0 185 if owner == scenario.winner { 186 experience, notifications = 100, 1 187 } 188 if world.characters[owner].experience != experience || restored.characters[owner].experience != experience { 189 t.Fatal("reward was copied or reassigned to the last hitter") 190 } 191 awards := 0 192 for _, packet := range output.packets { 193 if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 52 { 194 awards++ 195 if binary.BigEndian.Uint32(packet[24:28]) != owner || binary.BigEndian.Uint32(packet[33:37]) != 100 { 196 t.Fatal("private award used another player's identity or amount") 197 } 198 } 199 } 200 if awards != notifications { 201 t.Fatal("the winner's notification was missing or leaked to the competing player") 202 } 203 } 204 }) 205 } 206 } 207 208 func TestKillExperienceDoesNotWriteRewardsOnNonlethalTicksOrCheckpoints(t *testing.T) { 209 world, output := newKillExperienceWorld(t, "unarmed") 210 player, target := world.characters[1], world.characters[1000000] 211 target.health = 100 212 if _, err := world.db.Exec("CREATE TABLE xp_updates (player_id INTEGER); CREATE TRIGGER count_xp AFTER UPDATE OF experience ON characters WHEN NEW.id=1 BEGIN INSERT INTO xp_updates VALUES (NEW.id); END"); err != nil { 213 t.Fatal(err) 214 } 215 216 attackForExperience(t, world, "unarmed") 217 world.advanceCombat(player.nextAttack) 218 player.position[0]++ 219 if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil { 220 t.Fatal(err) 221 } 222 var writes uint32 223 if err := world.db.QueryRow("SELECT count(*) FROM xp_updates").Scan(&writes); err != nil { 224 t.Fatal(err) 225 } 226 var savedX float32 227 if err := world.db.QueryRow("SELECT position_x FROM characters WHERE id=1").Scan(&savedX); err != nil { 228 t.Fatal(err) 229 } 230 231 if target.health == 0 || target.health == 100 || target.corpse != nil || player.experience != 0 || writes != 0 || savedX != 1 { 232 t.Fatalf("nonlethal damage or movement checkpoint awarded/wrote XP: health=%d XP=%d writes=%d", target.health, player.experience, writes) 233 } 234 for _, packet := range output.packets { 235 if binary.BigEndian.Uint32(packet[16:20]) == 0x7f405a16 || 236 binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 52 { 237 t.Fatal("nonlethal simulation published an award") 238 } 239 } 240 }