Exemplo n.º 1
0
        private async Task StartWatcherAsync()
        {
            // observing a hardcoded player id for sample purposes
            // the load generator will update player ids within a range that includes this player
            var        playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
            var        player   = client.GetGrain <IPlayerGrain>(playerId);
            IGameGrain game     = null;

            // poll for this player to join a game
            while (game == null)
            {
                logger.LogInformation("Getting current game for player {@PlayerId}...",
                                      playerId);

                try
                {
                    game = await player.GetCurrentGameAsync();
                }
                catch (Exception error)
                {
                    logger.LogError(error,
                                    "Error while requesting current game for player {@PlayerId}",
                                    playerId);
                }

                if (game == null)
                {
                    try
                    {
                        await Task.Delay(1000, startCancellation.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }

            logger.LogInformation("Observing updates for game {@GameKey}",
                                  game.GetPrimaryKey());

            // subscribe for updates
            var watcher = client.ServiceProvider.GetService <IGameObserver>();
            await game.ObserveGameUpdatesAsync(
                await client.CreateObjectReference <IGameObserver>(watcher));

            logger.LogInformation("Subscribed successfully to game {@GameKey}",
                                  game.GetPrimaryKey());
        }
        public Task ChangeState(IGameGrain game, GameState oldState, GameState newState)
        {
            var gameId = game.GetPrimaryKey();
            switch (oldState)
            {
                case GameState.Unstarted:
                    _unstarted.Remove(gameId);
                    break;
                case GameState.InProgress:
                    _inProgress.Remove(gameId);
                    break;
                case GameState.Finished:
                    _finished.Remove(gameId);
                    break;
                default:
                    throw new InvalidOperationException();
            }

            switch (newState)
            {
                case GameState.Unstarted:
                    _unstarted.Add(gameId, game);
                    break;
                case GameState.InProgress:
                    _inProgress.Add(gameId, game);
                    break;
                case GameState.Finished:
                    _finished.Add(gameId, game);
                    break;
                default:
                    throw new InvalidOperationException();
            }

            return TaskDone.Done;
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            // the load generator will update player ids within a range that includes this player
            var playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
            var player   = _client.GetGrain <IPlayerGrain>(playerId);

            // poll for this player to join a game
            while (_game == null)
            {
                _logger.LogInformation("Getting current game for player {@PlayerId}...",
                                       playerId);

                try
                {
                    _game = await player.GetCurrentGameAsync();
                }
                catch (Exception error)
                {
                    _logger.LogError(error,
                                     "Error while requesting current game for player {@PlayerId}",
                                     playerId);
                }

                if (_game == null)
                {
                    try
                    {
                        await Task.Delay(1000, cancellationToken);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }
            }

            _logger.LogInformation("Observing updates for game {@GameKey}",
                                   _game.GetPrimaryKey());

            // subscribe for updates
            var reference = await _client.CreateObjectReference <IGameObserver>(_observer);

            await _game.ObserveGameUpdatesAsync(reference);

            _logger.LogInformation("Subscribed successfully to game {@GameKey}",
                                   _game.GetPrimaryKey());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Game grain calls this method to notify that the player has left the game.
        /// </summary>
        public Task LeaveGameAsync(IGameGrain game)
        {
            currentGame = null;

            logger.LogInformation("Player {@PlayerKey} left game {@GameKey}",
                                  GrainKey, game.GetPrimaryKey());

            return(Task.CompletedTask);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Game grain calls this method to notify that the player has joined the game.
        /// </summary>
        public Task JoinGameAsync(IGameGrain game)
        {
            currentGame = game;

            logger.LogInformation("Player {@PlayerKey} joined game {@GameKey}",
                                  GrainKey, game.GetPrimaryKey());

            return(Task.CompletedTask);
        }
Exemplo n.º 6
0
        // Game grain calls this method to notify that the player has left the game.
        public Task LeaveGame(IGameGrain game)
        {
            currentGame = null;
            Console.WriteLine(
                "Player {0} left game {1}",
                this.GetPrimaryKey(),
                game.GetPrimaryKey());

            return(Task.CompletedTask);
        }
Exemplo n.º 7
0
        // Game grain calls this method to notify that the player has joined the game.
        public Task JoinGame(IGameGrain game)
        {
            currentGame = game;
            Console.WriteLine(
                "Player {0} joined game {1}",
                this.GetPrimaryKey(),
                game.GetPrimaryKey());

            return(Task.CompletedTask);
        }
Exemplo n.º 8
0
        public Task JoinGame(IGameGrain game, IPlayerGrain player)
        {
            var primaryKey = game.GetPrimaryKey();

            if (_activeGames.ContainsKey(primaryKey))
            {
                return(TaskDone.Done);
            }
            _activeGames.Add(primaryKey, game);
            _activePlayers.Add(primaryKey, player);
            Console.WriteLine(" -- {0,-10} -- {1} joined a game.", "Account", _username);
            return(TaskDone.Done);
        }
Exemplo n.º 9
0
        public Task LeaveGame(IGameGrain game)
        {
            syncTimer.Dispose();
            syncTimer = null;

            currentGame = null;
            Console.WriteLine("Player {0} left game {1}", this.GetPrimaryKey(), game.GetPrimaryKey());

            // TODO: Have to consider a player leave a game during a running game.
            previous = null;

            return(TaskDone.Done);
        }
Exemplo n.º 10
0
        static async Task <IClusterClient> RunWatcher()
        {
            try

            {
                // Connect to local silo
                var config = ClientConfiguration.LocalhostSilo();
                var client = new ClientBuilder().UseConfiguration(config).Build();
                await client.Connect();

                // Hardcoded player ID
                Guid         playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
                IPlayerGrain player   = client.GetGrain <IPlayerGrain>(playerId);
                IGameGrain   game     = null;

                while (game == null)
                {
                    Console.WriteLine("Getting current game for player {0}...", playerId);

                    try
                    {
                        game = await player.GetCurrentGame();

                        if (game == null) // Wait until the player joins a game
                        {
                            await Task.Delay(5000);
                        }
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine("Exception: ", exc.GetBaseException());
                    }
                }

                Console.WriteLine("Subscribing to updates for game {0}...", game.GetPrimaryKey());

                // Subscribe for updates
                var watcher = new GameObserver();
                await game.SubscribeForGameUpdates(
                    await client.CreateObjectReference <IGameObserver>(watcher));

                Console.WriteLine("Subscribed successfully. Press <Enter> to stop.");

                return(client);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Unexpected Error: {0}", exc.GetBaseException());
                throw;
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Simulates a companion application that connects to the game that a particular player is currently part of
        /// and subscribes to receive live notifications about its progress.
        /// </summary>
        static void Main(string[] args)
        {
            try
            {
                var config = ClientConfiguration.LocalhostSilo();
                GrainClient.Initialize(config);

                // Hardcoded player ID
                Guid         playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
                IPlayerGrain player   = GrainClient.GrainFactory.GetGrain <IPlayerGrain>(playerId);
                IGameGrain   game     = null;

                while (game == null)
                {
                    Console.WriteLine("Getting current game for player {0}...", playerId);

                    try
                    {
                        game = player.GetCurrentGame().Result;
                        if (game == null) // Wait until the player joins a game
                        {
                            game = null;
                            Thread.Sleep(5000);
                        }
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine("Exception: ", exc.GetBaseException());
                    }
                }

                Console.WriteLine("Subscribing to updates for game {0}...", game.GetPrimaryKey());

                // Subscribe for updates
                var watcher = new GameObserver();
                game.SubscribeForGameUpdates(GrainClient.GrainFactory.CreateObjectReference <IGameObserver>(watcher).Result).Wait();

                // Block main thread so that the process doesn't exit. Updates arrive on thread pool threads.
                Console.WriteLine("Subscribed successfully. Press <Enter> to stop.");
                Console.ReadLine();
            }
            catch (Exception exc)
            {
                Console.WriteLine("Unexpected Error: {0}", exc.GetBaseException());
            }
        }
Exemplo n.º 12
0
 // Game grain calls this method to notify that the player has left the game.
 public Task LeaveGame(IGameGrain game)
 {
     currentGame = null;
     Console.WriteLine("Player {0} left game {1}", this.GetPrimaryKey(), game.GetPrimaryKey());
     return TaskDone.Done;
 }
Exemplo n.º 13
0
 // Game grain calls this method to notify that the player has joined the game.
 public Task JoinGame(IGameGrain game)
 {
     currentGame = game;
     Console.WriteLine("Player {0} joined game {1}", this.GetPrimaryKey(), game.GetPrimaryKey());
     return TaskDone.Done;
 }
 public Task NewGame(IGameGrain game)
 {
     var gameId = game.GetPrimaryKey();
     _unstarted.Add(gameId, game);
     return TaskDone.Done;
 }
Exemplo n.º 15
0
        public Task JoinGame(IGameGrain game)
        {
            currentGame = game;
            Console.WriteLine("Player {0} joined game {1}", this.GetPrimaryKeyLong(), game.GetPrimaryKey());

            if (previous == null)
            {
                previous = new Progression();
            }
            if (syncTimer == null)
            {
                syncTimer = base.RegisterTimer(TimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(2));
            }

            return(TaskDone.Done);
        }