aorecomp

Anarchy Online classic in the browser

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


server/regeneration_nano_test.go (5870B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestRegenerationNanoStrainsStackAndReplaceWithinFinalNCUCapacity(t *testing.T) {
     12 	world, _ := newStatBuffWorld(t)
     13 	player, client := world.characters[1], world.clients[1]
     14 	player.profession = 10
     15 	player.inventory.LearnedNanos = []uint32{27118, 27144, 28663, 28651}
     16 	for _, program := range []uint32{27144, 27118, 28663, 27144} {
     17 		player.nanoRechargeUntil = time.Time{}
     18 		castTestBuff(t, world, 1, 1, program)
     19 	}
     20 	before := player.nano
     21 	player.nanoRechargeUntil = time.Time{}
     22 
     23 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28651})[20:]); err != nil {
     24 		t.Fatal(err)
     25 	}
     26 	if client.nanoCast != nil || player.nano != before || player.usedNCU() != 33 || player.nanoBonus(343) != 11 || player.nanoBonus(364) != 2 ||
     27 		len(player.activeNanos) != 3 {
     28 		t.Fatal("over-capacity upgrade debited resources or generic regeneration failed to stack/refresh")
     29 	}
     30 	for _, program := range []uint32{27144, 27118} {
     31 		if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, program})[20:]); err != nil {
     32 			t.Fatal(err)
     33 		}
     34 	}
     35 	castTestBuff(t, world, 1, 1, 28651)
     36 	player.nanoRechargeUntil = time.Time{}
     37 	if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 28663})[20:]); err != nil {
     38 		t.Fatal(err)
     39 	}
     40 	cold := newTestInventoryServer(t).characters[1]
     41 	start := time.Unix(100, 0)
     42 	client.regenerationAt, client.regenerationSeconds = start, 810
     43 	world.advanceRegeneration(start.Add(2 * time.Second))
     44 
     45 	if client.nanoCast != nil || player.health != 37 || player.nano != 225 || player.nanoBonus(343) != 14 || player.nanoBonus(364) != 0 ||
     46 		player.usedNCU() != 45 || len(player.activeNanos) != 1 || player.activeNanos[0].program != 28651 ||
     47 		cold.nano != 222 || cold.nanoBonus(343) != 14 || cold.usedNCU() != 45 {
     48 		t.Fatal("same-strain replacement, weaker rejection, recovery or cold restore changed more than one contribution", player.activeNanos, player.health, player.nano)
     49 	}
     50 }
     51 
     52 func TestRegenerationNanoFailedExpiryRetainsContributionUntilDurableRetry(t *testing.T) {
     53 	world, conn := newStatBuffWorld(t)
     54 	player, client := world.characters[1], world.clients[1]
     55 	player.inventory.LearnedNanos = []uint32{27144}
     56 	castTestBuff(t, world, 1, 1, 27144)
     57 	deadline := player.activeNanos[0].expiresAt
     58 	if _, err := world.db.Exec("CREATE TRIGGER reject_expiry BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL, 'test expiry'); END"); err != nil {
     59 		t.Fatal(err)
     60 	}
     61 	conn.packets = nil
     62 
     63 	world.advanceNanos(deadline)
     64 	if len(conn.packets) != 0 || player.nanoBonus(343) != 3 || player.usedNCU() != 5 || len(newTestInventoryServer(t).characters[1].activeNanos) != 1 {
     65 		t.Fatal("failed expiry published removal or lost the contribution")
     66 	}
     67 	start := time.Unix(100, 0)
     68 	client.regenerationAt, client.regenerationSeconds = start, 810
     69 	world.advanceRegeneration(start.Add(2 * time.Second))
     70 	if player.health != 26 || player.nano != 979 {
     71 		t.Fatal("failed expiry removed regeneration before commit", player.health, player.nano)
     72 	}
     73 	if _, err := world.db.Exec("DROP TRIGGER reject_expiry"); err != nil {
     74 		t.Fatal(err)
     75 	}
     76 	world.advanceNanos(deadline.Add(time.Second))
     77 	world.advanceNanos(deadline.Add(2 * time.Second))
     78 	client.regenerationSeconds = 1622
     79 	world.advanceRegeneration(start.Add(4 * time.Second))
     80 
     81 	if player.health != 29 || player.nano != 982 || player.nanoBonus(343) != 0 || player.usedNCU() != 0 ||
     82 		len(newTestInventoryServer(t).characters[1].activeNanos) != 0 {
     83 		t.Fatal("durable expiry did not retire precisely its own contribution")
     84 	}
     85 	removed := 0
     86 	for _, packet := range conn.packets {
     87 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 {
     88 			removed++
     89 		}
     90 	}
     91 	if removed != 1 {
     92 		t.Fatal("expiry replayed its native removal", removed)
     93 	}
     94 }
     95 
     96 func TestRegenerationNanoDeathRemovesEffectsBeforeFurtherRecovery(t *testing.T) {
     97 	for _, fixture := range []struct {
     98 		program    uint32
     99 		profession uint16
    100 	}{
    101 		{27118, 1}, {27144, 1}, {28651, 10}, {28663, 10}, {29200, 8}, {30095, 8},
    102 		{27085, 1}, {29091, 1}, {29254, 1}, {95693, 1}, {95694, 1}, {95695, 1}, {95696, 1}, {95697, 1},
    103 	} {
    104 		t.Run(fmt.Sprint(fixture.program), func(t *testing.T) {
    105 			world, owner := newStatBuffWorld(t)
    106 			player := world.characters[1]
    107 			player.profession = fixture.profession
    108 			player.inventory.LearnedNanos = []uint32{fixture.program}
    109 			castTestBuff(t, world, 1, 1, fixture.program)
    110 			observer := &combatTestConn{}
    111 			world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    112 			world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true}
    113 			owner.packets = nil
    114 
    115 			fatal := defeatReclaimPlayer(t, world)
    116 			world.advanceNanos(fatal.Add(time.Hour))
    117 			world.advanceRegeneration(fatal.Add(time.Hour))
    118 			cold := newTestInventoryServer(t).characters[1]
    119 
    120 			if player.health != 0 || cold.health != 0 || player.usedNCU() != 0 || len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 ||
    121 				player.nanoBonus(343) != 0 || player.nanoBonus(364) != 0 || len(cold.inventory.Items) != 1 || cold.inventory.Items[0].Template != 173155 {
    122 				t.Fatal("death left a passive effect, revived the player or lost equipment", player.activeNanos, cold.inventory)
    123 			}
    124 			for _, conn := range []*combatTestConn{owner, observer} {
    125 				var removed []uint32
    126 				for _, packet := range conn.packets {
    127 					if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 {
    128 						removed = append(removed, binary.BigEndian.Uint32(packet[45:49]))
    129 					}
    130 				}
    131 				if !slices.Equal(removed, []uint32{fixture.program}) {
    132 					t.Fatal("death did not remove one native contribution for owner and observer", removed)
    133 				}
    134 			}
    135 		})
    136 	}
    137 }