aorecomp

Anarchy Online classic in the browser

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


server/grid_test.go (5863B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func newGridWorld(t *testing.T) (*server, *combatTestConn) {
     10 	t.Helper()
     11 	world, conn := newCombatWorld(t)
     12 	player := world.newCharacter()
     13 	player.stats = testCharacterStats()
     14 	player.playfield = 800
     15 	player.position = [3]float32{637, 67.3, 728}
     16 	player.stats.TradeRepair.ComputerLiteracy = 89 // Effective 91 with Intelligence 10.
     17 	player.returnDoor = &doorOrigin{1183, 0xc000049f}
     18 	if err := world.saveUser(player); err != nil {
     19 		t.Fatal(err)
     20 	}
     21 	return world, conn
     22 }
     23 
     24 func gridUseRequest(sequence uint32) []byte {
     25 	request := insuranceUseRequest(sequence)[20:]
     26 	binary.BigEndian.PutUint32(request[44:48], 0xc0040320)
     27 	return request
     28 }
     29 
     30 func TestGridTerminalEntersTheOrdinaryGridAndPersists(t *testing.T) {
     31 	t.Chdir(t.TempDir())
     32 	world, conn := newGridWorld(t)
     33 	client := world.clients[1]
     34 
     35 	if err := world.handleTerminalUse(client, gridUseRequest(1)); err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil {
     39 		t.Fatal(err)
     40 	}
     41 	restored := newTestInventoryServer(t).characters[1]
     42 
     43 	if restored.playfield != 152 || restored.position != [3]float32{242.3208, 4.6000195, 193.26456} || restored.returnDoor != nil || len(conn.packets) != 3 ||
     44 		binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x52526858 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 1 ||
     45 		binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x43197d22 || binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x3c {
     46 		t.Fatal("ordinary Grid use did not acknowledge and persist one native teleport")
     47 	}
     48 }
     49 
     50 func TestGridTerminalRejectsIneligibleUse(t *testing.T) {
     51 	for _, scenario := range []string{"skill 90", "outgoing combat", "incoming combat", "distance", "height", "wrong playfield", "original client", "dead", "loading", "not ready", "stale session", "wrong identity"} {
     52 		t.Run(scenario, func(t *testing.T) {
     53 			t.Chdir(t.TempDir())
     54 			world, conn := newGridWorld(t)
     55 			player, client := world.characters[1], world.clients[1]
     56 			request := gridUseRequest(1)
     57 			switch scenario {
     58 			case "skill 90":
     59 				player.stats.TradeRepair.ComputerLiteracy = 88
     60 			case "outgoing combat":
     61 				player.fighting = 1000000
     62 			case "incoming combat":
     63 				world.characters[1000000].fighting = 1
     64 				world.fighting[1000000] = world.characters[1000000]
     65 			case "distance":
     66 				player.position[0] += 6
     67 			case "height":
     68 				player.position[1] += 6
     69 			case "wrong playfield":
     70 				player.playfield = 705
     71 			case "original client":
     72 				client.target = false
     73 			case "dead":
     74 				player.health = 0
     75 			case "loading":
     76 				client.loading = true
     77 			case "not ready":
     78 				client.ready = false
     79 			case "stale session":
     80 				world.clients[1] = &worldClient{id: 1}
     81 			case "wrong identity":
     82 				binary.BigEndian.PutUint32(request[44:48], 0xc0040321)
     83 			}
     84 			before := player.playfield
     85 
     86 			if err := world.handleTerminalUse(client, request); err != nil {
     87 				t.Fatal(err)
     88 			}
     89 
     90 			stateOffset := 29
     91 			if !client.target {
     92 				stateOffset = 36
     93 			}
     94 			if player.playfield != before || len(conn.packets) > 1 || len(conn.packets) == 1 && binary.BigEndian.Uint32(conn.packets[0][stateOffset:stateOffset+4]) != 2 {
     95 				t.Fatal("ineligible terminal use teleported or acknowledged success")
     96 			}
     97 		})
     98 	}
     99 }
    100 
    101 func TestGridExitHeldMovementRoundTripAndReplay(t *testing.T) {
    102 	t.Chdir(t.TempDir())
    103 	world, first := newGridWorld(t)
    104 	player, old := world.characters[1], world.clients[1]
    105 	request := gridUseRequest(1)
    106 
    107 	if err := world.handleTerminalUse(old, request); err != nil {
    108 		t.Fatal(err)
    109 	}
    110 	second := &combatTestConn{}
    111 	client := &worldClient{id: 1, conn: second, target: true, ready: true}
    112 	world.characters[client.id].movement = heldPlayerMovement(world.characters[client.id], -1, time.Now())
    113 	world.clients[1] = client
    114 	if err := world.handleTerminalUse(old, request); err != nil {
    115 		t.Fatal(err)
    116 	}
    117 	world.advanceZoneCrossings(world.characters[client.id].movement.updatedAt)
    118 	if player.playfield != 152 || len(second.packets) != 0 || len(first.packets) != 3 {
    119 		t.Fatal("entry bounced immediately or an old session replayed Use")
    120 	}
    121 	world.advanceZoneCrossings(world.characters[client.id].movement.updatedAt.Add(3 * time.Second))
    122 	if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil {
    123 		t.Fatal(err)
    124 	}
    125 	restored := newTestInventoryServer(t).characters[1]
    126 
    127 	if restored.playfield != 800 || restored.position != [3]float32{620.773, 67.30926, 720.8844} || restored.returnDoor != nil ||
    128 		len(second.packets) != 2 || binary.BigEndian.Uint32(second.packets[0][16:20]) != 0x43197d22 || binary.BigEndian.Uint32(second.packets[1][16:20]) != 0x3c {
    129 		t.Fatal("held movement did not persist the Borealis exit through native teleport")
    130 	}
    131 }
    132 
    133 func TestGridExitRequiresSkillAndNearbyLivingSession(t *testing.T) {
    134 	for _, scenario := range []string{"skill 91", "skill 90", "distance", "height", "original client", "dead", "loading", "not ready"} {
    135 		t.Run(scenario, func(t *testing.T) {
    136 			t.Chdir(t.TempDir())
    137 			world, _ := newGridWorld(t)
    138 			player, client := world.characters[1], world.clients[1]
    139 			player.playfield, player.position = 152, [3]float32{242.31323, 4.7, 199.41223}
    140 			world.characters[client.id].movement = heldPlayerMovement(world.characters[client.id], 1, time.Now())
    141 			switch scenario {
    142 			case "skill 90":
    143 				player.stats.TradeRepair.ComputerLiteracy = 88
    144 			case "distance":
    145 				player.position[0] += 3
    146 			case "height":
    147 				player.position[1] += 3
    148 			case "original client":
    149 				client.target = false
    150 			case "dead":
    151 				player.health = 0
    152 			case "loading":
    153 				client.loading = true
    154 			case "not ready":
    155 				client.ready = false
    156 			}
    157 
    158 			world.advanceZoneCrossings(world.characters[client.id].movement.updatedAt)
    159 
    160 			if (player.playfield == 800) != (scenario == "skill 91") {
    161 				t.Fatal("exit ignored its skill, distance or session requirement")
    162 			}
    163 		})
    164 	}
    165 }