Invitations as inner entities

This commit is contained in:
Magnus Ulf Jørgensen 2017-04-20 11:05:56 +02:00
parent f10da26159
commit 60c9d7fcac
11 changed files with 199 additions and 80 deletions

View File

@ -6,13 +6,14 @@ import com.massivecraft.factions.entity.Board;
import com.massivecraft.factions.entity.BoardColl; import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.FactionColl; import com.massivecraft.factions.entity.FactionColl;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.entity.MPlayerColl; import com.massivecraft.factions.entity.MPlayerColl;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -70,11 +71,11 @@ public class CmdFactionsClean extends FactionsCommand
for (Faction faction : FactionColl.get().getAll()) for (Faction faction : FactionColl.get().getAll())
{ {
Collection<String> invitedPlayerIds = faction.getInvitedPlayerIds(); EntityInternalMap<Invitation> invitations = faction.getInvitations();
if (invitedPlayerIds.isEmpty()) continue; if (invitations.isEmpty()) continue;
ret += invitedPlayerIds.size(); ret += invitations.size();
invitedPlayerIds.clear(); invitations.clear();
faction.changed(); faction.changed();
} }

View File

@ -1,12 +1,14 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.cmd.type.TypeMPlayer; import com.massivecraft.factions.cmd.type.TypeMPlayer;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer; import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.factions.event.EventFactionsInvitedChange; import com.massivecraft.factions.event.EventFactionsInvitedChange;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.command.type.container.TypeSet; import com.massivecraft.massivecore.command.type.container.TypeSet;
import com.massivecraft.massivecore.mson.Mson; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -34,6 +36,9 @@ public class CmdFactionsInviteAdd extends FactionsCommand
// Args // Args
Collection<MPlayer> mplayers = this.readArg(); Collection<MPlayer> mplayers = this.readArg();
String senderId = IdUtil.getId(sender);
long creationMillis = System.currentTimeMillis();
// MPerm // MPerm
if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return;
@ -62,7 +67,8 @@ public class CmdFactionsInviteAdd extends FactionsCommand
msenderFaction.msg("%s<i> invited %s<i> to your faction.", msender.describeTo(msenderFaction, true), mplayer.describeTo(msenderFaction)); msenderFaction.msg("%s<i> invited %s<i> to your faction.", msender.describeTo(msenderFaction, true), mplayer.describeTo(msenderFaction));
// Apply // Apply
msenderFaction.setInvited(mplayer, true); Invitation invitation = new Invitation(senderId, creationMillis);
msenderFaction.invite(mplayer.getId(), invitation);
msenderFaction.changed(); msenderFaction.changed();
} }
else else

View File

