aorecomp

Anarchy Online classic in the browser

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


server/browser_outdoor_navigation_test.go (4031B)


      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 	"runtime"
     14 	"testing"
     15 	"time"
     16 )
     17 
     18 func TestBrowserOutdoorNPCNavigation(t *testing.T) {
     19 	if os.Getenv("AO_GAME_TEST") != "1" {
     20 		t.Skip("set AO_GAME_TEST=1 for the focused outdoor wall detour")
     21 	}
     22 	_, source, _, _ := runtime.Caller(0)
     23 	root := filepath.Dir(filepath.Dir(source))
     24 	output := filepath.Join(root, "local/outdoor-navigation", time.Now().Format("20060102-150405"))
     25 	world := newTestServer(t)
     26 	world.combatRoll = landedCombatRoll
     27 	var npc *character
     28 	for _, actor := range world.characters {
     29 		if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (npc == nil || actor.id < npc.id) {
     30 			npc = actor
     31 		}
     32 	}
     33 	if npc == nil {
     34 		t.Fatal("no admitted Leet for controlled outdoor navigation")
     35 	}
     36 	npc.position, npc.movement = [3]float32{46, 14.01, 58}, playerMovement{}
     37 	npc.motion = &npcMotion{home: npc.position}
     38 	world.mobileNPCs = map[uint32][]*character{950: {npc}}
     39 	for index := 0; index < 2; index++ {
     40 		player := world.newCharacter()
     41 		player.name, player.profession, player.level, player.appearance = fmt.Sprint("Detour", index), 9, 100, 0x315
     42 		player.position, player.rotation = [3]float32{50.0453, 14.01, 69.887344}, [4]float32{0, 1, 0, 0}
     43 		if index == 1 {
     44 			player.position = [3]float32{44, 14.21, 74}
     45 		}
     46 		player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     47 		player.health, player.nano = player.maximumHealth(), player.maximumNano()
     48 		player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("detour-", index))))
     49 		if err := world.saveUser(player); err != nil {
     50 			t.Fatal(err)
     51 		}
     52 	}
     53 	owner := world.characters[1]
     54 	if world.combatVisible(npc, owner, time.Now()) {
     55 		t.Fatal("original wall does not separate the controlled actors")
     56 	}
     57 	npcID := npc.id
     58 	shutdown := startTestService(t, world)
     59 	mux := http.NewServeMux()
     60 	mux.Handle("/", world.httpHandler())
     61 	mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     62 		owner := world.characters[1]
     63 		npc := world.characters[npcID]
     64 		json.NewEncoder(w).Encode(map[string]any{"enemy": npc.id, "position": npc.position, "home": npc.motion.home,
     65 			"route": npc.motion.route, "moving": npc.motion.moving, "returning": npc.motion.returning,
     66 			"health": npc.health, "fighting": npc.fighting, "ownerHealth": owner.health, "ownerFighting": owner.fighting,
     67 			"targetPosition": predictedPosition(owner, time.Now()),
     68 			"visible":        world.combatVisible(npc, owner, time.Now())})
     69 	}))
     70 	// Controlled engagement and target loss reach the wall scenario without a playthrough.
     71 	for _, action := range []string{"pursue", "return"} {
     72 		mux.HandleFunc("POST /test-"+action, testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     73 			owner := world.characters[1]
     74 			updates := make(map[*worldClient][][]byte)
     75 			if action == "pursue" {
     76 				world.startAttack(npc, owner, time.Now(), updates)
     77 			} else {
     78 				world.cancelCombat(owner.id, updates)
     79 			}
     80 			world.sendCombatUpdates(updates)
     81 			w.WriteHeader(http.StatusNoContent)
     82 		}))
     83 	}
     84 	host := httptest.NewServer(mux)
     85 	t.Cleanup(host.Close)
     86 	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
     87 	defer cancel()
     88 	command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_outdoor_navigation.mjs"), host.URL, output)
     89 	command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     90 	err := command.Run()
     91 	host.Close()
     92 	if err := shutdown(); err != nil {
     93 		t.Fatal(err)
     94 	}
     95 	cancel()
     96 	t.Logf("Browser artifacts: %s", output)
     97 
     98 	if err != nil || npc.motion.returning || npc.motion.moving || npc.fighting != 0 || owner.fighting != 0 ||
     99 		surfaceDistanceSquared(npc.position, npc.motion.home) >= 1.01 || npc.health == 0 || owner.health == 0 {
    100 		t.Fatal("native detour, combat or return failed", err, npc.position, npc.motion)
    101 	}
    102 }