Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Rework the item system #40

Merged
merged 1 commit into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions GameServer/GameServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@
<Compile Include="Logic\GameObjects\Stats.cs" />
<Compile Include="Logic\GameObjects\Target.cs" />
<Compile Include="Logic\GameObjects\Unit.cs" />
<Compile Include="Logic\Items\Item.cs" />
<Compile Include="Logic\Items\ItemManager.cs" />
<Compile Include="Logic\Content\ItemManager.cs" />
<Compile Include="Logic\Items\Shop.cs" />
<Compile Include="Logic\Logger.cs" />
<Compile Include="Logic\Maps\Map.cs" />
Expand Down
7 changes: 4 additions & 3 deletions GameServer/Logic/ChampionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LeagueSandbox.GameServer.Logic.GameObjects;
using LeagueSandbox.GameServer.Core.Logic;
using LeagueSandbox.GameServer.Logic.GameObjects;
using LeagueSandbox.GameServer.Logic.Maps;
using System;
using System.Collections.Generic;
Expand All @@ -10,9 +11,9 @@ namespace LeagueSandbox.GameServer.Logic
{
class ChampionFactory
{
public static Champion getChampionFromType(string type, Map map, uint id, uint playerId)
public static Champion getChampionFromType(Game game, string type, Map map, uint id, uint playerId)
{
return new Champion(type, map, id, playerId);
return new Champion(game, type, map, id, playerId);
}
}
}
91 changes: 64 additions & 27 deletions GameServer/Logic/Content/ItemCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using LeagueSandbox.GameServer.Core.Logic;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -8,43 +9,79 @@

