def __init__(self, modeFlags, queueType, subscriber): super(PreQueueEntity, self).__init__(entityFlags=FUNCTIONAL_FLAG.PRE_QUEUE, modeFlags=modeFlags) self._queueType = queueType self._subscriber = subscriber self._setListenerClass(IPreQueueListener) self._requestCtx = PrbCtrlRequestCtx()
class PreQueueEntity(BasePreQueueEntity, ListenersCollection): """ Pre queue entity class. """ __metaclass__ = EventVehicleMeta def __init__(self, modeFlags, queueType, subscriber): super(PreQueueEntity, self).__init__(entityFlags=FUNCTIONAL_FLAG.PRE_QUEUE, modeFlags=modeFlags) self._queueType = queueType self._subscriber = subscriber self._setListenerClass(IPreQueueListener) self._requestCtx = PrbCtrlRequestCtx() def init(self, ctx=None): self._subscriber.subscribe(self) result = super(PreQueueEntity, self).init(ctx=ctx) if self.isInQueue(): result |= self._goToQueueUI() return result def fini(self, ctx=None, woEvents=False): self.clear() self._subscriber.unsubscribe(self) if self._requestCtx.isProcessing: self._requestCtx.stopProcessing(True) self._requestCtx.clear() return super(PreQueueEntity, self).fini(ctx=ctx, woEvents=woEvents) def getQueueType(self): return self._queueType def doAction(self, action=None): if not self.isInQueue(): self.queue(self._makeQueueCtxByAction(action)) else: self.dequeue(DequeueCtx(waitingID='prebattle/leave')) return True def hasLockedState(self): return self.isInQueue() def getEntityType(self): return self._queueType def getPermissions(self, pID=None, **kwargs): raise pID is None or AssertionError( 'Current player has no any player in that mode') return PreQueuePermissions(self.isInQueue()) def getConfirmDialogMeta(self, ctx): meta = None if self.hasLockedState(): meta = rally_dialog_meta.RallyLeaveDisabledDialogMeta( CTRL_ENTITY_TYPE.PREQUEUE, self._queueType) return meta def showGUI(self, ctx=None): self._goToQueueUI() def exitFromQueue(self): self.doAction() def request(self, ctx, callback=None): requestType = ctx.getRequestType() if requestType == REQUEST_TYPE.QUEUE: self.queue(ctx, callback=callback) elif requestType == REQUEST_TYPE.DEQUEUE: self.dequeue(ctx, callback=callback) else: LOG_ERROR( 'PreQueueEntity supports QUEUE and DEQUEUE requests only', ctx) if callback is not None: callback(False) return def queue(self, ctx, callback=None): """ Sends request to enqueue. Args: ctx: queue context callback: operation callback """ if ctx is None: ctx = self._makeQueueCtxByAction() if self._requestCtx.isProcessing(): LOG_ERROR('Request is processing', self._requestCtx) if callback is not None: callback(False) return elif self.isInQueue(): LOG_ERROR('Player already is in the queue', self._requestCtx) if callback is not None: callback(False) return elif self._validateParentControl(): if callback is not None: callback(False) return else: self._requestCtx = ctx self._requestCtx.startProcessing(callback) try: self._doQueue(ctx) except (AttributeError, TypeError, NotImplementedError): LOG_CURRENT_EXCEPTION() self._requestCtx.stopProcessing(False) return def dequeue(self, ctx, callback=None): """ Sends request to dequeue. Args: ctx: dequeue context callback: operation callback """ if self._requestCtx.isProcessing(): LOG_ERROR('Request is processing', self._requestCtx) if callback: callback(False) return elif not self.isInQueue(): LOG_ERROR('Player is not in the queue', ctx) if callback is not None: callback(False) return else: self._requestCtx = ctx self._requestCtx.startProcessing(callback) try: self._doDequeue(ctx) except (AttributeError, TypeError, NotImplementedError): LOG_CURRENT_EXCEPTION() self._requestCtx.stopProcessing(False) return def leave(self, ctx, callback=None): def __leave(_=True): g_prbCtrlEvents.onPreQueueLeft() if callback is not None: callback(True) return if self.isInQueue(): self.dequeue(DequeueCtx(waitingID='prebattle/leave'), callback=__leave) else: __leave() def onEnqueued(self, *args): """ Routine must be invoked when player goes to queue. """ self._requestCtx.stopProcessing(True) self._invokeListeners('onEnqueued', self._queueType, *args) self._goToQueueUI() def onDequeued(self, *args): """ Routine must be invoked when player leaves queue. """ self._requestCtx.stopProcessing(True) self._invokeListeners('onDequeued', self._queueType, *args) self._exitFromQueueUI() def onEnqueueError(self, errorCode, *args): """ Routine must be invoked when player receives enqueue error. """ self._requestCtx.stopProcessing(True) self._invokeListeners('onEnqueueError', self._queueType, *args) self._exitFromQueueUI() SystemMessages.pushMessage(messages.getJoinFailureMessage(errorCode), type=SystemMessages.SM_TYPE.Error) def onKickedFromQueue(self, *args): """ Routine must be invoked when player is kicked from queue. """ self._requestCtx.stopProcessing(True) self._invokeListeners('onKickedFromQueue', self._queueType, *args) self._exitFromQueueUI() SystemMessages.pushI18nMessage( '#system_messages:arena_start_errors/prb/kick/timeout', type=SystemMessages.SM_TYPE.Warning) def onKickedFromArena(self, *args): """ Routine must be invoked when player is kicked from arena. """ self._requestCtx.stopProcessing(True) self._invokeListeners('onKickedFromArena', self._queueType, *args) self._exitFromQueueUI() def onArenaJoinFailure(self, *args): """ Routine must be invoked when player received an error during arena join process. """ self._invokeListeners('onArenaJoinFailure', self._queueType, *args) self._exitFromQueueUI() def _createActionsValidator(self): return PreQueueActionsValidator(self) def _goToQueueUI(self): """ Routine that must be invoked when UI should show proper queue component. Returns: functional flag of UI load action """ return FUNCTIONAL_FLAG.UNDEFINED def _exitFromQueueUI(self): """ Routine that must be invoked when UI should hide proper queue component. """ pass def _doQueue(self, ctx): """ Method that should send player into queue at system level. Args: ctx: queue request context """ raise NotImplementedError('Routine _doQueue must be overridden') def _doDequeue(self, ctx): """ Method that should remove player from queue at system level. Args: ctx: dequeue request context """ raise NotImplementedError('Routine _doDequeue must be overridden') def _makeQueueCtxByAction(self, action=None): """ Build queue context by prebattle aciton Args: action: prebattle action Returns: queue context """ raise NotImplementedError( 'Routine _makeDefQueueCtx must be overridden') def _validateParentControl(self): """ Validates parent control restrictions. Returns: has pre queue any restrictions """ result = prb_getters.isParentControlActivated() if result: g_eventDispatcher.showParentControlNotification() return result
class PreQueueEntity(BasePreQueueEntity, ListenersCollection): _QUEUE_TIMEOUT_MSG_KEY = '#system_messages:arena_start_errors/prb/kick/timeout' def __init__(self, modeFlags, queueType, subscriber): super(PreQueueEntity, self).__init__(entityFlags=FUNCTIONAL_FLAG.PRE_QUEUE, modeFlags=modeFlags) self._queueType = queueType self._subscriber = subscriber self._setListenerClass(IPreQueueListener) self._requestCtx = PrbCtrlRequestCtx() def init(self, ctx=None): self._subscriber.subscribe(self) result = super(PreQueueEntity, self).init(ctx=ctx) if self.isInQueue(): result |= self._goToQueueUI() return result def fini(self, ctx=None, woEvents=False): self.clear() self._subscriber.unsubscribe(self) if self._requestCtx.isProcessing: self._requestCtx.stopProcessing(True) self._requestCtx.clear() return super(PreQueueEntity, self).fini(ctx=ctx, woEvents=woEvents) def getQueueType(self): return self._queueType def isInQueue(self): return prb_getters.getQueueType() == self._queueType def doAction(self, action=None): if not self.isInQueue(): self.queue(self._makeQueueCtxByAction(action)) else: self.dequeue(DequeueCtx(waitingID='prebattle/leave')) return True def hasLockedState(self): return self.isInQueue() def getEntityType(self): return self.getQueueType() def getPermissions(self, pID=None, **kwargs): return PreQueuePermissions(self.isInQueue()) def getConfirmDialogMeta(self, ctx): meta = None if self.hasLockedState(): meta = rally_dialog_meta.RallyLeaveDisabledDialogMeta( CTRL_ENTITY_TYPE.PREQUEUE, self.getQueueType()) return meta def showGUI(self, ctx=None): self._goToQueueUI() def exitFromQueue(self): self.doAction() def request(self, ctx, callback=None): requestType = ctx.getRequestType() if requestType == REQUEST_TYPE.QUEUE: self.queue(ctx, callback=callback) elif requestType == REQUEST_TYPE.DEQUEUE: self.dequeue(ctx, callback=callback) else: LOG_ERROR( 'PreQueueEntity supports QUEUE and DEQUEUE requests only', ctx) if callback is not None: callback(False) return def queue(self, ctx, callback=None): if ctx is None: ctx = self._makeQueueCtxByAction() if self._requestCtx.isProcessing(): LOG_ERROR('Request is processing', self._requestCtx) if callback is not None: callback(False) return elif self.isInQueue(): LOG_ERROR('Player already is in the queue', self._requestCtx) if callback is not None: callback(False) return elif self._validateParentControl(): if callback is not None: callback(False) return else: self._requestCtx = ctx self._requestCtx.startProcessing(callback) try: self._doQueue(ctx) except (AttributeError, TypeError, NotImplementedError): LOG_CURRENT_EXCEPTION() self._requestCtx.stopProcessing(False) return def dequeue(self, ctx, callback=None): if self._requestCtx.isProcessing(): LOG_ERROR('Request is processing', self._requestCtx) if callback: callback(False) return elif not self.isInQueue(): LOG_ERROR('Player is not in the queue', ctx) if callback is not None: callback(False) return else: self._requestCtx = ctx self._requestCtx.startProcessing(callback) try: self._doDequeue(ctx) except (AttributeError, TypeError, NotImplementedError): LOG_CURRENT_EXCEPTION() self._requestCtx.stopProcessing(False) return def leave(self, ctx, callback=None): def __leave(_=True): g_prbCtrlEvents.onPreQueueLeft() if callback is not None: callback(True) return if self.isInQueue(): self.dequeue(DequeueCtx(waitingID='prebattle/leave'), callback=__leave) else: __leave() def onEnqueued(self, queueType, *args): if queueType != self._queueType: return if self._requestCtx.getRequestType() == REQUEST_TYPE.QUEUE: self._requestCtx.stopProcessing(True) self._invokeListeners('onEnqueued', self.getQueueType(), *args) self._goToQueueUI() def onDequeued(self, queueType, *args): if queueType != self._queueType: return if self._requestCtx.getRequestType() == REQUEST_TYPE.DEQUEUE: self._requestCtx.stopProcessing(True) self._invokeListeners('onDequeued', self.getQueueType(), *args) self._exitFromQueueUI() def onEnqueueError(self, queueType, errorCode, *args): if queueType != self._queueType: return if self._requestCtx.getRequestType() == REQUEST_TYPE.QUEUE: self._requestCtx.stopProcessing(True) self._invokeListeners('onEnqueueError', self.getQueueType(), *args) self._exitFromQueueUI() SystemMessages.pushMessage(messages.getJoinFailureMessage(errorCode), type=SystemMessages.SM_TYPE.Error) def onKickedFromQueue(self, queueType, *args): if queueType != self._queueType: return if self._requestCtx.getRequestType() in (REQUEST_TYPE.QUEUE, REQUEST_TYPE.DEQUEUE): self._requestCtx.stopProcessing(True) self._invokeListeners('onKickedFromQueue', self.getQueueType(), *args) self._exitFromQueueUI() if self._isNeedToShowSystemMessage(): SystemMessages.pushI18nMessage(self._QUEUE_TIMEOUT_MSG_KEY, type=SystemMessages.SM_TYPE.Warning) def onKickedFromArena(self, *args): self._requestCtx.stopProcessing(True) self._invokeListeners('onKickedFromArena', self.getQueueType(), *args) self._exitFromQueueUI() def onArenaJoinFailure(self, *args): self._invokeListeners('onArenaJoinFailure', self.getQueueType(), *args) self._exitFromQueueUI() def _createActionsValidator(self): return PreQueueActionsValidator(self) def _goToQueueUI(self): return FUNCTIONAL_FLAG.UNDEFINED def _exitFromQueueUI(self): pass def _doQueue(self, ctx): raise NotImplementedError('Routine _doQueue must be overridden') def _doDequeue(self, ctx): raise NotImplementedError('Routine _doDequeue must be overridden') def _makeQueueCtxByAction(self, action=None): raise NotImplementedError( 'Routine _makeDefQueueCtx must be overridden') def _validateParentControl(self): result = prb_getters.isParentControlActivated() if result: g_eventDispatcher.showParentControlNotification() return result def _isNeedToShowSystemMessage(self): return True