aorecomp

Anarchy Online classic in the browser

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


server/combat_lifecycle_test.go (5245B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"strings"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestCombatReplacedSessionCannotMoveRestOrFinishLoading(t *testing.T) {
     12 	world, currentOutput := newCombatWorld(t)
     13 	world.clients[1].loading = true
     14 	oldOutput := &combatTestConn{}
     15 	old := &worldClient{id: 1, conn: oldOutput, ready: true}
     16 	posture := make([]byte, 36)
     17 	binary.BigEndian.PutUint32(posture[16:20], 86)
     18 	movement := make([]byte, 56)
     19 	binary.BigEndian.PutUint32(movement[20:24], 0x41200000)
     20 
     21 	if err := world.handleCharacterReady(old); err != nil {
     22 		t.Fatal(err)
     23 	}
     24 	if err := world.handlePosture(old, posture); err != nil {
     25 		t.Fatal(err)
     26 	}
     27 	if err := world.handleMovement(old, movement); err != nil {
     28 		t.Fatal(err)
     29 	}
     30 
     31 	if !world.clients[1].loading || world.characters[1].sitting || world.characters[1].position != [3]float32{} ||
     32 		len(currentOutput.packets) != 0 || len(oldOutput.packets) != 0 {
     33 		t.Fatalf("replaced session changed the live actor: player=%+v client=%+v", world.characters[1], world.clients[1])
     34 	}
     35 }
     36 
     37 func TestCombatRepeatedWorldCookieCannotReinitializeTheActor(t *testing.T) {
     38 	world, _ := newCombatWorld(t)
     39 	delete(world.clients, 1)
     40 	cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
     41 	conn := &combatInterestConn{input: bytes.NewReader(append(cookie, cookie...))}
     42 
     43 	err := serveWorldClient(world, &worldClient{conn: conn, loading: true})
     44 
     45 	if err == nil || !strings.Contains(err.Error(), "already initialized") {
     46 		t.Fatalf("repeated cookie accepted: %v", err)
     47 	}
     48 	spawns := 0
     49 	for _, packet := range conn.packets {
     50 		if binary.BigEndian.Uint32(packet[20:24]) == 0x271b3a6b && binary.BigEndian.Uint32(packet[28:32]) == 1 {
     51 			spawns++
     52 		}
     53 	}
     54 	if spawns != 1 || world.clients[1] != nil {
     55 		t.Fatalf("repeated initialization changed lifecycle: spawns=%d client=%v", spawns, world.clients[1])
     56 	}
     57 }
     58 
     59 func TestCombatProfileEditStopsBothActorsInTheirOriginalPlayfield(t *testing.T) {
     60 	t.Chdir(t.TempDir())
     61 	world, _ := newCombatWorld(t)
     62 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
     63 	world.advanceCombat(world.characters[1].nextAttack)
     64 	observer := &combatTestConn{}
     65 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     66 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     67 	destination := world.characters[1].userData()
     68 	destination.Playfield = 705
     69 
     70 	err := world.updateUser(destination)
     71 
     72 	if err != nil {
     73 		t.Fatal(err)
     74 	}
     75 	stopped := make(map[uint32]bool)
     76 	for _, packet := range observer.packets {
     77 		if binary.BigEndian.Uint32(packet[16:20]) == 0x4a41203e {
     78 			stopped[binary.BigEndian.Uint32(packet[24:28])] = true
     79 		}
     80 	}
     81 	if !stopped[1] || !stopped[1000000] || len(world.fighting) != 0 {
     82 		t.Fatalf("profile edit did not flush both old-zone actors: stops=%v fighting=%v", stopped, world.fighting)
     83 	}
     84 }
     85 
     86 func TestCombatFailedProfileEditPreservesBothActorsAndResources(t *testing.T) {
     87 	t.Chdir(t.TempDir())
     88 	world, conn := newCombatWorld(t)
     89 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
     90 	world.advanceCombat(world.characters[1].nextAttack)
     91 	before := *world.characters[1]
     92 	destination := before.userData()
     93 	destination.Playfield = 705
     94 	failDatabaseWrites(t, world)
     95 	conn.packets = nil
     96 
     97 	err := world.updateUser(destination)
     98 
     99 	if err == nil || world.characters[1].playfield != before.playfield || world.characters[1].health != before.health ||
    100 		world.characters[1].fighting != 1000000 || world.characters[1000000].fighting != 1 || len(world.fighting) != 2 || len(conn.packets) != 0 {
    101 		t.Fatalf("failed edit changed live combat: player=%+v packets=%x error=%v", world.characters[1], conn.packets, err)
    102 	}
    103 }
    104 
    105 func TestRegenerationSchedulerJitterDoesNotAccumulate(t *testing.T) {
    106 	world, _, start := newRegenerationWorld(t)
    107 
    108 	for second := 2; second <= 58; second += 2 {
    109 		jitter := time.Millisecond
    110 		if second%4 != 0 {
    111 			jitter = 20 * time.Millisecond
    112 		}
    113 		world.advanceRegeneration(start.Add(time.Duration(second)*time.Second + jitter))
    114 	}
    115 
    116 	if world.characters[1].health != 23 || world.clients[1].regenerationAt != start.Add(58*time.Second) {
    117 		t.Fatalf("scheduler jitter changed recovery phase: hp=%d phase=%v", world.characters[1].health, world.clients[1].regenerationAt)
    118 	}
    119 }
    120 
    121 func TestCombatSlowLiveRecipientDoesNotBlockWorldTick(t *testing.T) {
    122 	world, _ := newCombatWorld(t)
    123 	delete(world.clients, 1)
    124 	startTestService(t, world)
    125 	peer := connectZoningClient(t, world, true)
    126 	if _, err := peer.Write(targetPacket(attackPacket(1, 1000000))); err != nil {
    127 		t.Fatal(err)
    128 	}
    129 	if _, err := readPacket(peer); err != nil {
    130 		t.Fatal(err)
    131 	}
    132 	zone, err := world.world.owner(1)
    133 	if err != nil {
    134 		t.Fatal(err)
    135 	}
    136 	finished := make(chan error, 1)
    137 	stopped := make(chan struct{})
    138 	t.Cleanup(func() { peer.Close(); <-stopped })
    139 
    140 	go func() {
    141 		defer close(stopped)
    142 		finished <- zone.call(zoneTestCommand(func(state *server) error {
    143 			state.advanceCombat(state.characters[1].nextAttack)
    144 			state.advanceNPCs(time.Now())
    145 			return nil
    146 		}))
    147 	}()
    148 	select {
    149 	case err := <-finished:
    150 		if err != nil {
    151 			t.Fatal(err)
    152 		}
    153 	case <-time.After(250 * time.Millisecond):
    154 		t.Fatal("a live recipient that stopped reading blocked its zone")
    155 	}
    156 }