2014-09-17 15:51:09 +02:00
|
|
|
package com.massivecraft.factions.update;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2014-10-01 22:00:08 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2014-09-17 15:51:09 +02:00
|
|
|
import java.util.List;
|
2014-10-01 22:00:08 +02:00
|
|
|
import java.util.Set;
|
2014-09-17 15:51:09 +02:00
|
|
|
|
|
|
|
import com.massivecraft.factions.Const;
|
|
|
|
import com.massivecraft.factions.Factions;
|
2014-10-01 22:00:08 +02:00
|
|
|
import com.massivecraft.factions.TerritoryAccess;
|
|
|
|
import com.massivecraft.factions.entity.Board;
|
|
|
|
import com.massivecraft.factions.entity.BoardColl;
|
|
|
|
import com.massivecraft.factions.entity.Faction;
|
|
|
|
import com.massivecraft.factions.entity.FactionColl;
|
2014-09-17 15:51:09 +02:00
|
|
|
import com.massivecraft.factions.entity.MConf;
|
|
|
|
import com.massivecraft.massivecore.MassiveCore;
|
|
|
|
import com.massivecraft.massivecore.store.Coll;
|
|
|
|
import com.massivecraft.massivecore.store.Db;
|
2015-11-08 17:26:36 +01:00
|
|
|
import com.massivecraft.massivecore.store.Entity;
|
2014-09-17 15:51:09 +02:00
|
|
|
import com.massivecraft.massivecore.store.MStore;
|
|
|
|
import com.massivecraft.massivecore.util.MUtil;
|
|
|
|
|
|
|
|
public class UpdateUtil
|
|
|
|
{
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// CONSTANTS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static final List<String> oldCollnamePrefixes = MUtil.list(
|
|
|
|
"factions_board@",
|
|
|
|
"factions_faction@",
|
|
|
|
"factions_uplayer@",
|
|
|
|
"factions_uconf@"
|
|
|
|
);
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// UPDATE
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// Assumes the MConfColl has been inited!
|
|
|
|
|
|
|
|
public static void update()
|
|
|
|
{
|
|
|
|
// Select the old universe of our attention ...
|
|
|
|
String universe = getUniverse();
|
|
|
|
if (universe == null) return;
|
|
|
|
|
2014-09-17 16:15:33 +02:00
|
|
|
Factions.get().log("Updating Database to New Version!");
|
|
|
|
|
2014-09-17 15:51:09 +02:00
|
|
|
// ... load the old uconf data ...
|
|
|
|
OldConfColls.get().init();
|
|
|
|
OldConf oldConf = OldConfColls.get().getForUniverse(universe).get(MassiveCore.INSTANCE, true);
|
|
|
|
|
|
|
|
// ... transfer the old uconf data over to the new mconf ...
|
|
|
|
oldConf.transferTo(MConf.get());
|
2014-09-18 00:18:07 +02:00
|
|
|
MConf.get().changed();
|
|
|
|
MConf.get().sync();
|
2014-09-17 15:51:09 +02:00
|
|
|
|
|
|
|
// ... rename target collections ...
|
|
|
|
Db db = MStore.getDb();
|
|
|
|
|
2014-09-17 16:15:33 +02:00
|
|
|
// The old mplayer data we don't care much for.
|
|
|
|
// Could even delete it but let's just move it out of the way.
|
2015-03-05 16:12:06 +01:00
|
|
|
db.renameColl(Const.COLLECTION_MPLAYER, "old_"+Const.COLLECTION_MPLAYER);
|
2014-09-17 16:15:33 +02:00
|
|
|
|
2015-03-05 16:12:06 +01:00
|
|
|
db.renameColl("factions_board@" + universe, Const.COLLECTION_BOARD);
|
|
|
|
db.renameColl("factions_faction@" + universe, Const.COLLECTION_FACTION);
|
|
|
|
db.renameColl("factions_uplayer@" + universe, Const.COLLECTION_MPLAYER);
|
2014-09-17 15:51:09 +02:00
|
|
|
|
|
|
|
// ... rename remaining collections ...
|
|
|
|
for (String collname : db.getCollnames())
|
|
|
|
{
|
|
|
|
if (!collname.startsWith("factions_")) continue;
|
|
|
|
if (!collname.contains("@")) continue;
|
2015-03-05 16:12:06 +01:00
|
|
|
db.renameColl(collname, "old_" + collname);
|
2014-09-17 15:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------- //
|
|
|
|
// UNIVERSE SELECTION
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static String getUniverse()
|
|
|
|
{
|
|
|
|
List<String> universes = getUniverses();
|
|
|
|
|
|
|
|
String ret = null;
|
|
|
|
int best = -1;
|
|
|
|
|
|
|
|
for (String universe : universes)
|
|
|
|
{
|
|
|
|
int count = getUniverseFactionCount(universe);
|
|
|
|
if (count > 0 && count > best)
|
|
|
|
{
|
|
|
|
ret = universe;
|
|
|
|
best = count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-08 17:26:36 +01:00
|
|
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
2014-09-17 15:51:09 +02:00
|
|
|
public static int getUniverseFactionCount(String universe)
|
|
|
|
{
|
2015-11-08 17:26:36 +01:00
|
|
|
Coll coll = new Coll("factions_faction@"+universe, Entity.class, MStore.getDb(), Factions.get());
|
2014-09-17 15:51:09 +02:00
|
|
|
|
2015-03-05 16:12:06 +01:00
|
|
|
Collection<String> ids = MStore.getDb().getIds(coll);
|
2014-09-17 15:51:09 +02:00
|
|
|
|
|
|
|
return ids.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> getUniverses()
|
|
|
|
{
|
|
|
|
List<String> ret = new ArrayList<String>();
|
|
|
|
|
|
|
|
for (String collname : MStore.getDb().getCollnames())
|
|
|
|
{
|
|
|
|
for (String prefix : oldCollnamePrefixes)
|
|
|
|
{
|
|
|
|
if (collname.startsWith(prefix))
|
|
|
|
{
|
|
|
|
ret.add(collname.substring(prefix.length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-01 22:00:08 +02:00
|
|
|
// -------------------------------------------- //
|
|
|
|
// UPDATE SPECIAL IDS
|
|
|
|
// -------------------------------------------- //
|
|
|
|
|
|
|
|
public static void updateSpecialIds()
|
|
|
|
{
|
|
|
|
if (MConf.get().factionIdNone != null)
|
|
|
|
{
|
|
|
|
updateSpecialId(MConf.get().factionIdNone, Factions.ID_NONE);
|
|
|
|
MConf.get().factionIdNone = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MConf.get().factionIdSafezone != null)
|
|
|
|
{
|
|
|
|
updateSpecialId(MConf.get().factionIdSafezone, Factions.ID_SAFEZONE);
|
|
|
|
MConf.get().factionIdSafezone = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MConf.get().factionIdWarzone != null)
|
|
|
|
{
|
|
|
|
updateSpecialId(MConf.get().factionIdWarzone, Factions.ID_WARZONE);
|
|
|
|
MConf.get().factionIdWarzone = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
MConf.get().sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateSpecialId(String from, String to)
|
|
|
|
{
|
|
|
|
// Get the coll.
|
|
|
|
FactionColl coll = FactionColl.get();
|
|
|
|
|
2014-12-05 00:34:48 +01:00
|
|
|
// Get the faction and detach it
|
|
|
|
Faction faction = coll.detachId(from);
|
|
|
|
if (faction == null) return;
|
|
|
|
coll.syncId(from);
|
|
|
|
|
2014-10-01 22:00:08 +02:00
|
|
|
// A faction may already be occupying the to-id.
|
|
|
|
// We must remove it to make space for renaming.
|
|
|
|
// This faction is simply an auto-created faction with no references yet.
|
|
|
|
coll.detachId(to);
|
|
|
|
coll.syncId(to);
|
|
|
|
|
|
|
|
// Attach it
|
|
|
|
coll.attach(faction, to);
|
|
|
|
coll.syncId(to);
|
|
|
|
|
|
|
|
// Update that config special config option.
|
|
|
|
if (MConf.get().defaultPlayerFactionId.equals(from))
|
|
|
|
{
|
|
|
|
MConf.get().defaultPlayerFactionId = to;
|
|
|
|
MConf.get().sync();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all board entries.
|
|
|
|
updateBoards(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateBoards(String from, String to)
|
|
|
|
{
|
|
|
|
for (Board board : BoardColl.get().getAll())
|
|
|
|
{
|
|
|
|
updateBoard(board, from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateBoard(Board board, String from, String to)
|
|
|
|
{
|
|
|
|
boolean changed = false;
|
|
|
|
for (TerritoryAccess ta : board.getMap().values())
|
|
|
|
{
|
|
|
|
changed |= updateTerritoryAccess(ta, from, to);
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
board.changed();
|
|
|
|
board.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean updateTerritoryAccess(TerritoryAccess entity, String from, String to)
|
|
|
|
{
|
|
|
|
boolean changed = false;
|
|
|
|
changed |= updateTerritoryHostFactionId(entity, from, to);
|
|
|
|
changed |= updateTerritoryAccessFactionIds(entity, from, to);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean updateTerritoryHostFactionId(TerritoryAccess entity, String from, String to)
|
|
|
|
{
|
|
|
|
String before = entity.hostFactionId;
|
|
|
|
if (before == null) return false;
|
|
|
|
if (!before.equals(from)) return false;
|
|
|
|
|
|
|
|
entity.hostFactionId = to;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean updateTerritoryAccessFactionIds(TerritoryAccess entity, String from, String to)
|
|
|
|
{
|
|
|
|
// Before and After
|
|
|
|
Set<String> before = entity.factionIds;
|
|
|
|
if (before == null) return false;
|
|
|
|
Set<String> after = new LinkedHashSet<String>();
|
|
|
|
for (String id : before)
|
|
|
|
{
|
|
|
|
if (id == null) continue;
|
|
|
|
if (id.equals(from))
|
|
|
|
{
|
|
|
|
after.add(to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
after.add(from);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoChange
|
|
|
|
if (MUtil.equals(before, after)) return false;
|
|
|
|
|
|
|
|
// Apply
|
|
|
|
entity.factionIds = after;
|
|
|
|
//entity.sync();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-17 15:51:09 +02:00
|
|
|
}
|