aorecomp

Anarchy Online classic in the browser

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


server/browser_city_doors_test.go (4858B)


      1 package main
      2 
      3 import (
      4 	"context"
      5 	"crypto/sha256"
      6 	"encoding/json"
      7 	"fmt"
      8 	"math"
      9 	"net/http"
     10 	"net/http/httptest"
     11 	"os"
     12 	"os/exec"
     13 	"path/filepath"
     14 	"runtime"
     15 	"testing"
     16 	"time"
     17 )
     18 
     19 func TestBrowserCityDoors(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for city door approaches")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/city-doors", time.Now().Format("20060102-150405"))
     26 	for _, route := range []struct {
     27 		name          string
     28 		playfield, id uint32
     29 		origin        doorOrigin
     30 	}{
     31 		{"entertainment-shop", 705, 0xc01102c1, doorOrigin{}},
     32 		{"shop-entertainment", 1183, 0xc000049f, doorOrigin{705, 0xc01102c1}},
     33 		{"bronto-shop", 705, 0xc03902c1, doorOrigin{}},
     34 		{"shop-bronto", 1191, 0xc00004a7, doorOrigin{705, 0xc03902c1}},
     35 		{"entertainment-trade", 705, 0xc00502c1, doorOrigin{}},
     36 		{"trade-icc", 710, 0xc00502c6, doorOrigin{}},
     37 		{"entertainment-rome", 705, 0xc00802c1, doorOrigin{}},
     38 		{"trade-entertainment", 710, 0xc00002c6, doorOrigin{}},
     39 		{"clan-shop", 540, 0xc01d021c, doorOrigin{}},
     40 		{"shop-clan", 1180, 0xc000049c, doorOrigin{540, 0xc01d021c}},
     41 		{"neutral-shop", 800, 0xc0170320, doorOrigin{}},
     42 		{"shop-neutral", 1186, 0xc00004a2, doorOrigin{800, 0xc0170320}},
     43 	} {
     44 		t.Run(route.name, func(t *testing.T) {
     45 			world := newTestServer(t)
     46 			var door zoneDoor
     47 			for _, candidate := range world.zoneLinks["target"][route.playfield].Doors {
     48 				if candidate.ID == route.id {
     49 					door = candidate
     50 				}
     51 			}
     52 			if door.ID == 0 {
     53 				t.Fatal("missing route")
     54 			}
     55 			forward, right := bodyAxes(door.Rotation)
     56 			start, near := door.Position, door.Position
     57 			for _, axis := range []int{0, 2} {
     58 				start[axis] += 8 * forward[axis]
     59 				near[axis] += 3 * forward[axis]
     60 			}
     61 			approach := [][3]float32{near}
     62 			if route.name == "clan-shop" {
     63 				// The original storefront's ramp runs across its raised platform.
     64 				ramp := door.Position
     65 				for _, axis := range []int{0, 2} {
     66 					ramp[axis] += 3.5*forward[axis] + 4.5*right[axis]
     67 				}
     68 				approach = append([][3]float32{ramp}, approach...)
     69 			}
     70 			for index := 0; index < 2; index++ {
     71 				player := world.newCharacter()
     72 				player.name, player.playfield, player.position = fmt.Sprint("Doorobserver", index), route.playfield, start
     73 				if route.origin.Playfield != 0 {
     74 					player.returnDoor = &route.origin
     75 				}
     76 				if index == 1 {
     77 					offset := float32(3)
     78 					if door.Return {
     79 						// Keep the observer inside the narrow interior entry corridor.
     80 						offset = 1
     81 					}
     82 					for _, axis := range []int{0, 2} {
     83 						player.position[axis] += offset * right[axis]
     84 					}
     85 				}
     86 				if ground := world.terrain[player.playfield]; ground != nil {
     87 					if height, inside := ground.elevation(player.position[0], player.position[2]); inside {
     88 						player.position[1] = height + .5
     89 					}
     90 				}
     91 				yaw := math.Atan2(float64(door.Position[0]-player.position[0]), float64(door.Position[2]-player.position[2]))
     92 				player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))}
     93 				player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint("city-door-", index))))
     94 				if err := world.saveUser(player); err != nil {
     95 					t.Fatal(err)
     96 				}
     97 			}
     98 			shutdown := startTestService(t, world)
     99 			mux := http.NewServeMux()
    100 			mux.Handle("/", world.httpHandler())
    101 			mux.HandleFunc("GET /test-state", func(w http.ResponseWriter, r *http.Request) {
    102 				players, err := world.adminPlayers()
    103 				if err != nil {
    104 					http.Error(w, err.Error(), http.StatusInternalServerError)
    105 					return
    106 				}
    107 				var open bool
    108 				snapshot, err := world.world.snapshot()
    109 				if err == nil {
    110 					err = snapshot.zones[route.playfield].call(zoneTestCommand(func(state *server) error {
    111 						open = state.cityDoors[doorOrigin{route.playfield, route.id}]
    112 						return nil
    113 					}))
    114 				}
    115 				if err != nil {
    116 					http.Error(w, err.Error(), http.StatusInternalServerError)
    117 					return
    118 				}
    119 				destination := door.Destination
    120 				if door.Return {
    121 					destination = route.origin.Playfield
    122 				}
    123 				json.NewEncoder(w).Encode(map[string]any{"door": door, "start": start, "approach": approach, "players": players,
    124 					"destination": destination, "open": open})
    125 			})
    126 			host := httptest.NewServer(mux)
    127 			defer host.Close()
    128 			ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    129 			defer cancel()
    130 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_city_doors.mjs"), host.URL, filepath.Join(output, route.name))
    131 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    132 			err := command.Run()
    133 			host.Close()
    134 			if err := shutdown(); err != nil {
    135 				t.Fatal(err)
    136 			}
    137 			t.Logf("Browser artifacts: %s", filepath.Join(output, route.name))
    138 			if err != nil {
    139 				t.Fatal(err)
    140 			}
    141 		})
    142 	}
    143 }