aorecomp

Anarchy Online classic in the browser

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


server/browser_basic_shop_weapons_test.go (4776B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"context"
      6 	"crypto/sha256"
      7 	"encoding/json"
      8 	"fmt"
      9 	"math"
     10 	"net/http"
     11 	"net/http/httptest"
     12 	"os"
     13 	"os/exec"
     14 	"path/filepath"
     15 	"reflect"
     16 	"runtime"
     17 	"testing"
     18 	"time"
     19 )
     20 
     21 func TestBrowserBasicShopWeapons(t *testing.T) {
     22 	if os.Getenv("AO_GAME_TEST") != "1" {
     23 		t.Skip("set AO_GAME_TEST=1 for the three basic weapon terminals")
     24 	}
     25 	_, source, _, _ := runtime.Caller(0)
     26 	root := filepath.Dir(filepath.Dir(source))
     27 	output := filepath.Join(root, "local/alpha-weapons", time.Now().Format("20060102-150405"))
     28 	for _, identity := range []uint32{0xc005049f, 0xc026049c, 0xc01204a2} {
     29 		t.Run(fmt.Sprintf("%x", identity), func(t *testing.T) {
     30 			world := newTestServer(t)
     31 			machine := gamedata.ShopMachines[identity]
     32 			player := world.newCharacter()
     33 			player.playfield, player.position = machine.Playfield, machine.Position
     34 			player.appearance = map[uint32]uint16{1183: 0x515, 1180: 0x315, 1186: 0x115}[machine.Playfield]
     35 			forward, _ := bodyAxes(machine.Rotation)
     36 			player.position[0] += 3 * forward[0]
     37 			player.position[2] += 3 * forward[2]
     38 			player.position[1] += .5
     39 			yaw := math.Atan2(float64(machine.Position[0]-player.position[0]), float64(machine.Position[2]-player.position[2]))
     40 			player.rotation = [4]float32{0, float32(math.Sin(yaw / 2)), 0, float32(math.Cos(yaw / 2))}
     41 			// Controlled level-ten checkpoint with sale loot and a nearby target.
     42 			// Funding and trained skills are fixtures, not earned-progression evidence.
     43 			trainBasicShopWeaponFixture(t, player)
     44 			player.health, player.nano = player.maximumHealth(), player.maximumNano()
     45 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("basic-shop-weapons")))
     46 			if err := world.saveUser(player); err != nil {
     47 				t.Fatal(err)
     48 			}
     49 			inventory := playerInventory{Credits: 50000, LastID: 1, Items: []ownedItem{
     50 				{ID: 1, Type: 51018, Template: 209283, Quality: 1, Slot: 64, Count: 1},
     51 			}}
     52 			if err := world.saveInventory(player, inventory); err != nil {
     53 				t.Fatal(err)
     54 			}
     55 			var target *character
     56 			for _, actor := range world.characters {
     57 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     58 					target = actor
     59 				}
     60 			}
     61 			if target == nil {
     62 				t.Fatal("missing original Leet fixture")
     63 			}
     64 			target.playfield, target.position = player.playfield, player.position
     65 			target.position[0] -= 1.5*forward[0] + .6*forward[2]
     66 			target.position[2] += -1.5*forward[2] + .6*forward[0]
     67 			target.motion.home, target.health = target.position, 10000
     68 			// Deterministic ordinary hits, including the target's retaliation.
     69 			world.combatRoll = func(limit uint32) uint32 {
     70 				if limit == 100 {
     71 					return 99
     72 				}
     73 				return 0
     74 			}
     75 			targetID := target.id
     76 			target.motion.roaming = false
     77 			target.regenerationAt = time.Now().Add(5 * time.Minute)
     78 			// Persist the fixture's cross-zone relocation before partitioning owners.
     79 			if err := world.flushWorld(); err != nil {
     80 				t.Fatal(err)
     81 			}
     82 			shutdown := startTestService(t, world)
     83 			mux := http.NewServeMux()
     84 			mux.Handle("/", world.httpHandler())
     85 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 1, func(world *server, w http.ResponseWriter, r *http.Request) {
     86 				player := world.characters[1]
     87 				target := world.characters[targetID]
     88 				open, sales, purchases := false, 0, 0
     89 				if client := world.clients[1]; client != nil && client.shop != nil {
     90 					open, sales = true, len(client.shop.items)
     91 					for _, count := range client.shop.purchases {
     92 						purchases += int(count)
     93 					}
     94 				}
     95 				json.NewEncoder(w).Encode(map[string]any{"machine": identity, "playfield": machine.Playfield,
     96 					"open": open, "sales": sales, "purchases": purchases, "inventory": player.inventory,
     97 					"target": target.id, "targetHealth": target.health, "fighting": player.fighting,
     98 					"recharging": time.Now().Before(player.rechargeUntil)})
     99 			}))
    100 			host := httptest.NewServer(mux)
    101 			defer host.Close()
    102 			ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
    103 			defer cancel()
    104 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_basic_shop_weapons.mjs"), host.URL, filepath.Join(output, fmt.Sprintf("%x", identity)))
    105 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
    106 			err := command.Run()
    107 			host.Close()
    108 			if err := shutdown(); err != nil {
    109 				t.Fatal(err)
    110 			}
    111 			t.Logf("Browser artifacts: %s", filepath.Join(output, fmt.Sprintf("%x", identity)))
    112 			if err != nil {
    113 				t.Fatal(err)
    114 			}
    115 			cold := newTestInventoryServer(t).characters[1]
    116 			if !reflect.DeepEqual(cold.inventory, player.inventory) {
    117 				t.Fatal("purchased weapon use changed during cold recovery")
    118 			}
    119 		})
    120 	}
    121 }