Add Inventory-Backend

This commit is contained in:
VegaZ
2018-12-09 22:44:52 +01:00
parent 47694ea343
commit f783a889c7
7 changed files with 143 additions and 44 deletions

View File

@@ -2,6 +2,7 @@
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;
@@ -33,7 +34,7 @@ namespace reallife_gamemode.Server.Managers
NAPI.Util.ConsoleOutput($"Loading Item {item.Name}");
if (Activator.CreateInstance(item) is IItem o)
{
if(GetItemById(o.Id) != null)
if (GetItemById(o.Id) != null)
{
throw new InvalidOperationException($"Double ItemID found: {o.Id} | {o.Name}");
}
@@ -63,12 +64,82 @@ namespace reallife_gamemode.Server.Managers
dbContext.SaveChanges();
if(userItem.Amount == 0)
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();
}
}
}
}