122 lines
4.2 KiB
C#
122 lines
4.2 KiB
C#
using CsvHelper;
|
|
using System.Globalization;
|
|
using PetCompanion.Models;
|
|
using PetCompanion.Repositories;
|
|
|
|
namespace PetCompanion.Services
|
|
{
|
|
public class GameItemService
|
|
{
|
|
private readonly GameItemsRepository gameItemsRepository;
|
|
|
|
public GameItemService(GameItemsRepository gameItemsRepository)
|
|
{
|
|
this.gameItemsRepository = gameItemsRepository;
|
|
}
|
|
|
|
public void LoadItemsFromCsv(string filePath)
|
|
{
|
|
using var reader = new StreamReader(filePath);
|
|
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
|
|
|
|
var items = csv.GetRecords<GameItem>().ToList();
|
|
|
|
foreach (var item in items)
|
|
{
|
|
var existingItem = gameItemsRepository.GetById(item.Id);
|
|
if (existingItem == null)
|
|
{
|
|
gameItemsRepository.Add(item);
|
|
}
|
|
else
|
|
{
|
|
//gameItemsRepository.Update(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ApplyItemEffect(Pet pet, GameItem item)
|
|
{
|
|
var effects = item.Effect.Split(';');
|
|
foreach (var effect in effects)
|
|
{
|
|
ApplySingleEffect(pet, effect.Trim());
|
|
}
|
|
}
|
|
|
|
public void RemoveItemEffect(Pet pet, GameItem item)
|
|
{
|
|
var effects = item.Effect.Split(';');
|
|
foreach (var effect in effects)
|
|
{
|
|
RemoveSingleEffect(pet, effect.Trim());
|
|
}
|
|
}
|
|
|
|
private void ApplySingleEffect(Pet pet, string effect)
|
|
{
|
|
if (effect == "Nothing") return;
|
|
|
|
var parts = effect.Split('_');
|
|
var action = parts[0];
|
|
var target = parts[1];
|
|
var value = parts.Length > 2 ? int.Parse(parts[2]) : 0;
|
|
|
|
switch ($"{action}_{target}")
|
|
{
|
|
case "ADD_FOOD_RESOURCES":
|
|
pet.Resources.Food += value;
|
|
break;
|
|
case "ADD_INTELLIGENCE":
|
|
pet.Stats.Intelligence += value;
|
|
break;
|
|
case "ADD_HEALTH":
|
|
pet.Health = Math.Min(pet.Health + value, pet.MaxHealth);
|
|
break;
|
|
case "ADD_MAX_HEALTH":
|
|
pet.MaxHealth += value;
|
|
break;
|
|
// Add more effect handlers as needed
|
|
}
|
|
}
|
|
|
|
private void RemoveSingleEffect(Pet pet, string effect)
|
|
{
|
|
if (effect == "Nothing") return;
|
|
|
|
var parts = effect.Split('_');
|
|
var action = parts[0];
|
|
var target = parts[1];
|
|
var value = parts.Length > 2 ? int.Parse(parts[2]) : 0;
|
|
|
|
switch ($"{action}_{target}")
|
|
{
|
|
case "ADD_MAX_HEALTH":
|
|
pet.MaxHealth -= value;
|
|
pet.Health = Math.Min(pet.Health, pet.MaxHealth);
|
|
break;
|
|
case "ADD_MAX_INTELLIGENCE":
|
|
pet.Stats.MaxIntelligence -= value;
|
|
pet.Stats.Intelligence = Math.Min(pet.Stats.Intelligence, pet.Stats.MaxIntelligence);
|
|
break;
|
|
case "ADD_MAX_STRENGTH":
|
|
pet.Stats.MaxStrength -= value;
|
|
pet.Stats.Strength = Math.Min(pet.Stats.Strength, pet.Stats.MaxStrength);
|
|
break;
|
|
case "ADD_MAX_CHARISMA":
|
|
pet.Stats.MaxCharisma -= value;
|
|
pet.Stats.Charisma = Math.Min(pet.Stats.Charisma, pet.Stats.MaxCharisma);
|
|
break;
|
|
case "ADD_MAX_ALL_STATS":
|
|
pet.Stats.MaxIntelligence -= value;
|
|
pet.Stats.MaxStrength -= value;
|
|
pet.Stats.MaxCharisma -= value;
|
|
pet.Stats.Intelligence = Math.Min(pet.Stats.Intelligence, pet.Stats.MaxIntelligence);
|
|
pet.Stats.Strength = Math.Min(pet.Stats.Strength, pet.Stats.MaxStrength);
|
|
pet.Stats.Charisma = Math.Min(pet.Stats.Charisma, pet.Stats.MaxCharisma);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|