bool CheckFactionStateChange(MyFactionStateChange action, long fromFactionId, long toFactionId, long playerId, long senderId)
        {
            Debug.Assert(Sync.IsServer, "Only server can check state of message!");
            if (!Sync.IsServer)
            {
                return(false);
            }

            if (!m_factions.ContainsKey(fromFactionId) || !m_factions.ContainsKey(toFactionId))
            {
                return(false);
            }

            var                        key = new MyFactionPair(fromFactionId, toFactionId);
            HashSet <long>             tmpSet;
            MyRelationsBetweenFactions tmp;

            switch (action)
            {
            case MyFactionStateChange.RemoveFaction: return(true);

            case MyFactionStateChange.SendPeaceRequest:   return((m_factionRequests.TryGetValue(fromFactionId, out tmpSet)) ? !tmpSet.Contains(toFactionId) : true);

            case MyFactionStateChange.CancelPeaceRequest: return((m_factionRequests.TryGetValue(fromFactionId, out tmpSet)) ?  tmpSet.Contains(toFactionId) : false);

            case MyFactionStateChange.AcceptPeace: return((m_relationsBetweenFactions.TryGetValue(key, out tmp)) ? tmp != MyRelationsBetweenFactions.Neutral : true);

            case MyFactionStateChange.DeclareWar:  return((m_relationsBetweenFactions.TryGetValue(key, out tmp)) ? tmp != MyRelationsBetweenFactions.Enemies : false);

            case MyFactionStateChange.FactionMemberSendJoin:   return(!m_factions[fromFactionId].IsMember(playerId) && !m_factions[fromFactionId].JoinRequests.ContainsKey(playerId));

            case MyFactionStateChange.FactionMemberCancelJoin: return(!m_factions[fromFactionId].IsMember(playerId) && m_factions[fromFactionId].JoinRequests.ContainsKey(playerId));

            case MyFactionStateChange.FactionMemberAcceptJoin: return(m_factions[fromFactionId].JoinRequests.ContainsKey(playerId));

            case MyFactionStateChange.FactionMemberKick:
                if (m_factions[fromFactionId].IsMember(playerId))
                {
                    if (MySession.Static.Settings.ScenarioEditMode && Sync.Players.IdentityIsNpc(playerId))
                    {
                        MyIdentity id = Sync.Players.TryGetIdentity(playerId);
                        id.TransferAllBlocksTo(senderId);
                    }
                    return(true);
                }
                return(false);

            case MyFactionStateChange.FactionMemberPromote:    return(m_factions[fromFactionId].IsMember(playerId));

            case MyFactionStateChange.FactionMemberDemote:     return(m_factions[fromFactionId].IsLeader(playerId));

            case MyFactionStateChange.FactionMemberLeave:      return(m_factions[fromFactionId].IsMember(playerId));
            }
            return(false);
        }
Ejemplo n.º 2
0
        void ApplyFactionStateChange(MyFactionStateChange action, long fromFactionId, long toFactionId, long playerId, long senderId)
        {
            switch (action)
            {
            case MyFactionStateChange.RemoveFaction:
                if (m_factions[fromFactionId].IsMember(MySession.LocalPlayerId))     // kick all players from faction
                {
                    m_playerFaction.Remove(playerId);
                }

                foreach (var faction in m_factions)
                {
                    if (faction.Key != fromFactionId)     // remove faction from neutrals and requests
                    {
                        ClearRequest(fromFactionId, faction.Key);
                        RemoveRelation(fromFactionId, faction.Key);
                    }
                }
                m_factions.Remove(fromFactionId);
                break;

            case MyFactionStateChange.SendPeaceRequest:
                HashSet <long> tmp;

                if (m_factionRequests.TryGetValue(fromFactionId, out tmp))
                {
                    tmp.Add(toFactionId);
                }
                else
                {
                    tmp = new HashSet <long>();
                    tmp.Add(toFactionId);
                    m_factionRequests.Add(fromFactionId, tmp);
                }
                break;

            case MyFactionStateChange.CancelPeaceRequest:
                ClearRequest(fromFactionId, toFactionId);
                break;

            case MyFactionStateChange.AcceptPeace:
                ClearRequest(fromFactionId, toFactionId);
                ChangeRelation(fromFactionId, toFactionId, MyRelationsBetweenFactions.Neutral);
                break;

            case MyFactionStateChange.DeclareWar:
                ClearRequest(fromFactionId, toFactionId);
                ChangeRelation(fromFactionId, toFactionId, MyRelationsBetweenFactions.Enemies);
                break;

            case MyFactionStateChange.FactionMemberSendJoin:   m_factions[fromFactionId].AddJoinRequest(playerId); break;

            case MyFactionStateChange.FactionMemberCancelJoin: m_factions[fromFactionId].CancelJoinRequest(playerId); break;

            case MyFactionStateChange.FactionMemberAcceptJoin:
                if (MySession.Static.Settings.ScenarioEditMode && m_factions[fromFactionId].IsEveryoneNpc())
                {
                    m_factions[fromFactionId].AcceptJoin(playerId);
                    m_factions[fromFactionId].PromoteMember(playerId);
                }
                else
                {
                    m_factions[fromFactionId].AcceptJoin(playerId);
                }
                break;

            case MyFactionStateChange.FactionMemberLeave:
            case MyFactionStateChange.FactionMemberKick:
                m_factions[fromFactionId].KickMember(playerId);
                if (Sync.IsServer && MySession.Static.Settings.ScenarioEditMode && Sync.Players.IdentityIsNpc(playerId))
                {
                    MyIdentity id = Sync.Players.TryGetIdentity(playerId);
                    id.TransferAllBlocksTo(senderId);
                }
                break;

            case MyFactionStateChange.FactionMemberPromote: m_factions[fromFactionId].PromoteMember(playerId); break;

            case MyFactionStateChange.FactionMemberDemote:  m_factions[fromFactionId].DemoteMember(playerId); break;
            }
        }