public override void Do(GameAction gameAction)
        {
            if (string.IsNullOrWhiteSpace(gameAction.UserId))
            {
                return;
            }

            UserStats statistics = this.statisticsRepository.Retrieve(gameAction.UserId);
            if (statistics == null)
            {
                statistics = new UserStats
                {
                    UserId = gameAction.UserId,
                    Victories = 0,
                    Defeats = 0,
                    GameCount = 0
                };
            }

            statistics.Defeats += GetValue(gameAction.CommandData, "Defeats");
            statistics.Victories += GetValue(gameAction.CommandData, "Victories");
            statistics.GameCount += GetValue(gameAction.CommandData, "GameCount");

            this.statisticsRepository.Save(statistics);
        }
        public void Add(GameAction gameAction)
        {
            if (gameAction == null)
            {
                throw new ArgumentException("gameAction");
            }

            GameActionStatisticsMessage message = new GameActionStatisticsMessage() { GameAction = gameAction };

            this.gameActionStatisticGameQueue.AddMessage(message);
        }
        public void PostEvent(string topic, dynamic formContent)
        {
            if (string.IsNullOrWhiteSpace(this.CurrentUserId))
            {
                throw new ServiceException("The user is not authenticated");
            }

            // Command Type
            int commandType;

            try
            {
                commandType = Convert.ToInt32(formContent.type.Value);
            }
            catch
            {
                throw new ServiceException("Invalid type parameter");
            }

            if (topic != "notifications" && topic != "statistics")
            {
                throw new ServiceException("Invalid topic parameter");
            }

            // Command Data
            var jsonCommandData = (JsonObject)(formContent.commandData ?? null);
            IDictionary<string, object> commandData = null;

            if (jsonCommandData != null)
            {
                commandData = jsonCommandData.ToDictionary();
            }

            // Add gameAction
            var gameAction = new GameAction
            {
                Id = Guid.NewGuid(),
                Type = commandType,
                CommandData = commandData,
                UserId = this.CurrentUserId,
                Timestamp = DateTime.UtcNow
            };

            if (topic == "notifications")
            {
                this.notificationQueue.Add(gameAction);
            }
            else if (topic == "statistics")
            {
                this.statisticsQueue.Add(gameAction);
            }
        }
Exemplo n.º 4
0
        public HttpResponseMessage Command(Guid gameId, HttpRequestMessage request)
        {
            dynamic formContent = request.Content.ReadAsAsync<JsonValue>().Result;
            var game = this.gameRepository.GetGame(gameId);
            if (game == null)
            {
                return BadRequest("Game does not exist. Game Id: " + gameId);
            }

            // Command Type
            int commandType;

            try
            {
                commandType = int.Parse(formContent.type.Value);
            }
            catch
            {
                return BadRequest("Invalid type parameter");
            }

            // Command Data
            var jsonCommandData = (JsonObject)(formContent.commandData ?? null);
            IDictionary<string, object> commandData = null;

            if (jsonCommandData != null)
            {
                commandData = jsonCommandData.ToDictionary();
            }

            try
            {
                // Add gameAction
                var gameAction = new GameAction
                {
                    Id = Guid.NewGuid(),
                    Type = commandType,
                    CommandData = commandData,
                    UserId = this.CurrentUserId,
                    Timestamp = DateTime.UtcNow
                };

                game.GameActions.Add(gameAction);
                this.gameRepository.AddOrUpdateGame(game);

                return SuccessResponse;
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
        private static UserStats UpdateCurrentStats(GameAction gameAction, UserStats originalStatistics)
        {
            if (originalStatistics == null)
            {
                originalStatistics = new UserStats();
                originalStatistics.UserId = gameAction.UserId;
            }

            originalStatistics.Victories += GetValue(gameAction.CommandData, "Victories");
            originalStatistics.Defeats += GetValue(gameAction.CommandData, "Defeats");
            originalStatistics.GameCount += GetValue(gameAction.CommandData, "GameCount");
            return originalStatistics;
        }
        public override void Do(GameAction gameAction)
        {
            if (string.IsNullOrWhiteSpace(gameAction.UserId))
            {
                return;
            }

            // Retrieve existent statistics and update them with the new results
            UserStats currentStatistics = this.statisticsRepository.Retrieve(gameAction.UserId);
            currentStatistics = UpdateCurrentStats(gameAction, currentStatistics);
            
            // Generate a new UserStats with the updated RowKey to add in the TableStorage
            UserStats statistics = null;
            statistics = CreateUpdatedStats(currentStatistics);
            statistics.PartitionKey = (int.MaxValue - statistics.Victories).ToString().PadLeft(int.MaxValue.ToString().Length);
            statistics.RowKey = statistics.UserId;
            this.statisticsRepository.Save(statistics);
        }
        public void SendVictoriesMetric()
        {
            string userId = Guid.NewGuid().ToString();
            var commandData = new Dictionary<string, object>();

            commandData.Add("Victories", 1);

            GameAction gameAction = new GameAction()
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                CommandData = commandData
            };

            this.command.Do(gameAction);

            var result = this.repository.Retrieve(userId);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Victories);
        }
        public void SendShot()
        {
            var queue = this.CreateQueue();

            var action = new GameAction()
            {
                Id = Guid.NewGuid(),
                UserId = "johnny",
                CommandData = new Dictionary<string, object>() { { "name", "peter" }, { "point", 1 } }
            };

            queue.Add(action);

            var message = this.gameActionStatisticsQueue.GetMessage(new TimeSpan(10000));

            Assert.IsNotNull(message);
            Assert.IsNotNull(message.GameAction);
            Assert.IsNotNull(message.GameAction.CommandData);

            Assert.AreEqual(action.Id, message.GameAction.Id);
            Assert.AreEqual(action.UserId, message.GameAction.UserId);
            Assert.AreEqual("peter", action.CommandData["name"]);
            Assert.AreEqual(1, action.CommandData["point"]);
        }
 public override void Do(GameAction gameAction)
 {
     // TODO: Add save statistics functionality
 }
 public abstract void Do(GameAction gameAction);
 public void Add(GameAction gameAction)
 {
     this.gameAction = gameAction;
 }
 public override void Do(GameAction gameAction)
 {
     // TODO Any code?
 }
