Start business management from menu

This commit is contained in:
hydrant
2018-11-25 20:10:39 +01:00
parent 054e9159e1
commit d30930a949
8 changed files with 205 additions and 20 deletions

109
Client/Business/main.js Normal file
View File

@@ -0,0 +1,109 @@
var keyBound = false;
var closeMenu = false;
var businessName;
var businessMoney;
var mainMenu;
var bankMenu;
const NativeUI = require("nativeui");
const Menu = NativeUI.Menu;
const UIMenuItem = NativeUI.UIMenuItem;
const UIMenuListItem = NativeUI.UIMenuListItem;
const UIMenuCheckboxItem = NativeUI.UIMenuCheckboxItem;
const UIMenuSliderItem = NativeUI.UIMenuSliderItem;
const BadgeStyle = NativeUI.BadgeStyle;
const Point = NativeUI.Point;
const ItemsCollection = NativeUI.ItemsCollection;
const Color = NativeUI.Color;
const ListItem = NativeUI.ListItem;
mp.events.add('business_showHelp', (bizName, bizMoney) => {
mp.game.ui.setTextComponentFormat('STRING');
mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um dein Business zu verwalten');
mp.game.ui.displayHelpTextFromStringLabel(0, true, true, -1);
businessName = bizName;
businessMoney = bizMoney;
mp.keys.bind(0x45, false, keyPressHandler);
keyBound = true;
});
mp.events.add('business_removeHelp', (unbind) => {
mp.game.ui.clearHelp(true);
mp.gui.chat.show(true);
if (keyBound && unbind) {
if (typeof mainMenu !== "undefined") mainMenu.Close();
if (typeof bankMenu !== "undefined") {
closeMenu = true;
bankMenu.Close();
}
mp.keys.unbind(0x45, false, keyPressHandler);
keyBound = false;
}
});
function keyPressHandler() {
mp.events.call('business_removeHelp', false);
mp.gui.chat.show(false);
if (typeof mainMenu !== "undefined" && mainMenu.Visible) {
return;
}
if (typeof bankMenu !== "undefined" && bankMenu.Visible) {
return;
}
mainMenu = new Menu("Businessverwaltung", businessName, new Point(50, 50));
var bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse");
bankAccountItem.SetRightLabel("~g~~h~" + businessMoney);
mainMenu.AddItem(bankAccountItem);
var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber");
partnerItem.SetRightLabel("Niemand");
mainMenu.AddItem(partnerItem);
mainMenu.Open();
mainMenu.ItemSelect.on((item, index) => {
if (item === bankAccountItem) {
// manage bank account
bankMenu = new Menu("Bankkonto", businessName, new Point(50, 50));
var infoItem = new UIMenuItem("Aktueller Kontostand");
infoItem.SetRightLabel("~g~~h~" + businessMoney);
bankMenu.AddItem(infoItem);
var depositItem = new UIMenuItem("Einzahlen", "Zahle Geld auf die Businesskasse ein");
bankMenu.AddItem(depositItem);
var withdrawItem = new UIMenuItem("Auszahlen", "Zahle Geld von der Businesskasse aus");
bankMenu.AddItem(withdrawItem);
bankMenu.MenuClose.on(() => {
if (closeMenu) {
closeMenu = false;
return;
}
mainMenu.Visible = true;
});
bankMenu.Visible = true;
mainMenu.Visible = false;
} else if (item === partnerItem) {
// manage partner
}
});
mainMenu.MenuClose.on(() => {
mp.events.call('business_removeHelp', false);
});
}

View File

