aorecomp

Anarchy Online classic in the browser

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


server/world_loading_test.go (6958B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"net"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestWorldLoadingPublishesResourceFoundationsAfterWeaponReplay(t *testing.T) {
     12 	world := newTestServer(t)
     13 	player := world.newCharacter()
     14 	player.level, player.profession, player.appearance = 100, 7, 0x515
     15 	player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
     16 	player.inventory.Items = []ownedItem{{ID: 1, Type: 51018, Template: 124261, Quality: 75, Slot: 6, Count: 1}}
     17 	startTestService(t, world)
     18 	client, connection := net.Pipe()
     19 	defer client.Close()
     20 	client.SetDeadline(time.Now().Add(5 * time.Second))
     21 	go func() { defer connection.Close(); world.serveWorldConnection(connection) }()
     22 
     23 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
     24 	binary.BigEndian.PutUint16(cookie, 0xdfdf)
     25 	if _, err := client.Write(cookie); err != nil {
     26 		t.Fatal(err)
     27 	}
     28 	weaponSeen := false
     29 	stats := make(map[uint32]uint32)
     30 	for {
     31 		packet, err := readPacket(client)
     32 		if err != nil {
     33 			t.Fatal(err)
     34 		}
     35 		key := binary.BigEndian.Uint32(packet[16:20])
     36 		if key == 0x3b1d2268 {
     37 			weaponSeen = true
     38 		}
     39 		if weaponSeen && key == 0x2b333d6e {
     40 			stats[binary.BigEndian.Uint32(packet[29:33])] = binary.BigEndian.Uint32(packet[33:37])
     41 		}
     42 		if key == 0x5f52412e {
     43 			break
     44 		}
     45 	}
     46 
     47 	if !weaponSeen || stats[1] != 872 || stats[221] != 681 {
     48 		t.Fatalf("weapon-derived resource foundations were not replayed last: %v", stats)
     49 	}
     50 }
     51 
     52 func TestWorldLoadingObserverReceivesPeriodicLifecycleAfterActorSnapshot(t *testing.T) {
     53 	for _, lethal := range []bool{false, true} {
     54 		t.Run(map[bool]string{false: "expiry", true: "death"}[lethal], func(t *testing.T) {
     55 			world, owner := newPeriodicNanoWorld(t)
     56 			observer := &combatTestConn{}
     57 			world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     58 			world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true, loading: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}}
     59 			if lethal {
     60 				world.characters[1000000].health = 7
     61 			}
     62 
     63 			at := castPeriodicNano(t, world, 43376)
     64 			world.advanceNanos(at.Add(time.Minute))
     65 
     66 			var expected [][]byte
     67 			for _, packet := range owner.packets {
     68 				if binary.BigEndian.Uint32(packet[24:28]) == 1000000 {
     69 					expected = append(expected, packet)
     70 				}
     71 			}
     72 			var actual [][]byte
     73 			for _, packet := range observer.packets {
     74 				if binary.BigEndian.Uint32(packet[24:28]) == 1000000 {
     75 					actual = append(actual, packet)
     76 				}
     77 			}
     78 			if len(expected) == 0 || len(actual) != len(expected) {
     79 				t.Fatalf("loading observer lost lifecycle packets: got %d want %d", len(actual), len(expected))
     80 			}
     81 			for index, packet := range expected {
     82 				if !bytes.Equal(actual[index][16:], packet[16:]) {
     83 					t.Fatalf("loading observer changed packet %d", index)
     84 				}
     85 			}
     86 			if len(world.characters[1000000].activeNanos) != 0 || !world.clients[2].loading {
     87 				t.Fatal("expiry or observer loading state changed")
     88 			}
     89 			if lethal && world.characters[1000000].corpse == nil {
     90 				t.Fatal("lethal sequence did not produce a corpse")
     91 			}
     92 		})
     93 	}
     94 }
     95 
     96 func TestWorldLoadingObserverReceivesWeaponTimelineWithoutBecomingATarget(t *testing.T) {
     97 	world, owner := newCombatWorld(t)
     98 	observer := &combatTestConn{}
     99 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    100 	world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true, loading: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}}
    101 
    102 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    103 		t.Fatal(err)
    104 	}
    105 	world.advanceCombat(world.characters[1].nextAttack)
    106 	if err := world.handleCombat(world.clients[2], attackPacket(2, 1000000)[20:]); err != nil {
    107 		t.Fatal(err)
    108 	}
    109 
    110 	if len(owner.packets) != 3 || len(observer.packets) != 3 {
    111 		t.Fatalf("loading observer missed weapon timeline: %d/%d", len(owner.packets), len(observer.packets))
    112 	}
    113 	for index, packet := range owner.packets {
    114 		if !bytes.Equal(observer.packets[index][16:], packet[16:]) {
    115 			t.Fatalf("weapon packet %d differs", index)
    116 		}
    117 	}
    118 	if world.characters[2].health != 100 || world.characters[2].fighting != 0 || world.characters[1000000].fighting != 1 {
    119 		t.Fatal("loading observer became a combat participant")
    120 	}
    121 }
    122 
    123 func TestWorldLoadingObserverReceivesResourceMaximumChanges(t *testing.T) {
    124 	world, connections := newResourceBackpackTeam(t)
    125 	world.clients[2].loading = true
    126 	world.clients[3].loading = true
    127 	for _, conn := range connections {
    128 		conn.packets = nil
    129 	}
    130 	stats := world.characters[1].baseStats()
    131 	stats.Body.BodyDevelopment, stats.Body.NanoPool = 6, 6
    132 
    133 	if err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]); err != nil {
    134 		t.Fatal(err)
    135 	}
    136 
    137 	for index, want := range [][2]uint32{{52, 46}, {142, 136}, {52, 0}} {
    138 		found := make(map[uint32]uint32)
    139 		for _, packet := range connections[index].packets {
    140 			if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 {
    141 				found[binary.BigEndian.Uint32(packet[29:33])] = binary.BigEndian.Uint32(packet[33:37])
    142 			}
    143 		}
    144 		if found[1] != want[0] || found[221] != want[1] {
    145 			t.Fatalf("observer %d maxima %v want %v", index+1, found, want)
    146 		}
    147 	}
    148 }
    149 
    150 func TestWorldLoadingPublishesAttackRatingAfterWeaponReplay(t *testing.T) {
    151 	world := newTestServer(t)
    152 	player := world.newCharacter()
    153 	player.level, player.appearance = 200, 0x515
    154 	player.stats.Abilities = characterAbilitiesStats{200, 200, 200, 200, 200, 200}
    155 	player.stats.Melee.OneHandBlunt, player.stats.MiscWeapons.HeavyWeapons = 238, 142
    156 	player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = 112, 112
    157 	player.stats.Body.Brawling, player.stats.Body.Dimach = 144, 17
    158 	player.inventory.Items = []ownedItem{{ID: 1, Type: 51018, Template: 125456, Quality: 10, Slot: 6, Count: 1}}
    159 	startTestService(t, world)
    160 	client, connection := net.Pipe()
    161 	defer client.Close()
    162 	client.SetDeadline(time.Now().Add(5 * time.Second))
    163 	go func() { defer connection.Close(); world.serveWorldConnection(connection) }()
    164 
    165 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
    166 	binary.BigEndian.PutUint16(cookie, 0xdfdf)
    167 	if _, err := client.Write(cookie); err != nil {
    168 		t.Fatal(err)
    169 	}
    170 	weaponSeen := false
    171 	var rating uint32
    172 	for {
    173 		packet, err := readPacket(client)
    174 		if err != nil {
    175 			t.Fatal(err)
    176 		}
    177 		key := binary.BigEndian.Uint32(packet[16:20])
    178 		if key == 0x3b1d2268 {
    179 			weaponSeen = true
    180 		}
    181 		if weaponSeen && key == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 22 {
    182 			rating = binary.BigEndian.Uint32(packet[33:37])
    183 		}
    184 		if key == 0x5f52412e {
    185 			break
    186 		}
    187 	}
    188 
    189 	var actualRating int32
    190 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { actualRating = state.characters[1].attackRating(); return nil }))
    191 	if !weaponSeen || rating != 256 || actualRating != 256 {
    192 		t.Fatalf("equipped attack rating was not restored after weapon replay: %d", rating)
    193 	}
    194 }