aorecomp

Anarchy Online classic in the browser

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


server/faction_insurance_test.go (5452B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"slices"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestFactionInsuranceUsesAuthoredScannerRestrictions(t *testing.T) {
     11 	for _, scenario := range []struct {
     12 		name       string
     13 		scanner    uint32
     14 		playfield  uint32
     15 		position   [3]float32
     16 		appearance uint16
     17 		accepted   bool
     18 	}{
     19 		{"Clan at Federated", 0xc0020280, 640, [3]float32{490.992, 8.5, 402.60376}, 0x315, true},
     20 		{"Neutral at Federated", 0xc0020280, 640, [3]float32{490.992, 8.5, 402.60376}, 0x115, false},
     21 		{"Omni at Federated", 0xc0020280, 640, [3]float32{490.992, 8.5, 402.60376}, 0x515, false},
     22 		{"Neutral at ICC", 0xc0020236, 566, [3]float32{238.89539, 32.91, 313.8018}, 0x115, true},
     23 		{"Clan at ICC", 0xc0020236, 566, [3]float32{238.89539, 32.91, 313.8018}, 0x315, true},
     24 		{"Omni at ICC", 0xc0020236, 566, [3]float32{238.89539, 32.91, 313.8018}, 0x515, true},
     25 	} {
     26 		t.Run(scenario.name, func(t *testing.T) {
     27 			world, output := newReclaimWorld(t)
     28 			player := world.characters[1]
     29 			player.playfield, player.position, player.appearance = scenario.playfield, scenario.position, scenario.appearance
     30 			request := insuranceUseRequest(1)[20:]
     31 			binary.BigEndian.PutUint32(request[44:48], scenario.scanner)
     32 
     33 			if err := world.handleTerminalUse(world.clients[1], request); err != nil {
     34 				t.Fatal(err)
     35 			}
     36 			restored := newTestInventoryServer(t).characters[1]
     37 
     38 			if scenario.accepted {
     39 				if player.resurrectionTerminal != scenario.scanner || restored.resurrectionTerminal != scenario.scanner ||
     40 					len(output.packets) != 2 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 1 ||
     41 					binary.BigEndian.Uint32(output.packets[1][29:33]) != 79 {
     42 					t.Fatal("authored scanner use did not persist and acknowledge its saved point")
     43 				}
     44 			} else if player.resurrectionTerminal != 0 || restored.resurrectionTerminal != 0 ||
     45 				len(output.packets) != 1 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 2 {
     46 				t.Fatal("foreign scanner use changed its saved point or received success")
     47 			}
     48 		})
     49 	}
     50 }
     51 
     52 func TestFactionInsuranceColdDeathReturnAndExactReclaim(t *testing.T) {
     53 	for _, scenario := range []struct {
     54 		name       string
     55 		scanner    uint32
     56 		reclaim    uint32
     57 		playfield  uint32
     58 		arrival    [3]float32
     59 		rotation   [4]float32
     60 		collection [3]float32
     61 		appearance uint16
     62 	}{
     63 		{"Clan", 0xc0020280, 0xc0020280, 640, [3]float32{490.992, 8.500121, 402.60376}, [4]float32{0, 0, 0, 1}, [3]float32{485.54395, 8.5, 399.3545}, 0x315},
     64 		{"Neutral", 0xc0020236, 0xc0010236, 566, [3]float32{238.89539, 32.910038, 313.8018}, [4]float32{0, .70710677, 0, .70710677}, [3]float32{314.1781, 32.9, 301.3122}, 0x115},
     65 	} {
     66 		t.Run(scenario.name, func(t *testing.T) {
     67 			world, _ := newReclaimWorld(t)
     68 			loadResurrectionLocations(t, world)
     69 			player, client := world.characters[1], world.clients[1]
     70 			player.playfield, player.position, player.appearance = scenario.playfield, scenario.arrival, scenario.appearance
     71 			before := slices.Clone(player.inventory.Items)
     72 			request := insuranceUseRequest(1)[20:]
     73 			binary.BigEndian.PutUint32(request[44:48], scenario.scanner)
     74 
     75 			if err := world.handleTerminalUse(client, request); err != nil {
     76 				t.Fatal(err)
     77 			}
     78 			defeatReclaimPlayer(t, world)
     79 			dead := newTestInventoryServer(t).characters[1]
     80 			if err := world.handleResurrection(client, resurrectionRequest(1)[20:]); err != nil {
     81 				t.Fatal(err)
     82 			}
     83 			returned := newTestInventoryServer(t).characters[1]
     84 
     85 			// Reconnect at the actual reclaim booth after its waiting period.
     86 			world.clients[1], client.ready = client, true
     87 			client.loading = false
     88 			player.position = scenario.collection
     89 			inventory := player.inventory
     90 			inventory.Reclaim = slices.Clone(inventory.Reclaim)
     91 			if len(inventory.Reclaim) != 1 {
     92 				t.Fatal("death did not retain exactly one reclaim item")
     93 			}
     94 			inventory.Reclaim[0].AvailableAt = time.Now().Add(-time.Second).UTC()
     95 			if err := world.saveInventory(player, inventory); err != nil {
     96 				t.Fatal(err)
     97 			}
     98 			binary.BigEndian.PutUint32(request[20:24], 2)
     99 			binary.BigEndian.PutUint32(request[40:44], 51059)
    100 			binary.BigEndian.PutUint32(request[44:48], scenario.reclaim)
    101 			if err := world.handleTerminalUse(client, request); err != nil {
    102 				t.Fatal(err)
    103 			}
    104 			if err := world.handleReclaim(client, reclaimRequest(109)); err != nil {
    105 				t.Fatal(err)
    106 			}
    107 			if err := world.handleInventory(client, reclaimTakeRequest(0)); err != nil {
    108 				t.Fatal(err)
    109 			}
    110 			if err := world.handleInventory(client, reclaimTakeRequest(0)); err != nil {
    111 				t.Fatal(err)
    112 			}
    113 			restored := newTestInventoryServer(t).characters[1]
    114 
    115 			if dead.health != 0 || dead.resurrectionTerminal != scenario.scanner || len(dead.inventory.Reclaim) != 1 ||
    116 				dead.inventory.Reclaim[0].Item != before[2] || !slices.Equal(dead.inventory.Items, before[:2]) {
    117 				t.Fatal("cold death lost its saved point or item ownership")
    118 			}
    119 			if returned.playfield != scenario.playfield || returned.position != scenario.arrival || returned.rotation != scenario.rotation ||
    120 				returned.health != 1 || returned.nano != 1 || returned.resurrectionTerminal != scenario.scanner {
    121 				t.Fatal("saved scanner did not override affiliated training return", returned.userData())
    122 			}
    123 			if !slices.Equal(player.inventory.Items, before) || !slices.Equal(restored.inventory.Items, before) ||
    124 				len(player.inventory.Reclaim) != 0 || len(restored.inventory.Reclaim) != 0 {
    125 				t.Fatal("reclaim lost or duplicated an original instance")
    126 			}
    127 		})
    128 	}
    129 }