Business kaufen

This commit is contained in:
hydrant
2019-09-11 22:18:37 +02:00
parent fe90891801
commit 146261bc03
9 changed files with 1618 additions and 105 deletions

View File

@@ -18,78 +18,64 @@ export default function business(globalData: GlobalData) {
var closeMenu = false;
var businessName;
var businessMoney;
var mainMenu;
var bankMenu;
var mainMenu: NativeUI.Menu;
mp.events.add('business_showHelp', (bizName, bizMoney) => {
var businessData: any;
var businessState: number;
mp.events.add('SERVER:Business_ShowMenuHelp', (bizName, bizMoney) => {
mp.game.ui.setTextComponentFormat('STRING');
mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um dein Business zu verwalten');
mp.game.ui.addTextComponentSubstringPlayerName('Drücke ~INPUT_CONTEXT~, um das Businessmenü zu öffnen');
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.events.add('SERVER:Business_SetData', (data, state: number) => {
businessData = JSON.parse(data);
businessState = state;
});
mp.events.add('SERVER: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();
}
if (mainMenu) mainMenu.Close();
mp.keys.unbind(0x45, false, keyPressHandler);
keyBound = false;
}
});
mp.events.add('business_updateMoney', (newMoney) => {
businessMoney = newMoney;
});
function keyPressHandler() {
mp.events.call('business_removeHelp', false);
mp.gui.chat.show(false);
mp.events.call('SERVER:Business_RemoveHelp', false);
if (globalData.InChat || globalData.InInput || globalData.InMenu) return;
if (typeof mainMenu !== "undefined" && mainMenu.Visible) {
return;
}
if (typeof bankMenu !== "undefined" && bankMenu.Visible) {
return;
}
globalData.InMenu = true;
mainMenu = new Menu("Businessverwaltung", businessName, new Point(50, 50), null, null);
mainMenu = new Menu("Businessverwaltung", businessData.Name, new Point(50, 50), null, null);
var bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse");
bankAccountItem.SetRightLabel("~h~" + businessMoney);
mainMenu.AddItem(bankAccountItem);
var bankAccountItem: NativeUI.UIMenuItem;
var buyItem: NativeUI.UIMenuItem;
//var partnerItem = new UIMenuItem("Inteilhaber", "Verwalte den Inteilhaber");
//partnerItem.SetRightLabel("Niemand");
//mainMenu.AddItem(partnerItem);
mainMenu.Open();
if (businessState == 1) { // Besitzer
bankAccountItem = new UIMenuItem("Businesskasse", "Verwalte die Businesskasse");
bankAccountItem.SetRightLabel("~h~" + businessData.Balance);
mainMenu.AddItem(bankAccountItem);
mainMenu.ItemSelect.on((item, index) => {
if (item === bankAccountItem) {
// manage bank account
bankMenu = new Menu("Bankkonto", businessName, new Point(50, 50), null, null);
var bankMenu = new Menu("Bankkonto", businessData.Name, new Point(50, 50), null, null);
bankMenu.Visible = false;
var infoItem = new UIMenuItem("Aktueller Kontostand");
infoItem.SetRightLabel("~h~" + businessMoney);
infoItem.SetRightLabel("~h~" + businessData.Balance);
bankMenu.AddItem(infoItem);
var depositItem = new UIMenuItem("Einzahlen", "Zahle Geld auf die Businesskasse ein");
@@ -110,6 +96,8 @@ export default function business(globalData: GlobalData) {
}
mp.events.callRemote('Business_DepositMoney', amount);
mainMenu.Close();
});
} else if (item === withdrawItem) {
var withdrawInput = new InputHelper("Wie viel Geld möchtest du von deiner Businesskasse abheben?", globalData);
@@ -122,29 +110,31 @@ export default function business(globalData: GlobalData) {
}
mp.events.callRemote('Business_WithdrawMoney', amount);
mainMenu.Close();
});
}
});
bankMenu.MenuClose.on(() => {
if (closeMenu) {
closeMenu = false;
return;
mainMenu.BindMenuToItem(bankMenu, bankAccountItem);
} else if (businessState == 0) { // Kein Besitzer
buyItem = new UIMenuItem("Kaufen", businessData.Price);
mainMenu.AddItem(buyItem);
} else if (businessState == -1) {
mp.game.graphics.notify("Hier gibt es nichts zu sehen");
}
mainMenu.ItemSelect.on((item, index) => {
if (item === buyItem) {
mainMenu.Close();
mp.events.callRemote("CLIENT:Business_BuyBusiness");
}
mainMenu.Visible = true;
});
bankMenu.Visible = true;
mainMenu.Visible = false;
}
//else if (item === partnerItem) {
// // manage partner
//}
});
mainMenu.Open();
mainMenu.MenuClose.on(() => {
mp.events.call('business_removeHelp', false);
mp.events.call('SERVER:Business_RemoveHelp', false);
globalData.InMenu = false;
});
}

