aorecomp

Anarchy Online classic in the browser

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


server/initial_resurrection_test.go (8889B)


      1 package main
      2 
      3 import (
      4 	"ao/server/assets"
      5 	"fmt"
      6 	"math"
      7 	"slices"
      8 	"testing"
      9 )
     10 
     11 var initialReturnCases = []struct {
     12 	name           string
     13 	side           uint16
     14 	training       uint32
     15 	position       [3]float32
     16 	backyard, door uint32
     17 }{
     18 	{"Neutral", 0, 954, [3]float32{239.886292, 6.1670566, 72.431137}, 3060, 3876588532},
     19 	{"Clan", 1, 952, [3]float32{42.737694, 14.75, 70.091897}, 3100, 3876588572},
     20 	{"Omni", 2, 950, [3]float32{52.597332, 14.75, 57.636177}, 3000, 3876588472},
     21 }
     22 
     23 func loadResurrectionLocations(t *testing.T, world *server) {
     24 	t.Helper()
     25 	var err error
     26 	world.zoneLinks, err = readZoneLinks()
     27 	if err != nil {
     28 		t.Fatal(err)
     29 	}
     30 	world.terrain, err = readTerrain()
     31 	if err != nil {
     32 		t.Fatal(err)
     33 	}
     34 }
     35 
     36 func TestUnsavedResurrectionUsesAffiliatedTrainingAndRetainsBelongings(t *testing.T) {
     37 	for _, scenario := range initialReturnCases {
     38 		t.Run(scenario.name, func(t *testing.T) {
     39 			useNPCData(t, testNPCData())
     40 			useResources(t, assets.Files)
     41 			world := newTestServer(t)
     42 			player := world.newCharacter()
     43 			player.appearance = player.appearance&^0x600 | scenario.side<<9
     44 			player.playfield, player.health, player.nano, player.experience = 705, 0, 0, 100
     45 			if err := world.saveUser(player); err != nil {
     46 				t.Fatal(err)
     47 			}
     48 			items := []ownedItem{{ID: 17, Type: 51018, Template: 121567, Quality: 1, Slot: 6, Count: 1}, {ID: 18, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1}}
     49 			if err := world.saveInventory(player, playerInventory{LastID: 18, Items: items}); err != nil {
     50 				t.Fatal(err)
     51 			}
     52 			conn := &combatTestConn{}
     53 			client := &worldClient{id: player.id, conn: conn, target: true, ready: true}
     54 			world.clients[player.id] = client
     55 
     56 			if err := world.handleResurrection(client, resurrectionRequest(player.id)[20:]); err != nil {
     57 				t.Fatal(err)
     58 			}
     59 			cold := newTestInventoryServer(t)
     60 
     61 			for _, current := range []*character{player, cold.characters[player.id]} {
     62 				if current.playfield != scenario.training || current.returnDoor == nil || *current.returnDoor != (doorOrigin{scenario.backyard, scenario.door}) ||
     63 					current.health != 1 || current.nano != 1 || current.experience != 100 || !slices.Equal(current.inventory.Items, items) {
     64 					t.Fatal("unsaved return crossed factions or lost earned state", current.playfield, current.returnDoor, current.inventory)
     65 				}
     66 				for index, want := range scenario.position {
     67 					if math.Abs(float64(current.position[index]-want)) > .0001 {
     68 						t.Fatal("return missed the authored training entrance", current.position)
     69 					}
     70 				}
     71 			}
     72 			if len(conn.packets) != 3 || world.clients[player.id] != nil || client.ready {
     73 				t.Fatal("return did not finish one native transition")
     74 			}
     75 		})
     76 	}
     77 }
     78 
     79 func TestUnsavedResurrectionKeepsAValidNumberedBackyard(t *testing.T) {
     80 	for _, scenario := range initialReturnCases {
     81 		t.Run(scenario.name, func(t *testing.T) {
     82 			useNPCData(t, testNPCData())
     83 			useResources(t, assets.Files)
     84 			world := newTestServer(t)
     85 			player := world.newCharacter()
     86 			player.appearance = player.appearance&^0x600 | scenario.side<<9
     87 			player.playfield = scenario.training
     88 			player.health = 0
     89 			player.returnDoor = &doorOrigin{scenario.backyard + 1, scenario.door + 1}
     90 			wanted := *player.returnDoor
     91 			if err := world.saveUser(player); err != nil {
     92 				t.Fatal(err)
     93 			}
     94 			client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true}
     95 			world.clients[player.id] = client
     96 
     97 			if err := world.handleResurrection(client, resurrectionRequest(player.id)[20:]); err != nil {
     98 				t.Fatal(err)
     99 			}
    100 			cold := newTestInventoryServer(t)
    101 
    102 			if player.playfield != scenario.training || player.returnDoor == nil || *player.returnDoor != wanted || cold.characters[player.id].returnDoor == nil || *cold.characters[player.id].returnDoor != wanted {
    103 				t.Fatal("death replaced the associated numbered backyard", player.returnDoor, cold.characters[player.id].returnDoor)
    104 			}
    105 		})
    106 	}
    107 }
    108 
    109 func TestSavedInsuranceReturnOverridesTheCurrentAffiliation(t *testing.T) {
    110 	for _, scenario := range initialReturnCases {
    111 		t.Run(scenario.name, func(t *testing.T) {
    112 			useNPCData(t, testNPCData())
    113 			useResources(t, assets.Files)
    114 			world := newTestServer(t)
    115 			player := world.newCharacter()
    116 			player.appearance, player.resurrectionTerminal = 0x515, 0xc00202c1
    117 			if err := world.saveUser(player); err != nil {
    118 				t.Fatal(err)
    119 			}
    120 			player.appearance = player.appearance&^0x600 | scenario.side<<9
    121 			player.health = 0
    122 			player.returnDoor = &doorOrigin{scenario.backyard, scenario.door}
    123 			if err := world.saveUser(player); err != nil {
    124 				t.Fatal(err)
    125 			}
    126 			client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true}
    127 			world.clients[player.id] = client
    128 
    129 			if err := world.handleResurrection(client, resurrectionRequest(player.id)[20:]); err != nil {
    130 				t.Fatal(err)
    131 			}
    132 			cold := newTestInventoryServer(t)
    133 
    134 			for _, current := range []*character{player, cold.characters[player.id]} {
    135 				if current.playfield != 705 || current.position != [3]float32{918, 20.7, 578} || current.returnDoor != nil || current.resurrectionTerminal != 0xc00202c1 {
    136 					t.Fatal("initial return replaced the saved scanner", current.playfield, current.position, current.returnDoor)
    137 				}
    138 			}
    139 		})
    140 	}
    141 }
    142 
    143 func TestUnsavedResurrectionFailedWritesKeepTheOldLocationAndCanRetry(t *testing.T) {
    144 	for _, scenario := range initialReturnCases {
    145 		for _, failure := range []struct{ name, statement string }{
    146 			{"update", "CREATE TRIGGER reject_initial_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(FAIL,'return'); END"},
    147 			{"ignored update", "CREATE TRIGGER reject_initial_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(IGNORE); END"},
    148 			{"commit", "CREATE TABLE pending_initial_return (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_initial_return AFTER UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN INSERT INTO pending_initial_return VALUES(999999); END"},
    149 		} {
    150 			t.Run(scenario.name+"/"+failure.name, func(t *testing.T) {
    151 				useNPCData(t, testNPCData())
    152 				useResources(t, assets.Files)
    153 				world := newTestServer(t)
    154 				player := world.newCharacter()
    155 				player.appearance = player.appearance&^0x600 | scenario.side<<9
    156 				player.health, player.nano = 0, 0
    157 				player.returnDoor = &doorOrigin{scenario.backyard + 1, scenario.door + 1}
    158 				if err := world.saveUser(player); err != nil {
    159 					t.Fatal(err)
    160 				}
    161 				before := player.record()
    162 				conn := &combatTestConn{}
    163 				client := &worldClient{id: player.id, conn: conn, target: true, ready: true}
    164 				world.clients[player.id] = client
    165 				if _, err := world.db.Exec(failure.statement); err != nil {
    166 					t.Fatal(err)
    167 				}
    168 
    169 				err := world.handleResurrection(client, resurrectionRequest(player.id)[20:])
    170 				cold := newTestInventoryServer(t)
    171 
    172 				if err == nil || player.record() != before || cold.characters[player.id].record() != before || world.clients[player.id] != client || !client.ready || len(conn.packets) != 0 {
    173 					t.Fatal("failed initial return changed durable state or published success", err, player.playfield, player.returnDoor)
    174 				}
    175 				if _, err := world.db.Exec("DROP TRIGGER reject_initial_return"); err != nil {
    176 					t.Fatal(err)
    177 				}
    178 
    179 				if err := world.handleResurrection(client, resurrectionRequest(player.id)[20:]); err != nil {
    180 					t.Fatal(err)
    181 				}
    182 				retried := newTestInventoryServer(t).characters[player.id]
    183 
    184 				if retried.playfield != scenario.training || retried.health != 1 || retried.nano != 1 || retried.returnDoor == nil || *retried.returnDoor != (doorOrigin{scenario.backyard + 1, scenario.door + 1}) || len(conn.packets) != 3 {
    185 					t.Fatal("retry lost the numbered backyard or repeated the transition", retried.playfield, retried.returnDoor, len(conn.packets))
    186 				}
    187 			})
    188 		}
    189 	}
    190 }
    191 
    192 func TestUnsavedResurrectionRejectsUnrelatedReturnDoorContext(t *testing.T) {
    193 	for _, origin := range []doorOrigin{{3000, 3876588472}, {705, 0xc01102c1}, {3100, 1}} {
    194 		t.Run(fmt.Sprint(origin), func(t *testing.T) {
    195 			useNPCData(t, testNPCData())
    196 			useResources(t, assets.Files)
    197 			world := newTestServer(t)
    198 			player := world.newCharacter()
    199 			player.appearance, player.health = 0x315, 0
    200 			player.returnDoor = &origin
    201 			if err := world.saveUser(player); err != nil {
    202 				t.Fatal(err)
    203 			}
    204 			client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true}
    205 			world.clients[player.id] = client
    206 
    207 			if err := world.handleResurrection(client, resurrectionRequest(player.id)[20:]); err != nil {
    208 				t.Fatal(err)
    209 			}
    210 
    211 			if player.playfield != 952 || player.returnDoor == nil || *player.returnDoor != (doorOrigin{3100, 3876588572}) {
    212 				t.Fatal("unrelated door context selected the initial return", player.playfield, player.returnDoor)
    213 			}
    214 		})
    215 	}
    216 }