Add faction votes

This commit is contained in:
Magnus Ulf
2019-01-19 19:27:54 +01:00
parent 1157607397
commit 7171d43a2a
19 changed files with 573 additions and 6 deletions

View File

@ -77,6 +77,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
this.setPowerBoost(that.powerBoost);
this.invitations.load(that.invitations);
this.ranks.load(that.ranks);
this.votes.load(that.votes);
this.setRelationWishes(that.relationWishes);
this.setFlagIds(that.flags);
this.perms = that.perms;
@ -143,12 +144,14 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
// This is the ids of the invited players.
// They are actually "senderIds" since you can invite "@console" to your faction.
// Null means no one is invited
private EntityInternalMap<Invitation> invitations = new EntityInternalMap<>(this, Invitation.class);
// This is where all the ranks are, they are faction specific
private EntityInternalMap<Rank> ranks = this.createRankMap();
// This is the votes currently open in the faction
private EntityInternalMap<Vote> votes = new EntityInternalMap<>(this, Vote.class);
// The keys in this map are factionIds.
// Null means no special relation whishes.
private MassiveMapDef<String, Rel> relationWishes = new MassiveMapDef<>();
@ -569,6 +572,26 @@ public class Faction extends Entity<Faction> implements FactionsParticipator, MP
return ret;
}
// -------------------------------------------- //
// FIELD: votes
// -------------------------------------------- //
// RAW
public EntityInternalMap<Vote> getVotes() { return this.votes; }
public void addVote(Vote vote)
{
if (vote == null) throw new NullPointerException("vote");
this.getVotes().attach(vote);
}
public Optional<Vote> getVoteByName(String name)
{
if (name == null) throw new NullPointerException("name");
return this.getVotes().getAll().stream().filter(vote -> vote.getName().equalsIgnoreCase(name)).findFirst();
}
// -------------------------------------------- //
// FIELD: relationWish
// -------------------------------------------- //

View File

@ -52,6 +52,8 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
public final static transient String ID_WITHDRAW = "withdraw";
public final static transient String ID_TERRITORY = "territory";
public final static transient String ID_ACCESS = "access";
public final static transient String ID_VOTE = "VOTE";
public final static transient String ID_CREATEVOTE = "createvote";
public final static transient String ID_CLAIMNEAR = "claimnear";
public final static transient String ID_REL = "rel";
public final static transient String ID_DISBAND = "disband";
@ -78,6 +80,8 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
public final static transient int PRIORITY_WITHDRAW = 16000;
public final static transient int PRIORITY_TERRITORY = 17000;
public final static transient int PRIORITY_ACCESS = 18000;
public final static transient int PRIORITY_VOTE = 18200;
public final static transient int PRIORITY_CREATEVOTE = 18600;
public final static transient int PRIORITY_CLAIMNEAR = 19000;
public final static transient int PRIORITY_REL = 20000;
public final static transient int PRIORITY_DISBAND = 21000;
@ -128,6 +132,8 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
getPermWithdraw();
getPermTerritory();
getPermAccess();
getPermVote();
getPermCreateVote();
getPermClaimnear();
getPermRel();
getPermDisband();
@ -155,6 +161,8 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
public static MPerm getPermWithdraw() { return getCreative(PRIORITY_WITHDRAW, ID_WITHDRAW, ID_WITHDRAW, "withdraw money", MUtil.set("LEADER"), false, true, true); }
public static MPerm getPermTerritory() { return getCreative(PRIORITY_TERRITORY, ID_TERRITORY, ID_TERRITORY, "claim or unclaim", MUtil.set("LEADER", "OFFICER"), false, true, true); }
public static MPerm getPermAccess() { return getCreative(PRIORITY_ACCESS, ID_ACCESS, ID_ACCESS, "grant territory", MUtil.set("LEADER", "OFFICER"), false, true, true); }
public static MPerm getPermVote() { return getCreative(PRIORITY_VOTE, ID_VOTE, ID_VOTE, "vote", MUtil.set("LEADER", "OFFICER", "MEMBER", "RECRUIT"), false, true, true); }
public static MPerm getPermCreateVote() { return getCreative(PRIORITY_CREATEVOTE, ID_CREATEVOTE, ID_CREATEVOTE, "manage votes", MUtil.set("LEADER", "OFFICER"), false, true, true); }
public static MPerm getPermClaimnear() { return getCreative(PRIORITY_CLAIMNEAR, ID_CLAIMNEAR, ID_CLAIMNEAR, "claim nearby", MUtil.set("LEADER", "OFFICER", "MEMBER", "RECRUIT", "ALLY"), false, false, false); } // non editable, non visible.
public static MPerm getPermRel() { return getCreative(PRIORITY_REL, ID_REL, ID_REL, "change relations", MUtil.set("LEADER", "OFFICER"), false, true, true); }
public static MPerm getPermDisband() { return getCreative(PRIORITY_DISBAND, ID_DISBAND, ID_DISBAND, "disband the faction", MUtil.set("LEADER"), false, true, true); }
@ -203,6 +211,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
private transient boolean registered = false;
public boolean isRegistered() { return this.registered; }
public void setRegistered(boolean registered) { this.registered = registered; }
// -------------------------------------------- //
// VERSION
// -------------------------------------------- //