View File

@@ -1,4 +1,5 @@
using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;
@@ -65,26 +66,86 @@ namespace ReallifeGamemode.Server.Business
dbContext.SaveChanges();
}
}
BusinessData data = GetData();
if(data == null)
{
using (var dbContext = new DatabaseContext())
{
data = new BusinessData()
{
BusinessId = this.Id,
Price = -1
};
dbContext.BusinessData.Add(data);
dbContext.SaveChanges();
}
}
}
private void EntityExitBusinessColShape(ColShape colShape, Client client)
{
client.TriggerEvent("business_removeHelp", true);
client.TriggerEvent("SERVER:Business_RemoveHelp", true);
}
public BusinessData GetData(DatabaseContext dbContext = null)
{
if(dbContext == null)
{
using (dbContext = new DatabaseContext())
{
return dbContext.BusinessData.Where(b => b.BusinessId == this.Id).FirstOrDefault();
}
}
else
{
return dbContext.BusinessData.Where(b => b.BusinessId == this.Id).FirstOrDefault();
}
}
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());
if (client.IsInVehicle || !client.IsLoggedIn()) return;
client.TriggerEvent("SERVER:Business_ShowMenuHelp");
SendBusinessDataToClient(client);
}
public void SendBusinessDataToClient(Client player)
{
User owner = GetOwner();
int state = -1; // Keine Beziehung
if (owner == null) // Kein Besitzer
{
state = 0;
}
else if (owner.Id == player.GetUser()?.Id) // Besitzer
{
state = 1;
}
var business = new
{
this.Name,
Price = this.GetData().Price.ToMoneyString(),
Balance = this.GetBankAccount().Balance.ToMoneyString()
};
player.TriggerEvent("SERVER:Business_SetData", JsonConvert.SerializeObject(business), state);
}
public void Update(int? money = null)
{
if (money == null) money = GetBankAccount()?.Balance ?? 0;
User owner = GetOwner();
string infoText = Name + "\n" + "Besitzer: " + (owner == null ? "Niemand" : owner.Name) + "\nKasse: ~g~" + money.ToMoneyString();
string infoText = Name;
if (owner == null) infoText += "\n~g~Zum Verkauf\n~s~Preis: ~y~" + this.GetData().Price.ToMoneyString();
else infoText += $"\nBesitzer: ~g~{owner.Name}\n~s~Kasse: ~y~{money.ToMoneyString()}";
_informationLabel.Text = infoText;
}

View File

@@ -2871,6 +2871,47 @@ namespace ReallifeGamemode.Server.Commands
}
}
[Command("business", "~m~Benutzung:~s~ /business [price] [Option]")]
public void CmdAdminBusiness(Client player, string option, string option1)
{
if (!player.GetUser()?.IsAdmin(AdminLevel.HEADADMIN) ?? true)
{
ChatService.NotAuthorized(player);
return;
}
BusinessBase business = BusinessManager.GetNearBusiness(player);
if(business == null)
{
ChatService.ErrorMessage(player, "In deiner Nähe ist kein Business");
return;
}
switch (option.ToLower())
{
case "price":
if(!int.TryParse(option1, out int price))
{
ChatService.ErrorMessage(player, "Es muss ein gültiger Preis angegeben werden");
return;
}
using (var dbContext = new DatabaseContext())
{
BusinessData data = business.GetData(dbContext);
data.Price = price;
dbContext.SaveChanges();
player.SendChatMessage($"Der Preis wurde auf {price.ToMoneyString()} gesetzt");
business.Update();
return;
}
break;
}
player.SendChatMessage("~m~Benutzung:~s~ /business [price] [Option]");
}
#endregion

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace ReallifeGamemode.Server.Entities
{
public class BusinessData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int BusinessId { get; set; }
public int Price { get; set; }
}
}

