コード例 #1
0
        private void CommonProcess(SyncFactionType syncType, long factionId, long playerId)
        {
            switch (syncType)
            {
                case SyncFactionType.Join:
                    {
                        var fc = MyAPIGateway.Session.Factions.GetObjectBuilder();
                        //var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId);

                        // AddPlayerToFaction() Doesn't work right on dedicated servers. To be removed by Keen in future. Is Depriated.
                        //MyAPIGateway.Session.Factions.AddPlayerToFaction(selectedPlayer.PlayerId, factionCollectionBuilder.FactionId);
                        //MyAPIGateway.Session.Factions.AddPlayerToFaction(PlayerId, FactionId);

                        var request = fc.Factions.FirstOrDefault(f => f.JoinRequests.Any(r => r.PlayerId == playerId));

                        if (request != null && request.FactionId != factionId)
                        {
                            // Cancel join request to other faction.
                            MyAPIGateway.Session.Factions.CancelJoinRequest(request.FactionId, playerId);
                        }
                        else if (request != null && request.FactionId == factionId)
                        {
                            MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId);
                            //MyAPIGateway.Utilities.ShowMessage("join", string.Format("{0} has been addded to faction.", selectedPlayer.DisplayName));
                            return;
                        }

                        // The SendJoinRequest and AcceptJoin cannot be called consecutively as the second call fails to work, so they must be run on individual game frames.
                        MyAPIGateway.Session.Factions.SendJoinRequest(factionId, playerId);
                        MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId);

                        //// The SendJoinRequest and AcceptJoin cannot be called consecutively as the second call fails to work, so they must be run on individual game frames.
                        //_workQueue.Enqueue(delegate () { MyAPIGateway.Session.Factions.SendJoinRequest(factionId, playerIdd); });
                        //_workQueue.Enqueue(delegate () { MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId); });
                    }
                    break;

                case SyncFactionType.Kick:
                    MyAPIGateway.Session.Factions.KickMember(factionId, playerId);
                    break;

                case SyncFactionType.Promote:
                    MyAPIGateway.Session.Factions.PromoteMember(factionId, playerId);
                    break;

                case SyncFactionType.Demote:
                    MyAPIGateway.Session.Factions.DemoteMember(factionId, playerId);
                    break;

                case SyncFactionType.Remove:
                    MyAPIGateway.Session.Factions.RemoveFaction(factionId);
                    break;

                case SyncFactionType.CancelJoin:
                    MyAPIGateway.Session.Factions.CancelJoinRequest(factionId, playerId);
                    break;

                case SyncFactionType.AcceptPeace:
                    {
                        var fc = MyAPIGateway.Session.Factions.GetObjectBuilder();

                        foreach (var faction in fc.Factions)
                        {
                            if (factionId == faction.FactionId)
                                continue;
                            if (MyAPIGateway.Session.Factions.IsPeaceRequestStatePending(factionId, faction.FactionId))
                                MyAPIGateway.Session.Factions.AcceptPeace(factionId, faction.FactionId);
                        }
                    }
                    break;
            }
        }
コード例 #2
0
        private void CommonProcess(SyncFactionType syncType, long factionId, long playerId)
        {
            switch (syncType)
            {
            case SyncFactionType.Join:
            {
                //var fc = MyAPIGateway.Session.Factions.GetObjectBuilder();
                //var faction = MyAPIGateway.Session.Factions.TryGetFactionById(factionId);

                // AddPlayerToFaction() Doesn't work right on dedicated servers. To be removed by Keen in future. Is Depriated.
                //MyAPIGateway.Session.Factions.AddPlayerToFaction(selectedPlayer.IdentityId, factionCollectionBuilder.FactionId);
                //MyAPIGateway.Session.Factions.AddPlayerToFaction(PlayerId, FactionId);

                //var request = fc.Factions.FirstOrDefault(f => f.JoinRequests.Any(r => r.PlayerId == playerId));

                var request = MyAPIGateway.Session.Factions.Factions.FirstOrDefault(f => f.Value.JoinRequests.Any(r => r.Value.PlayerId == playerId)).Value;

                if (request != null && request.FactionId != factionId)
                {
                    // Cancel join request to other faction.
                    MyAPIGateway.Session.Factions.CancelJoinRequest(request.FactionId, playerId);
                }
                else if (request != null && request.FactionId == factionId)
                {
                    MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId);
                    //MyAPIGateway.Utilities.ShowMessage("join", string.Format("{0} has been addded to faction.", selectedPlayer.DisplayName));
                    return;
                }

                // The SendJoinRequest and AcceptJoin cannot be called consecutively as the second call fails to work, so they must be run on individual game frames.
                MyAPIGateway.Session.Factions.SendJoinRequest(factionId, playerId);
                MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId);

                //// The SendJoinRequest and AcceptJoin cannot be called consecutively as the second call fails to work, so they must be run on individual game frames.
                //_workQueue.Enqueue(delegate () { MyAPIGateway.Session.Factions.SendJoinRequest(factionId, playerIdd); });
                //_workQueue.Enqueue(delegate () { MyAPIGateway.Session.Factions.AcceptJoin(factionId, playerId); });
            }
            break;

            case SyncFactionType.Kick:
                MyAPIGateway.Session.Factions.KickMember(factionId, playerId);
                break;

            case SyncFactionType.Promote:
                MyAPIGateway.Session.Factions.PromoteMember(factionId, playerId);
                break;

            case SyncFactionType.Demote:
                MyAPIGateway.Session.Factions.DemoteMember(factionId, playerId);
                break;

            case SyncFactionType.Remove:
                MyAPIGateway.Session.Factions.RemoveFaction(factionId);
                break;

            case SyncFactionType.CancelJoin:
                MyAPIGateway.Session.Factions.CancelJoinRequest(factionId, playerId);
                break;

            case SyncFactionType.AcceptPeace:
            {
                foreach (var faction in MyAPIGateway.Session.Factions.Factions)
                {
                    if (factionId == faction.Value.FactionId)
                    {
                        continue;
                    }
                    if (MyAPIGateway.Session.Factions.IsPeaceRequestStatePending(factionId, faction.Value.FactionId))
                    {
                        MyAPIGateway.Session.Factions.AcceptPeace(factionId, faction.Value.FactionId);
                    }
                }
            }
            break;
            }
        }