public async Task ListenCreations(Channel channel)
        {
            var client = CreateClient(channel);

            try
            {
                using (var call = GetCreationCall(client, channel.ShutdownToken))
                using (var creationsStream = call.ResponseStream)
                {
                    while (await creationsStream.MoveNext(channel.ShutdownToken))
                    {
                        channel.ShutdownToken.ThrowIfCancellationRequested();

                        var createdState = creationsStream.Current;
                        var info = new TInfo();
                        info.State.MergeFrom(createdState);

                        var orders = CreateOrders(client, info.ID);
                        
                        await mSyncContext.Execute(() => Created?.Invoke(orders, info));
                        var t = ListenStateUpdates(info, client, channel.ShutdownToken);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                throw;
            }
        }
Exemple #2
0
        private Task <TModel> CreateModelAndView <TView, TModel, TOrders, TInfo>(GameObject prefab, Func <TView, TModel> createModel, Vector2 position)
            where TView : ModelSelectableView <TOrders, TInfo>
            where TOrders : IGameObjectOrders
            where TInfo : IGameObjectInfo
            where TModel : RtsGameObject, TOrders, TInfo
        {
            return(mSyncContext.Execute(() =>
            {
                var instance = Instantiate(prefab);
                var view = instance.GetComponent <TView>();
                if (view == null)
                {
                    throw new Exception("Prefab not contains View script.");
                }

                var result = createModel(view);
                result.RemovedFromGame += o =>
                {
                    Destroy(instance);
                    instance.transform.parent = null;
                };
                view.Map = mMap;
                view.SyncContext = mSyncContext;
                view.LoadModel(result, result);

                instance.transform.parent = mMap.ChildContainer.transform;
                instance.transform.localPosition = mMap.GetWorldPosition(position);

                ViewCreated?.Invoke(view);

                return result;
            }));
        }
Exemple #3
0
        private async Task ListenGameState(Channel channel)
        {
            var mapState    = new ClientMapData();
            var playerState = new ClientPlayerState();
            await mSyncContext.Execute(() => PlayerConnected?.Invoke(playerState), channel.ShutdownToken);

            try
            {
                while (true)
                {
                    try
                    {
                        mGameService = new GameService.GameServiceClient(channel);
                        using (var call = mGameService.ConnectAndListenState(new Empty()))
                            using (var stateStream = call.ResponseStream)
                            {
                                while (await stateStream.MoveNext(channel.ShutdownToken))
                                {
                                    channel.ShutdownToken.ThrowIfCancellationRequested();
                                    var state = stateStream.Current;

                                    if (state.Map != null)
                                    {
                                        mapState.State = new MapState(state.Map);
                                    }

                                    if (state.Player != null)
                                    {
                                        playerState.PlayerState = new PlayerState(state.Player);
                                    }

                                    channel.ShutdownToken.ThrowIfCancellationRequested();

                                    if (!mMapLoaded)
                                    {
                                        mMapLoaded = true;
                                        await mSyncContext.Execute(() =>
                                        {
                                            MapLoaded?.Invoke(mapState);
                                            BaseCreated?.Invoke(state.BasePos.ToUnity());
                                        }, channel.ShutdownToken);

                                        var tChat = ListenChat(mGameService, channel);

                                        var t0 = mWorkerCreationStateListener.ListenCreations(mChannel);
                                        var t1 = mBuildingTemplateCreationStateListener.ListenCreations(mChannel);
                                        var t2 = mCentralBuildingCreationStateListener.ListenCreations(mChannel);
                                        var t3 = mMiningCampCreationListener.ListenCreations(mChannel);
                                        var t4 = mBarrakCreationListener.ListenCreations(mChannel);
                                        var t5 = mRangedWarriorCreationStateListener.ListenCreations(mChannel);
                                        var t6 = mMeeleeWarriorCreationStateListener.ListenCreations(mChannel);
                                    }
                                }
                            }

                        break;
                    }
                    catch (RpcException e)
                    {
                        if (e.Status.StatusCode != StatusCode.Unavailable)
                        {
                            throw;
                        }

                        await Task.Delay(TimeSpan.FromSeconds(0.5));
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                DisconnectedFromServer?.Invoke();
                throw;
            }
        }