aorecomp

Anarchy Online classic in the browser

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


server/pet_upgrade_test.go (4881B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestPetUpgradesRespectOriginalSkillBoundaries(t *testing.T) {
     12 	for _, fixture := range []struct{ program, threshold uint32 }{{43725, 18}, {43719, 40}, {43745, 51}} {
     13 		for _, stat := range []uint32{130, 131} {
     14 			t.Run(fmt.Sprintf("%d/%d", fixture.program, stat), func(t *testing.T) {
     15 				world, _ := newPetSummonWorld(t)
     16 				player, client := world.characters[1], world.clients[1]
     17 				trainBasicShopNanoFixture(player, 12)
     18 				player.inventory.LearnedNanos = []uint32{fixture.program}
     19 				actual, _ := player.effectiveStat(stat)
     20 				if stat == 130 {
     21 					player.stats.NanoAiding.MatterCreation -= uint16(actual) - uint16(fixture.threshold)
     22 				} else {
     23 					player.stats.NanoAiding.TimeAndSpace -= uint16(actual) - uint16(fixture.threshold)
     24 				}
     25 				if err := world.saveUser(player); err != nil {
     26 					t.Fatal(err)
     27 				}
     28 				before := player.nano
     29 				request := characterActionPacket(1, [5]uint32{19, 0, 0, 53019, fixture.program})[20:]
     30 
     31 				world.handleNanoCast(client, request)
     32 
     33 				if client.nanoCast != nil || player.nano != before || len(player.pets) != 0 {
     34 					t.Fatal("strict authored skill threshold was relaxed")
     35 				}
     36 				if stat == 130 {
     37 					player.stats.NanoAiding.MatterCreation++
     38 				} else {
     39 					player.stats.NanoAiding.TimeAndSpace++
     40 				}
     41 				if err := world.handleNanoCast(client, request); err != nil || client.nanoCast == nil {
     42 					t.Fatal("first valid skill point did not admit summon", err)
     43 				}
     44 			})
     45 		}
     46 	}
     47 }
     48 
     49 func TestPetUpgradesImproveCombatAndRetainOriginalLifetime(t *testing.T) {
     50 	for _, fixture := range []struct {
     51 		program, health, rating, debit uint32
     52 		level                          uint16
     53 		lifetime                       time.Duration
     54 	}{
     55 		{43725, 34, 10, 14, 2, 600 * time.Second},
     56 		{43719, 102, 30, 29, 6, 700 * time.Second},
     57 		{43745, 136, 40, 37, 8, 700 * time.Second},
     58 	} {
     59 		t.Run(fmt.Sprint(fixture.program), func(t *testing.T) {
     60 			world, _ := newPetSummonWorld(t)
     61 			player, client, target := world.characters[1], world.clients[1], world.characters[1000000]
     62 			trainBasicShopNanoFixture(player, 12)
     63 			player.inventory.LearnedNanos = []uint32{fixture.program}
     64 			if err := world.saveUser(player); err != nil {
     65 				t.Fatal(err)
     66 			}
     67 			before := player.nano
     68 			request := characterActionPacket(1, [5]uint32{19, 0, 0, 53019, fixture.program})[20:]
     69 			world.handleNanoCast(client, request)
     70 			if client.nanoCast == nil {
     71 				t.Fatal("upgrade could not cast")
     72 			}
     73 			completed := client.nanoCast.readyAt.Add(time.Nanosecond)
     74 
     75 			world.advanceNanos(completed)
     76 			pet := world.characters[1000001]
     77 			if pet == nil || pet.level != fixture.level || pet.health != fixture.health || pet.attackRating() != int32(fixture.rating) ||
     78 				player.nano != before-fixture.debit || !pet.pet.expiresAt.Equal(completed.Add(fixture.lifetime)) {
     79 				t.Fatal("upgrade profile/cost/lifetime changed", pet)
     80 			}
     81 			client.selectedTarget = [2]uint32{50000, target.id}
     82 			world.handlePetCommand(client, petAttackRequest())
     83 			world.advanceCombat(pet.nextAttack)
     84 			if target.health >= 84 {
     85 				t.Fatal("upgrade did not improve on the starter's 16-damage hit", target.health)
     86 			}
     87 
     88 			// Wait cancels combat; expiry removes this exact higher-level entity once.
     89 			wait := petFollowRequest(1)[20:]
     90 			binary.BigEndian.PutUint32(wait[16:], 4)
     91 			world.handlePetCommand(client, wait)
     92 			world.advanceNanos(pet.pet.expiresAt.Add(-time.Nanosecond))
     93 			if len(player.pets) != 1 {
     94 				t.Fatal("upgrade expired early")
     95 			}
     96 			world.advanceNanos(pet.pet.expiresAt)
     97 			if len(player.pets) != 0 || world.characters[pet.id] != nil {
     98 				t.Fatal("upgrade ignored authored expiry")
     99 			}
    100 		})
    101 	}
    102 }
    103 
    104 func TestPetUpgradeRejectsIncompleteSpawnContract(t *testing.T) {
    105 	for _, fixture := range mpShopNanoFixtures() {
    106 		for _, field := range []string{"level", "lifetime", "tail"} {
    107 			t.Run(fmt.Sprintf("%d/%s", fixture.program, field), func(t *testing.T) {
    108 				original := gamedata.NanoTemplates[fixture.program]
    109 				clone := *original
    110 				clone.Sections = append([]gamedata.ItemSection(nil), original.Sections...)
    111 				functions := append([]gamedata.ItemFunction(nil), (*original.Sections[2].Functions)...)
    112 				functions[0].Arguments = append([]gamedata.ItemArgument(nil), functions[0].Arguments...)
    113 				clone.Sections[2].Functions = &functions
    114 				wrong, tail := int32(999), "00000000"
    115 				switch field {
    116 				case "level":
    117 					functions[0].Arguments[1].Integer = &wrong
    118 				case "lifetime":
    119 					functions[0].Arguments[2].Integer = &wrong
    120 				case "tail":
    121 					functions[0].Arguments[3].BytesHex = &tail
    122 				}
    123 				gamedata.NanoTemplates[fixture.program] = &clone
    124 				t.Cleanup(func() { gamedata.NanoTemplates[fixture.program] = original })
    125 
    126 				if nanoCrystalProgram(fixture.crystal) != 0 {
    127 					t.Fatal("incomplete pet contract remained uploadable")
    128 				}
    129 			})
    130 		}
    131 	}
    132 }