namespace LeagueSandbox.GameServer.Logic.Content
{
public class ContentCollection
public class ContentCollectionEntry
{
public class ContentCollectionEntry
public Dictionary<string, Dictionary<string, object>> Values { get; set; }
public Dictionary<string, object> MetaData { get; set; }

public int ContentFormatVersion { get { return Convert.ToInt32(MetaData["ContentFormatVersion"]); } }

public T GetValue<T>(string section, string name)
{
public Dictionary<string, Dictionary<string, object>> Values { get; set; }
public Dictionary<string, object> MetaData { get; set; }
return (T)Convert.ChangeType(Values[section][name], typeof(T));
}

public int ContentFormatVersion { get { return Convert.ToInt32(MetaData["ContentFormatVersion"]); } }
public T SafeGetValue<T>(string section, string name, T defaultValue)
{
if (!Values.ContainsKey(section)) return defaultValue;
if (!Values[section].ContainsKey(name)) return defaultValue;
return GetValue<T>(section, name);
}

public T GetValue<T>(string section, string name)
{
return (T)Convert.ChangeType(Values[section][name], typeof(T));
}
public float SafeGetFloat(string section, string name, float defaultValue)
{
return SafeGetValue(section, name, defaultValue);
}

public T SafeGetValue<T>(string section, string name, T defaultValue)
{
if (!Values.ContainsKey(section)) return LogSkipAndReturn(section, name, defaultValue);
if (!Values[section].ContainsKey(name)) return LogSkipAndReturn(section, name, defaultValue);
return GetValue<T>(section, name);
}
public float SafeGetFloat(string section, string name)
{
return SafeGetFloat(section, name, 0f);
}

private T LogSkipAndReturn<T>(string section, string name, T value)
{
return value;
}
public int SafeGetInt(string section, string name, int defaultValue)
{
return SafeGetValue(section, name, defaultValue);
}
}

public class ItemCollection : ContentCollection
{
public class ItemCollectionEntry : ContentCollectionEntry
public int SafeGetInt(string section, string name)
{
return SafeGetInt(section, name, 0);
}

public string SafeGetString(string section, string name, string defaultValue)
{
return SafeGetValue(section, name, defaultValue);
}

public string SafeGetString(string section, string name)
{
public string ItemFileName { get { return Convert.ToString(MetaData["ItemFileName"]); } }
public string ItemName { get { return Convert.ToString(MetaData["ItemName"]); } }
public int ItemId { get { return Convert.ToInt32(MetaData["ItemId"]); } }
return SafeGetString(section, name, "");
}

public bool SafeGetBool(string section, string name, bool defaultValue)
{
return SafeGetValue(section, name, defaultValue);
}

public bool SafeGetBool(string section, string name)
{
return SafeGetBool(section, name, false);
}
}

public class ContentCollection
{
}

public class ItemCollectionEntry : ContentCollectionEntry
{
public string ItemFileName { get { return Convert.ToString(MetaData["ItemFileName"]); } }
public string ItemName { get { return Convert.ToString(MetaData["ItemName"]); } }
public int ItemId { get { return Convert.ToInt32(MetaData["ItemId"]); } }
}

public class ItemCollection : ContentCollection
{
private Dictionary<int, ItemCollectionEntry> _items;
public Dictionary<int, ItemCollectionEntry>.Enumerator GetEnumerator() { return _items.GetEnumerator(); }

Expand Down
249 changes: 249 additions & 0 deletions GameServer/Logic/Content/ItemManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
using LeagueSandbox.GameServer.Core.Logic;
using LeagueSandbox.GameServer.Logic.GameObjects;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LeagueSandbox.GameServer.Logic.Items;
using LeagueSandbox.GameServer.Core.Logic.PacketHandlers;

namespace LeagueSandbox.GameServer.Logic.Content
{
public class ItemManager
{
private Game _owner;
private Dictionary<int, ItemType> _itemTypes;

private ItemManager(Game owner)
{
_owner = owner;
_itemTypes = new Dictionary<int, ItemType>();
}

public ItemType GetItemType(int itemId)
{
return _itemTypes[itemId];
}

public ItemType SafeGetItemType(int itemId, ItemType defaultValue)
{
if (!_itemTypes.ContainsKey(itemId)) return defaultValue;
return _itemTypes[itemId];
}

public ItemType SafeGetItemType(int itemId)
{
return SafeGetItemType(itemId, null);
}

public static ItemManager LoadItems(Game game)
{
var result = new ItemManager(game);
var itemCollection = ItemCollection.LoadItemsFrom("Content/Data/LeagueSandbox-Default/Items");
foreach(var entry in itemCollection)
{
var itemType = ItemType.Load(game, result, entry.Value);
result._itemTypes.Add(entry.Key, itemType);
}
return result;
}
}

public class ItemType
{
private Game _game;
private ItemManager _owner;
private ItemCollectionEntry _itemInfo;
private StatMod[] _statMods;
private int _totalPrice;

// Meta
public int ItemId { get; private set; }
public string Name { get; private set; }

// General
public int MaxStack { get; private set; }
public int Price { get; private set; }
public string ItemGroup { get; private set; }
public float SellBackModifier { get; private set; }

// Stats
public float FlatPhysicalDamageMod { get; private set; }
public float PercentPhysicalDamageMod { get; private set; }
public float FlatMagicDamageMod { get; private set; }
public float FlatHPRegenMod { get; private set; }
public float FlatCritChanceMod { get; private set; }
public float FlatArmorMod { get; private set; }
public float FlatSpellBlockMod { get; private set; }
public float PercentAttackSpeedMod { get; private set; }
public float PercentLifeStealMod { get; private set; }
public float FlatHPPoolMod { get; private set; }
public float FlatMPPoolMod { get; private set; }
public float FlatMovementSpeedMod { get; private set; }

// Recipes
public int RecipeItem1 { get; private set; }
public int RecipeItem2 { get; private set; }
public int RecipeItem3 { get; private set; }
public int RecipeItem4 { get; private set; }

// Not from data
public ItemRecipe Recipe { get; private set; }
public int TotalPrice { get { return Recipe.TotalPrice; } }
public List<StatMod> StatMods { get { if (_statMods == null) CreateStatMods(); return _statMods.ToList(); } }

private ItemType(Game game, ItemManager owner, ItemCollectionEntry itemInfo)
{
_game = game;
_owner = owner;
_itemInfo = itemInfo;
_totalPrice = -1;
}

public void CreateStatMods()
{
_statMods = new StatMod[]
{
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Bonus_Ad_Flat, FlatPhysicalDamageMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Bonus_Ad_Pct, PercentPhysicalDamageMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Bonus_Ap_Flat, FlatMagicDamageMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Hp5, FlatHPRegenMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Crit_Chance, FlatCritChanceMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Armor, FlatArmorMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Magic_Armor, FlatSpellBlockMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_Atks_multiplier, PercentAttackSpeedMod),
StatMod.FromValues(MasterMask.MM_Two, FieldMask.FM2_LifeSteal, PercentLifeStealMod),
StatMod.FromValues(MasterMask.MM_Four, FieldMask.FM4_MaxHp, FlatHPPoolMod),
StatMod.FromValues(MasterMask.MM_Four, FieldMask.FM4_MaxMp, FlatMPPoolMod),
StatMod.FromValues(MasterMask.MM_Four, FieldMask.FM4_Speed, FlatMovementSpeedMod)
};
}

private void CreateRecipe()
{
Recipe = ItemRecipe.FromItemType(_game, this);
}

public static ItemType Load(Game game, ItemManager owner, ItemCollectionEntry itemInfo)
{
// Because IntelliSense is nice to have
var result = new ItemType(game, owner, itemInfo)
{
ItemId = itemInfo.ItemId,
Name = itemInfo.ItemName,
MaxStack = itemInfo.SafeGetInt("Data", "MaxStack"),
Price = itemInfo.SafeGetInt("Data", "Price"),
ItemGroup = itemInfo.SafeGetString("Data", "ItemGroup"),
SellBackModifier = itemInfo.SafeGetFloat("Data", "SellBackModifier", 0.7f),
FlatPhysicalDamageMod = itemInfo.SafeGetFloat("Data", "FlatPhysicalDamageMod"),
PercentPhysicalDamageMod = itemInfo.SafeGetFloat("Data", "PercentPhysicalDamageMod"),
FlatMagicDamageMod = itemInfo.SafeGetFloat("Data", "FlatMagicDamageMod"),
FlatHPRegenMod = itemInfo.SafeGetFloat("Data", "FlatHPRegenMod"),
FlatCritChanceMod = itemInfo.SafeGetFloat("Data", "FlatCritChanceMod"),
FlatArmorMod = itemInfo.SafeGetFloat("Data", "FlatArmorMod"),
FlatSpellBlockMod = itemInfo.SafeGetFloat("Data", "FlatSpellBlockMod"),
PercentAttackSpeedMod = itemInfo.SafeGetFloat("Data", "PercentAttackSpeedMod"),
PercentLifeStealMod = itemInfo.SafeGetFloat("Data", "PercentLifeStealMod"),
FlatHPPoolMod = itemInfo.SafeGetFloat("Data", "FlatHPPoolMod"),
FlatMPPoolMod = itemInfo.SafeGetFloat("Data", "FlatMPPoolMod"),
FlatMovementSpeedMod = itemInfo.SafeGetFloat("Data", "FlatMovementSpeedMod"),
RecipeItem1 = itemInfo.SafeGetInt("Data", "RecipeItem1", -1),
RecipeItem2 = itemInfo.SafeGetInt("Data", "RecipeItem2", -1),
RecipeItem3 = itemInfo.SafeGetInt("Data", "RecipeItem3", -1),
RecipeItem4 = itemInfo.SafeGetInt("Data", "RecipeItem4", -1)
};
result.CreateRecipe();
return result;
}

public bool GetIsTrinket()
{
return ItemGroup.ToLower() == "relicbase";
}
}

public class ItemRecipe
{
private Game _game;
private ItemType _owner;
private ItemType[] _items;
private int _totalPrice;

public List<ItemType> Items { get { if (_items == null) FindRecipeItems(); return _items.ToList(); } }
public int TotalPrice { get { if (_totalPrice < -1) FindPrice(); return _totalPrice; } }

private ItemRecipe(Game game, ItemType owner)
{
_game = game;
_owner = owner;
_totalPrice = -1;
}

private void FindRecipeItems()
{
_items = new ItemType[]
{
_game.ItemManager.SafeGetItemType(_owner.RecipeItem1),
_game.ItemManager.SafeGetItemType(_owner.RecipeItem2),
_game.ItemManager.SafeGetItemType(_owner.RecipeItem3),
_game.ItemManager.SafeGetItemType(_owner.RecipeItem4)
};
}

private void FindPrice()
{
_totalPrice = 0;
foreach (var item in Items)
{
_totalPrice += item.TotalPrice;
}
_totalPrice += _owner.Price;;
}

public static ItemRecipe FromItemType(Game game, ItemType type)
{
return new ItemRecipe(game, type);
}
}

public class Item
{
public byte Slot { get; private set; }
public byte StackSize { get; private set; }
public int TotalPrice { get; private set; }
public ItemType ItemType { get; private set; }

private Game _game;
private Inventory _owner;

private Item(Game game, Inventory owner, ItemType type, byte slot)
{
_game = game;
_owner = owner;
ItemType = type;
StackSize = 1;
Slot = slot;
}

public bool IncrementStackSize()
{
if (StackSize >= ItemType.MaxStack) return false;
StackSize++;
return true;
}

public bool DecrementStackSize()
{
if (StackSize <= 1) return false;
StackSize--;
return true;
}

internal static Item CreateFromType(Game _game, Inventory inventory, ItemType item, byte slot)
{
return new Item(_game, inventory, item, slot);
}
}
}