aorecomp

Anarchy Online classic in the browser

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


server/backyard_message_test.go (4307B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"slices"
      6 	"sync"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestBackyardCityExitExplainsRefusalOncePerEntry(t *testing.T) {
     12 	for _, fixture := range backyardFixtures {
     13 		t.Run(fixture.name, func(t *testing.T) {
     14 			world := newBackyardTestWorld(t)
     15 			player, client := world.characters[1], world.clients[1]
     16 			player.playfield = fixture.backyard
     17 			doors := world.zoneLinks["target"][fixture.backyard].Doors
     18 			door := doors[slices.IndexFunc(doors, func(door zoneDoor) bool { return door.ID == fixture.exitDoor })]
     19 			player.position = door.Position
     20 			inventory := player.inventory
     21 			inventory.Credits = 999
     22 			if err := world.saveInventory(player, inventory); err != nil {
     23 				t.Fatal(err)
     24 			}
     25 			if err := world.savePosition(player); err != nil {
     26 				t.Fatal(err)
     27 			}
     28 			chat, observer := &chatRecordingConn{}, &chatRecordingConn{}
     29 			setChatClients(t, world, map[uint32]*chatClient{1: {id: 1, conn: chat}, 2: {id: 2, conn: observer}})
     30 			move := func(position [3]float32) {
     31 				pose := *player
     32 				pose.position = position
     33 				packet := npcMovementPacket(&pose, 20)[20:]
     34 				packet = append(packet[:16:16], encodeMovement1491(packet[16:])...)
     35 				if err := world.handleMovement(client, packet); err != nil {
     36 					t.Fatal(err)
     37 				}
     38 			}
     39 
     40 			approach := door.Position
     41 			x, y, z, w := door.Rotation[0], door.Rotation[1], door.Rotation[2], door.Rotation[3]
     42 			approach[0] += 1.2 * (2*x*z + 2*y*w)
     43 			approach[2] += 1.2 * (1 - 2*x*x - 2*y*y)
     44 			move(approach)
     45 			world.advanceZoneCrossings(time.Now())
     46 			readChatState(t, world)
     47 			if len(chat.packets) != 0 {
     48 				t.Fatal("approach outside the door plane delivered a refusal")
     49 			}
     50 			move(door.Position)
     51 			position := player.position
     52 			zone, stop := startTestZone(t, world)
     53 			var visitors sync.WaitGroup
     54 			for range 5 {
     55 				visitors.Add(1)
     56 				go func() {
     57 					defer visitors.Done()
     58 					if err := zone.call(zoneTestCommand(func(state *server) error { state.advanceZoneCrossings(time.Now()); return nil })); err != nil {
     59 						t.Error(err)
     60 					}
     61 				}()
     62 			}
     63 			visitors.Wait()
     64 			if err := stop(); err != nil {
     65 				t.Fatal(err)
     66 			}
     67 			readChatState(t, world)
     68 			if len(chat.packets) != 1 {
     69 				t.Fatal("refused entry did not deliver exactly one explanation", len(chat.packets))
     70 			}
     71 			if player.position != position {
     72 				t.Fatal("refusal moved the player")
     73 			}
     74 			away := door.Position
     75 			away[0] += 5
     76 			move(away)
     77 			move(door.Position)
     78 			world.advanceZoneCrossings(time.Now())
     79 			readChatState(t, world)
     80 			cold := newTestInventoryServer(t).characters[1]
     81 
     82 			if len(chat.packets) != 2 || len(observer.packets) != 0 || player.playfield != fixture.backyard || cold.playfield != fixture.backyard ||
     83 				player.inventory.Credits != 999 || cold.inventory.Credits != 999 {
     84 				t.Fatal("re-entry, private delivery or rejected travel changed ownership or funds")
     85 			}
     86 			for _, packet := range chat.packets {
     87 				if binary.BigEndian.Uint16(packet) != 36 || string(packet[6:]) != "You need to be level 2 or have at least 1000 credits to exit to the outside." {
     88 					t.Fatal("refusal did not use the complete authored system text", packet)
     89 				}
     90 			}
     91 		})
     92 	}
     93 }
     94 
     95 func TestBackyardRefusalWaitsForChatConnectionWhileIdle(t *testing.T) {
     96 	world := newBackyardTestWorld(t)
     97 	player := world.characters[1]
     98 	player.playfield = 3000
     99 	chat := &chatRecordingConn{}
    100 
    101 	enterBackyardDoor(t, world, player, 3221294008)
    102 	for range 5 {
    103 		world.advanceZoneCrossings(time.Now())
    104 	}
    105 	setChatClients(t, world, map[uint32]*chatClient{1: {id: 1, conn: chat}})
    106 	for range 5 {
    107 		world.advanceZoneCrossings(time.Now())
    108 	}
    109 	readChatState(t, world)
    110 
    111 	if len(chat.packets) != 1 || player.playfield != 3000 || player.inventory.Credits != 0 {
    112 		t.Fatal("idle chat attachment lost or duplicated the refused entry")
    113 	}
    114 }
    115 
    116 func TestBackyardExitMessagesMatchAllOriginalDescriptors(t *testing.T) {
    117 	world := newBackyardTestWorld(t)
    118 	count := 0
    119 
    120 	for _, zone := range world.zoneLinks["target"] {
    121 		for _, door := range zone.Doors {
    122 			if door.RefusalMessage == "" {
    123 				continue
    124 			}
    125 			count++
    126 			if door.RefusalMessage != "You need to be level 2 or have at least 1000 credits to exit to the outside." {
    127 				t.Fatal("original refusal text changed", door.ID)
    128 			}
    129 		}
    130 	}
    131 	if count != 130 {
    132 		t.Fatal("original city-exit message coverage changed", count)
    133 	}
    134 }