View File

@@ -4,6 +4,7 @@ using ReallifeGamemode.Server.Business;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Util;
using System;
using System.Collections.Generic;
@@ -48,6 +49,11 @@ namespace ReallifeGamemode.Server.Managers
return Businesses.Find(b => b.Id == id);
}
public static BusinessBase GetNearBusiness(Client player)
{
return Businesses.Where(b => b.Position.DistanceTo(player.Position) < 5f).FirstOrDefault();
}
[RemoteEvent("Business_DepositMoney")]
public void BusinessDepositMoney(Client player, int amount)
{
@@ -61,7 +67,7 @@ namespace ReallifeGamemode.Server.Managers
TransactionResult result = BankManager.TransferMoney(user, playerBusiness, amount, "Überweisung");
/*if(result == TransactionResult.NEGATIVE_MONEY_SENT)
if (result == TransactionResult.NEGATIVE_MONEY_SENT)
{
player.SendNotification("~r~Es können nur positive Beträge überwiesen werden");
return;
@@ -71,10 +77,9 @@ namespace ReallifeGamemode.Server.Managers
player.SendNotification("~r~Du hast nicht genug Geld");
return;
}
else */
if (result == TransactionResult.SUCCESS)
else if (result == TransactionResult.SUCCESS)
{
player.TriggerEvent("business_updateMoney", playerBusiness.GetBankAccount().Balance.ToMoneyString());
playerBusiness.SendBusinessDataToClient(player);
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
return;
}
@@ -105,7 +110,7 @@ namespace ReallifeGamemode.Server.Managers
}
else if (result == TransactionResult.SUCCESS)
{
player.TriggerEvent("business_updateMoney", playerBusiness.GetBankAccount().Balance.ToMoneyString());
playerBusiness.SendBusinessDataToClient(player);
player.SendNotification("~g~Du hast erfolgreich ~s~" + amount.ToMoneyString() + " ~g~ überwiesen");
return;
}
@@ -209,5 +214,46 @@ namespace ReallifeGamemode.Server.Managers
newVeh.Spawn();
}
[RemoteEvent("CLIENT:Business_BuyBusiness")]
public void BusinessEventBuyBusiness(Client player)
{
BusinessBase business = GetNearBusiness(player);
if (business == null) return;
if (business.GetOwner() != null)
{
ChatService.ErrorMessage(player, "Dieses Business hat einen Besitzer");
return;
}
using (var dbContext = new DatabaseContext())
{
int price = business.GetData().Price;
User user = player.GetUser(dbContext);
if(user.BusinessId != null)
{
ChatService.ErrorMessage(player, "Du kannst nicht mehrere Businesse besitzen");
}
IBankAccount bankAccount = user.GetBankAccount(dbContext);
if (price > bankAccount.Balance)
{
ChatService.ErrorMessage(player, "Du hast nicht genung Geld");
return;
}
user.BusinessId = business.Id;
bankAccount.Balance -= price;
player.SendChatMessage("Business gekauft");
dbContext.SaveChanges();
business.Update();
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ReallifeGamemode.Migrations
{
public partial class BusinessData : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BusinessData",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
BusinessId = table.Column<int>(nullable: false),
Price = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BusinessData", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BusinessData");
}
}
}

View File

@@ -112,6 +112,20 @@ namespace ReallifeGamemode.Migrations
b.ToTable("BusinessBankAccounts");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.BusinessData", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("BusinessId");
b.Property<int>("Price");
b.HasKey("Id");
b.ToTable("BusinessData");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.Character", b =>
{
b.Property<int>("Id")

View File

@@ -79,6 +79,7 @@ namespace ReallifeGamemode.Server.Models
// Business
public DbSet<Entities.BusinessBankAccount> BusinessBankAccounts { get; set; }
public DbSet<Entities.BusinessData> BusinessData { get; set; }
// Control Panel
public DbSet<Entities.News> News { get; set; }