Example #1
0
        public void ChangeLeader(Gang gang, Character newLeader)
        {
            if (!gang.IsMember(newLeader))
            {
                throw new PerpetuumException(ErrorCodes.CharacterNotInTheCurrentGang);
            }

            _gangRepository.UpdateLeader(gang, newLeader);

            void Finish()
            {
                gang.Leader = newLeader;
                Message.Builder.SetCommand(Commands.GangSetLeader).WithData(new Dictionary <string, object>
                {
                    { k.leaderId, newLeader.Id }
                }).ToCharacters(gang.GetMembers()).Send();
                _channelManager.SetMemberRole(gang.ChannelName, newLeader, ChannelMemberRole.Operator);
                GangLeaderChanged?.Invoke(gang);
            }

            if (Transaction.Current != null)
            {
                Transaction.Current.OnCommited(Finish);
            }
            else
            {
                Finish();
            }
        }
Example #2
0
        protected virtual void OnGangMemberRemoved(Gang gang, Character member)
        {
            try
            {
                var members = gang.GetMembers().ToArray();

                if (members.Length <= 0)
                {
                    DisbandGang(gang);
                    return;
                }

                if (gang.Leader != member)
                {
                    return;
                }

                // nincs leader
                var newLeader = members.FirstOrDefault(mm => gang.HasRole(mm, GangRole.Assistant)) ?? Character.None;
                if (newLeader == Character.None)
                {
                    var firstMember = members.First();
                    newLeader = firstMember;
                }

                ChangeLeader(gang, newLeader);
            }
            finally
            {
                GangMemberRemoved?.Invoke(gang, member);
            }
        }
        public void Insert(Gang gang)
        {
            var res = Db.Query().CommandText("insert into gang (id,leaderid,name) values (@id,@leaderId,@name)")
                      .SetParameter("@id", gang.Id)
                      .SetParameter("@leaderId", gang.Leader.Id)
                      .SetParameter("@name", gang.Name)
                      .ExecuteNonQuery();

            if (res == 0)
            {
                throw new PerpetuumException(ErrorCodes.SQLInsertError);
            }

            foreach (var member in gang.GetMembers())
            {
                InsertMember(gang, member);
            }
        }
Example #4
0
        public void RemoveMember(Gang gang, Character member, bool isKick)
        {
            if (gang == null)
            {
                return;
            }

            if (!gang.IsMember(member))
            {
                throw new PerpetuumException(ErrorCodes.CharacterNotInTheCurrentGang);
            }

            _gangRepository.DeleteMember(gang, member);

            void Finish()
            {
                var data = new Dictionary <string, object>
                {
                    { k.data, gang.GetGangData() },
                    { k.memberID, member.Id }
                };

                var cmd = isKick ? Commands.GangKickMember : Commands.GangRemoveMember;

                Message.Builder.SetCommand(cmd).WithData(data).ToCharacters(gang.GetMembers()).Send();

                gang.RemoveMember(member);

                _channelManager.LeaveChannel(gang.ChannelName, member);

                OnGangMemberRemoved(gang, member);
            }

            if (Transaction.Current != null)
            {
                Transaction.Current.OnCommited(Finish);
            }
            else
            {
                Finish();
            }
        }
Example #5
0
        public void SetRole(Gang gang, Character member, GangRole newRole)
        {
            if (gang.Leader == member)
            {
                return;
            }

            if (!gang.IsMember(member))
            {
                throw new PerpetuumException(ErrorCodes.CharacterNotInTheCurrentGang);
            }

            _gangRepository.UpdateMemberRole(gang, member, newRole);

            void Finish()
            {
                gang.SetMember(member, newRole);
                Message.Builder.SetCommand(Commands.GangSetRole).WithData(new Dictionary <string, object>
                {
                    { k.memberID, member.Id },
                    { k.role, (int)newRole }
                }).ToCharacters(gang.GetMembers()).Send();

                var channelMemberRole = gang.HasRole(member, GangRole.Assistant) ? ChannelMemberRole.Operator : ChannelMemberRole.Undefined;

                _channelManager.SetMemberRole(gang.ChannelName, member, channelMemberRole);
            }

            if (Transaction.Current == null)
            {
                Transaction.Current.OnCommited(Finish);
            }
            else
            {
                Finish();
            }
        }
Example #6
0
        public void DisbandGang(Gang gang)
        {
            _gangRepository.Delete(gang);

            void Finish()
            {
                Message.Builder.SetCommand(Commands.GangDelete).WithData(gang.GetGangData()).ToCharacters(gang.GetMembers()).Send();
                _channelManager.DeleteChannel(gang.ChannelName);
                _gangs.Remove(gang.Id);
                GangDisbanded?.Invoke(gang);
            }

            if (Transaction.Current != null)
            {
                Transaction.Current.OnCommited(Finish);
            }
            else
            {
                Finish();
            }
        }
Example #7
0
        public void JoinMember(Gang gang, Character member, bool joinChannel)
        {
            _gangRepository.InsertMember(gang, member);

            void Finish()
            {
                gang.SetMember(member);

                var data = new Dictionary <string, object>
                {
                    { k.data, gang.GetGangData() },
                    { k.memberID, member.Id }
                };

                Message.Builder.SetCommand(Commands.GangAddMember).WithData(data).ToCharacters(gang.GetMembers()).Send();

                GangMemberJoined?.Invoke(gang, member);

                if (joinChannel)
                {
                    _channelManager.JoinChannel(gang.ChannelName, member);
                }
            }

            if (Transaction.Current != null)
            {
                Transaction.Current.OnCommited(Finish);
            }
            else
            {
                Finish();
            }
        }