Exemplo n.º 13
0
        public HttpResponseMessage PostEvent(string topic, HttpRequestMessage request)
        {
            if (string.IsNullOrWhiteSpace(this.CurrentUserId))
            {
                return BadRequest("The user is not authenticated");
            }

            dynamic formContent = request.Content.ReadAsAsync<JsonValue>().Result;

            // Command Type
            int commandType;

            try
            {
                commandType = int.Parse(formContent.type.Value);
            }
            catch
            {
                return BadRequest("Invalid type parameter");
            }

            if (topic != "notifications" && topic != "statistics")
            {
                return BadRequest("Invalid topic parameter");
            }

            // Command Data
            var jsonCommandData = (JsonObject)(formContent.commandData ?? null);
            IDictionary<string, object> commandData = null;

            if (jsonCommandData != null)
            {
                commandData = jsonCommandData.ToDictionary();
            }

            try
            {
                // Add gameAction
                var gameAction = new GameAction
                {
                    Id = Guid.NewGuid(),
                    Type = commandType,
                    CommandData = commandData,
                    UserId = this.CurrentUserId,
                    Timestamp = DateTime.UtcNow
                };

                if (topic == "notifications")
                {
                    this.notificationQueue.Add(gameAction);
                }
                else if (topic == "statistics")
                {
                    this.statisticsQueue.Add(gameAction);
                }

                return SuccessResponse;
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
        public void Command(Guid gameId, dynamic formContent)
        {
            var game = this.gameRepository.GetGame(gameId);
            
            if (game == null)
            {
                throw new ServiceException("Game does not exist. Game Id: " + gameId);
            }

            // Command Type
            int commandType;

            try
            {
                commandType = Convert.ToInt32(formContent.type.Value);
            }
            catch 
            {
                throw new ServiceException("Invalid type parameter");
            }

            // Command Data
            var jsonCommandData = (JsonObject)(formContent.commandData ?? null);
            IDictionary<string, object> commandData = null;

            if (jsonCommandData != null)
            {
                commandData = jsonCommandData.ToDictionary();
            }

            // Add gameAction
            var gameAction = new GameAction
            {
                Id = Guid.NewGuid(),
                Type = commandType,
                CommandData = commandData,
                UserId = this.CurrentUserId,
                Timestamp = DateTime.UtcNow
            };

            game.GameActions.Add(gameAction);
            this.gameRepository.AddOrUpdateGame(game);
        }