aorecomp

Anarchy Online classic in the browser

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


server/first_aid_test.go (7021B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"reflect"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestFirstAidCommitsDuringCombat(t *testing.T) {
     12 	for _, kit := range []struct{ template, low, high uint32 }{{23314, 10, 12}, {116629, 5, 7}} {
     13 		for _, other := range []bool{false, true} {
     14 			for _, count := range []uint16{1, 3} {
     15 				for _, clamp := range []bool{false, true} {
     16 					t.Run(fmt.Sprintf("%d/other%t/count%d/clamp%t", kit.template, other, count, clamp), func(t *testing.T) {
     17 						world, target, _ := newStartupLaboratoryWorld(t, other)
     18 						player, client := world.characters[1], world.clients[1]
     19 						player.stats.NanoAiding.FirstAid = 7
     20 						player.inventory.Items[0].Template, player.inventory.Items[0].Count = kit.template, count
     21 						player.fighting, target.fighting, target.sitting = 1000000, 1000000, false
     22 						if clamp {
     23 							target.health = target.maximumHealth() - 1
     24 							if kit.template == 116629 {
     25 								target.nano = target.maximumNano() - 1
     26 							}
     27 						}
     28 						if err := world.saveInventory(player, player.inventory); err != nil {
     29 							t.Fatal(err)
     30 						}
     31 						if err := world.saveUser(player); err != nil {
     32 							t.Fatal(err)
     33 						}
     34 						if _, err := world.db.Exec("UPDATE characters SET health=?, nano=? WHERE id=?", target.health, target.nano, target.id); err != nil {
     35 							t.Fatal(err)
     36 						}
     37 						conn := &consumableCommitConn{db: world.db, target: target.id}
     38 						client.conn = conn
     39 						started := time.Now()
     40 
     41 						err := world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:])
     42 						restored := newTestInventoryServer(t)
     43 
     44 						if err != nil || !conn.observed || conn.err != nil || conn.count != count-1 ||
     45 							conn.health != target.health || conn.nano != target.nano || !conn.firstAid.Valid || conn.patient.Valid ||
     46 							restored.characters[target.id].health != target.health || restored.characters[target.id].nano != target.nano ||
     47 							count > 1 && !reflect.DeepEqual(restored.characters[1].inventory.Items, player.inventory.Items) ||
     48 							!restored.characters[1].skillLocks[123].Equal(player.skillLocks[123]) {
     49 							t.Fatal("health, charge and user lock were not committed before success", err, conn.err)
     50 						}
     51 						if clamp && target.health != target.maximumHealth() || !clamp && (target.health < 1+kit.low || target.health > 1+kit.high) {
     52 							t.Fatal("First Aid differs from its authored health endpoints or maximum clamp", target.health)
     53 						}
     54 						if kit.template == 116629 {
     55 							if clamp && target.nano != target.maximumNano() || !clamp && (target.nano < 6 || target.nano > 8) {
     56 								t.Fatal("startup First Aid lost its nano effect or maximum clamp", target.nano)
     57 							}
     58 						} else if target.nano != 1 {
     59 							t.Fatal("health-only First Aid changed nano")
     60 						}
     61 						if player.skillLocks[123].Before(started.Add(40*time.Second)) || player.skillLocks[123].After(time.Now().Add(40*time.Second)) ||
     62 							player.fighting != 1000000 || target.fighting != 1000000 || other && !target.skillLocks[123].IsZero() {
     63 							t.Fatal("First Aid changed combat or locked the patient")
     64 						}
     65 						if count == 1 && (len(player.inventory.Items) != 0 || len(restored.items) != 0) || count == 3 && player.inventory.Items[0].Count != 2 {
     66 							t.Fatal("First Aid did not consume exactly one charge")
     67 						}
     68 						healthPackets := 0
     69 						for _, packet := range conn.packets {
     70 							if binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c {
     71 								healthPackets++
     72 								if binary.BigEndian.Uint32(packet[24:28]) != target.id || binary.BigEndian.Uint32(packet[29:33]) != target.health {
     73 									t.Fatal("combat health snapshot differs from the committed patient HP")
     74 								}
     75 							}
     76 						}
     77 						if healthPackets != 1 {
     78 							t.Fatal("First Aid suppressed or repeated the combat HP update", healthPackets)
     79 						}
     80 
     81 						// A replay and a new request during the shared First Aid lock cannot debit again.
     82 						before, _ := readTestItemStore(world)
     83 						world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 1)[20:])
     84 						world.handleOwnedItemUse(client, backpackUseRequest(1, 64, 2)[20:])
     85 						after, _ := readTestItemStore(world)
     86 						if string(before) != string(after) || target.health != conn.health || target.nano != conn.nano {
     87 							t.Fatal("replay or locked First Aid use changed the committed outcome")
     88 						}
     89 					})
     90 				}
     91 			}
     92 		}
     93 	}
     94 }
     95 
     96 func TestStartupFirstAidRollsBackBothResourcesAndRetries(t *testing.T) {
     97 	for _, phase := range []string{"charge", "lock", "health", "nano"} {
     98 		t.Run(phase, func(t *testing.T) {
     99 			world, target, conn := newStartupLaboratoryWorld(t, true)
    100 			player := world.characters[1]
    101 			player.stats.NanoAiding.FirstAid = 7
    102 			player.inventory.Items[0].Template, player.inventory.Items[0].Count = 116629, 3
    103 			player.fighting, target.fighting, target.sitting = 1000000, 1000000, false
    104 			if err := world.saveInventory(player, player.inventory); err != nil {
    105 				t.Fatal(err)
    106 			}
    107 			statement := "CREATE TRIGGER reject_stim BEFORE UPDATE ON items BEGIN SELECT RAISE(FAIL, 'charge'); END"
    108 			switch phase {
    109 			case "lock":
    110 				statement = "CREATE TRIGGER reject_stim BEFORE INSERT ON skill_locks BEGIN SELECT RAISE(FAIL, 'lock'); END"
    111 			case "health":
    112 				statement = "CREATE TRIGGER reject_stim BEFORE UPDATE OF health ON characters WHEN NEW.id=2 BEGIN SELECT RAISE(FAIL, 'health'); END"
    113 			case "nano":
    114 				statement = "CREATE TRIGGER reject_stim BEFORE UPDATE OF nano ON characters WHEN NEW.id=2 BEGIN SELECT RAISE(FAIL, 'nano'); END"
    115 			}
    116 			if _, err := world.db.Exec(statement); err != nil {
    117 				t.Fatal(err)
    118 			}
    119 			before, _ := readTestItemStore(world)
    120 
    121 			failed := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:])
    122 			after, _ := readTestItemStore(world)
    123 			var savedHealth, savedNano uint32
    124 			if err := world.db.QueryRow("SELECT health,nano FROM characters WHERE id=2").Scan(&savedHealth, &savedNano); err != nil {
    125 				t.Fatal(err)
    126 			}
    127 			if failed == nil || string(before) != string(after) || target.health != 1 || target.nano != 1 || player.inventory.Items[0].Count != 3 ||
    128 				savedHealth != 1 || savedNano != 1 || len(player.skillLocks) != 0 || len(target.skillLocks) != 0 || len(conn.packets) != 0 {
    129 				t.Fatal("failed startup First Aid left a partial restoration, charge, lock or success", failed)
    130 			}
    131 			if _, err := world.db.Exec("DROP TRIGGER reject_stim"); err != nil {
    132 				t.Fatal(err)
    133 			}
    134 			err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 2)[20:])
    135 			cold := newTestInventoryServer(t)
    136 
    137 			if err != nil || target.health < 6 || target.health > 8 || target.nano < 6 || target.nano > 8 || player.inventory.Items[0].Count != 2 ||
    138 				cold.characters[2].health != target.health || cold.characters[2].nano != target.nano ||
    139 				cold.characters[1].inventory.Items[0].Count != 2 || cold.characters[1].skillLocks[123].IsZero() || !cold.characters[2].skillLocks[123].IsZero() {
    140 				t.Fatal("startup First Aid retry did not commit both resources, one charge and the user lock", err)
    141 			}
    142 		})
    143 	}
    144 }