@ -1,18 +1,26 @@
package com.massivecraft.factions.cmd; package com.massivecraft.factions.cmd;
import com.massivecraft.factions.Perm; import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction; import com.massivecraft.factions.cmd.type.TypeFaction;
import com.massivecraft.factions.entity.Faction; import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.Invitation;
import com.massivecraft.factions.entity.MPerm; import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.command.Parameter; import com.massivecraft.massivecore.command.Parameter;
import com.massivecraft.massivecore.comparator.ComparatorSmart;
import com.massivecraft.massivecore.mixin.MixinDisplayName;
import com.massivecraft.massivecore.pager.Pager; import com.massivecraft.massivecore.pager.Pager;
import com.massivecraft.massivecore.pager.Stringifier; import com.massivecraft.massivecore.pager.Stringifier;
import com.massivecraft.massivecore.util.TimeDiffUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
public class CmdFactionsInviteList extends FactionsCommand public class CmdFactionsInviteList extends FactionsCommand
{ {
@ -45,25 +53,39 @@ public class CmdFactionsInviteList extends FactionsCommand
if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return; if ( ! MPerm.getPermInvite().has(msender, msenderFaction, true)) return;
// Pager Create // Pager Create
final List<MPlayer> mplayers = faction.getInvitedMPlayers(); final List<Entry<String, Invitation>> invitations = new MassiveList<>(faction.getInvitations().entrySet());
final Pager<MPlayer> pager = new Pager<>(this, "Invited Players List", page, mplayers, new Stringifier<MPlayer>()
Collections.sort(invitations, new Comparator<Entry<String, Invitation>>()
{ {
public String toString(MPlayer target, int index) @Override
public int compare(Entry<String, Invitation> i1, Entry<String, Invitation> i2)
{ {
// TODO: Madus would like to implement this in MPlayer return ComparatorSmart.get().compare(i2.getValue().getCreationMillis(), i1.getValue().getCreationMillis());
String targetName = target.getDisplayName(msender); }
String isAre = target == msender ? "are" : "is"; });
Rel targetRank = target.getRole();
Faction targetFaction = target.getFaction(); final long now = System.currentTimeMillis();
String theAan = targetRank == Rel.LEADER ? "the" : Txt.aan(targetRank.name());
String rankName = Txt.getNicedEnum(targetRank).toLowerCase(); final Pager<Entry<String, Invitation>> pager = new Pager<>(this, "Invited Players List", page, invitations, new Stringifier<Entry<String, Invitation>>()
String ofIn = targetRank == Rel.LEADER ? "of" : "in"; {
String factionName = targetFaction.describeTo(msender, true); public String toString(Entry<String, Invitation> entry, int index)
if (targetFaction == msenderFaction) {
String inviteeId = entry.getKey();
String inviterId = entry.getValue().getInviterId();
String inviteeDisplayName = MixinDisplayName.get().getDisplayName(inviteeId, sender);
String inviterDisplayName = inviterId != null ? MixinDisplayName.get().getDisplayName(inviterId, sender) : Txt.parse("<silver>unknown");
String ageDesc = "";
if (entry.getValue().getCreationMillis() != null)
{ {
factionName = factionName.toLowerCase(); long millis = now - entry.getValue().getCreationMillis();
LinkedHashMap<TimeUnit, Long> ageUnitcounts = TimeDiffUtil.limit(TimeDiffUtil.unitcounts(millis, TimeUnit.getAllButMillis()), 2);
ageDesc = TimeDiffUtil.formatedMinimal(ageUnitcounts, "<i>");
ageDesc = " " + ageDesc + Txt.parse(" ago");
} }
return Txt.parse("%s <i>%s %s <h>%s <i>%s %s<i>.", targetName, isAre, theAan, rankName, ofIn, factionName);
return Txt.parse("%s<i> was invited by %s<reset>%s<i>.", inviteeDisplayName, inviterDisplayName, ageDesc);
} }
}); });

View File

@ -39,15 +39,18 @@ public class CmdFactionsInviteRemove extends FactionsCommand
// Args // Args
if ("all".equalsIgnoreCase(this.argAt(0))) if ("all".equalsIgnoreCase(this.argAt(0)))
{ {
List<MPlayer> invitedPlayers = msenderFaction.getInvitedMPlayers(); Set<String> ids = msenderFaction.getInvitations().keySet();
// Doesn't show up if list is empty. Test at home if it worked. // Doesn't show up if list is empty. Test at home if it worked.
if (invitedPlayers == null || invitedPlayers.isEmpty()) if (ids == null || ids.isEmpty())
{ {
msg("<b>Your faction has not invited anyone."); throw new MassiveException().addMsg("<b>No one is invited to your faction.");
return;
} }
all = true; all = true;
mplayers.addAll(invitedPlayers);
for (String id : ids)
{
mplayers.add(MPlayer.get(id));
}
} }
else else
{ {
@ -98,7 +101,7 @@ public class CmdFactionsInviteRemove extends FactionsCommand
} }
// Apply // Apply
msenderFaction.setInvited(mplayer, false); msenderFaction.uninvite(msender);
// If all, we do this at last. So we only do it once. // If all, we do this at last. So we only do it once.
if (! all) msenderFaction.changed(); if (! all) msenderFaction.changed();

View File

@ -117,7 +117,7 @@ public class CmdFactionsJoin extends FactionsCommand
mplayer.resetFactionData(); mplayer.resetFactionData();
mplayer.setFaction(faction); mplayer.setFaction(faction);
faction.setInvited(mplayer, false); faction.uninvite(mplayer);
// Derplog // Derplog
if (MConf.get().logFactionJoin) if (MConf.get().logFactionJoin)

View File

@ -92,7 +92,7 @@ public class CmdFactionsKick extends FactionsCommand
{ {
mplayerFaction.promoteNewLeader(); mplayerFaction.promoteNewLeader();
} }
mplayerFaction.setInvited(mplayer, false); mplayerFaction.uninvite(mplayer);
mplayer.resetFactionData(); mplayer.resetFactionData();
} }

