146 lines
5.1 KiB
C#
146 lines
5.1 KiB
C#
using GTANetworkAPI;
|
|
using reallife_gamemode.Model;
|
|
using reallife_gamemode.Server.Commands;
|
|
using reallife_gamemode.Server.Entities;
|
|
using reallife_gamemode.Server.Extensions;
|
|
using reallife_gamemode.Server.Inventory.Interfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
|
|
/**
|
|
* @overview Life of German Reallife - Managers InventoryManager (InventoryManager.cs)
|
|
* @author hydrant, VegaZ
|
|
* @copyright (c) 2008 - 2018 Life of German
|
|
*/
|
|
|
|
namespace reallife_gamemode.Server.Managers
|
|
{
|
|
public class InventoryManager : Script
|
|
{
|
|
public static List<IItem> itemList;
|
|
|
|
public static void LoadItems()
|
|
{
|
|
itemList = new List<IItem>();
|
|
|
|
Type[] allTypes = Assembly.GetExecutingAssembly().GetTypes();
|
|
foreach (Type item in allTypes)
|
|
{
|
|
if (item.GetInterfaces().Contains((typeof(IItem))))
|
|
{
|
|
NAPI.Util.ConsoleOutput($"Loading Item {item.Name}");
|
|
if (Activator.CreateInstance(item) is IItem o)
|
|
{
|
|
if (GetItemById(o.Id) != null)
|
|
{
|
|
throw new InvalidOperationException($"Double ItemID found: {o.Id} | {o.Name}");
|
|
}
|
|
itemList.Add(o);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IItem GetItemById(int id)
|
|
{
|
|
return itemList.Find(i => i.Id == id);
|
|
}
|
|
|
|
public static IItem GetItemByName(string name)
|
|
{
|
|
return itemList.Find(i => i.Name.ToLower() == name.ToLower());
|
|
}
|
|
|
|
public static void RemoveUserItem(Entities.User user, UserItem item)
|
|
{
|
|
using (var dbContext = new DatabaseContext())
|
|
{
|
|
var userItem = dbContext.UserItems.FirstOrDefault(i => i.Id == item.Id);
|
|
|
|
userItem.Amount--;
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
if (userItem.Amount == 0)
|
|
{
|
|
dbContext.Remove(userItem);
|
|
dbContext.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void GetUserItems(Client player)
|
|
{
|
|
var user = player.GetUser();
|
|
var inventoryWeight = 0;
|
|
|
|
using (var context = new DatabaseContext())
|
|
{
|
|
List<UserItem> userItems = context.UserItems.ToList().FindAll(i => i.UserId == user.Id);
|
|
string[][] items = new string[userItems.Count][];
|
|
foreach (var item in userItems)
|
|
{
|
|
IItem iItem = GetItemById(item.ItemId);
|
|
var currentItemWeight = iItem.Gewicht * item.Amount;
|
|
inventoryWeight += currentItemWeight;
|
|
|
|
items[userItems.IndexOf(item)] = new string[6];
|
|
items[userItems.IndexOf(item)][0] = iItem.Name;
|
|
items[userItems.IndexOf(item)][1] = iItem.Description;
|
|
items[userItems.IndexOf(item)][2] = iItem.Gewicht.ToString();
|
|
items[userItems.IndexOf(item)][3] = item.Amount.ToString();
|
|
items[userItems.IndexOf(item)][4] = item.Slot.ToString();
|
|
items[userItems.IndexOf(item)][5] = item.Id.ToString();
|
|
}
|
|
player.TriggerEvent("showInventory", inventoryWeight, items);
|
|
}
|
|
}
|
|
|
|
[RemoteEvent("saveInventory")]
|
|
public void SavePlayerInteroy(Client player, string[][] itemArray)
|
|
{
|
|
var user = player.GetUser();
|
|
string[][] items = new string[itemArray.Length][];
|
|
|
|
List<UserItem> userItems = new List<UserItem>();
|
|
|
|
using (var context = new DatabaseContext())
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
UserItem cItem = new UserItem
|
|
{
|
|
Amount = int.Parse(item[3]),
|
|
ItemId = GetItemByName(item[0]).Id,
|
|
UserId = user.Id,
|
|
Slot = int.Parse(item[4]),
|
|
};
|
|
|
|
if (item[5] == "-1")
|
|
{
|
|
player.SendChatMessage("Item hinzugefügt");
|
|
context.Add(cItem);
|
|
}
|
|
else if(cItem.Slot == -1)
|
|
{
|
|
player.SendChatMessage("Item gelöscht");
|
|
UserItem fItem = context.UserItems.FirstOrDefault(i => i.Id == int.Parse(item[5]));
|
|
userItems.Remove(fItem);
|
|
}
|
|
else
|
|
{
|
|
player.SendChatMessage("Item geändert");
|
|
UserItem fItem = context.UserItems.FirstOrDefault(i => i.Id == int.Parse(item[5]));
|
|
fItem.Amount = cItem.Amount;
|
|
fItem.Slot = cItem.Slot;
|
|
}
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|