-
Notifications
You must be signed in to change notification settings - Fork 27
/
ZaupShop.cs
602 lines (596 loc) · 28 KB
/
ZaupShop.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
using System;
using System.Collections.Generic;
using Rocket.API;
using Rocket.API.Collections;
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
using Rocket.Unturned.Plugins;
using SDG.Unturned;
using UnityEngine;
using fr34kyn01535.Uconomy;
using Steamworks;
namespace ZaupShop
{
public class ZaupShop : RocketPlugin<ZaupShopConfiguration>
{
public DatabaseMgr ShopDB;
public static ZaupShop Instance;
public override TranslationList DefaultTranslations
{
get
{
return new TranslationList
{
{
"buy_command_usage",
"Usage: /buy [v.]<name or id> [amount] [quality of 25, 50, 75, or 100] (last 2 optional and only for items, default 1 amount, 100 quality)."
},
{
"cost_command_usage",
"Usage: /cost [v.]<name or id>."
},
{
"sell_command_usage",
"Usage: /sell <name or id> [amount] (optional)."
},
{
"shop_command_usage",
"Usage: /shop <add/rem/chng/buy> [v.]<itemid> <cost> <cost> is not required for rem, buy is only for items."
},
{
"error_giving_item",
"There was an error giving you {0}. You have not been charged."
},
{
"error_getting_cost",
"There was an error getting the cost of {0}!"
},
{
"item_cost_msg",
"The item {0} costs {1} {2} to buy and gives {3} {4} when you sell it."
},
{
"vehicle_cost_msg",
"The vehicle {0} costs {1} {2} to buy."
},
{
"item_buy_msg",
"You have bought {5} {0} for {1} {2}. You now have {3} {4}."
},
{
"vehicle_buy_msg",
"You have bought 1 {0} for {1} {2}. You now have {3} {4}."
},
{
"not_enough_currency_msg",
"You do not have enough {0} to buy {1} {2}."
},
{
"buy_items_off",
"I'm sorry, but the ability to buy items is turned off."
},
{
"buy_vehicles_off",
"I'm sorry, but the ability to buy vehicles is turned off."
},
{
"item_not_available",
"I'm sorry, but {0} is not available in the shop."
},
{
"vehicle_not_available",
"I'm sorry, but {0} is not available in the shop."
},
{
"could_not_find",
"I'm sorry, I couldn't find an id for {0}."
},
{
"sell_items_off",
"I'm sorry, but the ability to sell items is turned off."
},
{
"not_have_item_sell",
"I'm sorry, but you don't have any {0} to sell."
},
{
"not_enough_items_sell",
"I'm sorry, but you don't have {0} {1} to sell."
},
{
"not_enough_ammo_sell",
"I'm sorry, but you don't have enough ammo in {0} to sell."
},
{
"sold_items",
"You have sold {0} {1} to the shop and receive {2} {3} in return. Your balance is now {4} {5}."
},
{
"no_sell_price_set",
"The shop is not buying {0} right now"
},
{
"no_itemid_given",
"An itemid is required."
},
{
"no_cost_given",
"A cost is required."
},
{
"invalid_amt",
"You have entered in an invalid amount."
},
{
"v_not_provided",
"You must specify v for vehicle or just an item id. Ex. /shop rem/101"
},
{
"invalid_id_given",
"You need to provide a valid item or vehicle id."
},
{
"no_permission_shop_chng",
"You don't have permission to use the shop chng msg."
},
{
"no_permission_shop_add",
"You don't have permission to use the shop add msg."
},
{
"no_permission_shop_rem",
"You don't have permission to use the shop rem msg."
},
{
"no_permission_shop_buy",
"You don't have permission to use the shop buy msg."
},
{
"changed",
"changed"
},
{
"added",
"added"
},
{
"changed_or_added_to_shop",
"You have {0} the {1} with cost {2} to the shop."
},
{
"error_adding_or_changing",
"There was an error adding/changing {0}!"
},
{
"removed_from_shop",
"You have removed the {0} from the shop."
},
{
"not_in_shop_to_remove",
"{0} wasn't in the shop, so couldn't be removed."
},
{
"not_in_shop_to_set_buyback",
"{0} isn't in the shop so can't set a buyback price."
},
{
"set_buyback_price",
"You set the buyback price for {0} to {1} in the shop."
},
{
"invalid_shop_command",
"You entered an invalid shop command."
}
};
}
}
protected override void Load()
{
ZaupShop.Instance = this;
this.ShopDB = new DatabaseMgr();
}
public delegate void PlayerShopBuy(UnturnedPlayer player, decimal amt, byte items, ushort item, string type="item");
public event PlayerShopBuy OnShopBuy;
public delegate void PlayerShopSell(UnturnedPlayer player, decimal amt, byte items, ushort item);
public event PlayerShopSell OnShopSell;
public void Buy(UnturnedPlayer playerid, string[] components0)
{
string message;
if (components0.Length == 0)
{
message = ZaupShop.Instance.Translate("buy_command_usage", new object[] {});
// We are going to print how to use
UnturnedChat.Say(playerid, message);
return;
}
byte amttobuy = 1;
if (components0.Length > 1)
{
if (!byte.TryParse(components0[1], out amttobuy))
{
message = ZaupShop.Instance.Translate("invalid_amt", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
}
string[] components = Parser.getComponentsFromSerial(components0[0], '.');
if ((components.Length == 2 && components[0].Trim() != "v") || (components.Length == 1 && components[0].Trim() == "v") || components.Length > 2 || components0[0].Trim() == string.Empty)
{
message = ZaupShop.Instance.Translate("buy_command_usage", new object[] { });
// We are going to print how to use
UnturnedChat.Say(playerid, message);
return;
}
ushort id;
switch (components[0])
{
case "v":
if (!ZaupShop.Instance.Configuration.Instance.CanBuyVehicles)
{
message = ZaupShop.Instance.Translate("buy_vehicles_off", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
string name = null;
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 (Assets.find(EAssetType.VEHICLE, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
UnturnedChat.Say(playerid, message);
return;
}
else if (name == null && id != 0)
{
name = ((VehicleAsset)Assets.find(EAssetType.VEHICLE, id)).Name;
}
decimal cost = ZaupShop.Instance.ShopDB.GetVehicleCost(id);
decimal balance = Uconomy.Instance.Database.GetBalance(playerid.CSteamID.ToString());
if (cost <= 0m)
{
message = ZaupShop.Instance.Translate("vehicle_not_available", new object[] {name});
UnturnedChat.Say(playerid, message);
return;
}
if (balance < cost)
{
message = ZaupShop.Instance.Translate("not_enough_currency_msg", new object[] {Uconomy.Instance.Configuration.Instance.MoneyName, "1", name});
UnturnedChat.Say(playerid, message);
return;
}
if (!playerid.GiveVehicle(id))
{
message = ZaupShop.Instance.Translate("error_giving_item", new object[] { name });
UnturnedChat.Say(playerid, message);
return;
}
decimal newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), (cost * -1));
message = ZaupShop.Instance.Translate("vehicle_buy_msg", new object[] {name, cost, Uconomy.Instance.Configuration.Instance.MoneyName, newbal, Uconomy.Instance.Configuration.Instance.MoneyName});
if (ZaupShop.Instance.OnShopBuy != null)
ZaupShop.Instance.OnShopBuy(playerid, cost, 1, id, "vehicle");
playerid.Player.gameObject.SendMessage("ZaupShopOnBuy", new object[] { playerid, cost, amttobuy, id, "vehicle" }, SendMessageOptions.DontRequireReceiver);
UnturnedChat.Say(playerid, message);
break;
default:
if (!ZaupShop.Instance.Configuration.Instance.CanBuyItems)
{
message = ZaupShop.Instance.Translate("buy_items_off", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
name = null;
if (!ushort.TryParse(components[0], 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(components[0].ToLower()))
{
id = vAsset.id;
name = vAsset.Name;
break;
}
}
}
if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
return;
}
else if (name == null && id != 0)
{
name = ((ItemAsset)Assets.find(EAssetType.ITEM, id)).Name;
}
cost = decimal.Round(ZaupShop.Instance.ShopDB.GetItemCost(id) * amttobuy, 2);
balance = Uconomy.Instance.Database.GetBalance(playerid.CSteamID.ToString());
if (cost <= 0m)
{
message = ZaupShop.Instance.Translate("item_not_available", new object[] {name});
UnturnedChat.Say(playerid, message);
return;
}
if (balance < cost)
{
message = ZaupShop.Instance.Translate("not_enough_currency_msg", new object[] {Uconomy.Instance.Configuration.Instance.MoneyName, amttobuy, name});
UnturnedChat.Say(playerid, message);
return;
}
playerid.GiveItem(id, amttobuy);
newbal = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), (cost * -1));
message = ZaupShop.Instance.Translate("item_buy_msg", new object[] {name, cost, Uconomy.Instance.Configuration.Instance.MoneyName, newbal, Uconomy.Instance.Configuration.Instance.MoneyName, amttobuy});
if (ZaupShop.Instance.OnShopBuy != null)
ZaupShop.Instance.OnShopBuy(playerid, cost, amttobuy, id);
playerid.Player.gameObject.SendMessage("ZaupShopOnBuy", new object[] { playerid, cost, amttobuy, id, "item" }, SendMessageOptions.DontRequireReceiver);
UnturnedChat.Say(playerid, message);
break;
}
}
public void Cost(UnturnedPlayer playerid, string[] components)
{
string message;
if (components.Length == 0 || (components.Length == 1 && (components[0].Trim() == string.Empty || components[0].Trim() == "v")))
{
message = ZaupShop.Instance.Translate("cost_command_usage", new object[] { });
// We are going to print how to use
UnturnedChat.Say(playerid, message);
return;
}
if (components.Length == 2 && (components[0] != "v" || components[1].Trim() == string.Empty))
{
message = ZaupShop.Instance.Translate("cost_command_usage", new object[] { });
// We are going to print how to use
UnturnedChat.Say(playerid, message);
return;
}
ushort id;
switch (components[0])
{
case "v":
string name = null;
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 (Assets.find(EAssetType.VEHICLE, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[1]});
UnturnedChat.Say(playerid, message);
return;
}
else if (name == null && id != 0)
{
name = ((VehicleAsset)Assets.find(EAssetType.VEHICLE, id)).Name;
}
decimal cost = ZaupShop.Instance.ShopDB.GetVehicleCost(id);
message = ZaupShop.Instance.Translate("vehicle_cost_msg", new object[] {name, cost.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName});
if (cost <= 0m)
{
message = ZaupShop.Instance.Translate("error_getting_cost", new object[] {name});
}
UnturnedChat.Say(playerid, message);
break;
default:
name = null;
if (!ushort.TryParse(components[0], out id))
{
Asset[] array = Assets.find(EAssetType.ITEM);
Asset[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
ItemAsset iAsset = (ItemAsset)array2[i];
if (iAsset != null && iAsset.Name != null && iAsset.Name.ToLower().Contains(components[0].ToLower()))
{
id = iAsset.id;
name = iAsset.Name;
break;
}
}
}
if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
return;
}
else if (name == null && id != 0)
{
name = ((ItemAsset)Assets.find(EAssetType.ITEM, id)).Name;
}
cost = ZaupShop.Instance.ShopDB.GetItemCost(id);
decimal bbp = ZaupShop.Instance.ShopDB.GetItemBuyPrice(id);
message = ZaupShop.Instance.Translate("item_cost_msg", new object[] {name, cost.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName, bbp.ToString(), Uconomy.Instance.Configuration.Instance.MoneyName});
if (cost <= 0m)
{
message = ZaupShop.Instance.Translate("error_getting_cost", new object[] {name});
}
UnturnedChat.Say(playerid, message);
break;
}
}
public void Sell(UnturnedPlayer playerid, string[] components)
{
string message;
if (components.Length == 0 || (components.Length > 0 && components[0].Trim() == string.Empty))
{
message = ZaupShop.Instance.Translate("sell_command_usage", new object[] { });
// We are going to print how to use
UnturnedChat.Say(playerid, message);
return;
}
byte amttosell = 1;
if (components.Length > 1)
{
if (!byte.TryParse(components[1], out amttosell))
{
message = ZaupShop.Instance.Translate("invalid_amt", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
}
byte amt = amttosell;
ushort id;
if (!ZaupShop.Instance.Configuration.Instance.CanSellItems)
{
message = ZaupShop.Instance.Translate("sell_items_off", new object[] { });
UnturnedChat.Say(playerid, message);
return;
}
string name = null;
ItemAsset vAsset = null;
if (!ushort.TryParse(components[0], out id))
{
Asset[] array = Assets.find(EAssetType.ITEM);
Asset[] array2 = array;
for (int i = 0; i < array2.Length; i++)
{
vAsset = (ItemAsset)array2[i];
if (vAsset != null && vAsset.Name != null && vAsset.Name.ToLower().Contains(components[0].ToLower()))
{
id = vAsset.id;
name = vAsset.Name;
break;
}
}
}
if (Assets.find(EAssetType.ITEM, id) == null)
{
message = ZaupShop.Instance.Translate("could_not_find", new object[] {components[0]});
UnturnedChat.Say(playerid, message);
return;
}
else if (name == null && id != 0)
{
vAsset = (ItemAsset)Assets.find(EAssetType.ITEM, id);
name = vAsset.Name;
}
// Get how many they have
if (playerid.Inventory.has(id) == null)
{
message = ZaupShop.Instance.Translate("not_have_item_sell", new object[] {name});
UnturnedChat.Say(playerid, message);
return;
}
List<InventorySearch> list = playerid.Inventory.search(id, true, true);
if (list.Count == 0 || (vAsset.Amount == 1 && list.Count < amttosell))
{
message = ZaupShop.Instance.Translate("not_enough_items_sell", new object[] {amttosell.ToString(), name});
UnturnedChat.Say(playerid, message);
return;
}
if (vAsset.Amount > 1)
{
int ammomagamt = 0;
foreach (InventorySearch ins in list)
{
ammomagamt += ins.ItemJar.Item.Amount;
}
if (ammomagamt < amttosell)
{
message = ZaupShop.Instance.Translate("not_enough_ammo_sell", new object[] {name});
UnturnedChat.Say(playerid, message);
return;
}
}
// We got this far, so let's buy back the items and give them money.
// Get cost per item. This will be whatever is set for most items, but changes for ammo and magazines.
decimal price = ZaupShop.Instance.ShopDB.GetItemBuyPrice(id);
if (price <= 0.00m)
{
message = ZaupShop.Instance.Translate("no_sell_price_set", new object[] { name });
UnturnedChat.Say(playerid, message);
return;
}
byte quality = 100;
decimal peritemprice = 0;
decimal addmoney = 0;
switch (vAsset.Amount)
{
case 1:
// These are single items, not ammo or magazines
while (amttosell > 0)
{
if (playerid.Player.Equipment.checkSelection(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY))
{
playerid.Player.Equipment.dequip();
}
if (ZaupShop.Instance.Configuration.Instance.QualityCounts)
quality = list[0].ItemJar.Item.Durability;
peritemprice = decimal.Round(price * (quality / 100.0m), 2);
addmoney += peritemprice;
playerid.Inventory.removeItem(list[0].InventoryGroup, playerid.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
amttosell--;
}
break;
default:
// This is ammo or magazines
byte amttosell1 = amttosell;
while (amttosell > 0)
{
if (playerid.Player.Equipment.checkSelection(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY))
{
playerid.Player.Equipment.dequip();
}
if (list[0].ItemJar.Item.Amount >= amttosell)
{
byte left = (byte)(list[0].ItemJar.Item.Amount - amttosell);
list[0].ItemJar.Item.Amount = left;
playerid.Inventory.sendUpdateAmount(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY, left);
amttosell = 0;
if (left == 0)
{
playerid.Inventory.removeItem(list[0].InventoryGroup, playerid.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
}
}
else
{
amttosell -= list[0].ItemJar.Item.Amount;
playerid.Inventory.sendUpdateAmount(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY, 0);
playerid.Inventory.removeItem(list[0].InventoryGroup, playerid.Inventory.getIndex(list[0].InventoryGroup, list[0].ItemJar.PositionX, list[0].ItemJar.PositionY));
list.RemoveAt(0);
}
}
peritemprice = decimal.Round(price * ((decimal)amttosell1 / (decimal)vAsset.Amount), 2);
addmoney += peritemprice;
break;
}
decimal balance = Uconomy.Instance.Database.IncreaseBalance(playerid.CSteamID.ToString(), addmoney);
message = ZaupShop.Instance.Translate("sold_items", new object[] {amt, name, addmoney, Uconomy.Instance.Configuration.Instance.MoneyName, balance, Uconomy.Instance.Configuration.Instance.MoneyName});
if (ZaupShop.Instance.OnShopSell != null)
ZaupShop.Instance.OnShopSell(playerid, addmoney, amt, id);
playerid.Player.gameObject.SendMessage("ZaupShopOnSell", new object[] { playerid, addmoney, amt, id }, SendMessageOptions.DontRequireReceiver);
UnturnedChat.Say(playerid, message);
}
}
}