aorecomp

Anarchy Online classic in the browser

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


server/mission_key_copy.go (2212B)


      1 package main
      2 
      3 import (
      4 	"database/sql"
      5 	"encoding/binary"
      6 	"slices"
      7 	"time"
      8 )
      9 
     10 type missionKeyBinding struct {
     11 	itemID, missionID uint32
     12 }
     13 
     14 func (server *server) copyMissionKey(client *worldClient, tool, original ownedItem) error {
     15 	refuse := func() error {
     16 		return client.send(characterActionPacket(client.id, [5]uint32{84, 2}))
     17 	}
     18 	if tool.Type != 51054 || tool.Quality != 1 || tool.Count != 1 ||
     19 		original.Type != 51053 || original.Template != 28577 || original.Quality != 1 || original.Count != 1 {
     20 		return refuse()
     21 	}
     22 
     23 	// Copy the actual held key's binding, including keys received from another owner.
     24 	var missionID uint32
     25 	if err := server.db.QueryRow("SELECT mission_id FROM mission_keys WHERE item_id=?", original.ID).Scan(&missionID); err != nil {
     26 		if err == sql.ErrNoRows {
     27 			return refuse()
     28 		}
     29 		return err
     30 	}
     31 	missions := server.missionExpiry
     32 	if server.world != nil {
     33 		missions = server.world.queryMissions(missionLookup{id: missionID})
     34 	}
     35 	if !slices.ContainsFunc(missions, func(mission acceptedMission) bool {
     36 		return mission.id == missionID && !mission.completed && time.Now().Before(mission.expiresAt)
     37 	}) {
     38 		return refuse()
     39 	}
     40 	player := server.characters[client.id]
     41 	slot := uint32(64)
     42 	for slot < 94 && slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) {
     43 		slot++
     44 	}
     45 	if slot == 94 {
     46 		return refuse()
     47 	}
     48 	id, err := server.reserveItems(1)
     49 	if err != nil {
     50 		return err
     51 	}
     52 
     53 	// Persist the new instance, placement and assignment together; retain both inputs.
     54 	copy := ownedItem{ID: id, Type: 51053, Template: 28577, Quality: 1, Slot: slot, Count: 1}
     55 	inventory := player.inventory
     56 	inventory.LastID = copy.ID
     57 	inventory.Items = append(slices.Clone(inventory.Items), copy)
     58 	store, err := server.itemStore(player, inventory)
     59 	if err != nil {
     60 		return err
     61 	}
     62 	store.copiedMissionKey = &missionKeyBinding{copy.ID, missionID}
     63 	if err := server.writeItemStore(store); err != nil {
     64 		return err
     65 	}
     66 	player.inventory = inventory
     67 	var addition []byte
     68 	for _, value := range []uint32{28577, 28577, 1, 1} {
     69 		addition = binary.BigEndian.AppendUint32(addition, value)
     70 	}
     71 	return client.send(n3Packet(0x052e2f0c, 50000, client.id, addition))
     72 }