@@ -29,3 +29,5 @@ require('./Save/main.js');
require('./Speedometer/index.js'); require('./Speedometer/index.js');
require('./Tuning/main.js'); require('./Tuning/main.js');
require('./Business/main.js');

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Globalization;
using System.Linq; using System.Linq;
using GTANetworkAPI; using GTANetworkAPI;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -20,6 +21,8 @@ namespace reallife_gamemode
public static readonly Vector3 DEFAULT_SPAWN_POSITION = new Vector3(-427.5189, 1116.453, 326.7829); public static readonly Vector3 DEFAULT_SPAWN_POSITION = new Vector3(-427.5189, 1116.453, 326.7829);
public static readonly float DEFAULT_SPAWN_HEADING = 340.8f; public static readonly float DEFAULT_SPAWN_HEADING = 340.8f;
public static readonly CultureInfo SERVER_CULTURE = new CultureInfo("de-DE");
[ServerEvent(Event.ResourceStart)] [ServerEvent(Event.ResourceStart)]
public void OnResourceStart() public void OnResourceStart()
{ {

View File

@@ -1,6 +1,7 @@
using GTANetworkAPI; using GTANetworkAPI;
using reallife_gamemode.Model; using reallife_gamemode.Model;
using reallife_gamemode.Server.Entities; using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Util; using reallife_gamemode.Server.Util;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -12,6 +13,8 @@ namespace reallife_gamemode.Server.Business
public abstract class BusinessBase : IBankAccountOwner public abstract class BusinessBase : IBankAccountOwner
{ {
private TextLabel _informationLabel; private TextLabel _informationLabel;
private Marker _marker;
private ColShape _colShape;
public abstract int Id { get; } public abstract int Id { get; }
public abstract string Name { get; } public abstract string Name { get; }
@@ -35,7 +38,12 @@ namespace reallife_gamemode.Server.Business
public void Setup() public void Setup()
{ {
_informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position, 20.0f, 1.3f, 0, new Color(255, 255, 255)); _informationLabel = NAPI.TextLabel.CreateTextLabel(Name, Position.Add(new Vector3(0, 0, 0.5)), 20.0f, 1.3f, 0, new Color(255, 255, 255));
_marker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, Position.Subtract(new Vector3(0, 0, 1.5)), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255), true);
_colShape = NAPI.ColShape.CreateSphereColShape(Position.Subtract(new Vector3(0, 0, 1.5)), 3f);
_colShape.OnEntityEnterColShape += EntityEnterBusinessColShape;
_colShape.OnEntityExitColShape += EntityExitBusinessColShape;
if (GetBankAccount() == null) if (GetBankAccount() == null)
{ {
@@ -53,10 +61,24 @@ namespace reallife_gamemode.Server.Business
} }
} }
public void Update() private void EntityExitBusinessColShape(ColShape colShape, Client client)
{ {
client.TriggerEvent("business_removeHelp", true);
}
private void EntityEnterBusinessColShape(ColShape colShape, Client client)
{
if (GetOwner() != null && GetOwner().Id == client.GetUser()?.Id && !client.IsInVehicle)
{
client.TriggerEvent("business_showHelp", Name, (GetBankAccount()?.Balance ?? 0).ToMoneyString());
}
}
public void Update(int? money = null)
{
if (money == null) money = GetBankAccount()?.Balance ?? 0;
User owner = GetOwner(); User owner = GetOwner();
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + (GetBankAccount()?.Balance ?? 0) + "$"; string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + money.ToMoneyString();
_informationLabel.Text = infoText; _informationLabel.Text = infoText;
} }

View File

@@ -22,7 +22,7 @@ namespace reallife_gamemode.Server.Entities
set set
{ {
_balance = value; _balance = value;
BusinessManager.GetBusiness(BusinessId).Update(); BusinessManager.GetBusiness(BusinessId).Update(value);
} }
} }

View File

@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace reallife_gamemode.Server.Extensions
{
public static class IntegerExtension
{
public static string ToMoneyString(this int? money)
{
return string.Format(Main.SERVER_CULTURE, "{0:C0}", money ?? 0);
}
public static string ToMoneyString(this int money)
{
return string.Format(Main.SERVER_CULTURE, "{0:C0}", money);
}
}
}

View File

