Inheritance: InterfacedActorRef, IGamePlayer, IGamePlayer_NoReply
Exemple #1
0
 protected override async Task OnStart(bool restarted)
 {
     try
     {
         var observer = CreateObserver<IGameObserver>();
         var ret = await _game.Join(_userId, _userName, observer, null);
         _gamePlayer = _game.Cast<GamePlayerRef>().WithRequestWaiter(this);
         _playerId = ret.Item1;
     }
     catch (Exception e)
     {
         _logger.ErrorFormat("Failed to join game({0})", e, _game.CastToIActorRef().Path);
         Self.Tell(InterfacedPoisonPill.Instance);
     }
 }
Exemple #2
0
    private IEnumerator ProcessJoinGameInternal()
    {
        G.Logger.Info("ProcessJoinGame");

        // Finding Game
        // Register user to pairing queue and waiting for 5 secs.

        LoadingText.text = "Finding Game...";

        _pairedGame = null;

        var pairingObserver = G.Communicator.ObserverRegistry.Create<IUserPairingObserver>(this);
        yield return G.User.RegisterPairing(pairingObserver).WaitHandle;

        var startTime = DateTime.Now;
        while ((DateTime.Now - startTime).TotalSeconds < 5 && _pairedGame == null && _isLeaveRequested == false)
        {
            yield return null;
        }

        G.Communicator.ObserverRegistry.Remove(pairingObserver);

        if (_isLeaveRequested)
            yield break;

        if (_pairedGame == null)
        {
            yield return G.User.UnregisterPairing().WaitHandle;
            if (_isLeaveRequested == false)
                yield return UiMessageBox.Show("Cannot find game").WaitForHide();
            SceneManager.LoadScene("MainScene");
            yield break;
        }

        // Join Game

        var gameObserver = G.Communicator.ObserverRegistry.Create<IGameObserver>(this, startPending: true);
        gameObserver.GetEventDispatcher().KeepingOrder = true;

        var roomId = _pairedGame.Item1;
        var joinRet = G.User.JoinGame(roomId, gameObserver);
        yield return joinRet.WaitHandle;

        if (joinRet.Exception != null)
        {
            G.Communicator.ObserverRegistry.Remove(gameObserver);
            var box = UiMessageBox.Show("Failed to join\n" + joinRet.Exception);
            yield return StartCoroutine(box.WaitForHide());
            SceneManager.LoadScene("MainScene");
        }

        _gameObserver = gameObserver;
        _gameInfo = joinRet.Result.Item3;
        _myPlayerId = joinRet.Result.Item2;
        _myPlayer = (GamePlayerRef)joinRet.Result.Item1;

        if (_myPlayer.IsChannelConnected() == false)
        {
            var connectTask = _myPlayer.ConnectChannelAsync();
            yield return connectTask.WaitHandle;
            if (connectTask.Exception != null)
            {
                var box = UiMessageBox.Show("Failed to connect\n" + joinRet.Exception);
                G.Communicator.ObserverRegistry.Remove(gameObserver);
                yield return StartCoroutine(box.WaitForHide());
                _myPlayer = null;
                yield break;
            }
            ((IChannel)_myPlayer.RequestWaiter).StateChanged += (_, state) =>
            {
                if (state == ChannelStateType.Closed)
                    ChannelEventDispatcher.Post(OnChannelClose, _);
            };
        }

        gameObserver.GetEventDispatcher().Pending = false;
        LoadingText.text = "Waiting for " + _pairedGame.Item2 + "...";
    }