when to handle versus when to trytoHandle

This commit is contained in:
Fate-JH 2026-04-06 13:12:16 -04:00
parent 67990ecb83
commit 980eacdd65
4 changed files with 24 additions and 41 deletions

View file

@ -3,7 +3,7 @@ package net.psforever.actors.session
import akka.actor.{Actor, ActorRef, Cancellable, MDCContextAware, typed}
import net.psforever.actors.session.normal.NormalMode
import net.psforever.actors.session.support.{CommonHandlerFunctions, CommonHandlerFunctionsBase, CommonHandlerLogic, HandlerFilter, ZoningOperations}
import net.psforever.actors.session.support.{CommonHandlerFunctions, CommonHandlerFunctionsBase, CommonHandlerLogic, ZoningOperations}
import net.psforever.objects.TurretDeployable
import net.psforever.objects.serverobject.CommonMessages
import net.psforever.objects.serverobject.containable.Containable
@ -398,19 +398,21 @@ class SessionActor(middlewareActor: typed.ActorRef[MiddlewareActor.Command], con
responseHandler: CommonHandlerFunctionsBase,
envelope: GenericResponseEnvelope
): Unit = {
val GenericResponseEnvelope(_, guid, reply) = envelope
val GenericResponseEnvelope(toChannel, guid, reply) = envelope
//try the expected handler with the input response
val filter = HandlerFilter.set(data.handlerFilter, guid, data.player)
if (!responseHandler.tryToApply(reply)) {
if (!responseHandler.handle(toChannel, guid, reply)) {
//find any handler that might receive the response (ignore guard booleans during search)
data.handlerFilter.set(guid, guid, notSame = true, same = true)
if (!responseHandler.isDefinedAt(reply)) {
val potentiallyValidHandlers = listOfHandlers.filter(_.isDefinedAt(reply))
if (potentiallyValidHandlers.nonEmpty) {
data.handlerFilter.set(filter)
potentiallyValidHandlers.find(_.tryToApply(reply))
} else {
log.error(s"received completely unhandled response message - $envelope for ${envelope.stamp}")
listOfHandlers.filter(_.isDefinedAt(reply)) match {
case Nil =>
log.error(s"received completely unhandled response message - $envelope for ${envelope.stamp}")
case first :: Nil =>
first.handle(toChannel, guid, reply)
case first :: others =>
if (!first.handle(toChannel, guid, reply)) {
others.find(_.tryToHandle(reply))
}
}
}
}

View file

@ -15,7 +15,7 @@ import net.psforever.objects.vital.RevivingActivity
import net.psforever.packet.game.{AvatarImplantMessage, CreateShortcutMessage, ImplantAction}
import net.psforever.services.avatar.{AvatarAction, AvatarStamp}
import net.psforever.services.base.envelope.GenericResponseEnvelope
import net.psforever.services.base.message.{ChangeAmmo, ChangeFireState_Start, ChangeFireState_Stop, EventResponse, ReloadTool, WeaponDryFire}
import net.psforever.services.base.message.{ChangeAmmo, ChangeFireState_Start, ChangeFireState_Stop, ReloadTool, WeaponDryFire}
import net.psforever.types.ImplantType
//
@ -29,7 +29,6 @@ import net.psforever.objects.serverobject.terminals.{ProximityUnit, Terminal}
import net.psforever.objects.zones.Zoning
import net.psforever.packet.game.objectcreate.ObjectCreateMessageParent
import net.psforever.packet.game.{ArmorChangedMessage, ChangeAmmoMessage, ChangeFireModeMessage, ChangeFireStateMessage_Start, ChangeFireStateMessage_Stop, ChatMsg, DestroyMessage, DrowningTarget, GenericActionMessage, GenericObjectActionMessage, ItemTransactionResultMessage, ObjectCreateDetailedMessage, ObjectCreateMessage, ObjectDeleteMessage, ObjectHeldMessage, OxygenStateMessage, PlanetsideAttributeMessage, PlayerStateMessage, ProjectileStateMessage, ReloadMessage, UseItemMessage, WeaponDryFireMessage}
import net.psforever.services.Service
import net.psforever.types.{ChatMessageType, PlanetSideGUID, TransactionType, Vector3}
import net.psforever.util.Config
@ -44,27 +43,15 @@ class AvatarHandlerLogic(val ops: SessionAvatarHandlers, implicit val context: A
private val avatarActor: typed.ActorRef[AvatarActor.Command] = ops.avatarActor
private var tempGuid: PlanetSideGUID = Service.defaultPlayerGUID
override def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Unit = {
tempGuid = guid
super.handle(toChannel, guid, reply)
}
override def handleWith(guid: PlanetSideGUID): Receive = {
tempGuid = guid
super.handleWith(guid)
}
def receive: Receive = {
/* special messages */
case AvatarAction.TeardownConnection if player.spectator =>
context.self ! SessionActor.SetMode(CustomerServiceRepresentativeMode)
context.self.forward(GenericResponseEnvelope(AvatarStamp, "", tempGuid, AvatarAction.TeardownConnection))
context.self.forward(GenericResponseEnvelope(AvatarStamp, "", filterGuid, AvatarAction.TeardownConnection))
case AvatarAction.TeardownConnection =>
context.self ! SessionActor.SetMode(NormalMode)
context.self.forward(GenericResponseEnvelope(AvatarStamp, "", tempGuid, AvatarAction.TeardownConnection))
context.self.forward(GenericResponseEnvelope(AvatarStamp, "", filterGuid, AvatarAction.TeardownConnection))
/* really common messages (very frequently, every life) */
case pstate @ AvatarAction.PlayerState(

View file

@ -7,9 +7,8 @@ import net.psforever.actors.session.AvatarActor
import net.psforever.actors.session.support.{GalaxyHandlerFunctions, SessionData, SessionGalaxyHandlers}
import net.psforever.packet.game.{BroadcastWarpgateUpdateMessage, FriendsResponse, HotSpotUpdateMessage, ZoneInfoMessage, ZonePopulationUpdateMessage, HotSpotInfo => PacketHotSpotInfo}
import net.psforever.services.base.envelope.MessageEnvelope
import net.psforever.services.base.message.EventResponse
import net.psforever.services.galaxy.GalaxyAction
import net.psforever.types.{MemberAction, PlanetSideEmpire, PlanetSideGUID}
import net.psforever.types.{MemberAction, PlanetSideEmpire}
object GalaxyHandlerLogic {
def apply(ops: SessionGalaxyHandlers): GalaxyHandlerLogic = {
@ -35,10 +34,6 @@ class GalaxyHandlerLogic(val ops: SessionGalaxyHandlers, implicit val context: A
/* response handlers */
override def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Unit = {
receive.apply(reply)
}
def receive: Receive = {
case GalaxyAction.HotSpotUpdate(zone_index, priority, hot_spot_info) =>
sendResponse(

View file

@ -13,8 +13,11 @@ trait HandlerFilter {
def isNotSameTarget: Boolean
def isSameTarget: Boolean
def set(filter: HandlerFilter): HandlerFilter = {
set(filter.resolvedPlayerGuid, filter.otherPlayerGuid, filter.isNotSameTarget, filter.isSameTarget)
}
def set(resolved: PlanetSideGUID, other: PlanetSideGUID, notSame: Boolean, same: Boolean): HandlerFilter
def set(filter: HandlerFilter): HandlerFilter
}
class HandlerFilterRules extends HandlerFilter {
@ -30,10 +33,6 @@ class HandlerFilterRules extends HandlerFilter {
isSameTarget = same
this
}
def set(filter: HandlerFilter): HandlerFilter = {
set(filter.resolvedPlayerGuid, filter.otherPlayerGuid, filter.isNotSameTarget, filter.isSameTarget)
}
}
object HandlerFilter {
@ -57,13 +56,13 @@ trait CommonHandlerFunctionsBase {
* @param guid na
* @param reply na
*/
def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Unit
def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Boolean
def receive: Receive
def isDefinedAt(x: Any): Boolean = receive.isDefinedAt(x)
def tryToApply(x: Any): Boolean = {
final def tryToHandle(x: Any): Boolean = {
var passed = true
receive.applyOrElse(x, (_: Any) => { passed = false })
passed
@ -86,9 +85,9 @@ trait CommonHandlerFunctions extends CommonHandlerFunctionsBase {
* @param guid na
* @param reply na
*/
def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Unit = {
def handle(toChannel: String, guid: PlanetSideGUID, reply: EventResponse): Boolean = {
HandlerFilter.set(sessionLogic.handlerFilter, guid, player)
receive.apply(reply)
tryToHandle(reply)
}
def receive: Receive