-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCommandBuy.cs
241 lines (233 loc) · 10.3 KB
/
CommandBuy.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using Rocket.API;
using Rocket.Core.Logging;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using SDG.Unturned;
using fr34kyn01535.Uconomy;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace LIGHT
{
public class CommandBuy : IRocketCommand
{
public string Help
{
get { return "Allows you to buy items from the shop"; }
}
public string Name
{
get { return "buy"; }
}
public AllowedCaller AllowedCaller
{
get
{
return AllowedCaller.Player;
}
}
public string Syntax
{
get { return "[v.]<name or id> [amount] [25 | 50 | 75 | 100]"; }
}
public List<string> Aliases
{
get { return new List<string>(); }
}
public List<string> Permissions
{
get
{
return new List<string>() { "buy"};
}
}
public void Execute(IRocketPlayer caller, params string[] command)
{
if(!LIGHT.Instance.Configuration.Instance.EnableShop)
{
UnturnedChat.Say(caller, LIGHT.Instance.Translate("shop_disable"));
return;
}
UnturnedPlayer player = (UnturnedPlayer)caller;
if (command.Length == 0)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("buy_command_usage"));
return;
}
byte amttobuy = 1;
string Itemname = "";
byte CheckItemID = 0;
bool IsID = byte.TryParse(command[0], out CheckItemID);
bool IsAmtKeyed = false;
if (command.Length > 1)
IsAmtKeyed = byte.TryParse(command[command.Length - 1], out amttobuy);
for (int i = 0; i < (command.Length - 1); i++)
{
if (i == (command.Length - 2))
Itemname += command[i];
else
Itemname += command[i] + " ";
}
if (!IsAmtKeyed)
{
amttobuy = 1;
if (command.Length == 2)
Itemname = command[0] + " " + command[1];
else if (command.Length == 1)
Itemname = command[0];
}
string[] components = Parser.getComponentsFromSerial(command[0], '.');
if (components.Length == 2 && components[0] != "v")
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("buy_command_usage"));
return;
}
ushort id;
switch (components[0])
{
case "v":
if (!LIGHT.Instance.Configuration.Instance.CanBuyVehicles)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("buy_vehicles_off"));
return;
}
string name = "";
if (!ushort.TryParse(components[1], out id))
{
Asset[] array = Assets.find(EAssetType.VEHICLE);
Asset[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
VehicleAsset vAsset = (VehicleAsset)array2[i];
if (vAsset != null && vAsset.Name != null && vAsset.Name.ToLower().Contains(components[1].ToLower()))
{
id = vAsset.Id;
name = vAsset.Name;
break;
}
}
}
if (name == null && id == 0)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("could_not_find",components[1]));
return;
}
else if (name == null && id != 0)
{
try
{
name = ((VehicleAsset)Assets.find(EAssetType.VEHICLE, id)).Name;
}
catch
{
UnturnedChat.Say(caller, LIGHT.Instance.Translate("vehicle_invalid"));
return;
}
}
decimal cost = LIGHT.Instance.ShopDB.GetVehicleCost(id);
decimal balance = Uconomy.Instance.Database.GetBalance(player.Id);
if (cost <= 0m)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("vehicle_not_available", name));
return;
}
if (balance < cost)
{
UnturnedChat.Say(player, LIGHT.Instance.DefaultTranslations.Translate("not_enough_currency_msg",Uconomy.Instance.Configuration.Instance.MoneyName, name));
return;
}
if (!player.GiveVehicle(id))
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("error_giving_item",name));
return;
}
decimal newbal = Uconomy.Instance.Database.IncreaseBalance(player.CSteamID.ToString(), (cost * -1));
if (OnShopBuy != null)
OnShopBuy(player, cost, 1, id, "vehicle");
player.Player.gameObject.SendMessage("ZaupShopOnBuy", new object[] { player, cost, amttobuy, id, "vehicle" }, SendMessageOptions.DontRequireReceiver);
UnturnedChat.Say(player, LIGHT.Instance.Translate("vehicle_buy_msg", name, cost, Uconomy.Instance.Configuration.Instance.MoneyName, newbal, Uconomy.Instance.Configuration.Instance.MoneyName));
break;
default:
if (!LIGHT.Instance.Configuration.Instance.CanBuyItems)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("buy_items_off"));
return;
}
name = null;
if (!ushort.TryParse(Itemname, out id))
{
Asset[] array = Assets.find(EAssetType.ITEM);
Asset[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
ItemAsset vAsset = (ItemAsset)array2[i];
if (vAsset != null && vAsset.Name != null && vAsset.Name.ToLower().Contains(Itemname.ToLower()))
{
id = vAsset.Id;
name = vAsset.Name;
break;
}
}
}
if (name == null && id == 0)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("could_not_find", Itemname));
return;
}
else if (name == null && id != 0)
{
try
{
name = ((ItemAsset)Assets.find(EAssetType.ITEM, id)).Name;
}
catch
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("item_invalid"));
return;
}
}
cost = decimal.Round(LIGHT.Instance.ShopDB.GetItemCost(id) * amttobuy, 2);
if (LIGHT.Instance.Configuration.Instance.SaleEnable)
{
decimal saleprice = Convert.ToDecimal(Convert.ToDouble(LIGHT.Instance.Configuration.Instance.SalePercentage) / 100.00);
if (LIGHT.Instance.sale.salesStart == true)
cost = decimal.Round((cost * (Convert.ToDecimal(1.00) - saleprice)), 2);
}
balance = Uconomy.Instance.Database.GetBalance(player.Id);
if (cost <= 0m)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("item_not_available", name));
return;
}
if (balance < cost)
{
UnturnedChat.Say(player, LIGHT.Instance.Translate("not_enough_currency_msg", Uconomy.Instance.Configuration.Instance.MoneyName, amttobuy, name));
return;
}
player.GiveItem(id, amttobuy);
string[] freeItem = LIGHT.Instance.Database.GetFreeItem(caller.Id);
bool free = false;
for (int x = 0; x < freeItem.Length; x ++)
{
if(freeItem[x].Trim() == id.ToString().Trim())
{
free = true;
cost = 0;
break;
}
}
newbal = Uconomy.Instance.Database.IncreaseBalance(player.Id, (cost * -1));
if (OnShopBuy != null)
OnShopBuy(player, cost, amttobuy, id);
player.Player.gameObject.SendMessage("ZaupShopOnBuy", new object[] { player, cost, amttobuy, id, "item" }, SendMessageOptions.DontRequireReceiver);
if(free)
UnturnedChat.Say(player, LIGHT.Instance.Translate("item_buy_freemsg", amttobuy, name, LIGHT.Instance.Database.CheckUserGroup(caller.Id)));
else
UnturnedChat.Say(player, LIGHT.Instance.Translate("item_buy_msg", name, cost, Uconomy.Instance.Configuration.Instance.MoneyName, newbal, Uconomy.Instance.Configuration.Instance.MoneyName, amttobuy));
break;
}
}
public event PlayerShopBuy OnShopBuy;
public delegate void PlayerShopBuy(UnturnedPlayer player, decimal amt, byte items, ushort item, string type = "item");
}
}