Пример #1
0
        public void Move(Guid gameId, int row, int col)
        {
            // TODO: exceptions in following invocation are silently lost, so we need a way to log and show them.

            Action move = () =>
            {
                /*
                 * Here instance members are out of lifetime scope setup by Autofac because of delayed invocation
                 * TODO: use ServiceLocator to instantiate them?
                 */
                using (var repository = new EFRepository(new ApplicationDbContext()))
                {
                    var messageFactory = new JsonMessageFactory();

                    var game     = repository.GetGame(gameId);
                    var snapshot = repository.GetGameSnapshot(game);

                    game.Move(_player, row, col);

                    PlayerRole playerRole = game.GetPlayerRole(_player);

                    var message = messageFactory.CreateMovedMessage(gameId,
                                                                    playerRole, row, col, game.CurrentState);
                    repository.AddMessage(message);

                    snapshot.LastMessage = message;

                    repository.Save();
                }
            };

            MoveRequests.AddOrUpdate(gameId,
                                     id => Task.Factory.StartNew(move),                           // on first call run for this gameId
                                     (id, currentTask) => currentTask.ContinueWith(t => move())); // queue invocation for this gameId
        }