aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/kill_experience_rules_test.go (7758B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"testing"
      7 )
      8 
      9 func TestKillExperienceUsesWornPioneerPercentage(t *testing.T) {
     10 	for _, scenario := range []struct {
     11 		name                     string
     12 		template, base, expected uint32
     13 	}{
     14 		{"carried", 152039, 100, 100},
     15 		{"worn", 152039, 100, 101},
     16 		{"removed", 152039, 100, 100},
     17 		{"shrinking", 156831, 100, 101},
     18 		{"fraction below", 152039, 99, 99},
     19 		{"fraction above", 152039, 101, 102},
     20 	} {
     21 		t.Run(scenario.name, func(t *testing.T) {
     22 			world, output := newKillExperienceWorld(t, "unarmed")
     23 			player, client := world.characters[1], world.clients[1]
     24 			profile := *world.characters[1000000].combatProfile
     25 			profile.Experience = scenario.base
     26 			world.characters[1000000].combatProfile = &profile
     27 			if granted, err := world.grantItem(player, scenario.template); err != nil || !granted {
     28 				t.Fatalf("grant Pioneer: %t %v", granted, err)
     29 			}
     30 			itemID := player.inventory.Items[0].ID
     31 			if scenario.name != "carried" {
     32 				if err := world.handleInventory(client, inventoryMovePacket(1, 64, 19)[20:]); err != nil || client.equipment == nil {
     33 					t.Fatalf("wear Pioneer: %v", err)
     34 				}
     35 				world.advanceEquipment(client.equipment.readyAt)
     36 			}
     37 			if scenario.name == "removed" {
     38 				if err := world.handleInventory(client, itemMovePacket(1, 19, 111)[20:]); err != nil || client.equipment == nil {
     39 					t.Fatalf("remove Pioneer: %v", err)
     40 				}
     41 				world.advanceEquipment(client.equipment.readyAt)
     42 			}
     43 			output.packets = nil
     44 
     45 			attackForExperience(t, world, "unarmed")
     46 			restored := newTestInventoryServer(t).characters[1]
     47 
     48 			if player.experience != scenario.expected || restored.experience != scenario.expected || player.level != 1 ||
     49 				player.stats.ImprovementPoints != 1500 || len(player.inventory.Items) != 1 || player.inventory.Items[0].ID != itemID ||
     50 				len(restored.inventory.Items) != 1 || restored.inventory.Items[0].ID != itemID || restored.inventory.Items[0].Count != 1 {
     51 				t.Fatal("XP modifier or item identity did not survive the reward")
     52 			}
     53 			awards := 0
     54 			for _, packet := range output.packets {
     55 				key := binary.BigEndian.Uint32(packet[16:20])
     56 				if key == 0x7f405a16 {
     57 					t.Fatal("a below-threshold award produced a level event")
     58 				}
     59 				if key == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 52 {
     60 					awards++
     61 					if binary.BigEndian.Uint32(packet[33:37]) != scenario.expected {
     62 						t.Fatal("absolute native XP did not match the committed reward")
     63 					}
     64 				}
     65 			}
     66 			if awards != 1 {
     67 				t.Fatalf("native XP awards: %d", awards)
     68 			}
     69 		})
     70 	}
     71 }
     72 
     73 func TestKillExperienceDoesNotReassignAnIneligibleWinnersReward(t *testing.T) {
     74 	for _, reason := range []string{"dead", "offline", "loading", "other zone", "unseen", "unknown", "grey", "capped", "joined team", "team winner", "unprofiled", "no reward"} {
     75 		t.Run(reason, func(t *testing.T) {
     76 			world, _ := newKillExperienceWorld(t, "unarmed")
     77 			winner, target := world.characters[1], world.characters[1000000]
     78 			world.nextCharacter = 1
     79 			finisher := world.newCharacter()
     80 			finisher.playfield, finisher.position = 950, [3]float32{}
     81 			if err := world.saveUser(finisher); err != nil {
     82 				t.Fatal(err)
     83 			}
     84 			output := &combatTestConn{}
     85 			world.clients[2] = &worldClient{id: 2, conn: output, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     86 			target.damageContributors = map[damageContributor]uint32{{player: 1}: 99}
     87 			target.leadingContributor = damageContributor{player: 1}
     88 			if err := world.flushWorld(); err != nil {
     89 				t.Fatal(err)
     90 			}
     91 			switch reason {
     92 			case "dead":
     93 				winner.health = 0
     94 			case "offline":
     95 				delete(world.clients, 1)
     96 			case "loading":
     97 				world.clients[1].loading = true
     98 			case "other zone":
     99 				winner.playfield = 800
    100 			case "unseen":
    101 				delete(world.clients[1].npcs, target.id)
    102 			case "unknown":
    103 				target.damageContributors = map[damageContributor]uint32{{player: 3}: 99}
    104 				target.leadingContributor = damageContributor{player: 3}
    105 			case "grey":
    106 				winner.level = 4
    107 			case "capped":
    108 				winner.level = 200
    109 			case "joined team":
    110 				winner.team = &playerTeam{id: 1, leader: 1, members: []uint32{1}}
    111 			case "team winner":
    112 				target.damageContributors = map[damageContributor]uint32{{team: 1}: 99}
    113 				target.leadingContributor = damageContributor{team: 1}
    114 			case "unprofiled":
    115 				target.combatProfile = nil
    116 			case "no reward":
    117 				profile := *target.combatProfile
    118 				profile.Experience = 0
    119 				target.combatProfile = &profile
    120 			}
    121 
    122 			if err := world.handleCombat(world.clients[2], attackPacket(2, 1000000)[20:]); err != nil {
    123 				t.Fatal(err)
    124 			}
    125 			world.advanceCombat(finisher.nextAttack)
    126 			restored := newTestInventoryServer(t)
    127 
    128 			if target.health != 0 || target.corpse == nil || winner.experience != 0 || finisher.experience != 0 ||
    129 				restored.characters[1].experience != 0 || restored.characters[2].experience != 0 {
    130 				t.Fatal("the encounter's reward was reassigned or granted to an ineligible player")
    131 			}
    132 			for _, packet := range output.packets {
    133 				if binary.BigEndian.Uint32(packet[16:20]) == 0x7f405a16 ||
    134 					binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 52 {
    135 					t.Fatal("an ineligible kill published XP")
    136 				}
    137 			}
    138 		})
    139 	}
    140 }
    141 
    142 func TestKillExperienceUsesReachedTitleForIP(t *testing.T) {
    143 	for _, scenario := range []struct {
    144 		level                                 uint16
    145 		progress, title, points, announcement uint32
    146 	}{
    147 		{1, 1400, 1, 5500, 0}, {14, 9550, 2, 11500, 2}, {49, 101850, 3, 21500, 3},
    148 		{99, 1519850, 4, 41500, 4}, {149, 12141850, 5, 81500, 5}, {189, 52101150, 6, 151500, 6},
    149 	} {
    150 		t.Run(fmt.Sprint(scenario.level), func(t *testing.T) {
    151 			world, output := newKillExperienceWorld(t, "unarmed")
    152 			player, target := world.characters[1], world.characters[1000000]
    153 			player.level, player.experience = scenario.level, scenario.progress
    154 			target.level = scenario.level
    155 			if err := world.saveUser(player); err != nil {
    156 				t.Fatal(err)
    157 			}
    158 			observer := world.newCharacter()
    159 			observer.playfield = 950
    160 			otherOutput := &combatTestConn{}
    161 			world.clients[2] = &worldClient{id: 2, conn: otherOutput, target: true, ready: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}}
    162 
    163 			attackForExperience(t, world, "unarmed")
    164 			restored := newTestInventoryServer(t).characters[1]
    165 
    166 			if player.level != scenario.level+1 || player.experience != 50 || player.titleLevel() != scenario.title ||
    167 				player.stats.ImprovementPoints != scenario.points || restored.level != scenario.level+1 || restored.experience != 50 ||
    168 				restored.stats.ImprovementPoints != scenario.points {
    169 				t.Fatalf("reached-title IP: level=%d XP=%d title=%d IP=%d", player.level, player.experience, player.titleLevel(), player.stats.ImprovementPoints)
    170 			}
    171 			levels := 0
    172 			for _, packet := range output.packets {
    173 				if binary.BigEndian.Uint32(packet[16:20]) == 0x7f405a16 {
    174 					levels++
    175 					if binary.BigEndian.Uint32(packet[49:53]) != scenario.announcement || binary.BigEndian.Uint32(packet[33:37]) != scenario.points {
    176 						t.Fatal("native title/IP did not match the committed transition")
    177 					}
    178 				}
    179 			}
    180 			if levels != 1 {
    181 				t.Fatal("title transition did not produce exactly one level event")
    182 			}
    183 			titles := 0
    184 			for _, packet := range otherOutput.packets {
    185 				if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 37 {
    186 					titles++
    187 					if binary.BigEndian.Uint32(packet[33:37]) != scenario.title {
    188 						t.Fatal("an observer received the wrong profession title")
    189 					}
    190 				}
    191 			}
    192 			if scenario.announcement == 0 && titles != 0 || scenario.announcement != 0 && titles != 1 {
    193 				t.Fatal("visible title updates did not follow actual title transitions")
    194 			}
    195 		})
    196 	}
    197 }