This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Economy.cs
256 lines (232 loc) · 10.3 KB
/
Economy.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
using F.Economy.Database;
using F.Economy.Models;
using Rocket.API.Collections;
using Rocket.Core;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Events;
using Rocket.Unturned.Player;
using SDG.Unturned;
using Steamworks;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;
using Logger = Rocket.Core.Logging.Logger;
namespace F.Economy
{
public class Economy : RocketPlugin<EconomyConfiguration>
{
public static Economy Instance;
protected override void Load()
{
Instance = this;
Logger.Log("FEconomy Loaded");
if (Configuration.Instance.DownloadLibraries)
{
if (!File.Exists($@"{Rocket.Core.Environment.LibrariesDirectory}\LiteDB.dll"))
{
Logger.Log("Downloading Librarie LiteDB..", ConsoleColor.Yellow);
using (var Client = new WebClient())
{
Client.DownloadFile("https://cdn.discordapp.com/attachments/764924498364989481/764924576584695869/LiteDB.dll", $@"{Rocket.Core.Environment.LibrariesDirectory}\LiteDB.dll");
}
Logger.Log("LiteDB Downloaded!", ConsoleColor.Yellow);
}
}
var DirectoryD = $@"{System.Environment.CurrentDirectory}\Database";
if (!System.IO.Directory.Exists(DirectoryD))
{
Logger.Log("Creating Directory Database..", ConsoleColor.Yellow);
System.IO.Directory.CreateDirectory(DirectoryD);
Logger.Log("Directory Database Created!", ConsoleColor.Yellow);
}
if (!File.Exists($@"{DirectoryD}\Economy.db"))
{
Logger.Log("Creating Economy Database..", ConsoleColor.Yellow);
Database.EconomyDB.CreateDatabase();
Logger.Log("Economy Database Created!", ConsoleColor.Yellow);
}
U.Events.OnPlayerConnected += Events_OnPlayerConnected;
if (Configuration.Instance.MoneyUI == true)
{
if (Configuration.Instance.XpMode == false)
{
Instance.OnBalanceUpdated += Instance_OnBalanceUpdated;
}
else
{
UnturnedPlayerEvents.OnPlayerUpdateExperience += UnturnedPlayerEvents_OnPlayerUpdateExperience;
}
}
StartCoroutine((IEnumerator)Salary());
StartCoroutine((IEnumerator)Taxes());
}
private void UnturnedPlayerEvents_OnPlayerUpdateExperience(UnturnedPlayer player, uint experience)
{
EffectManager.sendUIEffectText(5456, player.CSteamID, true, "Dinero", $"${player.Experience}");
}
public delegate void PlayerBalanceUpdated(UnturnedPlayer player, int money);
public event PlayerBalanceUpdated OnBalanceUpdated;
private void Instance_OnBalanceUpdated(UnturnedPlayer player, int money)
{
EffectManager.sendUIEffectText(5456, player.CSteamID, true, "Dinero", $"${EconomyDB.GetBalance(player)}");
}
public void BalanceUpdate(CSteamID cSteamID, int money)
{
if (OnBalanceUpdated != null)
{
UnturnedPlayer player = UnturnedPlayer.FromCSteamID(cSteamID);
OnBalanceUpdated(player, money);
}
}
private void Events_OnPlayerConnected(UnturnedPlayer player)
{
EconomyDB.NewAccount(player, Configuration.Instance.InitialMoney);
if (EconomyDB.AccountExist(player) == false && Configuration.Instance.XpMode == true)
{
player.Experience = player.Experience + (uint)Configuration.Instance.InitialMoney;
}
if (Configuration.Instance.MoneyUI == true)
{
EffectManager.sendUIEffect(Configuration.Instance.UIID, 5456, player.CSteamID, true);
if (Configuration.Instance.XpMode == false)
{
EffectManager.sendUIEffectText(5456, player.CSteamID, true, "Dinero", $"${EconomyDB.GetBalance(player)}");
}
else
{
EffectManager.sendUIEffectText(5456, player.CSteamID, true, "Dinero", $"${player.Experience}");
}
}
}
private IEnumerator<WaitForSeconds> Salary()
{
for (; ; )
{
PaySalary();
yield return new WaitForSeconds((float)Configuration.Instance.SalaryInterval);
}
}
private IEnumerator<WaitForSeconds> Taxes()
{
for (; ; )
{
PayTaxes();
yield return new WaitForSeconds((float)Configuration.Instance.TaxesInterval);
}
}
private void PayTaxes()
{
if (Configuration.Instance.DisconectedPlayersPayTaxes == true)
{
if (Configuration.Instance.XpMode == true)
{
EconomyDB.PayTaxes(Configuration.Instance.DisconectedPlayersTaxes);
}
}
else
{
foreach (SteamPlayer steamPlayer in Provider.clients)
{
UnturnedPlayer player = UnturnedPlayer.FromSteamPlayer(steamPlayer);
int amount = 0;
foreach (Group group in Configuration.Instance.Groups)
{
var salarygroup = R.Permissions.GetGroup(group.GroupName);
if (salarygroup.Members.Contains(player.Id))
{
amount = group.Tax;
if (amount < 0)
{
amount = 0;
}
if (Configuration.Instance.XpMode == false)
{
EconomyDB.RemoveBalance(player, amount);
}
else
{
player.Experience = player.Experience - (uint)amount;
}
UnturnedChat.Say(player, string.Format(Translate("tax_pay"), amount, Configuration.Instance.CurrencyName, salarygroup.DisplayName));
}
}
}
}
}
private void PaySalary()
{
foreach (SteamPlayer steamPlayer in Provider.clients)
{
UnturnedPlayer player = UnturnedPlayer.FromSteamPlayer(steamPlayer);
int amount = 0;
foreach (Group group in Configuration.Instance.Groups)
{
var salarygroup = R.Permissions.GetGroup(group.GroupName);
if (salarygroup.Members.Contains(player.Id))
{
amount = group.Salary;
if (amount < 0)
{
amount = 0;
}
if (Configuration.Instance.XpMode == false)
{
EconomyDB.AddBalance(player, amount);
}
else
{
player.Experience = player.Experience + (uint)amount;
}
UnturnedChat.Say(player, string.Format(Translate("salary_pay"), amount, Configuration.Instance.CurrencyName, salarygroup.DisplayName));
}
}
}
}
public override TranslationList DefaultTranslations
{
get
{
TranslationList translationList = new TranslationList();
translationList.Add("pay_nopoint", "There is no point in paying yourself!");
translationList.Add("err_ammount", "The ammount of money must be positive!");
translationList.Add("no_balance", "You don't have enough money!");
translationList.Add("pay_success", "You paid {0} {1} to {2}!");
translationList.Add("c_pay_success", "{3} paid {0} {1} to {2}!");
translationList.Add("xppay_success", "You paid {0} to {1}!");
translationList.Add("pay_recieve", "You received {0} {1} from {2}!");
translationList.Add("xppay_recieve", "You received {0} from {1}!");
translationList.Add("mexange_success", "Successfuly exanged {0} {1} to: {2} Xp!");
translationList.Add("exange_success", "Successfuly exanged {0} experience to: {1} {2}!");
translationList.Add("xp_enabled", "Xp mode is enabled so you can't use this command!");
translationList.Add("wipe_success", "Successfuly wiped all player balace!");
translationList.Add("balance", "Your balance: {0}!");
translationList.Add("salary_pay", "You received {0} {1} as salary of {2}!");
translationList.Add("tax_pay", "You pay {0} {1} of {2} taxes!");
translationList.Add("player_find", "Failed to find a player called: {0}!");
translationList.Add("setbalance_success", "Successfuly set {0} balance to {1}!");
return translationList;
}
}
protected override void Unload()
{
StopAllCoroutines();
U.Events.OnPlayerConnected += Events_OnPlayerConnected;
if (Configuration.Instance.MoneyUI == true)
{
if (Configuration.Instance.XpMode == false)
{
Instance.OnBalanceUpdated -= Instance_OnBalanceUpdated;
}
else
{
UnturnedPlayerEvents.OnPlayerUpdateExperience -= UnturnedPlayerEvents_OnPlayerUpdateExperience;
}
}
}
}
}