Add Inventory-Backend
This commit is contained in:
@@ -54,6 +54,7 @@ mp.events.add("showInventory", (invWeight, itemArr) => {
|
|||||||
show = true;
|
show = true;
|
||||||
mp.game.graphics.requestStreamedTextureDict("itemimages", true);
|
mp.game.graphics.requestStreamedTextureDict("itemimages", true);
|
||||||
} else {
|
} else {
|
||||||
|
mp.events.callRemote('saveInventory', items);
|
||||||
mp.gui.chat.activate(true);
|
mp.gui.chat.activate(true);
|
||||||
mp.gui.cursor.show(false, false);
|
mp.gui.cursor.show(false, false);
|
||||||
show = false;
|
show = false;
|
||||||
@@ -93,7 +94,7 @@ mp.events.add("render", () => {
|
|||||||
mp.game.graphics.set2dLayer(3);
|
mp.game.graphics.set2dLayer(3);
|
||||||
//SPRITES + ITEMANZAHL
|
//SPRITES + ITEMANZAHL
|
||||||
for (var currentItem = 0; currentItem < items.length; currentItem++) {
|
for (var currentItem = 0; currentItem < items.length; currentItem++) {
|
||||||
if (items[currentItem][4] !== "null") {
|
if (items[currentItem][4] !== "-1") {
|
||||||
mp.game.graphics.drawSprite("itemimages", items[currentItem][0].toLowerCase(), grid[parseInt(items[currentItem][4] - 1)][0], grid[parseInt(items[currentItem][4] - 1)][1], rWidth / 8, rWidth / 5, 0, 255, 255, 255, 255);
|
mp.game.graphics.drawSprite("itemimages", items[currentItem][0].toLowerCase(), grid[parseInt(items[currentItem][4] - 1)][0], grid[parseInt(items[currentItem][4] - 1)][1], rWidth / 8, rWidth / 5, 0, 255, 255, 255, 255);
|
||||||
mp.game.graphics.drawText("(~y~" + items[currentItem][3] + "~s~)", [grid[parseInt(items[currentItem][4] - 1)][0] + (sizeMul / 2) - 0.008, grid[parseInt(items[currentItem][4] - 1)][1] + 0.05], {
|
mp.game.graphics.drawText("(~y~" + items[currentItem][3] + "~s~)", [grid[parseInt(items[currentItem][4] - 1)][0] + (sizeMul / 2) - 0.008, grid[parseInt(items[currentItem][4] - 1)][1] + 0.05], {
|
||||||
font: 0,
|
font: 0,
|
||||||
@@ -252,7 +253,7 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world
|
|||||||
if (isMouseOverItem(x, y) && dragItem === null) {
|
if (isMouseOverItem(x, y) && dragItem === null) {
|
||||||
dragItem = hoverItem;
|
dragItem = hoverItem;
|
||||||
oldDragSlot = items[dragItem][4];
|
oldDragSlot = items[dragItem][4];
|
||||||
items[dragItem][4] = "null";
|
items[dragItem][4] = "-1";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +271,7 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world
|
|||||||
} else if (isItemOverSameItem(x, y)) {
|
} else if (isItemOverSameItem(x, y)) {
|
||||||
var newAmount = parseInt(items[stackToItem][3]) + parseInt(items[dragItem][3]);
|
var newAmount = parseInt(items[stackToItem][3]) + parseInt(items[dragItem][3]);
|
||||||
items[stackToItem][3] = newAmount.toString();
|
items[stackToItem][3] = newAmount.toString();
|
||||||
items.splice(dragItem, 1);
|
items[dragItem][4] = "-1";
|
||||||
dragItem = null;
|
dragItem = null;
|
||||||
hoverItem = null;
|
hoverItem = null;
|
||||||
} else {
|
} else {
|
||||||
@@ -290,7 +291,7 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world
|
|||||||
if (isMouseOverItem(x, y) && dragItem === null) {
|
if (isMouseOverItem(x, y) && dragItem === null) {
|
||||||
dragItem = hoverItem;
|
dragItem = hoverItem;
|
||||||
oldDragSlot = items[hoverItem][4];
|
oldDragSlot = items[hoverItem][4];
|
||||||
bufferItem = [items[hoverItem][0], items[hoverItem][1], items[hoverItem][2], items[hoverItem][3], items[hoverItem][4]]
|
bufferItem = [items[hoverItem][0], items[hoverItem][1], items[hoverItem][2], items[hoverItem][3], items[hoverItem][4], items[hoverItem][5]];
|
||||||
items[hoverItem][3]--;
|
items[hoverItem][3]--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,15 +304,15 @@ mp.events.add('click', (x, y, upOrDown, leftOrRight, relativeX, relativeY, world
|
|||||||
if (dragItem !== null) {
|
if (dragItem !== null) {
|
||||||
if (isMouseOverFreeSlot(x, y)) {
|
if (isMouseOverFreeSlot(x, y)) {
|
||||||
|
|
||||||
items.push([items[dragItem][0], items[dragItem][1], items[dragItem][2], "1", actFreeSlot.toString()]);
|
items.push([items[dragItem][0], items[dragItem][1], items[dragItem][2], "1", actFreeSlot.toString(), "-1"]);
|
||||||
if (items[hoverItem][3] === 0) {
|
if (items[hoverItem][3] === 0) {
|
||||||
items.splice(hoverItem, 1);
|
items[hoverItem][4] = "-1";
|
||||||
}
|
}
|
||||||
dragItem = null;
|
dragItem = null;
|
||||||
hoverItem = null;
|
hoverItem = null;
|
||||||
} else if (isItemOverSameItem(x, y)) {
|
} else if (isItemOverSameItem(x, y)) {
|
||||||
items[stackToItem][3]++;
|
items[stackToItem][3]++;
|
||||||
items.splice(hoverItem, 1);
|
items[hoverItem][4] = "-1";
|
||||||
bufferItem = null;
|
bufferItem = null;
|
||||||
dragItem = null;
|
dragItem = null;
|
||||||
hoverItem = null;
|
hoverItem = null;
|
||||||
|
|||||||
@@ -42,17 +42,19 @@ mp.keys.bind(0x45, false, function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
//I //Spielerliste
|
//I //Inventar
|
||||||
mp.keys.bind(0x49, false, function () {
|
mp.keys.bind(0x49, false, function () {
|
||||||
if (!chat) {
|
if (!chat) {
|
||||||
mp.events.callRemote("keyPress:I");
|
mp.events.callRemote("keyPress:I");
|
||||||
//mp.events.call("showPlayerlist");
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//J
|
//J //Spielerliste
|
||||||
mp.keys.bind(0x4A, false, function () {
|
mp.keys.bind(0x4A, false, function () {
|
||||||
mp.events.callRemote("keyPress:J");
|
if (!chat) {
|
||||||
|
mp.events.callRemote("keyPress:J");
|
||||||
|
//mp.events.call("showPlayerlist");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//K //Dienstkleidung
|
//K //Dienstkleidung
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
version https://git-lfs.github.com/spec/v1
|
version https://git-lfs.github.com/spec/v1
|
||||||
oid sha256:dba019dfe6643a9825df882fe92ac77605a85feffcff226d70ce5351f12b8180
|
oid sha256:dcf279344c11eb9eac1801e56e6fa11172e577ff415363338c5f38d2d1152b31
|
||||||
size 24064
|
size 33792
|
||||||
|
|||||||
@@ -109,6 +109,12 @@ namespace reallife_gamemode.Server.Events
|
|||||||
}
|
}
|
||||||
[RemoteEvent("keyPress:I")]
|
[RemoteEvent("keyPress:I")]
|
||||||
public void KeyPressI(Client player)
|
public void KeyPressI(Client player)
|
||||||
|
{
|
||||||
|
if (!player.IsLoggedIn()) return;
|
||||||
|
InventoryManager.GetUserItems(player);
|
||||||
|
}
|
||||||
|
[RemoteEvent("keyPress:J")]
|
||||||
|
public void KeyPressJ(Client player)
|
||||||
{
|
{
|
||||||
if (!player.IsLoggedIn()) return;
|
if (!player.IsLoggedIn()) return;
|
||||||
List<Client> players = NAPI.Pools.GetAllPlayers();
|
List<Client> players = NAPI.Pools.GetAllPlayers();
|
||||||
@@ -124,34 +130,7 @@ namespace reallife_gamemode.Server.Events
|
|||||||
ListPlayers.Add(lPlayer);
|
ListPlayers.Add(lPlayer);
|
||||||
}
|
}
|
||||||
player.TriggerEvent("fetchPlayerList", JsonConvert.SerializeObject(ListPlayers));
|
player.TriggerEvent("fetchPlayerList", JsonConvert.SerializeObject(ListPlayers));
|
||||||
}
|
}
|
||||||
[RemoteEvent("keyPress:J")]
|
|
||||||
public void KeyPressJ(Client player)
|
|
||||||
{
|
|
||||||
if (!player.IsLoggedIn()) return;
|
|
||||||
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 = InventoryManager.GetItemById(item.ItemId);
|
|
||||||
var currentItemWeight = iItem.Gewicht * item.Amount;
|
|
||||||
inventoryWeight += currentItemWeight;
|
|
||||||
|
|
||||||
items[userItems.IndexOf(item)] = new string[5];
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
player.TriggerEvent("showInventory", inventoryWeight, items);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
[RemoteEvent("keyPress:K")]
|
[RemoteEvent("keyPress:K")]
|
||||||
public void KeyPressK(Client player)
|
public void KeyPressK(Client player)
|
||||||
{
|
{
|
||||||
|
|||||||
23
Server/Inventory/Items/Holz.cs
Normal file
23
Server/Inventory/Items/Holz.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using reallife_gamemode.Server.Inventory.Interfaces;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @overview Life of German Reallife - Inventory Items Hamburger (Hamburger.cs)
|
||||||
|
* @author VegaZ
|
||||||
|
* @copyright (c) 2008 - 2018 Life of German
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace reallife_gamemode.Server.Inventory.Items
|
||||||
|
{
|
||||||
|
public class Holz : FoodItem, IItem
|
||||||
|
{
|
||||||
|
public int Id => 4;
|
||||||
|
public string Name => "Holz";
|
||||||
|
public string Description => "Ich und mein Holz.";
|
||||||
|
public int Gewicht => 1000;
|
||||||
|
public string Einheit => "g";
|
||||||
|
public override int HpAmount => 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
Server/Inventory/Items/Kraftstoff.cs
Normal file
23
Server/Inventory/Items/Kraftstoff.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using reallife_gamemode.Server.Inventory.Interfaces;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @overview Life of German Reallife - Inventory Items Hamburger (Hamburger.cs)
|
||||||
|
* @author VegaZ
|
||||||
|
* @copyright (c) 2008 - 2018 Life of German
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace reallife_gamemode.Server.Inventory.Items
|
||||||
|
{
|
||||||
|
public class Kraftstoff : FoodItem, IItem
|
||||||
|
{
|
||||||
|
public int Id => 5;
|
||||||
|
public string Name => "Kraftstoff";
|
||||||
|
public string Description => "Der Stoff gibt dir Kraft.";
|
||||||
|
public int Gewicht => 1000;
|
||||||
|
public string Einheit => "g";
|
||||||
|
public override int HpAmount => 20;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using reallife_gamemode.Model;
|
using reallife_gamemode.Model;
|
||||||
using reallife_gamemode.Server.Commands;
|
using reallife_gamemode.Server.Commands;
|
||||||
using reallife_gamemode.Server.Entities;
|
using reallife_gamemode.Server.Entities;
|
||||||
|
using reallife_gamemode.Server.Extensions;
|
||||||
using reallife_gamemode.Server.Inventory.Interfaces;
|
using reallife_gamemode.Server.Inventory.Interfaces;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -33,7 +34,7 @@ namespace reallife_gamemode.Server.Managers
|
|||||||
NAPI.Util.ConsoleOutput($"Loading Item {item.Name}");
|
NAPI.Util.ConsoleOutput($"Loading Item {item.Name}");
|
||||||
if (Activator.CreateInstance(item) is IItem o)
|
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}");
|
throw new InvalidOperationException($"Double ItemID found: {o.Id} | {o.Name}");
|
||||||
}
|
}
|
||||||
@@ -63,12 +64,82 @@ namespace reallife_gamemode.Server.Managers
|
|||||||
|
|
||||||
dbContext.SaveChanges();
|
dbContext.SaveChanges();
|
||||||
|
|
||||||
if(userItem.Amount == 0)
|
if (userItem.Amount == 0)
|
||||||
{
|
{
|
||||||
dbContext.Remove(userItem);
|
dbContext.Remove(userItem);
|
||||||
dbContext.SaveChanges();
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user