-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoneyTime.cs
More file actions
346 lines (274 loc) · 11.6 KB
/
MoneyTime.cs
File metadata and controls
346 lines (274 loc) · 11.6 KB
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
/*
Fixed by SeesAll, version incremented by 1 to indicate a change. The readme notes can be found at https://github.com/SeesAll/MoneyTime-Fixed
I take no credit for this plugin, I merely fixed a few subtle issues.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Libraries.Covalence;
using Oxide.Core.Plugins;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Money Time", "Wulf", "2.3.1")]
[Description("Pays players with Economics money for playing")]
public class MoneyTime : CovalencePlugin
{
#region Configuration
private const string Perm = "moneytime.";
private Configuration _config;
public class Configuration
{
[JsonProperty("Enable Economics as default currency")]
public bool Economics = true;
[JsonProperty("Enable Server Rewards as default currency")]
public bool ServerRewards = false;
[JsonProperty("Enable AFK API plugin support")]
public bool AfkApi = false;
[JsonProperty("Base payout amount")]
public int BasePayout = 100;
[JsonProperty("Payout interval (seconds)")]
public int PayoutInterval = 600;
[JsonProperty("Time alive bonus")]
public bool TimeAliveBonus = false;
[JsonProperty("Time alive multiplier")]
public float TimeAliveMultiplier = 0f;
[JsonProperty("Allow Permission-based Multipliers to stack")]
public bool StackMultipliers = false;
[JsonProperty("New player welcome bonus")]
public float WelcomeBonus = 500f;
[JsonProperty("Permission-based mulitipliers", ObjectCreationHandling = ObjectCreationHandling.Replace)]
public SortedDictionary<string, float> PermissionMulitipliers = new SortedDictionary<string, float>
{
["vip"] = 5f,
["donor"] = 2.5f
};
public string ToJson() => JsonConvert.SerializeObject(this);
public Dictionary<string, object> ToDictionary() => JsonConvert.DeserializeObject<Dictionary<string, object>>(ToJson());
}
protected override void LoadDefaultConfig() => _config = new Configuration();
protected override void LoadConfig()
{
base.LoadConfig();
try
{
_config = Config.ReadObject<Configuration>();
if (_config == null)
{
throw new JsonException();
}
if (!_config.ToDictionary().Keys.SequenceEqual(Config.ToDictionary(x => x.Key, x => x.Value).Keys))
{
LogWarning("Configuration appears to be outdated; updating and saving");
SaveConfig();
}
}
catch
{
LogWarning($"Configuration file {Name}.json is invalid; using defaults");
LoadDefaultConfig();
}
}
protected override void SaveConfig()
{
Log($"Configuration changes saved to {Name}.json");
Config.WriteObject(_config, true);
}
#endregion
#region Localization
protected override void LoadDefaultMessages()
{
lang.RegisterMessages(new Dictionary<string, string>
{
["MissedPayout"] = "You have been inactive and have missed this payout",
["ReceivedForPlaying"] = "You have received $payout.amount for actively playing",
["ReceivedForTimeAlive"] = "You have received $payout.amount for staying alive for $time.alive",
["ReceivedWelcomeBonus"] = "You have received $payout.amount as a welcome bonus"
}, this);
}
#endregion
#region Data Storage
private StoredData _storedData;
private class StoredData
{
public Dictionary<string, PlayerInfo> Players = new Dictionary<string, PlayerInfo>();
}
private class PlayerInfo
{
public DateTime LastTimeAlive;
public bool WelcomeBonus;
public PlayerInfo()
{
LastTimeAlive = DateTime.Now;
WelcomeBonus = false;
}
}
private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject(Name, _storedData);
private void OnServerSave() => SaveData();
#endregion
#region Initialization
[PluginReference]
private Plugin Economics, ServerRewards, AFKAPI;
private Dictionary<string, Values> _payOut = new Dictionary<string, Values>();
private Dictionary<string, double> _perms = new Dictionary<string, double>();
private class Values
{
public double amount;
public float time;
}
private void Init()
{
foreach (KeyValuePair<string, float> perm in _config.PermissionMulitipliers)
{
string p = Perm + perm.Key;
_perms[p] = perm.Value;
permission.RegisterPermission(p, this);
Log($"Registered permission '{p}'; multiplier {perm.Value}");
}
_storedData = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
if (!_config.TimeAliveBonus)
Unsubscribe(nameof(OnUserRespawn));
}
private void InitializePlayer(IPlayer player, float current)
{
string id = player.Id;
if (!_storedData.Players.ContainsKey(id))
_storedData.Players.Add(id, new PlayerInfo());
double amt = _config.BasePayout;
double multi = 0;
if (_config.StackMultipliers)
{
foreach (var perm in _perms)
if (player.HasPermission(perm.Key))
multi += amt * perm.Value;
if (multi != 0)
amt = multi;
}
else
{
foreach (var perm in _perms)
if (player.HasPermission(perm.Key) && perm.Value > multi)
multi = perm.Value;
if (multi != 0)
amt *= multi;
}
if (!_payOut.ContainsKey(id))
_payOut.Add(id, new Values { amount = amt, time = current });
else
_payOut[id].amount = amt;
}
private void OnServerInitialized()
{
var current = Time.realtimeSinceStartup + _config.PayoutInterval;
foreach (IPlayer player in players.Connected)
if (player.Id.IsSteamId())
InitializePlayer(player, current);
timer.Every(_config.PayoutInterval, () =>
{
var newTime = Time.realtimeSinceStartup;
foreach (IPlayer player in players.Connected)
{
string id = player.Id;
if (!_payOut.ContainsKey(id)) continue;
var payout = _payOut[id];
if (payout.time <= newTime)
{
Payout(player, payout.amount, GetLang("ReceivedForPlaying", id));
payout.time = newTime + _config.PayoutInterval;
}
}
});
}
private void Unload()
{
SaveData();
_perms.Clear();
_payOut.Clear();
}
#endregion
#region On Perms Updated
private void OnGroupPermissionGranted(string name, string perm) => Edit(true, _perms.ContainsKey(perm));
private void OnGroupPermissionRevoked(string name, string perm) => Edit(true, _perms.ContainsKey(perm));
private void OnUserPermissionGranted(string id, string perm) => Edit(false, _perms.ContainsKey(perm), id);
private void OnUserPermissionRevoked(string id, string perm) => Edit(false, _perms.ContainsKey(perm), id);
private void Edit(bool all, bool mine, string user = "")
{
if (!mine) return;
var current = Time.realtimeSinceStartup + _config.PayoutInterval;
if (all)
{
foreach (IPlayer player in players.Connected)
if (player.Id.IsSteamId())
InitializePlayer(player, current);
}
else
{
IPlayer target = players.FindPlayerById(user);
if (target != null)
InitializePlayer(target, current);
}
}
#endregion
#region Payout Handling
private void Payout(IPlayer player, double amount, string message)
{
if (_config.AfkApi && AFKAPI != null && AFKAPI.IsLoaded)
{
bool isAfk = AFKAPI.Call<bool>("IsPlayerAFK", ulong.Parse(player.Id));
if (isAfk)
{
Message(player, "MissedPayout");
return;
}
}
if (_config.Economics && Economics != null && Economics.IsLoaded)
{
Economics.Call("Deposit", player.Id, amount);
Message(player, message.Replace("$payout.amount", amount.ToString()));
}
else if (_config.ServerRewards && ServerRewards != null && ServerRewards.IsLoaded)
{
ServerRewards.Call("AddPoints", player, (int)amount);
Message(player, message.Replace("$payout.amount", amount.ToString()));
}
}
private void OnUserConnected(IPlayer player)
{
var current = Time.realtimeSinceStartup + _config.PayoutInterval;
InitializePlayer(player, current);
if (_config.WelcomeBonus > 0f && !_storedData.Players[player.Id].WelcomeBonus)
{
Payout(player, _config.WelcomeBonus, GetLang("ReceivedWelcomeBonus", player.Id));
_storedData.Players[player.Id].WelcomeBonus = true;
SaveData();
}
}
private void OnUserDisconnected(IPlayer player) => _payOut.Remove(player.Id);
private void OnUserRespawn(IPlayer player)
{
if (!player.Id.IsSteamId()) return;
if (!_storedData.Players.ContainsKey(player.Id))
InitializePlayer(player, Time.realtimeSinceStartup + _config.PayoutInterval);
double secondsAlive = (DateTime.Now - _storedData.Players[player.Id].LastTimeAlive).TotalSeconds;
TimeSpan timeSpan = TimeSpan.FromSeconds(secondsAlive);
double amount = (_config.BasePayout > 0)
? (secondsAlive / _config.BasePayout) * _config.TimeAliveMultiplier
: 0d;
string timeAlive = $"{timeSpan.TotalHours:00}h {timeSpan.Minutes:00}m {timeSpan.Seconds:00}s".TrimStart(' ', 'd', 'h', 'm', 's', '0');
Payout(player, amount, GetLang("ReceivedForTimeAlive", player.Id).Replace("$time.alive", timeAlive));
_storedData.Players[player.Id].LastTimeAlive = DateTime.Now;
}
#endregion
#region Helpers
private string GetLang(string langKey, string playerId = null) => lang.GetMessage(langKey, this, playerId);
private void Message(IPlayer player, string textOrLang, params object[] args)
{
if (!player.IsConnected) return;
string message = GetLang(textOrLang, player.Id);
player.Reply(message != textOrLang ? message : textOrLang);
}
#endregion
}
}