private void ValidatePlayerLevelTooLowForSwordException(Guid playerId, NewItem item) { if (_repository.Get(playerId).Result.Level < 3 && item.ItemType == ItemType.Sword) { throw new PlayerLevelTooLowForSwordException(); } }
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)); }
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); }
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; })); }