aorecomp

Anarchy Online classic in the browser

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


server/browser_m103_test.go (5263B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/json"
      7 	"fmt"
      8 	"net/http"
      9 	"net/http/httptest"
     10 	"os"
     11 	"os/exec"
     12 	"path/filepath"
     13 	"reflect"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserM103Qualities(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for the focused native M-10.3 check")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/m10-3-quality", time.Now().Format("20060102-150405"))
     26 	t.Chdir(t.TempDir())
     27 	for _, phase := range []string{"warm", "cold"} {
     28 		t.Run(phase, func(t *testing.T) {
     29 			world := newServerForTest(t)
     30 			var target *character
     31 			for _, actor := range world.characters {
     32 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     33 					target = actor
     34 				}
     35 			}
     36 			if target == nil {
     37 				t.Fatal("no Leet for the controlled shot")
     38 			}
     39 			if phase == "warm" {
     40 				for index := 0; index < 2; index++ {
     41 					player := world.newCharacter()
     42 					player.name = fmt.Sprint("M103", index)
     43 					player.position = target.position
     44 					player.position[0] -= float32(index * 3)
     45 					player.position[2] -= 2
     46 					player.rotation = [4]float32{0, 0, 0, 1}
     47 					player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("m103-", index))))
     48 					if index == 0 {
     49 						player.level, player.profession, player.appearance, player.aggDef = 200, 6, 0x515, 75
     50 						player.stats.Abilities = characterAbilitiesStats{300, 300, 300, 300, 300, 300}
     51 						player.stats.Ranged.Pistol, player.stats.Ranged.FlingShot = 526, 109
     52 						if player.trainingMaximum(112) < 526 || player.trainingMaximum(150) < 109 {
     53 							t.Fatal("controlled skills exceed native training caps")
     54 						}
     55 						pistol, _ := player.effectiveStat(112)
     56 						fling, _ := player.effectiveStat(150)
     57 						if pistol != 600 || fling != 183 {
     58 							t.Fatal("controlled effective Pistol/Fling values differ", pistol, fling)
     59 						}
     60 					}
     61 					player.health, player.nano = player.maximumHealth(), player.maximumNano()
     62 					if err := world.saveUser(player); err != nil {
     63 						t.Fatal(err)
     64 					}
     65 				}
     66 				// Controlled contracts and target HP isolate delivery, not earned progression.
     67 				owner := world.characters[1]
     68 				for _, item := range []struct{ template, quality uint32 }{
     69 					{123748, 39}, {123749, 78}, {123749, 79}, {123748, 80}, {21605, 1},
     70 				} {
     71 					if granted, err := world.grantItem(owner, item.template); err != nil || !granted {
     72 						t.Fatal(granted, err)
     73 					}
     74 					owner.inventory.Items[len(owner.inventory.Items)-1].Quality = item.quality
     75 				}
     76 				owner.inventory.Items[4].Count = 100
     77 				if err := world.saveInventory(owner, owner.inventory); err != nil {
     78 					t.Fatal(err)
     79 				}
     80 				profile := *target.combatProfile
     81 				profile.Health = 10000
     82 				target.combatProfile, target.health = &profile, 10000
     83 				world.combatRoll = landedCombatRoll
     84 			}
     85 			// Keep the controlled target stationary and its hit damage observable.
     86 			target.motion.roaming = false
     87 			target.regenerationAt = time.Now().Add(3 * time.Minute)
     88 			targetID := target.id
     89 			shutdown := startTestService(t, world)
     90 			mux := http.NewServeMux()
     91 			mux.Handle("/", world.httpHandler())
     92 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     93 				owner, watcher := world.characters[1], world.characters[2]
     94 				health := uint32(0)
     95 				if target := world.characters[targetID]; target != nil {
     96 					health = target.health
     97 				}
     98 				json.NewEncoder(w).Encode(map[string]any{"inventory": owner.inventory, "observer": watcher.inventory,
     99 					"target": targetID, "health": health, "fighting": owner.fighting})
    100 			}))
    101 			host := httptest.NewServer(mux)
    102 			t.Cleanup(host.Close)
    103 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    104 			defer cancel()
    105 
    106 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_m103.mjs"), host.URL, filepath.Join(output, phase))
    107 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    108 			err := command.Run()
    109 			cancel()
    110 			host.Close()
    111 			if err := shutdown(); err != nil {
    112 				t.Fatal(err)
    113 			}
    114 			// Check committed state after owners and transports have stopped.
    115 			cold := newTestInventoryServer(t)
    116 			owner, watcher := cold.characters[1], cold.characters[2]
    117 			t.Logf("Browser artifacts: %s", output)
    118 
    119 			if err != nil {
    120 				t.Fatal(err)
    121 			}
    122 			expected := []ownedItem{
    123 				{ID: 1, Type: 51018, Template: 123748, Quality: 39, Slot: 64, Count: 1},
    124 				{ID: 2, Type: 51018, Template: 123749, Quality: 78, Slot: 65, Count: 1},
    125 				{ID: 3, Type: 51018, Template: 123749, Quality: 79, Slot: 6, Count: 1, Ammo: 19},
    126 				{ID: 4, Type: 51018, Template: 123748, Quality: 80, Slot: 67, Count: 1},
    127 				{ID: 5, Type: 51005, Template: 21605, Quality: 1, Slot: 68, Count: 80},
    128 			}
    129 			if !reflect.DeepEqual(owner.inventory.Items, expected) || owner.inventory.Credits != 0 ||
    130 				len(watcher.inventory.Items) != 0 || watcher.inventory.Credits != 0 || cold.nextItem != 5 {
    131 				t.Fatal("native wear, shot or cold recovery lost an instance, round or cash")
    132 			}
    133 		})
    134 		if t.Failed() {
    135 			return
    136 		}
    137 	}
    138 }