aorecomp

Anarchy Online classic in the browser

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


server/pet_retreat_test.go (5299B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"reflect"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestPetRetreatAndWaitCancelOnlyOwnedOutgoingCombat(t *testing.T) {
     12 	for _, command := range []uint32{2, 4} {
     13 		for _, fighting := range []bool{false, true} {
     14 			t.Run(fmt.Sprintf("%d/fighting%v", command, fighting), func(t *testing.T) {
     15 				world, output := newPetSummonWorld(t)
     16 				owner, client := world.characters[1], world.clients[1]
     17 				if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]); err != nil {
     18 					t.Fatal(err)
     19 				}
     20 				world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     21 				pet := world.characters[1000001]
     22 				target := &character{id: 2, playfield: owner.playfield, position: pet.position, health: 100, stats: testCharacterStats()}
     23 				peerOutput := &combatTestConn{}
     24 				world.characters[2] = target
     25 				world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: peerOutput, ready: true, target: true, npcs: map[uint32]bool{pet.id: true}}
     26 				updates := make(map[*worldClient][][]byte)
     27 				now := time.Now()
     28 				pet.rechargeUntil = now.Add(time.Second)
     29 				pet.threat = map[uint32]int64{2: 10}
     30 				if fighting {
     31 					world.startAttack(pet, target, now, updates)
     32 				}
     33 				// Retreat clears outgoing combat; the opponent's independent attack remains.
     34 				world.startAttack(target, pet, now, updates)
     35 				target.nextAttack = now.Add(time.Hour)
     36 				opponentDeadline := target.nextAttack
     37 				ownership, recharge, health := *pet.pet, pet.rechargeUntil, owner.health
     38 				request := petFollowRequest(1)[20:]
     39 				binary.BigEndian.PutUint32(request[16:], command)
     40 				output.packets, peerOutput.packets = nil, nil
     41 				auditCheckpointWrites(t, world)
     42 
     43 				err := world.handlePetCommand(client, request)
     44 				world.advanceCombat(now.Add(10 * time.Second))
     45 
     46 				if err != nil || pet.fighting != 0 || !pet.nextAttack.IsZero() || len(pet.threat) != 0 || world.fighting[pet.id] != nil ||
     47 					target.fighting != pet.id || !target.nextAttack.Equal(opponentDeadline) || world.fighting[target.id] != target ||
     48 					!pet.rechargeUntil.Equal(recharge) || target.health != 100 || owner.health != health || owner.nano != 25 ||
     49 					*pet.pet != ownership || !owner.pets[pet.id] || len(checkpointWrites(t, world)) != 0 {
     50 					t.Fatal("retreat retained/reselected outgoing combat or changed the opponent, resources, ownership or writes", err)
     51 				}
     52 				if command == 2 {
     53 					if pet.followTarget != 1 || pet.motion == nil {
     54 						t.Fatal("behind did not install its owner-follow controller")
     55 					}
     56 				} else if pet.followTarget != 0 || pet.motion != nil {
     57 					t.Fatal("wait retained follow or pursuit")
     58 				}
     59 				for _, packets := range [][][]byte{output.packets, peerOutput.packets} {
     60 					stop, follow := -1, -1
     61 					for index, packet := range packets {
     62 						if binary.BigEndian.Uint32(packet[16:20]) == 0x4a41203e {
     63 							if binary.BigEndian.Uint32(packet[24:28]) != pet.id || stop != -1 {
     64 								t.Fatal("retreat stopped another actor or repeated StopFight")
     65 							}
     66 							stop = index
     67 						}
     68 						if binary.BigEndian.Uint32(packet[16:20]) == 0x260f3671 {
     69 							follow = index
     70 						}
     71 					}
     72 					if follow == -1 || fighting && (stop == -1 || stop >= follow) || !fighting && stop != -1 {
     73 						t.Fatal("native stop did not precede the new controller, or an idle command invented combat")
     74 					}
     75 				}
     76 			})
     77 		}
     78 	}
     79 }
     80 
     81 func TestPetRetreatRejectsUnavailableAndForgedCommands(t *testing.T) {
     82 	for _, scenario := range []string{"foreign actor", "stale client", "not ready", "loading", "owner dead", "pet dead", "expired", "other zone", "argument", "selector", "boolean", "name"} {
     83 		t.Run(scenario, func(t *testing.T) {
     84 			world, output := newPetSummonWorld(t)
     85 			owner, client := world.characters[1], world.clients[1]
     86 			world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     87 			world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     88 			pet := world.characters[1000001]
     89 			pet.fighting, pet.nextAttack = 1000000, time.Now().Add(time.Second)
     90 			pet.threat = map[uint32]int64{1: 10}
     91 			world.fighting[pet.id] = pet
     92 			request := petFollowRequest(1)[20:]
     93 			binary.BigEndian.PutUint32(request[16:], 2)
     94 			switch scenario {
     95 			case "foreign actor":
     96 				binary.BigEndian.PutUint32(request[8:], 2)
     97 			case "stale client":
     98 				world.clients[1] = &worldClient{id: 1}
     99 			case "not ready":
    100 				client.ready = false
    101 			case "loading":
    102 				client.loading = true
    103 			case "owner dead":
    104 				owner.health = 0
    105 			case "pet dead":
    106 				pet.health = 0
    107 			case "expired":
    108 				pet.pet.expiresAt = time.Now().Add(-time.Second)
    109 			case "other zone":
    110 				pet.playfield++
    111 			case "argument":
    112 				binary.BigEndian.PutUint32(request[20:], 1000001)
    113 			case "selector":
    114 				binary.BigEndian.PutUint32(request[24:], 1010)
    115 			case "boolean":
    116 				binary.BigEndian.PutUint32(request[28:], 1)
    117 			case "name":
    118 				binary.BigEndian.PutUint32(request[32:], 1)
    119 				request = append(request, 'x')
    120 			}
    121 			before := *pet
    122 			output.packets = nil
    123 
    124 			err := world.handlePetCommand(client, request)
    125 
    126 			if err != nil || !reflect.DeepEqual(*pet, before) || world.fighting[pet.id] != pet || len(output.packets) != 0 {
    127 				t.Fatal("invalid retreat changed the pet's current attack or controller", err)
    128 			}
    129 		})
    130 	}
    131 }