aorecomp

Anarchy Online classic in the browser

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


server/character_initialization_test.go (5870B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"net/http/httptest"
      6 	"slices"
      7 	"testing"
      8 )
      9 
     10 func TestCharacterInitializationUsesNativeBreedAbilityBaselines(t *testing.T) {
     11 	for _, scenario := range []struct {
     12 		breed        uint16
     13 		abilities    characterAbilitiesStats
     14 		health, nano uint32
     15 	}{
     16 		{1, characterAbilitiesStats{6, 6, 6, 6, 6, 6}, 34, 32},
     17 		{2, characterAbilitiesStats{3, 15, 6, 6, 10, 3}, 38, 31},
     18 		{3, characterAbilitiesStats{3, 3, 3, 15, 6, 10}, 25, 48},
     19 		{4, characterAbilitiesStats{15, 6, 10, 3, 3, 3}, 59, 20},
     20 	} {
     21 		t.Run(fmt.Sprint(scenario.breed), func(t *testing.T) {
     22 			data := userData{ID: 1, Name: "New player", Level: 1, Appearance: scenario.breed << 4}
     23 
     24 			player := data.character()
     25 
     26 			if player.stats.Abilities != scenario.abilities || player.maximumHealth() != scenario.health || player.maximumNano() != scenario.nano {
     27 				t.Fatalf("breed %d baseline/resources: abilities=%+v HP=%d nano=%d", scenario.breed, player.stats.Abilities, player.maximumHealth(), player.maximumNano())
     28 			}
     29 			for _, stat := range player.stats.nativeStats() {
     30 				if stat[0] != 53 && stat[1] > player.trainingMaximum(stat[0]) {
     31 					t.Fatalf("fresh stat %d exceeds native level1 cap: %d > %d", stat[0], stat[1], player.trainingMaximum(stat[0]))
     32 				}
     33 			}
     34 		})
     35 	}
     36 }
     37 
     38 func TestBrowserCharacterStartsWithLegalSolitusAbilities(t *testing.T) {
     39 	useNPCData(t, testNPCData())
     40 	world := newTestServer(t)
     41 	response := httptest.NewRecorder()
     42 
     43 	launchTestVisitor(t, world, response, httptest.NewRequest("POST", "/browser-launch", nil))
     44 	restored := newTestInventoryServer(t).characters[1]
     45 
     46 	if restored.stats.Abilities != (characterAbilitiesStats{6, 6, 6, 6, 6, 6}) || restored.health != 34 || restored.stats.ImprovementPoints != 1500 {
     47 		t.Fatalf("fresh browser player used fixture stats: %+v", restored)
     48 	}
     49 }
     50 
     51 func TestFreshCharacterCanTrainStrengthToFirstLevelCap(t *testing.T) {
     52 	useNPCData(t, testNPCData())
     53 	world := newTestServer(t)
     54 	launchTestVisitor(t, world, httptest.NewRecorder(), httptest.NewRequest("POST", "/browser-launch", nil))
     55 	player := world.characters[1]
     56 	world.clients[1] = &worldClient{id: 1, conn: &combatTestConn{}, ready: true, target: true}
     57 	proposed := player.stats
     58 	proposed.Abilities.Strength = 9
     59 
     60 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(proposed)[20:]); err != nil {
     61 		t.Fatal(err)
     62 	}
     63 	proposed.Abilities.Strength = 10
     64 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(proposed)[20:]); err != nil {
     65 		t.Fatal(err)
     66 	}
     67 	restored := newTestInventoryServer(t).characters[1]
     68 
     69 	if player.stats.Abilities.Strength != 9 || restored.stats.Abilities.Strength != 9 || player.stats.ImprovementPoints != 1458 || restored.stats.ImprovementPoints != 1458 ||
     70 		player.health != 34 || player.nano != 0 {
     71 		t.Fatalf("fresh ability training exceeded its cap, repeated a charge or changed resources: %+v", player.stats)
     72 	}
     73 }
     74 
     75 func TestFreshCharacterCanTrainRunSpeed(t *testing.T) {
     76 	useNPCData(t, testNPCData())
     77 	world := newTestServer(t)
     78 	launchTestVisitor(t, world, httptest.NewRecorder(), httptest.NewRequest("POST", "/browser-launch", nil))
     79 	player := world.characters[1]
     80 	world.clients[1] = &worldClient{id: 1, conn: &combatTestConn{}, ready: true, target: true}
     81 	proposed := player.stats
     82 	proposed.Speed.RunSpeed = 6
     83 
     84 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(proposed)[20:]); err != nil {
     85 		t.Fatal(err)
     86 	}
     87 	proposed.Speed.RunSpeed = 100
     88 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(proposed)[20:]); err != nil {
     89 		t.Fatal(err)
     90 	}
     91 	restored := newTestInventoryServer(t).characters[1]
     92 
     93 	if player.stats.Speed.RunSpeed != 6 || restored.stats.Speed.RunSpeed != 6 || player.stats.ImprovementPoints != 1490 || restored.stats.ImprovementPoints != 1490 {
     94 		t.Fatalf("fresh Run Speed training was blocked by a fallback or bypassed its cap: %+v", player.stats)
     95 	}
     96 }
     97 
     98 func TestFreshCharacterNanoKitRequiresTrainingWithoutLosingCharges(t *testing.T) {
     99 	useNPCData(t, testNPCData())
    100 	world := newTestServer(t)
    101 	launchTestVisitor(t, world, httptest.NewRecorder(), httptest.NewRequest("POST", "/browser-launch", nil))
    102 	player := world.characters[1]
    103 	world.clients[1] = &worldClient{id: 1, conn: &combatTestConn{}, ready: true, target: true, selectedTarget: [2]uint32{50000, 1}}
    104 	if granted, err := world.grantItem(player, 85366); !granted || err != nil {
    105 		t.Fatal(granted, err)
    106 	}
    107 	// The new kit follows the visitor's starter equipment.
    108 	kitIndex := slices.IndexFunc(player.inventory.Items, func(item ownedItem) bool { return item.Template == 85366 })
    109 	kitSlot := player.inventory.Items[kitIndex].Slot
    110 	starterItems := slices.Clone(player.inventory.Items[:kitIndex])
    111 
    112 	if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, kitSlot, 1)[20:]); err != nil {
    113 		t.Fatal(err)
    114 	}
    115 	if player.inventory.Items[kitIndex].Count != 25 || player.nano != 0 || !player.skillLocks[123].IsZero() {
    116 		t.Fatal("untrained use consumed the kit or supplied its effect")
    117 	}
    118 	proposed := player.stats
    119 	proposed.NanoAiding.FirstAid = 7
    120 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(proposed)[20:]); err != nil {
    121 		t.Fatal(err)
    122 	}
    123 	if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, kitSlot, 2)[20:]); err != nil {
    124 		t.Fatal(err)
    125 	}
    126 	restored := newTestInventoryServer(t).characters[1]
    127 
    128 	if player.stats.ImprovementPoints != 1478 || restored.stats.NanoAiding.FirstAid != 7 || restored.inventory.Items[kitIndex].Count != 24 ||
    129 		restored.nano < 10 || restored.nano > 12 || restored.skillLocks[123].IsZero() ||
    130 		!slices.Equal(restored.inventory.Items[:kitIndex], starterItems) {
    131 		t.Fatalf("trained fresh player: IP=%d FirstAid=%d items=%+v nano=%d lock=%v", player.stats.ImprovementPoints,
    132 			restored.stats.NanoAiding.FirstAid, restored.inventory.Items, restored.nano, restored.skillLocks[123])
    133 	}
    134 }