Ejemplo n.º 1
0
        public string Execute([CurrentCharacterIfNull] ICharacter player, FactionMemberRole role)
        {
            var faction = FactionSystem.ServerGetFaction(player);

            if (faction is null)
            {
                return($"Player {player.Name} has no faction");
            }

            if (!Api.IsEditor)
            {
                var currentRole = FactionSystem.ServerGetRole(player);
                if (currentRole == FactionMemberRole.Leader)
                {
                    return("Cannot change the role of the faction leader");
                }

                if (role == FactionMemberRole.Leader)
                {
                    return("Cannot assign a leader role to anyone");
                }
            }

            FactionSystem.ServerSetMemberRoleNoChecks(player.Name, faction, role);
            return("Role changed to " + role);
        }
Ejemplo n.º 2
0
        private void ServerRemote_SetFactionAccessMode(
            IWorldObject worldObject,
            WorldObjectFactionAccessModes modes)
        {
            var character = ServerRemoteContext.Character;

            if (!(worldObject.ProtoGameObject is IProtoObjectWithAccessMode protoObjectWithAccessMode))
            {
                throw new Exception("This world object doesn't have an access mode");
            }

            if (!protoObjectWithAccessMode.SharedCanInteract(character, worldObject, writeToLog: true))
            {
                return;
            }

            var faction = ServerGetOwningFaction(worldObject);

            if (faction is null)
            {
                throw new Exception("No faction");
            }

            if (!CreativeModeSystem.SharedIsInCreativeMode(character))
            {
                // verify permission to access object
                FactionSystem.ServerValidateHasAccessRights(
                    character,
                    SharedGetFactionAccessRightRequirementForObject(worldObject),
                    out var characterFaction);

                if (!ReferenceEquals(faction, characterFaction))
                {
                    throw new Exception(worldObject + " belongs to another faction - cannot modify its access");
                }
            }

            if (modes == WorldObjectFactionAccessModes.Closed &&
                !protoObjectWithAccessMode.IsClosedAccessModeAvailable)
            {
                throw new Exception("Closed access mode is not supported for " + protoObjectWithAccessMode);
            }

            if ((modes == WorldObjectFactionAccessModes.Everyone ||
                 modes == WorldObjectFactionAccessModes.AllyFactionMembers) &&
                !protoObjectWithAccessMode.IsEveryoneAccessModeAvailable)
            {
                throw new Exception("Everyone and Ally access modes are not supported for "
                                    + protoObjectWithAccessMode);
            }

            var privateState  = worldObject.GetPrivateState <IObjectWithAccessModePrivateState>();
            var previousModes = privateState.FactionAccessMode;

            if (!protoObjectWithAccessMode.CanChangeFactionRoleAccessForSelfRole)
            {
                var role = FactionSystem.ServerGetRole(character);
                switch (role)
                {
                case FactionMemberRole.Officer1
                    when previousModes.HasFlag(WorldObjectFactionAccessModes.Officer1)
                    ^ modes.HasFlag(WorldObjectFactionAccessModes.Officer1):

                case FactionMemberRole.Officer2
                    when previousModes.HasFlag(WorldObjectFactionAccessModes.Officer2)
                    ^ modes.HasFlag(WorldObjectFactionAccessModes.Officer2):

                case FactionMemberRole.Officer3
                    when previousModes.HasFlag(WorldObjectFactionAccessModes.Officer3)
                    ^ modes.HasFlag(WorldObjectFactionAccessModes.Officer3):
                    throw new Exception("Cannot change access setting for self role " + protoObjectWithAccessMode);
                }
            }

            if (previousModes == modes)
            {
                return;
            }

            privateState.FactionAccessMode = modes;
            Logger.Info($"Faction access mode changed: {modes}; {worldObject}", character);
        }
Ejemplo n.º 3
0
        private static bool ServerHasFactionAccess(
            ICharacter character,
            string objectClanTag,
            WorldObjectFactionAccessModes factionAccessModes)
        {
            if (factionAccessModes == WorldObjectFactionAccessModes.Closed)
            {
                // always closed
                return(false);
            }

            if (factionAccessModes == WorldObjectFactionAccessModes.Everyone)
            {
                // always opened
                return(true);
            }

            var playerClanTag = FactionSystem.SharedGetClanTag(character);

            if (string.IsNullOrEmpty(playerClanTag))
            {
                // player don't have a faction
                return(false);
            }

            if (factionAccessModes.HasFlag(WorldObjectFactionAccessModes.AllyFactionMembers))
            {
                if (objectClanTag == playerClanTag)
                {
                    // current faction also allowed
                    return(true);
                }

                var objectFaction = FactionSystem.ServerGetFactionByClanTag(objectClanTag);
                return(FactionSystem.SharedGetFactionDiplomacyStatus(objectFaction, playerClanTag)
                       == FactionDiplomacyStatus.Ally);
            }

            // all further checks are only for the current faction
            if (objectClanTag != playerClanTag)
            {
                return(false);
            }

            if (factionAccessModes.HasFlag(WorldObjectFactionAccessModes.AllFactionMembers))
            {
                // all current faction members allowed
                return(true);
            }

            // only a specific role or roles are allowed
            var characterRole = FactionSystem.ServerGetRole(character);

            return(CheckAccessRights(WorldObjectFactionAccessModes.Leader, FactionMemberRole.Leader) ||
                   CheckAccessRights(WorldObjectFactionAccessModes.Officer1, FactionMemberRole.Officer1) ||
                   CheckAccessRights(WorldObjectFactionAccessModes.Officer2, FactionMemberRole.Officer2) ||
                   CheckAccessRights(WorldObjectFactionAccessModes.Officer3, FactionMemberRole.Officer3));

            bool CheckAccessRights(WorldObjectFactionAccessModes flag, FactionMemberRole role)
            => characterRole == role &&
            factionAccessModes.HasFlag(flag);
        }