server/world_mission_retirement.go (7956B)
1 package main 2 3 import ( 4 "database/sql" 5 "encoding/binary" 6 "errors" 7 "fmt" 8 "log" 9 ) 10 11 // Lifecycle requests are serialized independently of routing and simulation. 12 // Participants keep their live state on their own goroutines while the worker 13 // combines detached writes into one transaction. 14 type missionOperation struct { 15 zone *zoneRuntime 16 packet zonePacket 17 expired *acceptedMission 18 done chan error 19 } 20 21 type missionParticipantsQuery struct { 22 world uint32 23 entities []uint32 24 reply chan []*zoneRuntime 25 } 26 27 type missionRetirementInput struct { 28 mission acceptedMission 29 completed bool 30 expired bool 31 } 32 33 type missionPrepared struct { 34 write itemWrite 35 err error 36 } 37 38 type missionParticipant struct { 39 input missionRetirementInput 40 primary bool 41 held chan struct{} 42 credentials chan map[uint32]bool 43 prepared chan missionPrepared 44 decision chan error 45 done chan error 46 } 47 48 func newMissionParticipant(input missionRetirementInput, primary bool) *missionParticipant { 49 return &missionParticipant{input: input, primary: primary, held: make(chan struct{}), credentials: make(chan map[uint32]bool), 50 prepared: make(chan missionPrepared, 1), decision: make(chan error), done: make(chan error, 1)} 51 } 52 53 func (part *missionParticipant) retire(state *server, updates map[*worldClient][][]byte) (map[*worldClient][][]byte, error) { 54 close(part.held) 55 removed, proceed := <-part.credentials 56 if !proceed { 57 return nil, <-part.decision 58 } 59 draft, err := state.prepareMissionRetirement(part.input.mission, part.input.completed, removed, part.primary) 60 ready := missionPrepared{err: err} 61 if err == nil { 62 ready.write = draft.write 63 } 64 part.prepared <- ready 65 if err := <-part.decision; err != nil { 66 return nil, err 67 } 68 client := state.clients[part.input.mission.owner] 69 departures, err := draft.apply(updates) 70 if err == nil && part.input.expired && !part.input.mission.completed && client != nil && (client.ready && !client.loading || departures[client] != nil) { 71 updates[client] = append(updates[client], missionExpiryPacket(part.input.mission.owner, part.input.mission.id)) 72 } 73 return departures, err 74 } 75 76 func (part *missionParticipant) execute(state *server) error { 77 updates := make(map[*worldClient][][]byte) 78 departures, err := part.retire(state, updates) 79 if err != nil { 80 return err 81 } 82 for client, packets := range departures { 83 updates[client] = append(updates[client], packets...) 84 } 85 state.sendCombatUpdates(updates) 86 return nil 87 } 88 89 type missionSourceCommand struct { 90 operation missionOperation 91 participant *missionParticipant 92 requested chan missionRetirementInput 93 } 94 95 func (command missionSourceCommand) execute(state *server) error { 96 retire := func(mission acceptedMission, completed bool, updates map[*worldClient][][]byte) (map[*worldClient][][]byte, error) { 97 command.participant.input = missionRetirementInput{mission: mission, completed: completed, expired: command.operation.expired != nil} 98 command.requested <- missionRetirementInput{mission: *cloneStoredMission(&mission), completed: completed, expired: command.operation.expired != nil} 99 return command.participant.retire(state, updates) 100 } 101 if command.operation.expired != nil { 102 mission := *command.operation.expired 103 updates := make(map[*worldClient][][]byte) 104 departures, err := retire(mission, false, updates) 105 if err != nil { 106 return err 107 } 108 for leaving, packets := range departures { 109 updates[leaving] = append(updates[leaving], packets...) 110 } 111 state.sendCombatUpdates(updates) 112 return nil 113 } 114 packet := command.operation.packet 115 if binary.BigEndian.Uint32(packet.body[:4]) == 0x212c487a { 116 return state.removeMission(packet.client, packet.body, retire) 117 } 118 return state.returnMission(packet.client, packet.body, retire) 119 } 120 121 func missionLifecyclePacket(body []byte) bool { 122 if len(body) < 4 { 123 return false 124 } 125 kind := binary.BigEndian.Uint32(body[:4]) 126 return kind == 0x212c487a || kind == 0x52526858 && len(body) == 56 && 127 (binary.BigEndian.Uint32(body[24:28]) == 5 && binary.BigEndian.Uint32(body[48:52]) == 56001 || 128 binary.BigEndian.Uint32(body[24:28]) == 84 && binary.BigEndian.Uint32(body[48:52]) == 56003) 129 } 130 131 func missionCredentialOwners(db *sql.DB, mission uint32) ([]uint32, error) { 132 // Bags cannot nest. Escrow retains its durable placement until settlement. 133 rows, err := db.Query(`SELECT DISTINCT COALESCE(l.player_id,l.bank_player_id,l.corpse_id, 134 bag.player_id,bag.bank_player_id,bag.corpse_id,0) 135 FROM mission_keys k JOIN item_locations l ON l.item_id=k.item_id 136 LEFT JOIN item_locations bag ON bag.item_id=l.backpack_id WHERE k.mission_id=?`, mission) 137 if err != nil { 138 return nil, err 139 } 140 defer rows.Close() 141 var owners []uint32 142 for rows.Next() { 143 var id uint32 144 if err := rows.Scan(&id); err != nil { 145 return nil, err 146 } 147 if id == 0 { 148 return nil, fmt.Errorf("mission %d has a key without an actor owner", mission) 149 } 150 owners = append(owners, id) 151 } 152 return owners, rows.Err() 153 } 154 155 func (world *worldRuntime) retireMission(coordinator *worldCoordinator, operation missionOperation) error { 156 if operation.expired != nil { 157 missions := world.queryMissions(missionLookup{id: operation.expired.id}) 158 if len(missions) == 0 { 159 return nil 160 } 161 operation.expired = &missions[0] 162 var err error 163 operation.zone, err = world.owner(operation.expired.owner) 164 if err != nil { 165 return err 166 } 167 } 168 primary := newMissionParticipant(missionRetirementInput{}, true) 169 requested := make(chan missionRetirementInput) 170 go func() { primary.done <- operation.zone.call(missionSourceCommand{operation, primary, requested}) }() 171 var input missionRetirementInput 172 select { 173 case input = <-requested: 174 case err := <-primary.done: 175 return err 176 } 177 <-primary.held 178 participants := []*missionParticipant{primary} 179 acquired := map[*zoneRuntime]bool{operation.zone: true} 180 // Re-read holders after acquiring their zones: copies and completed transfers 181 // may have introduced another participant while an earlier owner was busy. 182 var err error 183 for { 184 var owners []uint32 185 owners, err = missionCredentialOwners(coordinator.db, input.mission.id) 186 if err != nil { 187 break 188 } 189 owners = append(owners, input.mission.owner) 190 query := missionParticipantsQuery{world: input.mission.world, entities: owners, reply: make(chan []*zoneRuntime, 1)} 191 world.missionParticipants <- query 192 added := false 193 for _, zone := range <-query.reply { 194 if acquired[zone] { 195 continue 196 } 197 part := newMissionParticipant(input, false) 198 acquired[zone] = true 199 go func() { part.done <- zone.call(part) }() 200 select { 201 case <-part.held: 202 participants = append(participants, part) 203 added = true 204 case err = <-part.done: 205 } 206 if err != nil { 207 break 208 } 209 } 210 if err != nil || !added { 211 break 212 } 213 } 214 var removed map[uint32]bool 215 if err == nil { 216 removed, err = missionCredentials(coordinator.db, input.mission) 217 } 218 if err != nil { 219 for _, part := range participants { 220 close(part.credentials) 221 part.decision <- err 222 } 223 for _, part := range participants { 224 <-part.done 225 } 226 return err 227 } 228 writes := make([]itemWrite, len(participants)) 229 for index, part := range participants { 230 part.credentials <- removed 231 ready := <-part.prepared 232 writes[index] = ready.write 233 err = errors.Join(err, ready.err) 234 } 235 if err == nil { 236 // All remote keys and NPCs retire before the source's final contract checks. 237 batch := make(databaseBatch, 0, len(writes)) 238 for _, write := range writes[1:] { 239 batch = append(batch, write) 240 } 241 batch = append(batch, writes[0]) 242 err = coordinator.databaseWriter.commit(batch) 243 } 244 for _, part := range participants { 245 part.decision <- err 246 } 247 for _, part := range participants { 248 result := <-part.done 249 if err == nil { 250 err = result 251 } 252 } 253 return err 254 } 255 256 func finishMissionOperation(operation missionOperation, err error) { 257 if operation.done != nil { 258 operation.done <- err 259 } else if err != nil { 260 log.Printf("Expiring routed mission: %v", err) 261 } 262 }