Factions/src/com/massivecraft/factions/entity/FPlayerColl.java

153 lines
4.1 KiB
Java
Raw Normal View History

2013-04-22 09:37:53 +02:00
package com.massivecraft.factions.entity;
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;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.Const;
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Rel;
import com.massivecraft.mcore.mixin.Mixin;
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;
import com.massivecraft.mcore.util.TimeUnit;
import com.massivecraft.mcore.xlib.gson.reflect.TypeToken;
2013-04-12 08:56:26 +02:00
public class FPlayerColl extends SenderColl<FPlayer>
{
2013-04-12 08:56:26 +02:00
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
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()
{
2013-04-16 09:28:07 +02:00
super(Const.COLLECTION_BASENAME_PLAYER, FPlayer.class, MStore.getDb(ConfServer.dburi), Factions.get());
}
2013-04-12 08:56:26 +02:00
// -------------------------------------------- //
// OVERRIDE: COLL
2013-04-12 08:56:26 +02:00
// -------------------------------------------- //
// TODO: Init and migration routine!
@Override
2013-04-12 08:56:26 +02:00
public void init()
{
super.init();
this.migrate();
}
public void migrate()
{
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);
}
@Override
protected synchronized String attach(FPlayer entity, Object oid, boolean noteChange)
{
String ret = super.attach(entity, oid, noteChange);
// If inited ...
if (!this.inited()) return ret;
if (!FactionColl.get().inited()) return ret;
// ... update the index.
Faction faction = entity.getFaction();
faction.fplayers.add(entity);
return ret;
}
@Override
public FPlayer detachId(Object oid)
{
FPlayer ret = super.detachId(oid);
if (ret == null) return null;
// If inited ...
if (!this.inited()) return ret;
// ... update the index.
Faction faction = ret.getFaction();
faction.fplayers.remove(ret);
return ret;
}
2013-04-12 08:56:26 +02:00
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
public void clean()
{
2013-04-12 08:56:26 +02:00
for (FPlayer fplayer : this.getAll())
{
if (FactionColl.get().containsId(fplayer.getFactionId())) continue;
Factions.get().log("Reset faction data (invalid faction) for player "+fplayer.getName());
fplayer.resetFactionData(false);
}
}
public void autoLeaveOnInactivityRoutine()
{
if (ConfServer.autoLeaveAfterDaysOfInactivity <= 0.0) return;
long now = System.currentTimeMillis();
double toleranceMillis = ConfServer.autoLeaveAfterDaysOfInactivity * TimeUnit.MILLIS_PER_DAY;
2013-04-12 08:56:26 +02:00
for (FPlayer fplayer : this.getAll())
{
Long lastPlayed = Mixin.getLastPlayed(fplayer.getId());
if (lastPlayed == null) continue;
if (fplayer.isOnline()) continue;
if (now - lastPlayed <= toleranceMillis) continue;
if (MConf.get().logFactionLeave || MConf.get().logFactionKick)
{
Factions.get().log("Player "+fplayer.getName()+" was auto-removed due to inactivity.");
}
// if player is faction leader, sort out the faction since he's going away
if (fplayer.getRole() == Rel.LEADER)
{
Faction faction = fplayer.getFaction();
if (faction != null)
{
fplayer.getFaction().promoteNewLeader();
}
}
fplayer.leave(false);
fplayer.detach();
}
}
}