View File

@ -0,0 +1,127 @@
package com.massivecraft.factions.entity;
import com.massivecraft.massivecore.Named;
import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.store.EntityInternal;
import com.massivecraft.massivecore.store.EntityInternalMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Vote extends EntityInternal<Vote> implements Named
{
// -------------------------------------------- //
// OVERRIDE: ENTITY
// -------------------------------------------- //
@Override
public Vote load(Vote that)
{
this.creatorId = that.creatorId;
this.creationMillis = that.creationMillis;
this.options = that.options;
this.id2Vote = that.id2Vote;
return this;
}
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private String creatorId;
public String getCreatorId() { return this.creatorId; }
private long creationMillis;
public long getCreationMillis() { return this.creationMillis; }
private String name;
@Override public String getName() { return this.name; }
private List<String> options;
public List<String> getOptions() { return this.options; }
private Map<String, String> id2Vote;
public Map<String, String> getId2Vote() { return id2Vote; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
// For GSON
private Vote()
{
this(null, 0, null, null);
}
public Vote(String creatorId, String name, List<String> options)
{
this(creatorId, System.currentTimeMillis(), name, options);
}
public Vote(String creatorId, long creationMillis, String name, List<String> options)
{
this.creatorId = creatorId;
this.creationMillis = creationMillis;
this.name = name;
this.options = options;
this.id2Vote = new MassiveMap<>();
}
// -------------------------------------------- //
// OTHER
// -------------------------------------------- //
public void setVote(MPlayer mplayer, String choice)
{
if (mplayer == null) throw new NullPointerException("mplayer");
if (choice == null) throw new NullPointerException("choice");
if (!this.getOptions().contains(choice)) throw new IllegalArgumentException(choice + " is not in " + this.getOptions());
id2Vote.put(mplayer.getId(), choice);
this.changed();
}
public String getVote(MPlayer mplayer)
{
if (mplayer == null) throw new NullPointerException("mplayer");
return this.getId2Vote().get(mplayer.getId());
}
public Faction getFaction()
{
EntityInternalMap<Vote> internalMap = (EntityInternalMap<Vote>) this.getContainer();
Faction faction = (Faction) internalMap.getEntity();
return faction;
}
public void clean()
{
Faction faction = this.getFaction();
for (Iterator<Entry<String, String>> it = this.id2Vote.entrySet().iterator(); it.hasNext();)
{
Entry<String, String> entry = it.next();
String id = entry.getKey();
// MPlayer must be a member
if ( ! faction.getMPlayerIds().contains(id))
{
it.remove();
break;
}
// And they must have the perm
MPlayer mplayer = MPlayer.get(id);
if (! MPerm.getPermVote().has(mplayer, faction, false))
{
it.remove();
break;
}
}
}
}

View File

@ -48,7 +48,7 @@ public class MigratorMPlayer001Ranks extends MigratorRoot
// Get faction
JsonElement jsonFaction = entity.get("factionId");
String factionId;
if (jsonFaction == null) factionId = MConf.get().defaultPlayerFactionId;
else factionId = jsonFaction.getAsString();