@@ -18,28 +18,62 @@ namespace reallife_gamemode.Server.Managers
{ {
public class BankManager public class BankManager
{ {
public static TransactionResult SetMoney(Client admin, IBankAccountOwner owner, int amount, string reason = "Von Admin gesetzt")
{
using (var transferMoney = new Model.DatabaseContext())
{
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
IBankAccount account = owner.GetBankAccount(transferMoney);
if (account == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
var transactionLog = new Logs.BankAccountTransactionHistory
{
Sender = "ADMIN: " + admin.Name,
SenderBalance = 0,
Receiver = owner.Name,
ReceiverBalance = amount,
NewReceiverBalance = amount,
NewSenderBalance = 0,
MoneySent = amount,
Fee = 0,
Origin = reason
};
// add log
transferMoney.BankAccountTransactionLogs.Add(transactionLog);
account.Balance += amount;
transferMoney.SaveChanges();
return TransactionResult.SUCCESS;
}
}
public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin) public static TransactionResult TransferMoney(IBankAccountOwner sender, IBankAccountOwner receiver, int amount, string origin)
{ {
using (var transferMoney = new Model.DatabaseContext()) using (var transferMoney = new Model.DatabaseContext())
{ {
if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT; if (amount < 0) return TransactionResult.NEGATIVE_MONEY_SENT;
IBankAccount senderAccount = sender?.GetBankAccount(transferMoney) ?? null; IBankAccount senderAccount = sender.GetBankAccount(transferMoney);
IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney); IBankAccount receiverAccount = receiver.GetBankAccount(transferMoney);
if (senderAccount == null && sender != null) return TransactionResult.SENDER_NO_BANKACCOUNT; if (senderAccount == null) return TransactionResult.SENDER_NO_BANKACCOUNT;
if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT; if (receiverAccount == null) return TransactionResult.RECEIVER_NO_BANKACCOUNT;
if ((senderAccount?.Balance ?? amount) < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY; if (senderAccount.Balance < amount) return TransactionResult.SENDER_NOT_ENOUGH_MONEY;
var transactionLog = new Logs.BankAccountTransactionHistory var transactionLog = new Logs.BankAccountTransactionHistory
{ {
Sender = sender?.Name ?? "Admin", Sender = sender.Name,
SenderBalance = senderAccount?.Balance ?? 0, SenderBalance = senderAccount.Balance,
Receiver = receiver.Name, Receiver = receiver.Name,
ReceiverBalance = receiverAccount.Balance, ReceiverBalance = receiverAccount.Balance,
NewReceiverBalance = receiverAccount.Balance + amount, NewReceiverBalance = receiverAccount.Balance + amount,
NewSenderBalance = (senderAccount?.Balance ?? amount) - amount, NewSenderBalance = senderAccount.Balance - amount,
MoneySent = amount, MoneySent = amount,
Fee = 0, Fee = 0,
Origin = origin Origin = origin
@@ -48,13 +82,11 @@ namespace reallife_gamemode.Server.Managers
// add log // add log
transferMoney.BankAccountTransactionLogs.Add(transactionLog); transferMoney.BankAccountTransactionLogs.Add(transactionLog);
if(senderAccount != null) senderAccount.Balance -= amount; senderAccount.Balance -= amount;
receiverAccount.Balance += amount; receiverAccount.Balance += amount;
transferMoney.SaveChanges(); transferMoney.SaveChanges();
if (receiver is BusinessBase business) business.Update();
return TransactionResult.SUCCESS; return TransactionResult.SUCCESS;
} }
} }

View File

@@ -10,12 +10,11 @@ namespace reallife_gamemode.Server.Managers
{ {
class BusinessManager : Script class BusinessManager : Script
{ {
private static List<BusinessBase> businesses; public static List<BusinessBase> Businesses { get; private set; }
public static List<BusinessBase> Businesses { get => businesses; }
public static void LoadBusinesses() public static void LoadBusinesses()
{ {
businesses = new List<BusinessBase>(); Businesses = new List<BusinessBase>();
IEnumerable<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(BusinessBase))); IEnumerable<Type> allTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(BusinessBase)));
foreach (Type item in allTypes) foreach (Type item in allTypes)
@@ -23,11 +22,11 @@ namespace reallife_gamemode.Server.Managers
NAPI.Util.ConsoleOutput($"Loading Business {item.Name}"); NAPI.Util.ConsoleOutput($"Loading Business {item.Name}");
if (Activator.CreateInstance(item) is BusinessBase o) if (Activator.CreateInstance(item) is BusinessBase o)
{ {
if (businesses.Any(x => x.Id == o.Id)) if (Businesses.Any(x => x.Id == o.Id))
{ {
throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}"); throw new InvalidOperationException($"Double Business ID found: {o.Id} | {o.Name}");
} }
businesses.Add(o); Businesses.Add(o);
o.Setup(); o.Setup();
o.Load(); o.Load();
o.Update(); o.Update();
@@ -37,12 +36,12 @@ namespace reallife_gamemode.Server.Managers
public static T GetBusiness<T>() where T : BusinessBase public static T GetBusiness<T>() where T : BusinessBase
{ {
return (T)businesses.Find(b => b.GetType() == typeof(T)); return (T)Businesses.Find(b => b.GetType() == typeof(T));
} }
public static BusinessBase GetBusiness(int? id) public static BusinessBase GetBusiness(int? id)
{ {
return businesses.Find(b => b.Id == id); return Businesses.Find(b => b.Id == id);
} }
} }
} }