forked from Zamirathe/Uconomy_Essentials
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CommandExchange.cs
114 lines (99 loc) · 4.61 KB
/
CommandExchange.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.Collections.Generic;
using fr34kyn01535.Uconomy;
using Rocket.API;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
namespace ZaupUconomyEssentials
{
public class CommandExchange : IRocketCommand
{
public AllowedCaller AllowedCaller => AllowedCaller.Player;
public string Name => "exchange";
public string Help => "Exchanges experience for economy currency.";
public string Syntax => "<amount> [money]";
public List<string> Aliases => new List<string>();
public List<string> Permissions => new List<string>();
public void Execute(IRocketPlayer caller, string[] amt)
{
var playerid = (UnturnedPlayer) caller;
string message;
if (amt.Length == 0)
{
message = UconomyEssentials.Instance.Translate("exchange_usage_msg");
UnturnedChat.Say(playerid, message);
return;
}
if (amt.Length > 2)
{
message = UconomyEssentials.Instance.Translate("exchange_usage_msg");
UnturnedChat.Say(playerid, message);
return;
}
if (!UconomyEssentials.Instance.Configuration.Instance.ExpExchange && amt.Length == 1)
{
message = UconomyEssentials.Instance.Translate("experience_exchange_not_available");
UnturnedChat.Say(playerid, message);
return;
}
if (!UconomyEssentials.Instance.Configuration.Instance.MoneyExchange && amt.Length == 2)
{
message = UconomyEssentials.Instance.Translate("money_exchange_not_available");
UnturnedChat.Say(playerid, message);
return;
}
// Get expereience balance first
var exp = playerid.Experience;
uint.TryParse(amt[0], out var examt);
if (examt <= 0)
{
message = UconomyEssentials.Instance.Translate("exchange_zero_amount_error");
UnturnedChat.Say(playerid, message);
return;
}
if (exp < examt && amt.Length == 1)
{
message = UconomyEssentials.Instance.Translate("exchange_insufficient_experience", examt.ToString());
UnturnedChat.Say(playerid, message);
return;
}
// Get balance first
var bal = Uconomy.Instance.Database.GetBalance(playerid.CSteamID.ToString());
if (amt.Length > 1 && bal < examt)
{
message = UconomyEssentials.Instance.Translate("exchange_insufficient_money", examt.ToString(),
Uconomy.Instance.Configuration.Instance.MoneyName);
UnturnedChat.Say(playerid, message);
return;
}
switch (amt.Length)
{
case 1:
var gain = (decimal) (examt *
UconomyEssentials.Instance.Configuration.Instance.ExpExchangerate);
// Just to make sure to avoid any errors
gain = decimal.Round(gain, 2);
var newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), gain);
message = UconomyEssentials.Instance.Translate("new_balance_msg", newbal,
Uconomy.Instance.Configuration.Instance.MoneyName);
UnturnedChat.Say(playerid, message);
playerid.Experience -= examt;
UconomyEssentials.HandleEvent(playerid, gain, "exchange", examt);
break;
case 2:
var gainm = (uint) (examt *
UconomyEssentials.Instance.Configuration.Instance.MoneyExchangerate);
// Just to make sure to avoid any errors
newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), examt * -1.0m);
message = UconomyEssentials.Instance.Translate("new_balance_msg", newbal,
Uconomy.Instance.Configuration.Instance.MoneyName);
UnturnedChat.Say(playerid, message);
playerid.Experience += gainm;
UconomyEssentials.HandleEvent(playerid, gainm, "exchange", examt, "money");
break;
}
playerid.Player.channel.send("tellExperience", ESteamCall.OWNER, ESteamPacket.UPDATE_RELIABLE_BUFFER,
playerid.Experience);
}
}
}