-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCommandExchange.cs
134 lines (131 loc) · 5.22 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using Rocket.API;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Commands;
using Rocket.Unturned.Player;
using fr34kyn01535.Uconomy;
using SDG.Unturned;
using Steamworks;
namespace Uconomy_Essentials
{
public class CommandExchange : IRocketCommand
{
public AllowedCaller AllowedCaller
{
get
{
return AllowedCaller.Player;
}
}
public string Name
{
get
{
return "exchange";
}
}
public string Help
{
get
{
return "Exchanges experience for economy currency.";
}
}
public string Syntax
{
get
{
return "<amount> [money]";
}
}
public List<string> Aliases
{
get { return new List<string>(); }
}
public List<string> Permissions
{
get { return new List<string>(); }
}
public void Execute(IRocketPlayer caller, string[] amt)
{
UnturnedPlayer playerid = (UnturnedPlayer)caller;
string message;
if (amt.Length == 0)
{
message = Uconomy_Essentials.Instance.Translate("exchange_usage_msg", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
if (amt.Length > 2)
{
message = Uconomy_Essentials.Instance.Translate("exchange_usage_msg", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
if (!Uconomy_Essentials.Instance.Configuration.Instance.ExpExchange && amt.Length == 1)
{
message = Uconomy_Essentials.Instance.Translate("experience_exchange_not_available", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
if (!Uconomy_Essentials.Instance.Configuration.Instance.MoneyExchange && amt.Length == 2)
{
message = Uconomy_Essentials.Instance.Translate("money_exchange_not_available", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
// Get expereience balance first
uint exp = playerid.Experience;
uint examt = 0;
UInt32.TryParse(amt[0], out examt);
if (examt <= 0)
{
message = Uconomy_Essentials.Instance.Translate("exchange_zero_amount_error", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
if (exp < examt && amt.Length == 1)
{
message = Uconomy_Essentials.Instance.Translate("exchange_insufficient_experience", new object[] { examt.ToString() });
UnturnedChat.Say(playerid, message);
return;
}
// Get balance first
decimal bal = Uconomy.Instance.Database.GetBalance(playerid.CSteamID.ToString());
if (amt.Length > 1 && bal < examt)
{
message = Uconomy_Essentials.Instance.Translate("exchange_insufficient_money", new object[] { examt.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName });
UnturnedChat.Say(playerid, message);
return;
}
switch (amt.Length)
{
case 1:
decimal gain = (decimal)((float)examt * Uconomy_Essentials.Instance.Configuration.Instance.ExpExchangerate);
// Just to make sure to avoid any errors
gain = Decimal.Round(gain, 2);
decimal newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), gain);
message = Uconomy_Essentials.Instance.Translate("new_balance_msg", new object[] { newbal.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName });
UnturnedChat.Say(playerid, message);
playerid.Experience -= examt;
Uconomy_Essentials.HandleEvent(playerid, gain, "exchange", examt);
break;
case 2:
uint gainm = (uint)((float)examt * Uconomy_Essentials.Instance.Configuration.Instance.MoneyExchangerate);
// Just to make sure to avoid any errors
newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), (examt * -1.0m));
message = Uconomy_Essentials.Instance.Translate("new_balance_msg", new object[] { newbal.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName });
UnturnedChat.Say(playerid, message);
playerid.Experience += gainm;
Uconomy_Essentials.HandleEvent(playerid, gainm, "exchange", examt, "money");
break;
}
playerid.Player.SteamChannel.send("tellExperience", ESteamCall.OWNER, ESteamPacket.UPDATE_RELIABLE_BUFFER, new object[]
{
playerid.Experience
});
}
}
}