com.softwaremagico.tm.Restrictions Maven / Gradle / Ivy
package com.softwaremagico.tm;
/*-
* #%L
* Think Machine 4E (Rules)
* %%
* Copyright (C) 2017 - 2024 Softwaremagico
* %%
* This software is designed by Jorge Hortelano Otero. Jorge Hortelano Otero
* Valencia (Spain).
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; If not, see .
* #L%
*/
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.softwaremagico.tm.character.CharacterPlayer;
import com.softwaremagico.tm.character.callings.CallingFactory;
import com.softwaremagico.tm.character.capabilities.CapabilityFactory;
import com.softwaremagico.tm.character.factions.FactionFactory;
import com.softwaremagico.tm.character.specie.SpecieFactory;
import com.softwaremagico.tm.exceptions.InvalidXmlElementException;
import com.softwaremagico.tm.log.MachineLog;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class Restrictions extends XmlData {
//Cannot be chosen by the character. Is assigned by race or faction.
@JsonProperty("restricted")
private boolean restricted = false;
@JsonProperty("species")
private Set restrictedToSpecies = new HashSet<>();
@JsonProperty("upbringings")
private Set restrictedToUpbringing = new HashSet<>();
@JsonProperty("factions")
private Set restrictedToFactions = new HashSet<>();
@JsonProperty("callings")
private Set restrictedToCallings = new HashSet<>();
@JsonProperty("capabilities")
private Set restrictedToCapabilities = new HashSet<>();
@JsonProperty("perks")
private Set restrictedPerks = new HashSet<>();
@JsonProperty("perksGroups")
private Set restrictedToPerksGroups = new HashSet<>();
@JacksonXmlProperty(isAttribute = true)
private RestrictionMode mode = RestrictionMode.ANY;
@JacksonXmlProperty(isAttribute = true)
public Set getRestrictedToSpecies() {
return restrictedToSpecies;
}
public void setRestrictedToSpecies(Set restrictedToSpecies) {
this.restrictedToSpecies = restrictedToSpecies;
}
public Set getRestrictedToUpbringing() {
return restrictedToUpbringing;
}
public void setRestrictedToUpbringing(Set restrictedToUpbringing) {
this.restrictedToUpbringing = restrictedToUpbringing;
}
public Set getRestrictedToFactions() {
return restrictedToFactions;
}
public void setRestrictedToFactions(Set restrictedToFactions) {
this.restrictedToFactions = restrictedToFactions;
}
public Set getRestrictedToCallings() {
return restrictedToCallings;
}
public void setRestrictedToCallings(Set restrictedToCallings) {
this.restrictedToCallings = restrictedToCallings;
}
public Set getRestrictedToPerksGroups() {
return restrictedToPerksGroups;
}
public void setRestrictedToPerksGroups(Set restrictedToPerksGroups) {
this.restrictedToPerksGroups = restrictedToPerksGroups;
}
public Set getRestrictedToCapabilities() {
return restrictedToCapabilities;
}
public void setRestrictedToCapabilities(Set restrictedToCapabilities) {
this.restrictedToCapabilities = restrictedToCapabilities;
}
public void setRestricted(boolean restricted) {
this.restricted = restricted;
}
public boolean isRestricted(CharacterPlayer characterPlayer) {
switch (mode) {
case ANY_FROM_GROUP:
return !accomplishAnyRestrictionFromEachGroup(characterPlayer);
case ALL:
return !accomplishAllRestriction(characterPlayer);
case ANY:
default:
return !accomplishAnyRestriction(characterPlayer);
}
}
public boolean isOpen() {
return !this.restricted
&& (restrictedToSpecies == null || restrictedToSpecies.isEmpty())
&& (restrictedToUpbringing == null || restrictedToUpbringing.isEmpty())
&& (restrictedToFactions == null || restrictedToFactions.isEmpty())
&& (restrictedToCallings == null || restrictedToCallings.isEmpty())
&& (restrictedToCapabilities == null || restrictedToCapabilities.isEmpty())
&& (restrictedPerks == null || restrictedPerks.isEmpty())
&& (restrictedToPerksGroups == null || restrictedToPerksGroups.isEmpty());
}
private boolean accomplishAnyRestriction(CharacterPlayer characterPlayer) {
if (characterPlayer == null) {
return false;
}
if (!characterPlayer.getSettings().isRestrictionsChecked()) {
return true;
}
try {
return isOpen()
//Check Specie
|| ((!getRestrictedToSpecies().isEmpty() && (characterPlayer.getSpecie() != null && getRestrictedToSpecies()
.contains(characterPlayer.getSpecie())))
// Check Faction
|| (!getRestrictedToFactions().isEmpty() && (characterPlayer.getFaction() != null && getRestrictedToFactions()
.contains(characterPlayer.getFaction().getId())))
// Check Uprising
|| (!getRestrictedToUpbringing().isEmpty() && (characterPlayer.getUpbringing() != null && getRestrictedToUpbringing()
.contains(characterPlayer.getUpbringing().getId())))
//Check Callings
|| (!getRestrictedToCallings().isEmpty() && (characterPlayer.getCalling() != null && getRestrictedToCallings()
.contains(characterPlayer.getCalling().getId())))
// Check Perks
|| (!getRestrictedPerks().isEmpty() && (characterPlayer.getPerks() != null && !Collections.disjoint(
getRestrictedPerks(), (characterPlayer.getPerks()))))
// Check perks Groups
|| (!getRestrictedToPerksGroups().isEmpty() && (characterPlayer.getPerks() != null && getRestrictedToPerksGroups().stream()
.anyMatch(characterPlayer.getPerks().stream().map(Element::getGroup).collect(Collectors.toList())::contains)))
//Check capabilities
|| (!getRestrictedToCapabilities().isEmpty() && (characterPlayer.getCapabilitiesWithSpecialization() != null && !Collections.disjoint(
getRestrictedToCapabilities(), (characterPlayer.getCapabilitiesWithSpecialization())))));
} catch (InvalidXmlElementException e) {
MachineLog.errorMessage("Is restricted!", e);
}
return false;
}
private boolean accomplishAnyRestrictionFromEachGroup(CharacterPlayer characterPlayer) {
if (characterPlayer == null) {
return false;
}
if (!characterPlayer.getSettings().isRestrictionsChecked()) {
return true;
}
try {
return isOpen()
//Check Specie
|| ((getRestrictedToSpecies().isEmpty() || (characterPlayer.getSpecie() != null && getRestrictedToSpecies()
.contains(characterPlayer.getSpecie())))
// Check Faction
&& (getRestrictedToFactions().isEmpty() || (characterPlayer.getFaction() != null && getRestrictedToFactions()
.contains(characterPlayer.getFaction().getId())))
// Check Uprising
&& (getRestrictedToUpbringing().isEmpty() || (characterPlayer.getUpbringing() != null && getRestrictedToUpbringing()
.contains(characterPlayer.getUpbringing().getId())))
//Check Callings
&& (getRestrictedToCallings().isEmpty() || (characterPlayer.getCalling() != null && getRestrictedToCallings()
.contains(characterPlayer.getCalling().getId())))
// Check Perks
&& (getRestrictedPerks().isEmpty() || (characterPlayer.getPerks() != null && !Collections.disjoint(
getRestrictedPerks(), (characterPlayer.getPerks()))))
// Check perks Groups
&& (getRestrictedToPerksGroups().isEmpty() || (characterPlayer.getPerks() != null && getRestrictedToPerksGroups().stream()
.anyMatch(characterPlayer.getPerks().stream().map(Element::getGroup).collect(Collectors.toList())::contains)))
//Check capabilities
&& (getRestrictedToCapabilities().isEmpty() || (characterPlayer.getCapabilitiesWithSpecialization() != null && !Collections.disjoint(
getRestrictedToCapabilities(), (characterPlayer.getCapabilitiesWithSpecialization())))));
} catch (InvalidXmlElementException e) {
MachineLog.errorMessage("Is restricted!", e);
}
return false;
}
private boolean accomplishAllRestriction(CharacterPlayer characterPlayer) {
if (characterPlayer == null) {
return false;
}
if (!characterPlayer.getSettings().isRestrictionsChecked()) {
return true;
}
try {
return isOpen()
//Check Specie
|| (((getRestrictedToSpecies().isEmpty() || (characterPlayer.getSpecie() != null && getRestrictedToSpecies()
.contains(characterPlayer.getSpecie())))
// Check Faction
|| (getRestrictedToFactions().isEmpty() || (characterPlayer.getFaction() != null && getRestrictedToFactions()
.contains(characterPlayer.getFaction().getId())))
// Check Uprising
|| (getRestrictedToUpbringing().isEmpty() || (characterPlayer.getUpbringing() != null && getRestrictedToUpbringing()
.contains(characterPlayer.getUpbringing().getId())))
//Check Callings
|| (getRestrictedToCallings().isEmpty() || (characterPlayer.getCalling() != null && getRestrictedToCallings()
.contains(characterPlayer.getCalling().getId())))
// Check Perks
|| (getRestrictedPerks().isEmpty() || (characterPlayer.getPerks() != null && !Collections.disjoint(
getRestrictedPerks(), (characterPlayer.getPerks()))))
// Check perks Groups
|| (getRestrictedToPerksGroups().isEmpty() || (characterPlayer.getPerks() != null && getRestrictedToPerksGroups().stream()
.anyMatch(characterPlayer.getPerks().stream().map(Element::getGroup).collect(Collectors.toList())::contains)))
//Check capabilities
|| (getRestrictedToCapabilities().isEmpty() || (characterPlayer.getCapabilitiesWithSpecialization() != null && !Collections.disjoint(
getRestrictedToCapabilities(), (characterPlayer.getCapabilitiesWithSpecialization()))))));
} catch (InvalidXmlElementException e) {
MachineLog.errorMessage("Is restricted!", e);
}
return false;
}
public RestrictionMode getMode() {
return mode;
}
public void setMode(RestrictionMode mode) {
this.mode = mode;
}
public Set getRestrictedPerks() {
return restrictedPerks;
}
public void setRestrictedPerks(Set restrictedPerks) {
this.restrictedPerks = restrictedPerks;
}
public boolean isRestricted() {
return restricted;
}
public boolean isRequiredCapability(String capability) {
return restrictedToCapabilities.stream().anyMatch(c -> Objects.equals(c, capability));
}
@Override
public void validate() throws InvalidXmlElementException {
for (String race : restrictedToSpecies) {
SpecieFactory.getInstance().getElement(race);
}
for (String faction : restrictedToFactions) {
FactionFactory.getInstance().getElement(faction);
}
for (String calling : restrictedToCallings) {
CallingFactory.getInstance().getElement(calling);
}
for (String capability : restrictedToCapabilities) {
CapabilityFactory.getInstance().getElement(capability);
}
}
}