aorecomp

Anarchy Online classic in the browser

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


server/player_death_test.go (10139B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"reflect"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func newBuffedDeathWorld(t *testing.T) (*server, *combatTestConn) {
     11 	t.Helper()
     12 	world, conn := newReclaimWorld(t)
     13 	player := world.characters[1]
     14 	player.stats.Abilities.Strength, player.stats.Abilities.Stamina, player.stats.Abilities.Psychic = 10, 10, 10
     15 	player.stats.Body.NanoPool = 200
     16 	player.stats.NanoAiding.MatterMetamorphosis, player.stats.NanoAiding.BiologicalMetamorphosis = 39, 33
     17 	player.nano = 500
     18 	player.inventory.LearnedNanos = []uint32{26360, 27168, 27134}
     19 	if err := world.saveUser(player); err != nil {
     20 		t.Fatal(err)
     21 	}
     22 	if err := world.saveInventory(player, player.inventory); err != nil {
     23 		t.Fatal(err)
     24 	}
     25 	for _, program := range []uint32{26360, 27168, 27134} {
     26 		player.nanoRechargeUntil = time.Time{}
     27 		castTestBuff(t, world, 1, 1, program)
     28 	}
     29 	player.health, player.nano = 1, player.maximumNano()
     30 	if err := world.savePosition(player); err != nil {
     31 		t.Fatal(err)
     32 	}
     33 	return world, conn
     34 }
     35 
     36 func TestPlayerDeathRemovesBuffsAndCommitsReclaimBeforeCheckpoint(t *testing.T) {
     37 	world, owner := newBuffedDeathWorld(t)
     38 	player := world.characters[1]
     39 	observer := &combatTestConn{}
     40 	world.characters[2] = &character{id: 2, playfield: 705, health: 100}
     41 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     42 	owner.packets = nil
     43 
     44 	fatal := defeatReclaimPlayer(t, world)
     45 	world.advanceCombat(fatal.Add(time.Second))
     46 	world.advanceNanos(fatal.Add(time.Hour))
     47 	restored := newTestInventoryServer(t).characters[1]
     48 
     49 	if player.health != 0 || restored.health != 0 || len(player.activeNanos) != 0 || len(restored.activeNanos) != 0 ||
     50 		player.usedNCU() != 0 || player.effectiveAbility(16) != 10 || player.effectiveAbility(18) != 10 || player.effectiveAbility(21) != 10 ||
     51 		player.nano != player.maximumNano() || restored.nano != player.nano || player.nanoReserved != 0 ||
     52 		len(player.inventory.Items) != 2 || len(restored.inventory.Items) != 2 || len(restored.inventory.Reclaim) != 1 ||
     53 		!restored.inventory.Reclaim[0].AvailableAt.Equal(fatal.Add(75*time.Second)) {
     54 		t.Fatal("death left stale buffs, resources or reclaim ownership")
     55 	}
     56 	for _, conn := range []*combatTestConn{owner, observer} {
     57 		var removed []uint32
     58 		death := false
     59 		for _, packet := range conn.packets {
     60 			if binary.BigEndian.Uint32(packet[16:20]) != 0x5e477770 || binary.BigEndian.Uint32(packet[24:28]) != 1 {
     61 				continue
     62 			}
     63 			switch binary.BigEndian.Uint32(packet[29:33]) {
     64 			case 99:
     65 				death = true
     66 			case 65:
     67 				if !death || binary.BigEndian.Uint32(packet[41:45]) != 1 {
     68 					t.Fatal("removal preceded death or lost its caster")
     69 				}
     70 				removed = append(removed, binary.BigEndian.Uint32(packet[45:49]))
     71 			}
     72 		}
     73 		if !reflect.DeepEqual(removed, []uint32{26360, 27168, 27134}) {
     74 			t.Fatalf("death removals were missing or duplicated: %v", removed)
     75 		}
     76 	}
     77 }
     78 
     79 func TestPlayerDeathTransactionFailuresKeepBuffsAndItems(t *testing.T) {
     80 	for _, failure := range []struct{ name, statement string }{
     81 		{"health", "CREATE TRIGGER reject_death BEFORE UPDATE ON characters WHEN NEW.id=1 AND NEW.health=0 BEGIN SELECT RAISE(FAIL, 'death'); END"},
     82 		{"ignored health", "CREATE TRIGGER ignore_death BEFORE UPDATE ON characters WHEN NEW.id=1 AND NEW.health=0 BEGIN SELECT RAISE(IGNORE); END"},
     83 		{"buff removal", "CREATE TRIGGER reject_buffs BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'buff'); END"},
     84 		{"ignored buff removal", "CREATE TRIGGER ignore_buffs BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"},
     85 		{"reclaim", "CREATE TRIGGER reject_reclaim BEFORE UPDATE ON item_locations WHEN NEW.reclaim_at IS NOT NULL BEGIN SELECT RAISE(FAIL, 'reclaim'); END"},
     86 		{"ignored reclaim", "CREATE TRIGGER ignore_reclaim BEFORE UPDATE ON item_locations WHEN NEW.reclaim_at IS NOT NULL BEGIN SELECT RAISE(IGNORE); END"},
     87 		{"commit", "CREATE TABLE pending_death (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_commit AFTER DELETE ON active_nanos BEGIN INSERT INTO pending_death VALUES (999999); END"},
     88 	} {
     89 		t.Run(failure.name, func(t *testing.T) {
     90 			world, conn := newBuffedDeathWorld(t)
     91 			player := world.characters[1]
     92 			before := *player
     93 			if _, err := world.db.Exec(failure.statement); err != nil {
     94 				t.Fatal(err)
     95 			}
     96 			conn.packets = nil
     97 
     98 			defeatReclaimPlayer(t, world)
     99 			restored := newTestInventoryServer(t).characters[1]
    100 
    101 			if player.health != 1 || restored.health != 1 || player.nano != before.nano || restored.nano != before.nano ||
    102 				!reflect.DeepEqual(player.activeNanos, before.activeNanos) || len(restored.activeNanos) != 3 ||
    103 				!reflect.DeepEqual(player.inventory, before.inventory) || !reflect.DeepEqual(restored.inventory.Items, before.inventory.Items) ||
    104 				len(restored.inventory.Reclaim) != 0 || len(world.fighting) != 0 {
    105 				t.Fatal("failed death partially published its state")
    106 			}
    107 			for _, packet := range conn.packets {
    108 				if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 {
    109 					action := binary.BigEndian.Uint32(packet[29:33])
    110 					if action == 65 || action == 99 {
    111 						t.Fatal("failed death published removal or death")
    112 					}
    113 				}
    114 			}
    115 		})
    116 	}
    117 }
    118 
    119 func TestPlayerDeathRefundsPendingCastWithoutRestoringItsBuff(t *testing.T) {
    120 	world, _ := newBuffedDeathWorld(t)
    121 	player, client := world.characters[1], world.clients[1]
    122 	player.nanoRechargeUntil = time.Time{}
    123 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 26360})[20:]); err != nil {
    124 		t.Fatal(err)
    125 	}
    126 	if client.nanoCast == nil || player.nanoReserved != 24 {
    127 		t.Fatal("fixture did not reserve a pending refresh")
    128 	}
    129 	deadline := client.nanoCast.readyAt
    130 
    131 	defeatReclaimPlayer(t, world)
    132 	world.advanceNanos(deadline.Add(time.Hour))
    133 	restored := newTestInventoryServer(t).characters[1]
    134 
    135 	if player.health != 0 || restored.health != 0 || client.nanoCast != nil || player.nanoReserved != 0 || restored.nanoReserved != 0 ||
    136 		player.nano != player.maximumNano() || restored.nano != player.nano || len(player.activeNanos) != 0 || len(restored.activeNanos) != 0 {
    137 		t.Fatal("death lost a reservation refund or a pending cast restored the removed effect")
    138 	}
    139 }
    140 
    141 func TestPlayerResurrectionDoesNotReplayRemovedBuffs(t *testing.T) {
    142 	world, conn := newBuffedDeathWorld(t)
    143 	loadResurrectionLocations(t, world)
    144 	defeatReclaimPlayer(t, world)
    145 	conn.packets = nil
    146 
    147 	if err := world.handleResurrection(world.clients[1], resurrectionRequest(1)[20:]); err != nil {
    148 		t.Fatal(err)
    149 	}
    150 	restored := newTestInventoryServer(t).characters[1]
    151 
    152 	if restored.health != 1 || restored.nano != 1 || restored.usedNCU() != 0 || len(activeNanoPackets(restored, time.Now())) != 0 ||
    153 		restored.effectiveAbility(16) != 10 || restored.effectiveAbility(18) != 10 || restored.effectiveAbility(21) != 10 || len(restored.inventory.Reclaim) != 1 {
    154 		t.Fatal("resurrection restored effects or lost pending reclaim")
    155 	}
    156 	for _, packet := range conn.packets {
    157 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 {
    158 			t.Fatal("resurrection replayed a removed buff")
    159 		}
    160 	}
    161 }
    162 
    163 func TestPlayerNonlethalHitKeepsBuffsAndWaitsForCheckpoint(t *testing.T) {
    164 	world, _ := newBuffedDeathWorld(t)
    165 	player, npc := world.characters[1], world.characters[1000000]
    166 	player.health = 10
    167 	npc.playfield, npc.position = player.playfield, player.position
    168 	if err := world.savePosition(player); err != nil {
    169 		t.Fatal(err)
    170 	}
    171 	if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
    172 		t.Fatal(err)
    173 	}
    174 	world.advanceCombat(player.nextAttack)
    175 
    176 	world.advanceCombat(npc.nextAttack)
    177 	restored := newTestInventoryServer(t).characters[1]
    178 
    179 	if player.health != 5 || restored.health != 10 || player.usedNCU() != 8 || restored.usedNCU() != 8 ||
    180 		len(player.inventory.Reclaim) != 0 || len(restored.inventory.Reclaim) != 0 {
    181 		t.Fatal("nonlethal hit cleared buffs, reclaimed items or forced a health save")
    182 	}
    183 }
    184 
    185 func TestPlayerRangedDeathCommitsTheRoundWithBuffsAndReclaim(t *testing.T) {
    186 	for _, fail := range []bool{false, true} {
    187 		t.Run(map[bool]string{false: "commit", true: "rollback"}[fail], func(t *testing.T) {
    188 			world, _ := newBuffedDeathWorld(t)
    189 			target := world.characters[1]
    190 			attacker := *target
    191 			attacker.id, attacker.name, attacker.activeNanos = 2, "Shooter", nil
    192 			world.characters[2] = &attacker
    193 			if err := world.saveUser(&attacker); err != nil {
    194 				t.Fatal(err)
    195 			}
    196 			if err := world.saveInventory(&attacker, playerInventory{LastID: 4, Items: []ownedItem{{ID: 4, Type: 51018, Template: 123742, Quality: 1, Slot: 6, Count: 1, Ammo: 3}}}); err != nil {
    197 				t.Fatal(err)
    198 			}
    199 			if fail {
    200 				if _, err := world.db.Exec("CREATE TABLE pending_round (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_round AFTER UPDATE OF ammo ON items BEGIN INSERT INTO pending_round VALUES (999999); END"); err != nil {
    201 					t.Fatal(err)
    202 				}
    203 			}
    204 
    205 			// Exercise the shared damage boundary; client PvP admission is disabled.
    206 			fired, _, err := world.applyWeaponDamage(&attacker, target, 1, time.Now())
    207 			restored := newTestInventoryServer(t)
    208 
    209 			if fail {
    210 				if err == nil || fired || target.health != 1 || restored.characters[1].health != 1 || target.usedNCU() != 8 || restored.characters[1].usedNCU() != 8 ||
    211 					len(target.inventory.Reclaim) != 0 || len(restored.characters[1].inventory.Reclaim) != 0 ||
    212 					attacker.inventory.Items[0].Ammo != 3 || world.items[4].Ammo != 3 || restored.items[4].Ammo != 3 {
    213 					t.Fatal("failed killing round changed ammunition, buffs, health or reclaim")
    214 				}
    215 			} else if err != nil || !fired || target.health != 0 || restored.characters[1].health != 0 || target.usedNCU() != 0 || restored.characters[1].usedNCU() != 0 ||
    216 				len(target.inventory.Reclaim) != 1 || len(restored.characters[1].inventory.Reclaim) != 1 ||
    217 				attacker.inventory.Items[0].Ammo != 2 || world.items[4].Ammo != 2 || restored.items[4].Ammo != 2 {
    218 				t.Fatal("killing round did not commit ammunition, buffs, health and reclaim together", err)
    219 			}
    220 		})
    221 	}
    222 }