Skip to content

Commit e770de1

Browse files
authored
Several Bugfixes in EcoCommands (#212)
* Fix: It possible KeyAlreadyExistException workarounded It apparently is possible to have 2 Players with Same Steam ID but different Serial ID. Cause unknown. For both of them the same Identity ID is looked up because that ignores the Serial ID. And therefore I can just replace the value. Could auso check with if(!containsKey) but since the result will be identical I didn't bother. * Fix: Credits are Number-Formatted in all eco commands * Fix: Players can nolonger pay themselves Players were able to duplicate money by paying themselves. If Player A has 10,000 credits And Pays Player A 1,000 credits He ended up with 11,000 credits. But there was a warning on server side that the client was out of sync. And if you do anything else, like for example !econ set !econ take or !econ reset it crashed said client. * Fix: Added missing .DisplayName when Paying player * Feat: !econ check can now also check offline players I dont exactly know why that limitation of online only was in place. set, take, give, check should work for offlines too. Since the others are not important enough (for me at least) I am not doing that with this commit. * Fix: Responses now inform about the possability that the player is offline These kind of commands should work with IMyIdentity instead of IMyPlayer especially since the "*" parameter would work for ALL Players (not NPCs) but since I have no intention to fix it at this point in time. The better message will have to do.
1 parent c7443ec commit e770de1

1 file changed

Lines changed: 44 additions & 25 deletions

File tree

‎Essentials/Commands/EcoModule.cs‎

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,30 @@ namespace Essentials.Commands
2222
{
2323
[Category("econ")]
2424
public class EcoModule : CommandModule {
25+
2526
[Command("give", "Add a specified anount of credits into a users account. Use '*' to affect all players")]
2627
[Permission(MyPromoteLevel.Admin)]
2728
public void EcoGive(string Player, long amount) {
2829
if (Player != "*") {
2930
var p = Utilities.GetPlayerByNameOrId(Player);
3031
if (p == null) {
31-
Context.Respond("Player not found");
32+
Context.Respond("Player is not online or cannot be found!");
3233
return;
3334
}
3435
p.TryGetBalanceInfo(out long balance);
35-
Context.Respond($"new bal will be {balance + amount}");
36+
Context.Respond($"new bal will be {balance + amount:#,##0}");
3637
p.RequestChangeBalance(amount);
37-
ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been added to your virtual account", 10000, "Blue"), p.SteamUserId);
38+
ModCommunication.SendMessageTo(new NotificationMessage($"{amount:#,##0} credits have been added to your virtual account", 10000, "Blue"), p.SteamUserId);
3839

3940
}
4041
else {
4142
foreach (var p in MySession.Static.Players.GetAllPlayers()) {
4243
long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId);
4344
MyBankingSystem.ChangeBalance(IdentityID, amount);
44-
ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been added to your virtual account", 10000, "Blue"), p.SteamId);
45+
ModCommunication.SendMessageTo(new NotificationMessage($"{amount:#,##0} credits have been added to your virtual account", 10000, "Blue"), p.SteamId);
4546
}
4647
}
47-
Context.Respond($"{amount} credits given to account(s)");
48+
Context.Respond($"{amount:#,##0} credits given to account(s)");
4849
}
4950

5051
[Command("take", "Take a specified anount of credits from a users account. Use '*' to affect all players")]
@@ -53,22 +54,22 @@ public void EcoTake(string Player, long amount) {
5354
if (Player != "*") {
5455
var p = Utilities.GetPlayerByNameOrId(Player);
5556
if (p == null) {
56-
Context.Respond("Player not found");
57+
Context.Respond("Player is not online or cannot be found!");
5758
return;
5859
}
5960
long changefactor = 0 - amount;
6061
p.RequestChangeBalance(changefactor);
61-
ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been taken to your virtual account", 10000, "Blue"), p.SteamUserId);
62+
ModCommunication.SendMessageTo(new NotificationMessage($"{amount:#,##0} credits have been taken to your virtual account", 10000, "Blue"), p.SteamUserId);
6263
}
6364
else {
6465
foreach (var p in MySession.Static.Players.GetAllPlayers()) {
6566
long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId);
6667
long balance = MyBankingSystem.GetBalance(IdentityID);
6768
MyBankingSystem.ChangeBalance(IdentityID, -amount);
68-
ModCommunication.SendMessageTo(new NotificationMessage($"{amount} credits have been taken to your virtual account", 10000, "Blue"), p.SteamId);
69+
ModCommunication.SendMessageTo(new NotificationMessage($"{amount:#,##0} credits have been taken to your virtual account", 10000, "Blue"), p.SteamId);
6970
}
7071
}
71-
Context.Respond($"{amount} credits taken from account(s)");
72+
Context.Respond($"{amount:#,##0} credits taken from account(s)");
7273
}
7374

7475
[Command("set", "Set a users account to a specifed balance. Use '*' to affect all players")]
@@ -77,24 +78,24 @@ public void EcoSet(string Player, long amount) {
7778
if (Player != "*") {
7879
var p = Utilities.GetPlayerByNameOrId(Player);
7980
if (p == null) {
80-
Context.Respond("Player not found");
81+
Context.Respond("Player is not online or cannot be found!");
8182
return;
8283
}
8384
p.TryGetBalanceInfo(out long balance);
8485
long difference = (balance - amount);
8586
p.RequestChangeBalance(-difference);
86-
ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount} credits!", 10000, "Blue"), p.SteamUserId);
87+
ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount:#,##0} credits!", 10000, "Blue"), p.SteamUserId);
8788
}
8889
else {
8990
foreach (var p in MySession.Static.Players.GetAllPlayers()) {
9091
long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId);
9192
long balance = MyBankingSystem.GetBalance(IdentityID);
9293
long difference = (balance - amount);
9394
MyBankingSystem.ChangeBalance(IdentityID, -difference);
94-
ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount} credits!", 10000, "Blue"), p.SteamId);
95+
ModCommunication.SendMessageTo(new NotificationMessage($"Your balance has been set to {amount:#,##0} credits!", 10000, "Blue"), p.SteamId);
9596
}
9697
}
97-
Context.Respond($"Balance(s) set to {amount}");
98+
Context.Respond($"Balance(s) set to {amount:#,##0}");
9899
}
99100

