aorecomp

Anarchy Online classic in the browser

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


server/world_posture_test.go (5566B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"net"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestWorldEntryResetsSavedPostureBeforeTreatment(t *testing.T) {
     12 	for _, scenario := range []string{"reconnect", "restart", "replacement"} {
     13 		t.Run(scenario, func(t *testing.T) {
     14 			world, _, _ := newConsumableOutcomeWorld(t, 25812, false)
     15 			if scenario == "restart" {
     16 				world = newTestInventoryServer(t)
     17 			} else if scenario == "replacement" {
     18 				world.clients[1].conn = &combatInterestConn{}
     19 			} else {
     20 				delete(world.clients, 1)
     21 			}
     22 			world.characters[1].nextPositionSave = time.Now().Add(20 * time.Second)
     23 			selection := binary.BigEndian.AppendUint32(nil, 50000)
     24 			selection = binary.BigEndian.AppendUint32(selection, 1)
     25 			selection = binary.BigEndian.AppendUint32(selection, 0)
     26 
     27 			startTestService(t, world)
     28 			client := connectZoningClient(t, world, true)
     29 			if _, err := client.Write(targetPacket(n3Packet(0x2252445f, 50000, 1, selection))); err != nil {
     30 				t.Fatal(err)
     31 			}
     32 			if _, err := client.Write(targetPacket(backpackUseRequest(1, 64, 1))); err != nil {
     33 				t.Fatal(err)
     34 			}
     35 			rejected, err := readPacket(client)
     36 			if err != nil {
     37 				t.Fatal(err)
     38 			}
     39 
     40 			var vitals playerVitals
     41 			var count uint16
     42 			var locked bool
     43 			observe := zoneTestCommand(func(state *server) error {
     44 				vitals = state.characters[1].vitals()
     45 				count = state.characters[1].inventory.Items[0].Count
     46 				locked = !state.characters[1].skillLocks[124].IsZero()
     47 				return nil
     48 			})
     49 			callTestZone(t, world, 1, observe)
     50 			if vitals.Sitting || vitals.Health != 1 || count != 50 || locked {
     51 				t.Fatalf("entry retained rest or consumed a standing patient's kit: %+v count=%d locked=%t", vitals, count, locked)
     52 			}
     53 			if binary.BigEndian.Uint32(rejected[16:20]) != 0x52526858 || binary.BigEndian.Uint32(rejected[29:33]) != 2 {
     54 				t.Fatalf("standing treatment was not rejected: %x", rejected)
     55 			}
     56 			var savedSitting bool
     57 			if err := world.db.QueryRow("SELECT sitting FROM characters WHERE id=1").Scan(&savedSitting); err != nil || !savedSitting {
     58 				t.Fatalf("entry eagerly rewrote checkpointed posture: sitting=%t error=%v", savedSitting, err)
     59 			}
     60 			callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     61 				return state.flushPositions(time.Now().Add(20 * time.Second))
     62 			}))
     63 			if err := world.db.QueryRow("SELECT sitting FROM characters WHERE id=1").Scan(&savedSitting); err != nil || savedSitting {
     64 				t.Fatalf("standing posture did not checkpoint: sitting=%t error=%v", savedSitting, err)
     65 			}
     66 
     67 			// Sit through the same connection, then confirm the treatment can succeed.
     68 			posture := binary.BigEndian.AppendUint32(nil, 86)
     69 			posture = append(posture, make([]byte, 16)...)
     70 			if _, err := client.Write(targetPacket(n3Packet(0x5e477770, 50000, 1, posture))); err != nil {
     71 				t.Fatal(err)
     72 			}
     73 			if _, err := readPacket(client); err != nil {
     74 				t.Fatal(err)
     75 			}
     76 			if _, err := client.Write(targetPacket(backpackUseRequest(1, 64, 2))); err != nil {
     77 				t.Fatal(err)
     78 			}
     79 			accepted, err := readPacket(client)
     80 			if err != nil {
     81 				t.Fatal(err)
     82 			}
     83 			for range 3 {
     84 				if _, err := readPacket(client); err != nil {
     85 					t.Fatal(err)
     86 				}
     87 			}
     88 			callTestZone(t, world, 1, observe)
     89 			if binary.BigEndian.Uint32(accepted[29:33]) != 1 || !vitals.Sitting || vitals.Health != 31 || count != 49 {
     90 				t.Fatalf("seated treatment did not heal and consume one charge: packet=%x vitals=%+v count=%d", accepted, vitals, count)
     91 			}
     92 		})
     93 	}
     94 }
     95 
     96 func TestWorldObserverEntryReceivesExistingPosture(t *testing.T) {
     97 	for _, sitting := range []bool{false, true} {
     98 		for _, running := range []bool{false, true} {
     99 			t.Run(map[bool]string{false: "standing", true: "sitting"}[sitting]+"/"+map[bool]string{false: "walk", true: "run"}[running], func(t *testing.T) {
    100 				world, _ := newCombatWorld(t)
    101 				delete(world.characters, 1000000)
    102 				world.characters[1].name, world.characters[1].sitting, world.characters[1].running = "Patient", sitting, running
    103 				world.characters[2] = &character{id: 2, name: "Observer", playfield: 950, health: 100}
    104 				cookie := systemPacket(0x1b, []byte{0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0})
    105 				binary.BigEndian.PutUint16(cookie, 0xdfdf)
    106 				address, peer := net.Pipe()
    107 				defer address.Close()
    108 				defer peer.Close()
    109 				conn := &combatInterestConn{combatTestConn: combatTestConn{Conn: address}, input: bytes.NewReader(cookie)}
    110 
    111 				shutdown := startTestService(t, world)
    112 				if err := world.serveWorldClient(&worldClient{conn: conn, loading: true}); err != nil {
    113 					t.Fatal(err)
    114 				}
    115 				var currentSitting bool
    116 				callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    117 					currentSitting = state.characters[1].sitting
    118 					return nil
    119 				}))
    120 				if err := shutdown(); err != nil {
    121 					t.Fatal(err)
    122 				}
    123 
    124 				var spawn []byte
    125 				for _, packet := range conn.packets {
    126 					if binary.BigEndian.Uint32(packet[16:20]) == 0x271b3a6b && binary.BigEndian.Uint32(packet[24:28]) == 1 {
    127 						spawn = packet
    128 					}
    129 				}
    130 				start := bytes.Index(spawn, []byte("Patient\x00")) + len("Patient") + 40
    131 				if len(spawn) < start+28 {
    132 					t.Fatalf("existing player's vehicle state missing: %x", spawn)
    133 				}
    134 				vehicle := spawn[start : start+28]
    135 				ground := byte(2)
    136 				if running {
    137 					ground = 3
    138 				}
    139 				mode := ground
    140 				if sitting {
    141 					mode = 8
    142 				}
    143 				if vehicle[12] != mode || binary.BigEndian.Uint32(vehicle[22:26]) != uint32(ground) || currentSitting != sitting {
    144 					t.Fatalf("observer posture/ground preference changed: vehicle=%x sitting=%t", vehicle, currentSitting)
    145 				}
    146 			})
    147 		}
    148 	}
    149 }