291 lines
12 KiB
Java
291 lines
12 KiB
Java
![]() |
package jesse.keeblarcraft.FactionMgr;
|
||
|
|
||
|
import static java.util.Map.entry;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
import java.util.Map.Entry;
|
||
|
import java.util.HashMap;
|
||
|
|
||
|
// This class is written to a config file typically and represents
|
||
|
// all the details surrounding a faction
|
||
|
public class FactionConfig {
|
||
|
// Enum may be extended in future
|
||
|
public static enum VALID_FACTION_ROLES {
|
||
|
OWNER,
|
||
|
COOWNER,
|
||
|
MANAGEMENT,
|
||
|
EMPLOYEE
|
||
|
}
|
||
|
|
||
|
private static Map<VALID_FACTION_ROLES, Integer> ROLE_LEVELS = Map.ofEntries
|
||
|
(
|
||
|
entry (VALID_FACTION_ROLES.EMPLOYEE, 0),
|
||
|
entry (VALID_FACTION_ROLES.MANAGEMENT, 1),
|
||
|
entry (VALID_FACTION_ROLES.COOWNER, 2),
|
||
|
entry (VALID_FACTION_ROLES.OWNER, 3)
|
||
|
);
|
||
|
|
||
|
// The below map is 100x easier to just have statically typed here and maintained manually than expensively getting value by key (I am also lazy)
|
||
|
private static Map<Integer, VALID_FACTION_ROLES> ROLES_BY_LEVEL = Map.ofEntries
|
||
|
(
|
||
|
entry (0, VALID_FACTION_ROLES.EMPLOYEE),
|
||
|
entry (1, VALID_FACTION_ROLES.MANAGEMENT),
|
||
|
entry (2, VALID_FACTION_ROLES.COOWNER),
|
||
|
entry (3, VALID_FACTION_ROLES.OWNER)
|
||
|
);
|
||
|
|
||
|
public class WriteableFaction {
|
||
|
// Key = Player UUID
|
||
|
// Val = Faction role of player
|
||
|
HashMap<String, VALID_FACTION_ROLES> factionPlayerList = new HashMap<String, VALID_FACTION_ROLES>();
|
||
|
// Key = Player DISPLAY name
|
||
|
// Val = Faction role of player
|
||
|
HashMap<String, VALID_FACTION_ROLES> DISPLAY_ONLY_LIST = new HashMap<String, VALID_FACTION_ROLES>();
|
||
|
|
||
|
// List contains UUID of players who are openly invited to this faction
|
||
|
List<String> openInvites = new ArrayList<String>();
|
||
|
|
||
|
Integer factionBankBalance;
|
||
|
Integer factionPower;
|
||
|
String factionName;
|
||
|
}
|
||
|
|
||
|
public Boolean CreateFaction(String factionName, String creatorUuid, String creatorDisplayName) {
|
||
|
Boolean success = false;
|
||
|
if (!IsValid(factionName)) {
|
||
|
WriteableFaction newFaction = new WriteableFaction();
|
||
|
newFaction.factionPlayerList.put(creatorUuid, VALID_FACTION_ROLES.OWNER);
|
||
|
newFaction.DISPLAY_ONLY_LIST.put(creatorDisplayName, VALID_FACTION_ROLES.OWNER);
|
||
|
newFaction.factionBankBalance = 0;
|
||
|
newFaction.factionPower = 1;
|
||
|
allFactions.put(factionName, newFaction);
|
||
|
success = true;
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public Boolean DeleteFaction(String factionName, String callerUuid) {
|
||
|
Boolean success = false;
|
||
|
// faction must exist
|
||
|
if (IsValid(factionName)) {
|
||
|
// faction must contain player
|
||
|
if (allFactions.get(factionName).factionPlayerList.containsKey(callerUuid)) {
|
||
|
// player must be owner of faction
|
||
|
if (allFactions.get(factionName).factionPlayerList.get(callerUuid) == VALID_FACTION_ROLES.OWNER) {
|
||
|
//TODO: BankManager will connect with this in future and the money of the faction must go somewhere!
|
||
|
allFactions.remove(factionName);
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
private Boolean CanInvite(String factionName, String callerUuid) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsValid(factionName)) {
|
||
|
if (allFactions.get(factionName).factionPlayerList.containsKey(callerUuid)) {
|
||
|
VALID_FACTION_ROLES callerRole = allFactions.get(factionName).factionPlayerList.get(callerUuid);
|
||
|
if (callerRole == VALID_FACTION_ROLES.MANAGEMENT || callerRole == VALID_FACTION_ROLES.COOWNER || callerRole == VALID_FACTION_ROLES.OWNER) {
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
private Boolean IsOnInviteList(String factionName, String playerUuid) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsValid(factionName)) {
|
||
|
if (allFactions.get(factionName).openInvites.contains(playerUuid)) {
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
private void AddToFaction(String factionName, String playerUuid, String playerDisplayName) {
|
||
|
if (allFactions.containsKey(factionName)) {
|
||
|
allFactions.get(factionName).factionPlayerList.put(playerUuid, VALID_FACTION_ROLES.EMPLOYEE);
|
||
|
allFactions.get(factionName).DISPLAY_ONLY_LIST.put(playerDisplayName, VALID_FACTION_ROLES.EMPLOYEE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public Boolean InvitePlayerToFaction(String factionName, String callerUuid, String invitedUuid) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (CanInvite(factionName, callerUuid)) {
|
||
|
allFactions.get(factionName).openInvites.add(invitedUuid);
|
||
|
success = true;
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public Boolean JoinFaction(String factionName, String playerUuid, String playerDisplayName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsOnInviteList(factionName, playerUuid) ) {
|
||
|
AddToFaction(factionName, playerUuid, playerDisplayName);
|
||
|
success = true;
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public Boolean LeaveFaction(String factionName, String playerUuid, String playerName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsValid(factionName)) {
|
||
|
allFactions.get(factionName).factionPlayerList.remove(playerUuid);
|
||
|
allFactions.get(factionName).DISPLAY_ONLY_LIST.remove(playerName);
|
||
|
|
||
|
// TODO: In future add ability if owner leave then promote next person
|
||
|
// Delete faction if all the players are gone
|
||
|
if (allFactions.get(factionName).factionPlayerList.size() == 0) {
|
||
|
allFactions.remove(factionName);
|
||
|
}
|
||
|
success = true;
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
private Boolean HasPlayer(String factionName, String playerUuid) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsValid(factionName)) {
|
||
|
success = allFactions.get(factionName).factionPlayerList.containsKey(playerUuid);
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public Boolean IsValid(String factionName) {
|
||
|
if (allFactions.containsKey(factionName)) {
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Empty string means no faction found. This is expensive!
|
||
|
public String FindFactionOfPlayer(String playerUuid) {
|
||
|
String faction = "";
|
||
|
|
||
|
System.out.println("Attempting to find player factions with uuid " + playerUuid);
|
||
|
for (Entry<String, WriteableFaction> entry : allFactions.entrySet()) {
|
||
|
if (entry.getValue().factionPlayerList.containsKey(playerUuid)) {
|
||
|
System.out.println("FAC [" + entry.getKey() + "]: PLAY-LIST: " + entry.getValue().factionPlayerList);
|
||
|
faction = entry.getKey();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return faction;
|
||
|
}
|
||
|
|
||
|
public Boolean PromotePlayer(String factionName, String callerUuid, String promoteeUuid, String promoteeDisplayName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (CanInvite(factionName, callerUuid) && HasPlayer(factionName, promoteeUuid)) {
|
||
|
VALID_FACTION_ROLES callerRole = allFactions.get(factionName).factionPlayerList.get(callerUuid);
|
||
|
VALID_FACTION_ROLES promoteeRole = allFactions.get(factionName).factionPlayerList.get(promoteeUuid);
|
||
|
Integer callerRoleLevel = ROLE_LEVELS.get(callerRole);
|
||
|
Integer promoteeRoleLevel = ROLE_LEVELS.get(promoteeRole);
|
||
|
|
||
|
// Factions is setup so that anyone can promote anybody UNDERNEATH them. However, you CANNOT promote a player to your level!
|
||
|
if (callerRoleLevel > promoteeRoleLevel + 1) {
|
||
|
// Get the new employee role
|
||
|
promoteeRole = ROLES_BY_LEVEL.get(promoteeRoleLevel + 1);
|
||
|
|
||
|
// Update role in faction
|
||
|
allFactions.get(factionName).factionPlayerList.put(promoteeUuid, promoteeRole);
|
||
|
allFactions.get(factionName).DISPLAY_ONLY_LIST.put(promoteeDisplayName, promoteeRole);
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
// Factions is setup in a way where anybody can demote anybody underneath them and demotions of the lowest level lead to an automatic kick. The
|
||
|
// same behavior is kept here
|
||
|
public Boolean CanKickPlayer(String factionName, String callerUuid, String kickeeUuid, String kickeeDisplayName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (IsValid(factionName) && HasPlayer(factionName, kickeeUuid)) {
|
||
|
VALID_FACTION_ROLES callerRole = allFactions.get(factionName).factionPlayerList.get(callerUuid);
|
||
|
VALID_FACTION_ROLES kickeeRole = allFactions.get(factionName).factionPlayerList.get(kickeeUuid);
|
||
|
Integer callerRoleLevel = ROLE_LEVELS.get(callerRole);
|
||
|
Integer kickeeRoleLevel = ROLE_LEVELS.get(kickeeRole);
|
||
|
|
||
|
if (callerRoleLevel > kickeeRoleLevel) {
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
private Boolean KickPlayer(String factionName, String callerUuid, String kickeeUuid, String kickeeDisplayName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (CanKickPlayer(factionName, callerUuid, kickeeUuid, kickeeDisplayName)) {
|
||
|
allFactions.get(factionName).factionPlayerList.remove(kickeeUuid);
|
||
|
allFactions.get(factionName).DISPLAY_ONLY_LIST.remove(kickeeDisplayName);
|
||
|
success = true;
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public Boolean DemotePlayer(String factionName, String callerUuid, String demoteeUuid, String demoteeDisplayName) {
|
||
|
Boolean success = false;
|
||
|
|
||
|
if (CanInvite(factionName, callerUuid) && HasPlayer(factionName, demoteeUuid)) {
|
||
|
VALID_FACTION_ROLES callerRole = allFactions.get(factionName).factionPlayerList.get(callerUuid);
|
||
|
VALID_FACTION_ROLES demoteeRole = allFactions.get(factionName).factionPlayerList.get(demoteeUuid);
|
||
|
Integer callerRoleLevel = ROLE_LEVELS.get(callerRole);
|
||
|
Integer demoteeRoleLevel = ROLE_LEVELS.get(demoteeRole);
|
||
|
|
||
|
// Factions is setup so that anyone can demote anybody underneath them & the lowest level will cause a demotion to be a KICK from faction
|
||
|
if (callerRoleLevel > demoteeRoleLevel) {
|
||
|
// If the role level would be lower than bottom level, KICK player
|
||
|
if (demoteeRoleLevel - 1 < ROLE_LEVELS.get(VALID_FACTION_ROLES.EMPLOYEE)) {
|
||
|
success = KickPlayer(factionName, callerUuid, demoteeUuid, demoteeDisplayName);
|
||
|
} else {
|
||
|
// Regular demotion!
|
||
|
demoteeRole = ROLES_BY_LEVEL.get(demoteeRoleLevel - 1);
|
||
|
|
||
|
// Update faction
|
||
|
allFactions.get(factionName).factionPlayerList.put(demoteeUuid, demoteeRole);
|
||
|
allFactions.get(factionName).DISPLAY_ONLY_LIST.put(demoteeDisplayName, demoteeRole);
|
||
|
success = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
public List<String> ListOfAllFactions() {
|
||
|
List<String> facs = new ArrayList<String>();
|
||
|
|
||
|
System.out.println("ListOfFactions - map size: " + allFactions.size());
|
||
|
for (Entry<String, WriteableFaction> entry : allFactions.entrySet()) {
|
||
|
System.out.println("Adding fac " + entry.getKey() + " to fac");
|
||
|
facs.add(entry.getKey());
|
||
|
}
|
||
|
|
||
|
return facs;
|
||
|
}
|
||
|
|
||
|
// Key = Faction identifier
|
||
|
// Val = Faction object
|
||
|
HashMap<String, WriteableFaction> allFactions = new HashMap<String, WriteableFaction>();
|
||
|
}
|