Refactored Punish a bit so punishments don't expire early or jack up based on server's time.

This commit is contained in:
Jonathan Williams 2014-06-27 20:24:44 -07:00
parent 681dfc5f7c
commit 0287cfb2f6
8 changed files with 18 additions and 59 deletions

View File

@ -8,4 +8,5 @@ public class ClientToken
public int EconomyBalance; public int EconomyBalance;
public AccountToken AccountToken; public AccountToken AccountToken;
public long LastLogin;
} }

View File

@ -58,49 +58,6 @@ public class Punish extends MiniPlugin
@EventHandler @EventHandler
public void OnClientWebResponse(ClientWebResponseEvent event) public void OnClientWebResponse(ClientWebResponseEvent event)
{ {
/*
JsonReader reader = null;
try
{
while (reader.hasNext())
{
if (reader.nextName().equalsIgnoreCase("Punish"))
{
reader.beginObject();
// TODO Parse infractions/punishments here
// PunishClient client = new PunishClient();
// client.AddInfraction(token.Category, new Infraction(token.Reason, token.Admin, token.Time));
// client.AddPunishment(token.Category, new Punishment(token.PunishmentSentence, token.Reason, token.Admin, token.Hours, token.Time));
// _punishClients.put(event.GetClient().GetPlayerName(), client);
break;
}
reader.endObject();
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
*/
PunishClientToken token = new Gson().fromJson(event.GetResponse(), PunishClientToken.class); PunishClientToken token = new Gson().fromJson(event.GetResponse(), PunishClientToken.class);
LoadClient(token); LoadClient(token);
} }
@ -121,7 +78,7 @@ public class Punish extends MiniPlugin
if (client.IsBanned()) if (client.IsBanned())
{ {
Punishment punishment = client.GetPunishment(PunishmentSentence.Ban); Punishment punishment = client.GetPunishment(PunishmentSentence.Ban);
String time = F.time(UtilTime.convertString((long)(punishment.GetHours() * 3600000), 0, TimeUnit.FIT)); String time = F.time(UtilTime.convertString(punishment.GetRemaining(), 0, TimeUnit.FIT));
if (punishment.GetHours() == -1) if (punishment.GetHours() == -1)
time = C.cRed + "Permanent"; time = C.cRed + "Permanent";
@ -234,16 +191,20 @@ public class Punish extends MiniPlugin
} }
} }
} }
}, playerName, category.toString(), sentence, reason, duration, caller == null ? "Mineplex Anti-Cheat" : caller.getName(), severity, System.currentTimeMillis()); }, playerName, category.toString(), sentence, reason, duration, caller == null ? "Mineplex Anti-Cheat" : caller.getName(), severity);
} }
public void LoadClient(PunishClientToken token) public void LoadClient(PunishClientToken token)
{ {
PunishClient client = new PunishClient(); PunishClient client = new PunishClient();
long timeDifference = System.currentTimeMillis() - token.Time;
System.out.println("TimeDifference : " + timeDifference);
for (PunishmentToken punishment : token.Punishments) for (PunishmentToken punishment : token.Punishments)
{ {
client.AddPunishment(Category.valueOf(punishment.Category), new Punishment(punishment.PunishmentId, PunishmentSentence.valueOf(punishment.Sentence), Category.valueOf(punishment.Category), punishment.Reason, punishment.Admin, punishment.Duration, punishment.Severity, punishment.Time, punishment.Active, punishment.Removed, punishment.RemoveAdmin, punishment.RemoveReason)); client.AddPunishment(Category.valueOf(punishment.Category), new Punishment(punishment.PunishmentId, PunishmentSentence.valueOf(punishment.Sentence), Category.valueOf(punishment.Category), punishment.Reason, punishment.Admin, punishment.Duration, punishment.Severity, punishment.Time + timeDifference, punishment.Active, punishment.Removed, punishment.RemoveAdmin, punishment.RemoveReason));
} }
_punishClients.put(token.Name.toLowerCase(), client); _punishClients.put(token.Name.toLowerCase(), client);

View File

@ -20,7 +20,7 @@ public class PunishRepository
_webAddress = webServerAddress; _webAddress = webServerAddress;
} }
public void Punish(Callback<String> callback, String target, String category, PunishmentSentence punishment, String reason, double duration, String admin, int severity, long time) public void Punish(Callback<String> callback, String target, String category, PunishmentSentence punishment, String reason, double duration, String admin, int severity)
{ {
PunishToken token = new PunishToken(); PunishToken token = new PunishToken();
token.Target = target; token.Target = target;
@ -29,7 +29,6 @@ public class PunishRepository
token.Reason = reason; token.Reason = reason;
token.Duration = duration; token.Duration = duration;
token.Admin = admin; token.Admin = admin;
token.Time = time;
token.Severity = severity; token.Severity = severity;
new AsyncJsonWebCall(_webAddress + "PlayerAccount/Punish").Execute(String.class, callback, token); new AsyncJsonWebCall(_webAddress + "PlayerAccount/Punish").Execute(String.class, callback, token);

View File

@ -5,5 +5,6 @@ import java.util.List;
public class PunishClientToken public class PunishClientToken
{ {
public String Name; public String Name;
public long Time;
public List<PunishmentToken> Punishments; public List<PunishmentToken> Punishments;
} }

View File

@ -20,6 +20,7 @@
Name = account.Name; Name = account.Name;
Uuid = account.Uuid; Uuid = account.Uuid;
Rank = account.Rank.Name; Rank = account.Rank.Name;
Time = (long)TimeUtil.GetCurrentMilliseconds();
EconomyBalance = account.EconomyBalance; EconomyBalance = account.EconomyBalance;
@ -118,6 +119,8 @@
public string Rank { get; set; } public string Rank { get; set; }
public long Time { get; set; }
public int EconomyBalance { get; set; } public int EconomyBalance { get; set; }
public List<Punishment> Punishments { get; set; } public List<Punishment> Punishments { get; set; }

View File

@ -69,7 +69,7 @@
{ {
var account = repository.Where<Account>(x => x.Uuid == loginToken.Uuid).FirstOrDefault() ?? CreateAccount(loginToken, repository); var account = repository.Where<Account>(x => x.Uuid == loginToken.Uuid).FirstOrDefault() ?? CreateAccount(loginToken, repository);
account.LoadNavigationProperties(repository.Context); account.LoadNavigationProperties(repository.Context);
var edited = false; account.LastLogin = (long)TimeUtil.GetCurrentMilliseconds();
// Expire punishments // Expire punishments
if (account.Punishments != null) if (account.Punishments != null)
@ -77,7 +77,6 @@
foreach (var expiredPunishment in account.Punishments.Where(x => x.Active && (x.Duration - 0d) > 0 && TimeUtil.GetCurrentMilliseconds() > (x.Time + (x.Duration * 3600000)))) foreach (var expiredPunishment in account.Punishments.Where(x => x.Active && (x.Duration - 0d) > 0 && TimeUtil.GetCurrentMilliseconds() > (x.Time + (x.Duration * 3600000))))
{ {
expiredPunishment.Active = false; expiredPunishment.Active = false;
edited = true;
} }
} }
@ -85,7 +84,6 @@
if (String.IsNullOrEmpty(account.Uuid) && !String.IsNullOrEmpty(loginToken.Uuid)) if (String.IsNullOrEmpty(account.Uuid) && !String.IsNullOrEmpty(loginToken.Uuid))
{ {
account.Uuid = loginToken.Uuid; account.Uuid = loginToken.Uuid;
edited = true;
} }
// Expire ranks // Expire ranks
@ -93,13 +91,9 @@
{ {
account.Rank = repository.Where<Rank>(x => x.Name == "ALL").First(); account.Rank = repository.Where<Rank>(x => x.Name == "ALL").First();
repository.Attach(account.Rank); repository.Attach(account.Rank);
edited = true;
} }
if (edited)
{
repository.CommitChanges(); repository.CommitChanges();
}
return account; return account;
} }
@ -268,7 +262,7 @@
Admin = punish.Admin, Admin = punish.Admin,
Category = punish.Category, Category = punish.Category,
Sentence = punish.Sentence, Sentence = punish.Sentence,
Time = punish.Time, Time = (long)TimeUtil.GetCurrentMilliseconds(),
Reason = punish.Reason, Reason = punish.Reason,
Duration = punish.Duration, Duration = punish.Duration,
Severity = punish.Severity, Severity = punish.Severity,
@ -308,7 +302,7 @@
punishment.Active = false; punishment.Active = false;
punishment.Removed = true; punishment.Removed = true;
punishment.RemoveAdmin = token.Admin; punishment.RemoveAdmin = token.Admin;
punishment.RemoveTime = DateTime.Now.Ticks; punishment.RemoveTime = (long)TimeUtil.GetCurrentMilliseconds();
punishment.RemoveReason = token.Reason; punishment.RemoveReason = token.Reason;
repository.Edit(punishment); repository.Edit(punishment);

Binary file not shown.