aorecomp

Anarchy Online classic in the browser

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


server/browser_aimed_shot_test.go (4452B)


      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 TestBrowserAimedShot(t *testing.T) {
     20 	if os.Getenv("AO_GAME_TEST") != "1" {
     21 		t.Skip("set AO_GAME_TEST=1 for native issued and stocked rifle Aimed Shot")
     22 	}
     23 	_, source, _, _ := runtime.Caller(0)
     24 	root := filepath.Dir(filepath.Dir(source))
     25 	output := filepath.Join(root, "local/alpha-aimed-shot", time.Now().Format("20060102-150405"))
     26 	for _, name := range []string{"solar", "gripo"} {
     27 		t.Run(name, func(t *testing.T) {
     28 			world := newTestServer(t)
     29 			fields := creationForm()
     30 			fields.Set("profession", "5")
     31 			if response := creationRequest(world, accountVisitor(t, world), fields); response.Code != http.StatusNoContent {
     32 				t.Fatal(response.Code, response.Body.String())
     33 			}
     34 			player := world.characters[2]
     35 			damage := 12
     36 			if name == "gripo" {
     37 				// The stocked QL10 upgrade uses the existing cap-checked checkpoint.
     38 				trainBasicShopWeaponFixture(t, player)
     39 				player.inventory.Items[3].Type, player.inventory.Items[3].Template, player.inventory.Items[3].Quality = 51018, 123976, 10
     40 				player.profession = 5
     41 				player.inventory.Items[3].Ammo = 8
     42 				damage = 88
     43 			}
     44 			world.combatRoll = func(uint32) uint32 { return 0 }
     45 			player.browserTokenHash = fmt.Sprintf("%x", sha256.Sum256([]byte("aimed_shot")))
     46 			if err := world.saveUser(player); err != nil {
     47 				t.Fatal(err)
     48 			}
     49 			if err := world.saveInventory(player, player.inventory); err != nil {
     50 				t.Fatal(err)
     51 			}
     52 			var target *character
     53 			for _, actor := range world.characters {
     54 				if actor.npc && actor.playfield == 950 && actor.monsterData == 17655 && actor.level == 1 && actor.combatProfile != nil && (target == nil || actor.id < target.id) {
     55 					target = actor
     56 				}
     57 			}
     58 			if target == nil {
     59 				t.Fatal("original Leet unavailable")
     60 			}
     61 			forward, _ := bodyAxes(player.rotation)
     62 			target.position = [3]float32{player.position[0] + 2*forward[0], player.position[1], player.position[2] + 2*forward[2]}
     63 			target.health = 1000
     64 			if err := world.flushWorld(); err != nil {
     65 				t.Fatal(err)
     66 			}
     67 			// Keep target attacks and recovery beyond this focused special-action check.
     68 			target.rechargeUntil = time.Now().Add(5 * time.Minute)
     69 			target.regenerationAt = target.rechargeUntil
     70 			target.motion.roaming = false
     71 			targetID := target.id
     72 			shutdown := startTestService(t, world)
     73 			mux := http.NewServeMux()
     74 			mux.Handle("/", world.httpHandler())
     75 			// Suppress ordinary player attacks only after the real equipment action.
     76 			mux.HandleFunc("POST /test-hold-attacks", testPlayerHTTP(world, 2, func(world *server, w http.ResponseWriter, r *http.Request) {
     77 				world.characters[2].rechargeUntil = time.Now().Add(5 * time.Minute)
     78 				w.WriteHeader(http.StatusNoContent)
     79 			}))
     80 			mux.HandleFunc("GET /test-state", testPlayerHTTP(world, 2, func(world *server, w http.ResponseWriter, r *http.Request) {
     81 				player, target := world.characters[2], world.characters[targetID]
     82 				json.NewEncoder(w).Encode(map[string]any{"target": target.id, "health": target.health, "fighting": player.fighting,
     83 					"locked": time.Now().Before(player.skillLocks[151]), "remaining": time.Until(player.skillLocks[151]).Seconds(), "inventory": player.inventory, "level": player.level,
     84 					"ip": player.stats.ImprovementPoints, "damage": damage, "weapon": name, "sneaking": player.movement.components[0] == 6})
     85 			}))
     86 			host := httptest.NewServer(mux)
     87 			defer host.Close()
     88 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
     89 			defer cancel()
     90 			command := exec.CommandContext(ctx, "node", filepath.Join(root, "tools/client/test_aimed_shot.mjs"), host.URL, filepath.Join(output, name))
     91 			command.Dir, command.Stdout, command.Stderr = root, os.Stdout, os.Stderr
     92 			err := command.Run()
     93 			cancel()
     94 			host.Close()
     95 			if err := shutdown(); err != nil {
     96 				t.Fatal(err)
     97 			}
     98 			t.Logf("Browser artifacts: %s", filepath.Join(output, name))
     99 			if err != nil {
    100 				t.Fatal(err)
    101 			}
    102 			cold := newTestInventoryServer(t)
    103 			if cold.characters[target.id].health != uint32(1000-damage) || !reflect.DeepEqual(cold.characters[2].inventory, player.inventory) ||
    104 				!cold.characters[2].skillLocks[151].Equal(player.skillLocks[151]) || cold.characters[2].skillLocks[151].IsZero() {
    105 				t.Fatal("native AimedShot damage, ownership or lock changed on cold recovery")
    106 			}
    107 		})
    108 	}
    109 }