aorecomp

Anarchy Online classic in the browser

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


server/maximum_health_nano_test.go (13454B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func newMaximumHealthNanoWorld(t *testing.T) (*server, *combatTestConn) {
     12 	t.Helper()
     13 	world, conn := newStatBuffWorld(t)
     14 	player := world.characters[1]
     15 	player.level, player.profession = 1, 1
     16 	if err := world.saveUser(player); err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	return world, conn
     20 }
     21 
     22 func TestMaximumHealthNanoCrystalsLearnCastRestoreAndCancel(t *testing.T) {
     23 	for _, fixture := range []struct {
     24 		crystal, program, quality uint32
     25 		health, delta, ncu, debit uint32
     26 	}{
     27 		{27216, 27085, 7, 47, 0, 3, 24}, {29092, 29091, 1, 57, 0, 1, 10},
     28 		{29366, 29254, 113, 453, 2, 35, 415}, {95724, 95693, 139, 398, 3, 43, 519},
     29 		{95725, 95694, 63, 213, 2, 20, 240}, {95723, 95695, 33, 142, 1, 11, 145},
     30 		{95727, 95696, 17, 95, 1, 6, 82}, {95726, 95697, 152, 442, 3, 47, 580},
     31 	} {
     32 		t.Run(fmt.Sprint(fixture.program), func(t *testing.T) {
     33 			world, conn := newMaximumHealthNanoWorld(t)
     34 			player, client := world.characters[1], world.clients[1]
     35 			if granted, err := world.grantItem(player, fixture.crystal); err != nil || !granted {
     36 				t.Fatal(granted, err)
     37 			}
     38 
     39 			if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil {
     40 				t.Fatal(err)
     41 			}
     42 			if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][37:41]) != fixture.quality {
     43 				t.Fatal("original health crystal upload was rejected or changed quality")
     44 			}
     45 			castTestBuff(t, world, 1, 1, fixture.program)
     46 			cold := newTestInventoryServer(t).characters[1]
     47 			packet := fullCharacterPacket(cold)
     48 			cursor := 60 + len(cold.inventory.Items)*32 + len(cold.inventory.LearnedNanos)*4
     49 			cursor += 4 + 8*int(binary.BigEndian.Uint32(packet[cursor:])/1009-1)
     50 			count := int(binary.BigEndian.Uint32(packet[cursor:])/1009 - 1)
     51 			stats := make(map[uint32]uint32)
     52 			for offset := cursor + 4; offset < cursor+4+count*8; offset += 8 {
     53 				stats[binary.BigEndian.Uint32(packet[offset:])] = binary.BigEndian.Uint32(packet[offset+4:])
     54 			}
     55 
     56 			if player.maximumHealth() != fixture.health || cold.maximumHealth() != fixture.health || player.health != 20 || cold.health != 20 ||
     57 				player.nanoBonus(343) != int32(fixture.delta) || player.usedNCU() != fixture.ncu || player.nano != 1000-fixture.debit ||
     58 				cold.nano != player.nano || !slices.Equal(cold.inventory.LearnedNanos, []uint32{fixture.program}) || len(cold.inventory.Items) != 1 || stats[1] != 37 {
     59 				t.Fatal("complete health buff changed current HP, lost an effect or failed cold recovery", player.maximumHealth(), cold.maximumHealth(), player.health, player.nano)
     60 			}
     61 			if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, fixture.program})[20:]); err != nil {
     62 				t.Fatal(err)
     63 			}
     64 			if player.maximumHealth() != 37 || player.health != 20 || player.nanoBonus(343) != 0 || player.usedNCU() != 0 ||
     65 				len(newTestInventoryServer(t).characters[1].activeNanos) != 0 {
     66 				t.Fatal("health buff cancellation retained its maximum or delta")
     67 			}
     68 		})
     69 	}
     70 }
     71 
     72 func TestMaximumHealthNanoReplacementClampsAndCommitsTheFinalMaximum(t *testing.T) {
     73 	for _, scenario := range []string{"success", "failed health write", "failed commit"} {
     74 		t.Run(scenario, func(t *testing.T) {
     75 			world, conn := newMaximumHealthNanoWorld(t)
     76 			player := world.characters[1]
     77 			player.inventory.LearnedNanos = []uint32{29254, 95693}
     78 			castTestBuff(t, world, 1, 1, 29254)
     79 			player.health = 453
     80 			if err := world.savePosition(player); err != nil {
     81 				t.Fatal(err)
     82 			}
     83 			if scenario == "failed health write" {
     84 				if _, err := world.db.Exec("CREATE TRIGGER fail_health_buff BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(FAIL, 'test health buff'); END"); err != nil {
     85 					t.Fatal(err)
     86 				}
     87 			}
     88 			if scenario == "failed commit" {
     89 				if _, err := world.db.Exec(`CREATE TABLE invalid_health_buff (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED);
     90 					CREATE TRIGGER fail_health_buff AFTER UPDATE OF health ON characters BEGIN INSERT INTO invalid_health_buff VALUES (999999); END`); err != nil {
     91 					t.Fatal(err)
     92 				}
     93 			}
     94 			player.nanoRechargeUntil = time.Time{}
     95 			conn.packets = nil
     96 
     97 			// Higher NCU priority can replace a larger HP bonus with a smaller one.
     98 			castTestBuff(t, world, 1, 1, 95693)
     99 			cold := newTestInventoryServer(t).characters[1]
    100 
    101 			if scenario != "success" {
    102 				if player.health != 453 || cold.health != 453 || player.maximumHealth() != 453 || cold.maximumHealth() != 453 ||
    103 					player.nano != 585 || cold.nano != 585 || player.activeNanos[0].program != 29254 {
    104 					t.Fatal("failed replacement changed health, effects or the refunded reservation")
    105 				}
    106 				for _, packet := range conn.packets {
    107 					if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 {
    108 						t.Fatal("failed health write published the replacement")
    109 					}
    110 				}
    111 				return
    112 			}
    113 			if player.maximumHealth() != 398 || cold.maximumHealth() != 398 || player.health != 398 || cold.health != 398 ||
    114 				player.nano != 66 || cold.nano != 66 || player.usedNCU() != 43 || player.nanoBonus(343) != 3 ||
    115 				len(player.activeNanos) != 1 || player.activeNanos[0].program != 95693 || player.savedVitals.Health != 398 {
    116 				t.Fatal("replacement did not atomically clamp to its final maximum", player.health, cold.health, player.maximumHealth(), player.nano)
    117 			}
    118 		})
    119 	}
    120 }
    121 
    122 func TestMaximumHealthNanoReplacementPersistsTheOtherRecipientsClamp(t *testing.T) {
    123 	world, _ := newMaximumHealthNanoWorld(t)
    124 	player := world.characters[1]
    125 	caster := *player
    126 	caster.id, caster.name = 2, "Health Caster"
    127 	caster.inventory = playerInventory{LearnedNanos: []uint32{29254, 95693}}
    128 	world.characters[2], world.nextCharacter = &caster, 2
    129 	world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true}
    130 	if err := world.saveUser(&caster); err != nil {
    131 		t.Fatal(err)
    132 	}
    133 	if err := world.saveInventory(&caster, caster.inventory); err != nil {
    134 		t.Fatal(err)
    135 	}
    136 	castTestBuff(t, world, 2, 1, 29254)
    137 	player.health = 453
    138 	if err := world.savePosition(player); err != nil {
    139 		t.Fatal(err)
    140 	}
    141 	caster.nanoRechargeUntil = time.Time{}
    142 
    143 	castTestBuff(t, world, 2, 1, 95693)
    144 	cold := newTestInventoryServer(t)
    145 
    146 	if player.health != 398 || cold.characters[1].health != 398 || player.maximumHealth() != 398 || player.savedVitals.Health != 398 ||
    147 		caster.health != 20 || cold.characters[2].health != 20 || caster.nano != 66 || cold.characters[2].nano != 66 ||
    148 		player.nano != 1000 || cold.characters[1].nano != 1000 || player.activeNanos[0].caster != 2 {
    149 		t.Fatal("recipient health clamp was lost or charged to its caster")
    150 	}
    151 }
    152 
    153 func TestMaximumHealthNanoReplacementAndRefreshRestoreUnchangedNativeHP(t *testing.T) {
    154 	for _, program := range []uint32{29254, 95693} {
    155 		t.Run(fmt.Sprint(program), func(t *testing.T) {
    156 			world, conn := newMaximumHealthNanoWorld(t)
    157 			player := world.characters[1]
    158 			player.nano = 1100
    159 			player.inventory.LearnedNanos = []uint32{29091, program}
    160 			castTestBuff(t, world, 1, 1, 29091)
    161 			player.health = 57
    162 			if err := world.savePosition(player); err != nil {
    163 				t.Fatal(err)
    164 			}
    165 			if _, err := world.db.Exec("CREATE TRIGGER reject_unchanged_health BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(FAIL, 'unchanged health'); END"); err != nil {
    166 				t.Fatal(err)
    167 			}
    168 			player.nanoRechargeUntil = time.Time{}
    169 			conn.packets = nil
    170 
    171 			castTestBuff(t, world, 1, 1, program)
    172 
    173 			var add, correction int
    174 			for index, packet := range conn.packets {
    175 				if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 {
    176 					add = index + 1
    177 				}
    178 				if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c {
    179 					correction = index + 1
    180 					if binary.BigEndian.Uint32(packet[29:33]) != 57 || binary.BigEndian.Uint32(packet[33:37]) != 0 {
    181 						t.Fatal("replacement correction changed HP or showed damage feedback")
    182 					}
    183 				}
    184 			}
    185 			if add == 0 || correction <= add || player.health != 57 || newTestInventoryServer(t).characters[1].health != 57 {
    186 				t.Fatal("native modifier removal can clamp HP without a following authoritative correction", add, correction)
    187 			}
    188 
    189 			// Refresh must correct the same temporary native removal without adding HP.
    190 			player.nanoRechargeUntil = time.Time{}
    191 			conn.packets = nil
    192 			castTestBuff(t, world, 1, 1, program)
    193 			corrections := 0
    194 			for _, packet := range conn.packets {
    195 				if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c && binary.BigEndian.Uint32(packet[29:33]) == 57 {
    196 					corrections++
    197 				}
    198 			}
    199 			if corrections != 1 || player.health != 57 || len(player.activeNanos) != 1 {
    200 				t.Fatal("refresh omitted native HP reconciliation or duplicated state", corrections, player.health)
    201 			}
    202 		})
    203 	}
    204 }
    205 
    206 func TestMaximumHealthNanoExpiryClampsAtTheAuthoredDeadline(t *testing.T) {
    207 	for _, fixture := range []struct {
    208 		program uint32
    209 		minutes time.Duration
    210 	}{
    211 		{27085, 20}, {29091, 30}, {29254, 50}, {95693, 60},
    212 		{95694, 40}, {95695, 35}, {95696, 30}, {95697, 60},
    213 	} {
    214 		t.Run(fmt.Sprint(fixture.program), func(t *testing.T) {
    215 			world, conn := newMaximumHealthNanoWorld(t)
    216 			player := world.characters[1]
    217 			player.inventory.LearnedNanos = []uint32{fixture.program}
    218 			castTestBuff(t, world, 1, 1, fixture.program)
    219 			deadline := player.activeNanos[0].expiresAt
    220 			player.health = player.maximumHealth()
    221 			if err := world.savePosition(player); err != nil {
    222 				t.Fatal(err)
    223 			}
    224 			conn.packets = nil
    225 
    226 			world.advanceNanos(deadline.Add(-time.Nanosecond))
    227 			if len(player.activeNanos) != 1 || player.activeNanos[0].remaining != fixture.minutes*time.Minute || len(conn.packets) != 0 {
    228 				t.Fatal("health buff expired early or used a different duration")
    229 			}
    230 			world.advanceNanos(deadline)
    231 			world.advanceNanos(deadline.Add(time.Second))
    232 			cold := newTestInventoryServer(t).characters[1]
    233 
    234 			if player.health != 37 || cold.health != 37 || player.maximumHealth() != 37 || cold.maximumHealth() != 37 ||
    235 				player.usedNCU() != 0 || len(cold.activeNanos) != 0 || player.nanoBonus(343) != 0 {
    236 				t.Fatal("expiry retained a maximum, delta, excess HP or NCU")
    237 			}
    238 			removals := 0
    239 			for _, packet := range conn.packets {
    240 				if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 {
    241 					removals++
    242 				}
    243 			}
    244 			if removals != 1 {
    245 				t.Fatal("health buff expiry did not publish exactly one removal", removals)
    246 			}
    247 		})
    248 	}
    249 }
    250 
    251 func TestMaximumHealthNanoObserverBasisSurvivesCancellationAndTraining(t *testing.T) {
    252 	for _, scenario := range []string{"quiet team", "inspection", "departed team", "combat before snapshot", "combat after snapshot"} {
    253 		t.Run(scenario, func(t *testing.T) {
    254 			world, connections := newResourceBackpackTeam(t)
    255 			player := world.characters[1]
    256 			player.stats.NanoAiding.MatterMetamorphosis, player.stats.NanoAiding.BiologicalMetamorphosis = 47, 39
    257 			player.stats.Body.NanoPool, player.nano = 400, 1000
    258 			player.inventory.LearnedNanos = []uint32{27085}
    259 			if err := world.saveUser(player); err != nil {
    260 				t.Fatal(err)
    261 			}
    262 			if err := world.saveInventory(player, player.inventory); err != nil {
    263 				t.Fatal(err)
    264 			}
    265 			if scenario == "combat before snapshot" {
    266 				player.fighting = 3
    267 				player.rechargeUntil = time.Now().Add(time.Minute)
    268 				world.fighting[1] = player
    269 			}
    270 			castTestBuff(t, world, 1, 1, 27085)
    271 			if scenario == "inspection" || scenario == "departed team" {
    272 				if err := world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0)); err != nil {
    273 					t.Fatal(err)
    274 				}
    275 			}
    276 			if scenario == "inspection" {
    277 				if err := world.handleCharacterInfo(world.clients[2], teamRequest(2, 105, 1, 0)); err != nil {
    278 					t.Fatal(err)
    279 				}
    280 			}
    281 			if scenario == "combat after snapshot" {
    282 				player.fighting = 3
    283 				world.fighting[1] = player
    284 			}
    285 			absorbed := int32(100)
    286 			if scenario == "combat before snapshot" {
    287 				absorbed = 90
    288 			}
    289 			if player.maximumHealth() != 149 || world.clients[2].absorbedBonuses[1].health != absorbed {
    290 				t.Fatal("snapshot did not account for both equipment and nano health bonuses", player.maximumHealth(), world.clients[2].absorbedBonuses)
    291 			}
    292 
    293 			if err := world.handleNanoRemoval(world.clients[1], characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 27085})[20:]); err != nil {
    294 				t.Fatal(err)
    295 			}
    296 			for _, connection := range connections {
    297 				connection.packets = nil
    298 			}
    299 			stats := player.baseStats()
    300 			stats.Body.BodyDevelopment = 6
    301 			if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]); err != nil {
    302 				t.Fatal(err)
    303 			}
    304 
    305 			if player.maximumHealth() != 142 || newTestInventoryServer(t).characters[1].maximumHealth() != 142 || player.nanoBonus(1) != 0 {
    306 				t.Fatal("training retained the cancelled health modifier")
    307 			}
    308 			peer := uint32(152)
    309 			if scenario == "quiet team" || scenario == "combat before snapshot" {
    310 				peer = 142
    311 			}
    312 			for index, expected := range []uint32{52, peer, 52} {
    313 				var maxima []uint32
    314 				for _, packet := range connections[index].packets {
    315 					if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    316 						maxima = append(maxima, binary.BigEndian.Uint32(packet[33:37]))
    317 					}
    318 				}
    319 				if !slices.Equal(maxima, []uint32{expected}) {
    320 					t.Fatal("maximum correction lost the observer's absorbed modifier basis", index, maxima, expected)
    321 				}
    322 			}
    323 		})
    324 	}
    325 }