Esempio n. 1
0
 private void ValidatePlayerLevelTooLowForSwordException(Guid playerId, NewItem item)
 {
     if (_repository.Get(playerId).Result.Level < 3 && item.ItemType == ItemType.Sword)
     {
         throw new PlayerLevelTooLowForSwordException();
     }
 }
Esempio n. 2
0
        public Task <NewItem> CreateItem(Guid playerId, [FromBody] NewItem item)
        {
            Console.WriteLine(item.Price + " " + playerId);
            Player player = _repository.Get(playerId).Result;

            if (player.Level < 3 && item.ItemType == ItemType.Sword)
            {
                throw new PlayerLevelTooLowForSwordException();
            }
            //ValidatePlayerLevelTooLowForSwordException(playerId, item);
            return(_repository.CreateItem(playerId, item));
        }
Esempio n. 3
0
        public async Task <NewItem> CreateItem(Guid playerId, NewItem item)
        {
            FilterDefinition <Player> filter = Builders <Player> .Filter.Eq(p => p.Id, playerId);

            Item createdItem = new Item();

            createdItem.ItemType = item.ItemType;
            createdItem.Price    = item.Price;
            createdItem.Id       = Guid.NewGuid();
            Player player = Get(playerId).Result;

            player.Items.Add(createdItem);

            await _collection.ReplaceOneAsync(filter, player);

            return(item);
        }
Esempio n. 4
0
        public Task <NewItem> CreateItem(Guid playerId, NewItem item)
        {
            return(Task.Run(() => {
                var jsonData = System.IO.File.ReadAllText(dataFilePath);
                PlayersList playerList = JsonConvert.DeserializeObject <PlayersList>(jsonData);

                foreach (var player in playerList.players)
                {
                    if (player.Id == playerId)
                    {
                        Item createdItem = new Item();
                        createdItem.ItemType = item.ItemType;
                        createdItem.Price = item.Price;
                        createdItem.Id = Guid.NewGuid();
                        player.Items.Add(createdItem);
                        var json = JsonConvert.SerializeObject(playerList, Formatting.Indented);
                        System.IO.File.WriteAllText(dataFilePath, json);

                        return item;
                    }
                }
                return null;
            }));
        }