100101
[Command("reset", "Reset the credits in a users account to 10,000. Use '*' to affect all players")]
@@ -103,7 +104,7 @@ public void EcoReset(string Player) {
103104
if (Player != "*") {
104105
var p = Utilities.GetPlayerByNameOrId(Player);
105106
if (p == null) {
106-
Context.Respond("Player not found");
107+
Context.Respond("Player is not online or cannot be found!");
107108
return;
108109
}
109110
p.TryGetBalanceInfo(out long balance);
@@ -130,14 +131,24 @@ public void EcoTop() {
130131
ecodata.AppendLine("Summary of balanaces accross the server");
131132
Dictionary<ulong, long> balances = new Dictionary<ulong, long>();
132133
foreach (var p in MySession.Static.Players.GetAllPlayers()) {
134+
133135
long IdentityID = MySession.Static.Players.TryGetIdentityId(p.SteamId);
134136
long balance = MyBankingSystem.GetBalance(IdentityID);
135-
balances.Add(p.SteamId, balance);
137+
138+
/*
139+
* Add or Update. We have seen that it is possible to have
140+
* two players with the same SteamID but different SerialIDs.
141+
*
142+
* Those also had different identities. But one of which was dead.
143+
* TryGetIdentityId() Returned the same value in both cases. So no damage done if
144+
* Value is just overwritten.
145+
*/
146+
balances[p.SteamId] = balance;
136147
}
137148
var sorted = balances.OrderByDescending(x => x.Value).ThenBy(x => x.Key);
138149
foreach (var value in sorted) {
139150
var test = MySession.Static.Players.TryGetIdentityNameFromSteamId(value.Key);
140-
ecodata.AppendLine($"Player: {MySession.Static.Players.TryGetIdentityNameFromSteamId(value.Key).ToString()} - Balance: {value.Value.ToString()}");
151+
ecodata.AppendLine($"Player: {MySession.Static.Players.TryGetIdentityNameFromSteamId(value.Key)} - Balance: {value.Value:#,##0}");
141152
}
142153

143154
if (Context.Player == null) {
@@ -150,13 +161,13 @@ public void EcoTop() {
150161
[Command("check", "Check the balance of a specific player")]
151162
[Permission(MyPromoteLevel.None)]
152163
public void EcoCheck(string Player) {
153-
var p = Utilities.GetPlayerByNameOrId(Player);
164+
var p = Utilities.GetIdentityByNameOrIds(Player);
154165
if (p == null) {
155-
Context.Respond("Player is not online or cannot be found!");
166+
Context.Respond("Player cannot be found!");
156167
return;
157168
}
158-
long balance = MyBankingSystem.GetBalance(p.Identity.IdentityId);
159-
Context.Respond($"{p.DisplayName}'s balance is {balance} credits");
169+
long balance = MyBankingSystem.GetBalance(p.IdentityId);
170+
Context.Respond($"{p.DisplayName}'s balance is {balance:#,##0} credits");
160171
}
161172

162173
[Command("pay")]
@@ -171,13 +182,21 @@ public void EcoPay(string Player, long amount) {
171182
Context.Respond("Player is not online or cannot be found!");
172183
return;
173184
}
174-
175-
var finalFromBalance = MyBankingSystem.GetBalance(Context.Player.Identity.IdentityId) - amount;
176-
var finalToBalance = MyBankingSystem.GetBalance(p.Identity.IdentityId) + amount;
185+
186+
var fromIdentitiyId = Context.Player.Identity.IdentityId;
187+
var toIdentitiyId = p.Identity.IdentityId;
188+
189+
if(fromIdentitiyId == toIdentitiyId) {
190+
Context.Respond("You cannot pay yourself!");
191+
return;
192+
}
193+
194+
var finalFromBalance = MyBankingSystem.GetBalance(fromIdentitiyId) - amount;
195+
var finalToBalance = MyBankingSystem.GetBalance(toIdentitiyId) + amount;
177196

178197
MyBankingSystem.RequestTransfer_BroadcastToClients(Context.Player.Identity.IdentityId, p.Identity.IdentityId, amount, finalFromBalance, finalToBalance);
179-
ModCommunication.SendMessageTo(new NotificationMessage($"Your have recieved {amount} credits from {Context.Player}!", 10000, "Blue"),p.SteamUserId);
180-
ModCommunication.SendMessageTo(new NotificationMessage($"Your have sent {amount} credits to {p.DisplayName}!", 10000, "Blue"),Context.Player.SteamUserId);
198+
ModCommunication.SendMessageTo(new NotificationMessage($"Your have recieved {amount:#,##0} credits from {Context.Player.DisplayName}!", 10000, "Blue"),p.SteamUserId);
199+
ModCommunication.SendMessageTo(new NotificationMessage($"Your have sent {amount:#,##0} credits to {p.DisplayName}!", 10000, "Blue"),Context.Player.SteamUserId);
181200
}
182201

183202
}

0 commit comments

Comments
 (0)