aorecomp

Anarchy Online classic in the browser

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


server/mind_pain_test.go (8182B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"slices"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func newMindPainWorld(t *testing.T) (*server, *combatTestConn) {
     11 	t.Helper()
     12 	world, conn := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 29193, Profession: 12})
     13 	if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil {
     14 		t.Fatal(err)
     15 	}
     16 	if !slices.Equal(world.characters[1].inventory.LearnedNanos, []uint32{29114}) || len(world.characters[1].inventory.Items) != 0 {
     17 		t.Fatal("Mind Pain's starter crystal did not upload its complete program")
     18 	}
     19 	conn.packets = nil
     20 	return world, conn
     21 }
     22 
     23 func castMindPain(t *testing.T, world *server) time.Time {
     24 	t.Helper()
     25 	client := world.clients[1]
     26 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29114})[20:]); err != nil {
     27 		t.Fatal(err)
     28 	}
     29 	if client.nanoCast == nil {
     30 		t.Fatal("qualified Mind Pain was rejected")
     31 	}
     32 	at := client.nanoCast.readyAt.Add(time.Nanosecond)
     33 	world.advanceNanos(at)
     34 	return at
     35 }
     36 
     37 func TestMindPainUploadAndCastEnforceBothSkillsAndProfession(t *testing.T) {
     38 	for _, scenario := range []struct {
     39 		name       string
     40 		profession uint16
     41 		matter     uint16
     42 		psycho     uint16
     43 		accepted   bool
     44 	}{
     45 		{"matter equality", 12, 8, 7, false},
     46 		{"psycho equality", 12, 9, 6, false},
     47 		{"both above", 12, 9, 7, true},
     48 		{"other profession", 11, 9, 7, false},
     49 	} {
     50 		t.Run(scenario.name, func(t *testing.T) {
     51 			world, _ := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 29193, Profession: 12})
     52 			player, client := world.characters[1], world.clients[1]
     53 			player.profession = scenario.profession
     54 			player.stats.NanoAiding.MatterCreation = scenario.matter
     55 			player.stats.NanoAiding.PsychologicalModification = scenario.psycho
     56 
     57 			if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil {
     58 				t.Fatal(err)
     59 			}
     60 			learned := slices.Equal(player.inventory.LearnedNanos, []uint32{29114})
     61 			// A learned entry cannot bypass the cast's own requirements.
     62 			player.inventory.LearnedNanos = []uint32{29114}
     63 			if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29114})[20:]); err != nil {
     64 				t.Fatal(err)
     65 			}
     66 
     67 			if learned != scenario.accepted || (client.nanoCast != nil) != scenario.accepted ||
     68 				scenario.accepted && (len(player.inventory.Items) != 0 || player.nano != 49986 || player.nanoReserved != 14) ||
     69 				!scenario.accepted && (len(player.inventory.Items) != 1 || player.nano != 50000 || player.nanoReserved != 0) {
     70 				t.Fatal("upload or casting ignored Mind Pain's requirements", player.nano, player.nanoReserved)
     71 			}
     72 		})
     73 	}
     74 }
     75 
     76 func TestMindPainDamageDebuffsRefreshExpiryAndColdRestore(t *testing.T) {
     77 	world, conn := newMindPainWorld(t)
     78 	player, target := world.characters[1], world.characters[1000000]
     79 	var skills []int32
     80 	for _, stat := range []uint32{122, 127, 128, 129, 130, 131} {
     81 		value, _ := target.effectiveStat(stat)
     82 		skills = append(skills, value)
     83 	}
     84 
     85 	castMindPain(t, world)
     86 	firstHealth := target.health
     87 	firstDeadline := target.activeNanos[0].expiresAt
     88 	cold := newTestInventoryServer(t).characters[1000000]
     89 	player.nanoRechargeUntil = time.Time{}
     90 	refreshed := castMindPain(t, world)
     91 	deadline := target.activeNanos[0].expiresAt
     92 	world.advanceNanos(deadline.Add(-time.Nanosecond))
     93 
     94 	if firstHealth < 77 || firstHealth > 91 || firstHealth-target.health < 9 || firstHealth-target.health > 23 ||
     95 		player.nano != 49972 || player.nanoReserved != 0 || len(target.activeNanos) != 1 || len(cold.activeNanos) != 1 ||
     96 		cold.activeNanos[0].program != 144616 || cold.activeNanos[0].remaining != 6*time.Second || cold.usedNCU() != 0 ||
     97 		!deadline.Equal(refreshed.Add(6*time.Second)) || !deadline.After(firstDeadline) {
     98 		t.Fatal("damage, debit, duration, refresh or cold recovery changed", player.nano, target.activeNanos, cold.activeNanos)
     99 	}
    100 	for index, stat := range []uint32{122, 127, 128, 129, 130, 131} {
    101 		value, _ := target.effectiveStat(stat)
    102 		if target.nanoBonus(stat) != -5 || cold.nanoBonus(stat) != -5 || value != skills[index]-5 {
    103 			t.Fatal("complete six-skill debuff was omitted or stacked", stat)
    104 		}
    105 	}
    106 	before := len(conn.packets)
    107 	world.advanceNanos(deadline)
    108 	world.advanceNanos(deadline.Add(time.Second))
    109 	restored := newTestInventoryServer(t).characters[1000000]
    110 	if len(target.activeNanos) != 0 || len(restored.activeNanos) != 0 || len(conn.packets) != before+1 ||
    111 		binary.BigEndian.Uint32(conn.packets[before][29:33]) != 65 || binary.BigEndian.Uint32(conn.packets[before][45:49]) != 144616 {
    112 		t.Fatal("expiry did not remove and persist the child exactly once")
    113 	}
    114 	for index, stat := range []uint32{122, 127, 128, 129, 130, 131} {
    115 		value, _ := target.effectiveStat(stat)
    116 		if target.nanoBonus(stat) != 0 || value != skills[index] {
    117 			t.Fatal("expiry retained a skill penalty", stat)
    118 		}
    119 	}
    120 }
    121 
    122 func TestMindPainFailedChildWriteRollsBackDamageAndCost(t *testing.T) {
    123 	for _, statement := range []string{
    124 		"CREATE TRIGGER fail_mind BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL, 'child'); END",
    125 		"CREATE TRIGGER fail_mind BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END",
    126 		"CREATE TABLE pending_mind (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_mind AFTER INSERT ON active_nanos BEGIN INSERT INTO pending_mind VALUES (999999); END",
    127 	} {
    128 		t.Run(statement, func(t *testing.T) {
    129 			world, conn := newMindPainWorld(t)
    130 			if _, err := world.db.Exec(statement); err != nil {
    131 				t.Fatal(err)
    132 			}
    133 
    134 			client := world.clients[1]
    135 			if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29114})[20:]); err != nil || client.nanoCast == nil {
    136 				t.Fatal("Mind Pain did not start", err)
    137 			}
    138 			conn.packets = nil
    139 			world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    140 			cold := newTestInventoryServer(t)
    141 
    142 			for _, state := range []*server{world, cold} {
    143 				if state.characters[1].nano != 50000 || state.characters[1].nanoReserved != 0 || state.characters[1000000].health != 100 || len(state.characters[1000000].activeNanos) != 0 {
    144 					t.Fatal("failed child write published partial damage, cost or modifiers")
    145 				}
    146 			}
    147 			for _, packet := range conn.packets {
    148 				if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) != 108 {
    149 					t.Fatal("failed cast published success")
    150 				}
    151 			}
    152 			if _, err := world.db.Exec("DROP TRIGGER fail_mind"); err != nil {
    153 				t.Fatal(err)
    154 			}
    155 			castMindPain(t, world)
    156 			if world.characters[1].nano != 49986 || world.characters[1000000].health >= 100 || len(world.characters[1000000].activeNanos) != 1 {
    157 				t.Fatal("cast could not retry after rollback")
    158 			}
    159 		})
    160 	}
    161 }
    162 
    163 func TestMindScreamExecutesTheSameCompleteSixSkillContract(t *testing.T) {
    164 	world, _ := newDirectDamageWorld(t, directDamageNanoFixture{Crystal: 29395, Profession: 12})
    165 	player, target, client := world.characters[1], world.characters[1000000], world.clients[1]
    166 	target.health = 1000
    167 
    168 	if err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:]); err != nil {
    169 		t.Fatal(err)
    170 	}
    171 	if !slices.Equal(player.inventory.LearnedNanos, []uint32{29298}) {
    172 		t.Fatal("Mind Scream crystal did not upload")
    173 	}
    174 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1000000, 53019, 29298})[20:]); err != nil || client.nanoCast == nil {
    175 		t.Fatal("Mind Scream did not start", err)
    176 	}
    177 	at := client.nanoCast.readyAt.Add(time.Nanosecond)
    178 	world.advanceNanos(at)
    179 	cold := newTestInventoryServer(t).characters[1000000]
    180 
    181 	if target.health < 854 || target.health > 922 || player.nano != 49923 || len(cold.activeNanos) != 1 ||
    182 		cold.activeNanos[0].program != 144619 || cold.activeNanos[0].remaining != 9*time.Second || !target.activeNanos[0].expiresAt.Equal(at.Add(9*time.Second)) {
    183 		t.Fatal("Mind Scream's damage, cost or duration changed", target.health, player.nano, cold.activeNanos)
    184 	}
    185 	for _, stat := range []uint32{122, 127, 128, 129, 130, 131} {
    186 		if target.nanoBonus(stat) != -20 || cold.nanoBonus(stat) != -20 {
    187 			t.Fatal("Mind Scream omitted a skill penalty", stat)
    188 		}
    189 	}
    190 }