View File

@ -281,7 +281,7 @@ public class CmdFactionsRank extends FactionsCommand
target.setFaction(endFaction); target.setFaction(endFaction);
// No longer invited. // No longer invited.
endFaction.setInvited(target, false); endFaction.uninvite(target);
// Create recipients // Create recipients
Set<MPlayer> recipients = new HashSet<>(); Set<MPlayer> recipients = new HashSet<>();

View File

@ -13,7 +13,6 @@ import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveMapDef; import com.massivecraft.massivecore.collections.MassiveMapDef;
import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.collections.MassiveSetDef;
import com.massivecraft.massivecore.mixin.MixinMessage; import com.massivecraft.massivecore.mixin.MixinMessage;
import com.massivecraft.massivecore.money.Money; import com.massivecraft.massivecore.money.Money;
import com.massivecraft.massivecore.predicate.Predicate; import com.massivecraft.massivecore.predicate.Predicate;
@ -21,6 +20,7 @@ import com.massivecraft.massivecore.predicate.PredicateAnd;
import com.massivecraft.massivecore.predicate.PredicateVisibleTo; import com.massivecraft.massivecore.predicate.PredicateVisibleTo;
import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.store.Entity; import com.massivecraft.massivecore.store.Entity;
import com.massivecraft.massivecore.store.EntityInternalMap;
import com.massivecraft.massivecore.store.SenderColl; import com.massivecraft.massivecore.store.SenderColl;
import com.massivecraft.massivecore.util.IdUtil; import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
@ -70,7 +70,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
this.setCreatedAtMillis(that.createdAtMillis); this.setCreatedAtMillis(that.createdAtMillis);
this.setHome(that.home); this.setHome(that.home);
this.setPowerBoost(that.powerBoost); this.setPowerBoost(that.powerBoost);
this.setInvitedPlayerIds(that.invitedPlayerIds); this.invitations.load(that.invitations);
this.setRelationWishes(that.relationWishes); this.setRelationWishes(that.relationWishes);
this.setFlagIds(that.flags); this.setFlagIds(that.flags);
this.setPermIds(that.perms); this.setPermIds(that.perms);
@ -92,6 +92,12 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
} }
} }
// -------------------------------------------- //
// VERSION
// -------------------------------------------- //
public int version = 1;
// -------------------------------------------- // // -------------------------------------------- //
// FIELDS: RAW // FIELDS: RAW
// -------------------------------------------- // // -------------------------------------------- //
@ -136,7 +142,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
// This is the ids of the invited players. // This is the ids of the invited players.
// They are actually "senderIds" since you can invite "@console" to your faction. // They are actually "senderIds" since you can invite "@console" to your faction.
// Null means no one is invited // Null means no one is invited
private MassiveSetDef<String> invitedPlayerIds = new MassiveSetDef<>(); private EntityInternalMap<Invitation> invitations = new EntityInternalMap<>(this, Invitation.class);
// The keys in this map are factionIds. // The keys in this map are factionIds.
// Null means no special relation whishes. // Null means no special relation whishes.
@ -436,31 +442,14 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
// RAW // RAW
public Set<String> getInvitedPlayerIds()
{
return this.invitedPlayerIds;
}
public void setInvitedPlayerIds(Collection<String> invitedPlayerIds) public EntityInternalMap<Invitation> getInvitations() { return this.invitations; }
{
// Clean input
MassiveSetDef<String> target = new MassiveSetDef<>(invitedPlayerIds);
// Detect Nochange
if (MUtil.equals(this.invitedPlayerIds, target)) return;
// Apply
this.invitedPlayerIds = target;
// Mark as changed
this.changed();
}
// FINER // FINER
public boolean isInvited(String playerId) public boolean isInvited(String playerId)
{ {
return this.getInvitedPlayerIds().contains(playerId); return this.getInvitations().containsKey(playerId);
} }
public boolean isInvited(MPlayer mplayer) public boolean isInvited(MPlayer mplayer)
@ -468,38 +457,20 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
return this.isInvited(mplayer.getId()); return this.isInvited(mplayer.getId());
} }
public boolean setInvited(String playerId, boolean invited) public boolean uninvite(String playerId)
{ {
List<String> invitedPlayerIds = new MassiveList<>(this.getInvitedPlayerIds()); return this.getInvitations().remove(playerId) != null;
boolean ret;
if (invited)
{
ret = invitedPlayerIds.add(playerId);
}
else
{
ret = invitedPlayerIds.remove(playerId);
}
this.setInvitedPlayerIds(invitedPlayerIds);
return ret;
} }
public void setInvited(MPlayer mplayer, boolean invited) public boolean uninvite(MPlayer mplayer)
{ {
this.setInvited(mplayer.getId(), invited); return uninvite(mplayer.getId());
} }
public List<MPlayer> getInvitedMPlayers() public void invite(String playerId, Invitation invitation)
{ {
List<MPlayer> mplayers = new MassiveList<>(); uninvite(playerId);
this.invitations.attach(invitation, playerId);
for (String id : this.getInvitedPlayerIds())
{
MPlayer mplayer = MPlayer.get(id);
mplayers.add(mplayer);
}
return mplayers;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -0,0 +1,47 @@
package com.massivecraft.factions.entity;
import com.massivecraft.massivecore.store.EntityInternal;
public class Invitation extends EntityInternal<Invitation>
{
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public Invitation load(Invitation that)
{
this.inviterId = that.inviterId;
this.creationMillis = that.creationMillis;
return this;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private String inviterId;
public String getInviterId() { return inviterId; }
public void setInviterId(String inviterId) { this.inviterId = inviterId; }
private Long creationMillis;
public Long getCreationMillis() { return creationMillis; }
public void setCreationMillis(Long creationMillis) { this.creationMillis = creationMillis; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public Invitation()
{
this(null, null);
}
public Invitation(String inviterId, Long creationMillis)
{
this.inviterId = inviterId;
this.creationMillis = creationMillis;
}
}

View File

@ -70,7 +70,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
return this; return this;
} }
// -------------------------------------------- // // -------------------------------------------- //
// IS DEFAULT // IS DEFAULT
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -0,0 +1,69 @@
package com.massivecraft.factions.entity.migrator;
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.massivecore.store.migrator.MigratorFieldConvert;
import com.massivecraft.massivecore.store.migrator.MigratorFieldRename;
import com.massivecraft.massivecore.store.migrator.MigratorRoot;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonObject;
public class MigratorFaction001Invitations extends MigratorRoot
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations i = new com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations();
public static com.massivecraft.factions.entity.migrator.MigratorFaction001Invitations get() { return i; }
private MigratorFaction001Invitations()
{
super(Faction.class);
this.addInnerMigrator(MigratorFieldRename.get("invitedPlayerIds", "invitations"));
this.addInnerMigrator(new MigratorFaction001InvitationsField());
}
public class MigratorFaction001InvitationsField extends MigratorFieldConvert
{
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
private MigratorFaction001InvitationsField()
{
super("invitations");
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
public Object migrateInner(JsonElement idList)
{
JsonObject ret = new JsonObject();
//EntityInternalMap<Invitation> ret = new EntityInternalMap<>(null, Invitation.class);
// If non-null
if (!idList.isJsonNull())
{
// ... and proper type ...
if (!idList.isJsonArray()) throw new IllegalArgumentException(idList.toString());
// ... fill!
for (JsonElement playerId : idList.getAsJsonArray())
{
String id = playerId.getAsString();
// Create invitation
JsonObject invitation = new JsonObject();
// Attach
ret.add(id, invitation);
}
}
return ret;
}
}
}