2011-10-08 22:03:44 +02:00
|
|
|
package com.massivecraft.factions;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.Map;
|
2013-04-12 08:56:26 +02:00
|
|
|
import java.util.Map.Entry;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
import com.massivecraft.mcore.store.MStore;
|
|
|
|
import com.massivecraft.mcore.store.SenderColl;
|
|
|
|
import com.massivecraft.mcore.util.DiscUtil;
|
2013-04-10 10:18:34 +02:00
|
|
|
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
public class FPlayerColl extends SenderColl<FPlayer>
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// INSTANCE & CONSTRUCT
|
|
|
|
// -------------------------------------------- //
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
private static FPlayerColl i = new FPlayerColl();
|
|
|
|
public static FPlayerColl get() { return i; }
|
2013-04-09 13:22:23 +02:00
|
|
|
private FPlayerColl()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 08:56:26 +02:00
|
|
|
super(MStore.getDb(ConfServer.dburi), Factions.get(), Const.COLLECTION_BASENAME_PLAYER, FPlayer.class, true, true);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// OVERRIDE
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
// TODO: Init and migration routine!
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
@Override
|
2013-04-12 08:56:26 +02:00
|
|
|
public void init()
|
|
|
|
{
|
|
|
|
super.init();
|
|
|
|
|
|
|
|
this.migrate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void migrate()
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-12 08:56:26 +02:00
|
|
|
// Create file objects
|
|
|
|
File oldFile = new File(Factions.get().getDataFolder(), "players.json");
|
|
|
|
File newFile = new File(Factions.get().getDataFolder(), "players.json.migrated");
|
|
|
|
|
|
|
|
// Already migrated?
|
|
|
|
if ( ! oldFile.exists()) return;
|
|
|
|
|
|
|
|
// Read the file content through GSON.
|
|
|
|
Type type = new TypeToken<Map<String, FPlayer>>(){}.getType();
|
|
|
|
Map<String, FPlayer> id2fplayer = Factions.get().gson.fromJson(DiscUtil.readCatch(oldFile), type);
|
|
|
|
|
|
|
|
// Set the data
|
|
|
|
for (Entry<String, FPlayer> entry : id2fplayer.entrySet())
|
|
|
|
{
|
|
|
|
String playerId = entry.getKey();
|
|
|
|
FPlayer fplayer = entry.getValue();
|
|
|
|
FPlayerColl.get().create(playerId).load(fplayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as migrated
|
|
|
|
oldFile.renameTo(newFile);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// EXTRAS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
public void clean()
|
|
|
|
{
|
2013-04-12 08:56:26 +02:00
|
|
|
for (FPlayer fplayer : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-09 12:58:39 +02:00
|
|
|
if ( ! FactionColl.i.exists(fplayer.getFactionId()))
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-10 08:40:41 +02:00
|
|
|
Factions.get().log("Reset faction data (invalid faction) for player "+fplayer.getName());
|
2012-01-07 22:56:17 +01:00
|
|
|
fplayer.resetFactionData(false);
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void autoLeaveOnInactivityRoutine()
|
|
|
|
{
|
2013-04-09 13:15:25 +02:00
|
|
|
if (ConfServer.autoLeaveAfterDaysOfInactivity <= 0.0)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long now = System.currentTimeMillis();
|
2013-04-09 13:15:25 +02:00
|
|
|
double toleranceMillis = ConfServer.autoLeaveAfterDaysOfInactivity * 24 * 60 * 60 * 1000;
|
2011-10-08 22:03:44 +02:00
|
|
|
|
2013-04-12 08:56:26 +02:00
|
|
|
for (FPlayer fplayer : this.getAll())
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2012-02-26 23:55:58 +01:00
|
|
|
if (fplayer.isOffline() && now - fplayer.getLastLoginTime() > toleranceMillis)
|
2011-10-08 22:03:44 +02:00
|
|
|
{
|
2013-04-09 13:15:25 +02:00
|
|
|
if (ConfServer.logFactionLeave || ConfServer.logFactionKick)
|
2013-04-09 13:12:13 +02:00
|
|
|
Factions.get().log("Player "+fplayer.getName()+" was auto-removed due to inactivity.");
|
2011-12-18 14:50:41 +01:00
|
|
|
|
|
|
|
// if player is faction leader, sort out the faction since he's going away
|
|
|
|
if (fplayer.getRole() == Rel.LEADER)
|
2012-01-28 10:16:25 +01:00
|
|
|
{
|
|
|
|
Faction faction = fplayer.getFaction();
|
|
|
|
if (faction != null)
|
|
|
|
fplayer.getFaction().promoteNewLeader();
|
|
|
|
}
|
2011-12-18 14:50:41 +01:00
|
|
|
|
2011-10-08 22:03:44 +02:00
|
|
|
fplayer.leave(false);
|
2012-01-10 04:37:16 +01:00
|
|
|
fplayer.detach();
|
2011-10-08 22:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|