|
|
|
@ -1,9 +1,12 @@
|
|
|
|
|
package ru.simsonic.rscPermissions.InternalCache; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.HashSet; |
|
|
|
|
import java.util.Map.Entry; |
|
|
|
|
import ru.simsonic.rscPermissions.API.EntityType; |
|
|
|
|
import ru.simsonic.rscPermissions.API.PlayerType; |
|
|
|
|
import ru.simsonic.rscPermissions.API.RowEntity; |
|
|
|
|
import ru.simsonic.rscPermissions.API.RowInheritance; |
|
|
|
|
import ru.simsonic.rscPermissions.API.RowPermission; |
|
|
|
@ -19,55 +22,160 @@ public class InternalCache
|
|
|
|
|
defaultInheritance.deriveInstance(); |
|
|
|
|
} |
|
|
|
|
private final HashMap<String, RowEntity> entities_g = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowEntity> entities_u = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowEntity> entities_u = new HashMap<>();/* |
|
|
|
|
private final ArrayList<RowPermission> permissions_p2g = new ArrayList<>(); |
|
|
|
|
private final ArrayList<RowPermission> permissions_p2u = new ArrayList<>(); |
|
|
|
|
private final ArrayList<RowInheritance> inheritance_g2g = new ArrayList<>(); |
|
|
|
|
private final ArrayList<RowInheritance> inheritance_g2u = new ArrayList<>(); |
|
|
|
|
public static class InheritanceLeaf implements Comparable<InheritanceLeaf> |
|
|
|
|
{ |
|
|
|
|
public RowInheritance node; |
|
|
|
|
public String instantiator; |
|
|
|
|
public InheritanceLeaf[] subleafs; |
|
|
|
|
public String prefix; |
|
|
|
|
public String suffix; |
|
|
|
|
@Override |
|
|
|
|
public int compareTo(InheritanceLeaf other) |
|
|
|
|
private final ArrayList<RowInheritance> inheritance_g2u = new ArrayList<>();*/ |
|
|
|
|
public synchronized void fill(DatabaseContents contents) |
|
|
|
|
{ |
|
|
|
|
clear(); |
|
|
|
|
// Import data
|
|
|
|
|
importEntities(contents); |
|
|
|
|
importPermissions(contents.permissions); |
|
|
|
|
importInheritance(contents.inheritance); |
|
|
|
|
// Parse PlayerType's
|
|
|
|
|
for(RowEntity row : entities_u.values()) |
|
|
|
|
row.playerType = PlayerType.scanPlayerEntity(row.entity); |
|
|
|
|
} |
|
|
|
|
private void importEntities(DatabaseContents contents) |
|
|
|
|
{ |
|
|
|
|
final HashSet<String> names_u = new HashSet<>(); |
|
|
|
|
final HashSet<String> names_g = new HashSet<>(); |
|
|
|
|
for(RowEntity row : contents.entities) |
|
|
|
|
if(row.entityType == EntityType.group) |
|
|
|
|
{ |
|
|
|
|
entities_g.put(row.entity.toLowerCase(), row); |
|
|
|
|
names_g.add(row.entity.toLowerCase()); |
|
|
|
|
} else { |
|
|
|
|
entities_u.put(row.entity, row); |
|
|
|
|
names_u.add(row.entity); |
|
|
|
|
} |
|
|
|
|
for(RowPermission row : contents.permissions) |
|
|
|
|
if(row.entityType == EntityType.group) |
|
|
|
|
names_g.add(row.entity.toLowerCase()); |
|
|
|
|
else |
|
|
|
|
names_u.add(row.entity); |
|
|
|
|
for(RowInheritance row : contents.inheritance) |
|
|
|
|
{ |
|
|
|
|
return (other.node != null && node != null) ? other.node.compareTo(node) : 0; |
|
|
|
|
names_g.add(row.parent.toLowerCase()); |
|
|
|
|
if(row.childType == EntityType.group) |
|
|
|
|
names_g.add(row.entity.toLowerCase()); |
|
|
|
|
else |
|
|
|
|
names_u.add(row.entity); |
|
|
|
|
} |
|
|
|
|
for(String name : names_g) |
|
|
|
|
if(!entities_g.containsKey(name)) |
|
|
|
|
entities_g.put(name, new RowEntity()); |
|
|
|
|
for(String name : names_u) |
|
|
|
|
if(!entities_u.containsKey(name)) |
|
|
|
|
entities_u.put(name, new RowEntity()); |
|
|
|
|
} |
|
|
|
|
private final HashMap<String, InheritanceLeaf> entityTrees = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowPermission[]> permissionsTreesG = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowPermission[]> permissionsTreesU = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowInheritance[]> inheritanceTreesG = new HashMap<>(); |
|
|
|
|
private final HashMap<String, RowInheritance[]> inheritanceTreesU = new HashMap<>(); |
|
|
|
|
public synchronized void fill(DatabaseContents contents) |
|
|
|
|
private void importPermissions(RowPermission[] rows) |
|
|
|
|
{ |
|
|
|
|
clear(); |
|
|
|
|
if(contents.entities != null) |
|
|
|
|
importEntities(contents.entities); |
|
|
|
|
if(contents.permissions != null) |
|
|
|
|
importPermissions(contents.permissions); |
|
|
|
|
if(contents.inheritance != null) |
|
|
|
|
importInheritance(contents.inheritance); |
|
|
|
|
buildInheritanceForest(); |
|
|
|
|
final ArrayList<RowPermission> permissions_p2g = new ArrayList<>(); |
|
|
|
|
final ArrayList<RowPermission> permissions_p2u = new ArrayList<>(); |
|
|
|
|
for(RowPermission row : rows) |
|
|
|
|
if(row.entityType == EntityType.group) |
|
|
|
|
permissions_p2g.add(row); |
|
|
|
|
else |
|
|
|
|
permissions_p2u.add(row); |
|
|
|
|
for(String entry : entities_g.keySet()) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<RowPermission> permissions = new ArrayList<>(); |
|
|
|
|
for(RowPermission row : permissions_p2g) |
|
|
|
|
if(row.entity.toLowerCase().equals(entry)) |
|
|
|
|
permissions.add(row); |
|
|
|
|
entities_g.get(entry).permissions = permissions.toArray(new RowPermission[permissions.size()]); |
|
|
|
|
} |
|
|
|
|
for(String entry : entities_u.keySet()) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<RowPermission> permissions = new ArrayList<>(); |
|
|
|
|
for(RowPermission row : permissions_p2u) |
|
|
|
|
if(row.entity.equals(entry)) |
|
|
|
|
permissions.add(row); |
|
|
|
|
entities_u.get(entry).permissions = permissions.toArray(new RowPermission[permissions.size()]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private void importInheritance(RowInheritance[] rows) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<RowInheritance> inheritance_g2g = new ArrayList<>(); |
|
|
|
|
final ArrayList<RowInheritance> inheritance_g2u = new ArrayList<>(); |
|
|
|
|
for(RowInheritance row : rows) |
|
|
|
|
if(row.childType == EntityType.group) |
|
|
|
|
inheritance_g2g.add(row); |
|
|
|
|
else |
|
|
|
|
inheritance_g2u.add(row); |
|
|
|
|
for(Entry<String, RowEntity> entry : entities_g.entrySet()) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<RowInheritance> inheritances = new ArrayList<>(); |
|
|
|
|
final String name = entry.getKey(); |
|
|
|
|
for(RowInheritance row : inheritance_g2g) |
|
|
|
|
if(row.entity.toLowerCase().equals(name)) |
|
|
|
|
inheritances.add(row); |
|
|
|
|
entry.getValue().inheritance = inheritances.toArray(new RowInheritance[inheritances.size()]); |
|
|
|
|
} |
|
|
|
|
for(Entry<String, RowEntity> entry : entities_u.entrySet()) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<RowInheritance> inheritance = new ArrayList<>(); |
|
|
|
|
final String name = entry.getKey(); |
|
|
|
|
for(RowInheritance row : inheritance_g2u) |
|
|
|
|
if(row.entity.equals(name)) |
|
|
|
|
inheritance.add(row); |
|
|
|
|
entry.getValue().inheritance = inheritance.toArray(new RowInheritance[inheritance.size()]); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
public synchronized ResolutionResult resolvePlayer(String player, String serverId) |
|
|
|
|
public synchronized ResolutionResult resolvePlayer(String player) |
|
|
|
|
{ |
|
|
|
|
return resolvePlayer(new String[] { player }, serverId); |
|
|
|
|
return resolvePlayer(new String[] { player }); |
|
|
|
|
} |
|
|
|
|
public synchronized ResolutionResult resolvePlayer(String[] player, String serverId) |
|
|
|
|
public synchronized ResolutionResult resolvePlayer(String[] player) |
|
|
|
|
{ |
|
|
|
|
final ResolutionParams params = new ResolutionParams(); |
|
|
|
|
params.applicableIdentifiers = player; |
|
|
|
|
params.destRegions = new String[] {}; |
|
|
|
|
// params.destWorld = "";
|
|
|
|
|
params.destServerId = serverId; |
|
|
|
|
// params.expirience = 0;
|
|
|
|
|
return resolvePlayer(params); |
|
|
|
|
} |
|
|
|
|
public synchronized ResolutionResult resolvePlayer(ResolutionParams params) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<ResolutionResult> intermediate = new ArrayList<>(); |
|
|
|
|
if(entities_g.containsKey("")) |
|
|
|
|
{ |
|
|
|
|
params.parentEntity = entities_g.get(""); |
|
|
|
|
params.instantiator = ""; |
|
|
|
|
intermediate.add(resolveParent(params)); |
|
|
|
|
} |
|
|
|
|
for(RowEntity row : entities_u.values()) |
|
|
|
|
for(String identifier : params.applicableIdentifiers) |
|
|
|
|
if(row.playerType.isEntityApplicable(row.entity, identifier)) |
|
|
|
|
{ |
|
|
|
|
params.parentEntity = row; |
|
|
|
|
params.instantiator = ""; |
|
|
|
|
intermediate.add(resolveParent(params)); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
final ResolutionResult result = processResultColumn(params, intermediate); |
|
|
|
|
|
|
|
|
|
parents.addAll(Arrays.asList(implicitInheritance_u)); |
|
|
|
|
parents.add(defaultInheritance); |
|
|
|
|
for(Entry<String, RowInheritance[]> entity : inheritanceTrees_u.entrySet()) |
|
|
|
|
{ |
|
|
|
|
for(RowInheritance row : entity.getValue()) |
|
|
|
|
if(PlayerType.isEntityApplicable(entity, , entity)) |
|
|
|
|
}; |
|
|
|
|
final ResolutionResult result = new ResolutionResult(); |
|
|
|
|
intermediate.addAll(Arrays.asList(implicitPermissions_u)); |
|
|
|
|
final ArrayList<RowPermission> inheritance = new ArrayList<>(); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
public synchronized ResolutionResult resolveParent(ResolutionParams params) |
|
|
|
|
{ |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
/* |
|
|
|
|
public synchronized ResolutionResult resolvePlayerOld(ResolutionParams params) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<InheritanceLeaf> applicableBranches = new ArrayList<>(); |
|
|
|
|
// Grab all inheritance rows applicable to this player
|
|
|
|
@ -85,34 +193,6 @@ public class InternalCache
|
|
|
|
|
intermediateResults.clear(); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
private void importEntities(RowEntity[] rows) |
|
|
|
|
{ |
|
|
|
|
for(RowEntity row : rows) |
|
|
|
|
{ |
|
|
|
|
if(row.entityType == EntityType.group) |
|
|
|
|
entities_g.put(row.entity.toLowerCase(), row); |
|
|
|
|
else |
|
|
|
|
entities_u.put(row.entity.toLowerCase(), row); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private void importPermissions(RowPermission[] rows) |
|
|
|
|
{ |
|
|
|
|
for(RowPermission row : rows) |
|
|
|
|
{ |
|
|
|
|
if(row.entityType == EntityType.group) |
|
|
|
|
permissions_p2g.add(row); |
|
|
|
|
else |
|
|
|
|
permissions_p2u.add(row); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private void importInheritance(RowInheritance[] rows) |
|
|
|
|
{ |
|
|
|
|
for(RowInheritance row : rows) |
|
|
|
|
if(row.childType == EntityType.group) |
|
|
|
|
inheritance_g2g.add(row); |
|
|
|
|
else |
|
|
|
|
inheritance_g2u.add(row); |
|
|
|
|
} |
|
|
|
|
// FROM HERE I SHOULD MAKE IT WORKING
|
|
|
|
|
private void buildInheritanceForest() |
|
|
|
|
{ |
|
|
|
@ -143,7 +223,7 @@ public class InternalCache
|
|
|
|
|
} |
|
|
|
|
final ArrayList<RowInheritance> parents = new ArrayList<>(); |
|
|
|
|
for(RowInheritance row : inheritance_g2g) |
|
|
|
|
if(row.parent.equalsIgnoreCase(source.entity)) |
|
|
|
|
if(row.parentEntity.equalsIgnoreCase(source.entity)) |
|
|
|
|
parents.add(row); |
|
|
|
|
Collections.sort(parents); |
|
|
|
|
final ArrayList<InheritanceLeaf> subleafs = new ArrayList<>(); |
|
|
|
@ -152,7 +232,7 @@ public class InternalCache
|
|
|
|
|
result.subleafs = subleafs.toArray(new InheritanceLeaf[subleafs.size()]); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
private ResolutionResult resolveBranch(ResolutionParams params, InheritanceLeaf branch, String instantiator) |
|
|
|
|
private ResolutionResult resolveBranch(ResolutionParams params, RowInheritance[] rows, String instantiator) |
|
|
|
|
{ |
|
|
|
|
final ArrayList<ResolutionResult> intermediateResults = new ArrayList<>(); |
|
|
|
|
for(InheritanceLeaf subleaf : branch.subleafs) |
|
|
|
@ -166,31 +246,32 @@ public class InternalCache
|
|
|
|
|
intermediateResults.clear(); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
private ResolutionResult processResultColumn(ResolutionParams params, ArrayList<ResolutionResult> resultList, String instantiator) |
|
|
|
|
*/ |
|
|
|
|
private ResolutionResult processResultColumn(ResolutionParams params, ArrayList<ResolutionResult> intermediate) |
|
|
|
|
{ |
|
|
|
|
switch(resultList.size()) |
|
|
|
|
switch(intermediate.size()) |
|
|
|
|
{ |
|
|
|
|
case 0: |
|
|
|
|
return new ResolutionResult(); |
|
|
|
|
case 1: |
|
|
|
|
return resultList.get(0); |
|
|
|
|
return intermediate.get(0); |
|
|
|
|
default: |
|
|
|
|
final ResolutionResult result = new ResolutionResult(); |
|
|
|
|
final ArrayList<RowPermission> permissions = new ArrayList<>(); |
|
|
|
|
result.prefix = ""; |
|
|
|
|
result.suffix = ""; |
|
|
|
|
for(ResolutionResult intermediate : resultList) |
|
|
|
|
for(ResolutionResult oneOf : intermediate) |
|
|
|
|
{ |
|
|
|
|
// Prefixes & suffixes
|
|
|
|
|
if(intermediate.prefix != null && !"".equals(intermediate.prefix)) |
|
|
|
|
result.prefix = result.prefix.replace("%", result.prefix); |
|
|
|
|
if(intermediate.suffix != null && !"".equals(intermediate.suffix)) |
|
|
|
|
result.suffix = result.suffix.replace("%", result.suffix); |
|
|
|
|
result.prefix = result.prefix.replace(Settings.instantiator, instantiator); |
|
|
|
|
result.suffix = result.suffix.replace(Settings.instantiator, instantiator); |
|
|
|
|
if(oneOf.prefix != null && !"".equals(oneOf.prefix)) |
|
|
|
|
result.prefix = result.prefix.replace("%", oneOf.prefix); |
|
|
|
|
if(oneOf.suffix != null && !"".equals(oneOf.suffix)) |
|
|
|
|
result.suffix = result.suffix.replace("%", oneOf.suffix); |
|
|
|
|
result.prefix = result.prefix.replace(Settings.instantiator, params.instantiator); |
|
|
|
|
result.suffix = result.suffix.replace(Settings.instantiator, params.instantiator); |
|
|
|
|
// Permissions
|
|
|
|
|
for(RowPermission permission : intermediate.permissions) |
|
|
|
|
if(isPermissionApplicable(params, permission, instantiator)) |
|
|
|
|
for(RowPermission permission : oneOf.permissions) |
|
|
|
|
if(isPermissionApplicable(params, permission, params.instantiator)) |
|
|
|
|
permissions.add(permission); |
|
|
|
|
} |
|
|
|
|
result.permissions = permissions.toArray(new RowPermission[permissions.size()]); |
|
|
|
@ -217,9 +298,11 @@ public class InternalCache
|
|
|
|
|
{ |
|
|
|
|
entities_g.clear(); |
|
|
|
|
entities_u.clear(); |
|
|
|
|
/* |
|
|
|
|
permissions_p2g.clear(); |
|
|
|
|
permissions_p2u.clear(); |
|
|
|
|
inheritance_g2g.clear(); |
|
|
|
|
inheritance_g2u.clear(); |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|