Ejemplo n.º 1
0
        public Task Initialize(IGameGrain game, string name)
        {
            _game = game;
            _name = name;
//            Console.WriteLine(" -- {0,-10} -- {1} initialized.", "Star", _name);
            return(TaskDone.Done);
        }
Ejemplo n.º 2
0
        public Task ProcessExited(Guid processId)
        {
            IGameGrain session = GrainFactory.GetGrain <IGameGrain>(processId);

            session.UnsubscribeStatus(this);
            return(TaskDone.Done);
        }
Ejemplo n.º 3
0
        public Task Heartbeat(byte[] data)
        {
            Packet.Heartbeat packet = PacketSerializer.Deserialize <Packet.Heartbeat>(data);
            IGameGrain       game   = base.GrainFactory.GetGrain <IGameGrain>(packet.Game);

            return(game.UpdateGameStatistics(packet.Status));
        }
Ejemplo n.º 4
0
        public Task GameStarts(byte[] data)
        {
            Packet.GameStarts packet = PacketSerializer.Deserialize <Packet.GameStarts>(data);
            IGameGrain        game   = base.GrainFactory.GetGrain <IGameGrain>(packet.Game);

            return(game.GameStarts(packet.Players));
        }
        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;
        }
Ejemplo n.º 6
0
        public Task Heartbeat(byte[] data)
        {
            HeartbeatData heartbeatData = HeartbeatDataDotNetSerializer.Deserialize(data);
            IGameGrain    game          = GrainFactory.GetGrain <IGameGrain>(heartbeatData.Game);

            return(game.UpdateGameStatus(heartbeatData.Status));
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
 public Task Create(IGameGrain game, IAccountGrain account, string name)
 {
     _game         = game;
     _accountGrain = account;
     _name         = name;
     _stars        = new List <IStarGrain>();
     Console.WriteLine(" -- {0,-10} -- {1} created.", "Player", _name);
     return(TaskDone.Done);
 }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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;
            }
        }
Ejemplo n.º 15
0
        public void GameStart(Guid gameId, int seconds)
        {
            currentGame = GrainFactory.GetGrain <IGameGrain>(gameId);
            Console.WriteLine("Player {0} joined game {1}", this.GetPrimaryKeyLong(), gameId);

            if (previous == null)
            {
                previous = new Progression();
            }
            if (syncTimer == null)
            {
                syncTimer = base.RegisterTimer(TimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(2));
            }
        }
Ejemplo n.º 16
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());
        }
Ejemplo n.º 17
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);
        }
        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());
        }
Ejemplo n.º 19
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());
            }
        }
Ejemplo n.º 20
0
        // Reported by dedicated server manager: process created, with processId as id.
        public Task ProcessCreated(Guid processId)
        {
            source.Peek().SetResult(processId);

            IGameGrain session = GrainFactory.GetGrain <IGameGrain>(processId);

            session.SubscribeStatus(this);

            try
            {
                sessionStatus.Add(processId, true);
            }
            catch (Exception)
            {
                throw new Exception("Unexpected state: processId should NOT exist in dictionary.");
            }
            return(TaskDone.Done);
        }
 public Task NewGame(IGameGrain game)
 {
     var gameId = game.GetPrimaryKey();
     _unstarted.Add(gameId, game);
     return TaskDone.Done;
 }
Ejemplo n.º 22
0
 public override Task OnActivateAsync()
 {
     currentGame = null;
     return(TaskDone.Done);
 }
Ejemplo n.º 23
0
 public override Task OnActivateAsync()
 {
     currentGame = null;
     return TaskDone.Done;
 }
Ejemplo n.º 24
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;
 }
Ejemplo n.º 25
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;
 }