public ChannelConnectionDataModel UnsubscribeUserFromGroupChannel(IDbConnection connection, int channelId, int removeUserId, Action <int> setChannelAdminUserId)
        {
            ChannelConnectionDataModel result = null;
            var groupType = (byte)ChannelTypes.Group;
            var channel   = _channelRepo.GetChannelWithConnectedUsers(connection, channelId, new List <int>(removeUserId));

            if (channel == null)
            {
                throw new Exception(Error.ChannelNotExist);
            }
            setChannelAdminUserId(channel.creatorId);
            var channelConnection = channel.GetConnections().SingleOrDefault(i => i.userId == removeUserId && i.channelType == groupType);

            if (channelConnection == null)
            {
                throw new NullReferenceException(Error.IsEmpty);
            }
            var data = _channelConnRepo.ConvertToWorkModel(channelConnection);
            var suc  = _channelConnRepo.Delete(connection, data.Id);

            if (!suc)
            {
                throw new NotImplementedException();
            }
            result = data;
            return(result);
        }
Пример #2
0
        public void OnUserChangeAlliance(IDbConnection connection, int oldAllianceId, AllianceUserDataModel newAllianceUserDataModel, Action <ChannelConnectionDataModel, ChannelDataModel> setNewChannelData, Action <int> setOldChannelId)
        {
            ChannelConnectionDataModel newChannelConnectionDataModel = null;
            ChannelDataModel           newChannelDataModel           = null;


            _onUserChangeAlliance(connection, oldAllianceId, newAllianceUserDataModel, (chtConn, channel) =>
            {
                newChannelConnectionDataModel = chtConn;
                newChannelDataModel           = channel;
            }, oldChannelId =>
            {
                if (oldChannelId == 0)
                {
                    throw new NullReferenceException(nameof(oldChannelId));
                }
                setOldChannelId(oldChannelId);
            });

            if (newChannelDataModel == null)
            {
                throw new NullReferenceException(nameof(newChannelDataModel));
            }
            if (newChannelConnectionDataModel == null)
            {
                throw new NullReferenceException(nameof(newChannelConnectionDataModel));
            }
            setNewChannelData(newChannelConnectionDataModel, newChannelDataModel);
        }
        public async Task <ChannelConnectionUserOut> UserChannelsGroupUpdateUser(ChannelConnectionUserOut tragetUser,
                                                                                 bool updatePasswordByChannel, int channelAdminUserId)
        {
            return(await _contextActionAsync(async connection =>
            {
                //ChannelConnectionUserOut
                var cr = _getCurrentUser(connection);

                if (cr.UserId != channelAdminUserId)
                {
                    throw new SecurityException(Error.NotPermitted);
                }
                GroupChannelOut targetChannel = null;
                ChannelConnectionDataModel oldModel = null;

                var channelId = tragetUser.ChannelId;
                var hasChange = _channelService.UpdateGroupUser(connection, tragetUser, updatePasswordByChannel,
                                                                channelAdminUserId,
                                                                targetUserChat => { targetChannel = targetUserChat; },
                                                                oldM => { oldModel = oldM; });

                if (!hasChange)
                {
                    return tragetUser;
                }
                var tu = _getOnlineSingleUser(connection, tragetUser.UserId);
                if (tu == null)
                {
                    return tragetUser;
                }
                var iHubGroupItem = cr.GetUserChannelGroup(channelId);
                // changes for target  user
                if (targetChannel != null)
                {
                    await tu.AddOrReplaceGroupChannelGroupNameAsync(Groups, channelId, iHubGroupItem.NativeName);
                    _hubCache.AddOrUpdateLocal(tu, true);
                    await Clients.Client(tu.ConnectionId).InvokeAsync("userChannelsAddOrUpdateGroupChannel",
                                                                      targetChannel, iHubGroupItem);
                    //todo Clients.Client(tu.ConnectionId).
                    return tragetUser;
                }
                if (oldModel.MessageRead && !tragetUser.MessageRead)
                {
                    await tu.RemoveGroupChannelNameAsync(Groups, channelId);
                    _hubCache.AddOrUpdateLocal(tu, true);
                    await Clients.Client(tu.ConnectionId).InvokeAsync("userChannelsGroupDropChannel", channelId, false,
                                                                      iHubGroupItem.GroupeName);
                    return tragetUser;
                }

                await Clients.Client(tu.ConnectionId)
                .InvokeAsync("onUserChannelsCrUserGroupUpdatedPermition", tragetUser);
                return tragetUser;
            }));
        }
        /// <summary>
        /// </summary>
        /// <param name="targetDropUserId"></param>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotExist</exception>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotConnected</exception>
        /// <returns></returns>
        public async Task <bool> AllianceDropUserFromAlliance(int targetDropUserId)
        {
            return(await _transactionAsync(async transaction =>
            {
                var connection = transaction.Connection;
                var cr = _getCurrentUser(connection);
                var role = AllianceRoleHelper.GetByRoleId(cr.AllianceRoleId);
                if (!role.DeleteMembers)
                {
                    throw new SecurityException(Error.NotPermitted);
                }

                var oldTargetAllianceUser = _allianceService.GetAllianceUserByUserId(connection, targetDropUserId);
                var oldAllianceId = oldTargetAllianceUser.AllianceId;
                var newTargetDropAllianceUser =
                    _allianceService.LeaveFromAlliance(connection, oldTargetAllianceUser, false);
                //todo обработать чаты
                ChannelConnectionDataModel newChannelConnectionDataModel = null;
                ChannelDataModel newChannelDataModel = null;
                var oldChannelId = 0;
                _channelService.OnUserChangeAlliance(connection, oldAllianceId, newTargetDropAllianceUser,
                                                     (chtConn, channel) =>
                {
                    newChannelConnectionDataModel = chtConn;
                    newChannelDataModel = channel;
                }, oldChId => { oldChannelId = oldChId; });

                var targetUser = _getOnlineSingleUser(connection, targetDropUserId);
                if (targetUser != null)
                {
                    await targetUser.RemoveAllianceGroupNameAsync(Groups);
                    targetUser.SetNewAllianceData(newTargetDropAllianceUser.Id, Npc.ConfederationName,
                                                  newTargetDropAllianceUser.RoleId, (byte)NpcAllianceId.Confederation);

                    var newGroupName = await targetUser.AddOrReplaceAllianceGroupAsync(Groups);
                    var updHubUser = _hubCache.AddOrUpdateLocal(targetUser, true);
                    // обновляем данные
                    var newTargetUserPlanshet =
                        _allianceSetNewPlanshetAndNotifyNewGroup(connection, updHubUser, newTargetDropAllianceUser, newGroupName);

                    var newAllianceChannelOutDataModel =
                        _channelService.GetAllianceChannelOut(connection, newChannelDataModel,
                                                              newChannelConnectionDataModel);
                    await Clients.Client(targetUser.ConnectionId).InvokeAsync("onAllianceUserDroped", cr.AllianceId,
                                                                              updHubUser,
                                                                              newTargetUserPlanshet, oldChannelId, newAllianceChannelOutDataModel);
                }
                var oldGroupName = cr.CreateAllianceGroupName();
                _allianceNotifyOldAllianceUserLeft(targetDropUserId, cr.AllianceId, oldGroupName);

                return true;
            }));
        }
 private void _userChannelsCheckAndFixChannelCoonectionUser(IDbConnection connection, ChannelDataModel channel,
                                                            ChannelConnectionDataModel conn)
 {
     if (!conn.MessageRead || !conn.MessageSend || conn.Password != channel.Password)
     {
         if (!conn.MessageRead)
         {
             conn.MessageRead = true;
         }
         if (!conn.MessageSend)
         {
             conn.MessageSend = true;
         }
         if (conn.Password != channel.Password)
         {
             conn.Password = channel.Password;
         }
         _channelService.UpdateChannelConnection(connection, conn);
     }
 }
        /// <summary>
        /// </summary>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotExist</exception>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotConnected</exception>
        /// <returns></returns>
        public async Task <IPlanshetViewData> AllianceLeaveFromUserAlliance()
        {
            return(await _transactionAsync(async transaction =>
            {
                var connection = transaction.Connection;
                var leaveUser = _getCurrentUser(connection);
                var npcAllianceId = (byte)NpcAllianceId.Confederation;
                var oldGroupName = await leaveUser.RemoveAllianceGroupNameAsync(Groups);
                var oldAllianceId = leaveUser.AllianceId;
                var newAllianceUser = _allianceService.LeaveFromAlliance(connection, leaveUser.UserId, false);

                //todo обработать чаты
                ChannelConnectionDataModel newChannelConnectionDataModel = null;
                ChannelDataModel newChannelDataModel = null;
                var oldChannelId = 0;
                _channelService.OnUserChangeAlliance(connection, oldAllianceId, newAllianceUser,
                                                     (chtConn, channel) =>
                {
                    newChannelConnectionDataModel = chtConn;
                    newChannelDataModel = channel;
                }, oldChId => { oldChannelId = oldChId; });

                leaveUser.SetNewAllianceData(newAllianceUser.Id, Npc.ConfederationName, newAllianceUser.RoleId,
                                             npcAllianceId);
                var newGroupName = await leaveUser.AddOrReplaceAllianceGroupAsync(Groups);
                var updHubUser = _hubCache.AddOrUpdateLocal(leaveUser, true);
                _allianceNotifyOldAllianceUserLeft(leaveUser.UserId, oldAllianceId, oldGroupName);

                var newAlliancePlanshet =
                    _allianceSetNewPlanshetAndNotifyNewGroup(connection, updHubUser, newAllianceUser, newGroupName);

                var newAllianceChannel =
                    _channelService.GetAllianceChannelOut(connection, newChannelDataModel,
                                                          newChannelConnectionDataModel);
                await Clients.Client(updHubUser.ConnectionId).InvokeAsync("onUserChannelsUserChangeAlliance",
                                                                          oldChannelId, newAllianceChannel, updHubUser);

                return newAlliancePlanshet;
            }));
        }
 public ChannelConnectionUserOut(ChannelConnectionDataModel data, string userName, string channelPassword) :
     base(data)
 {
     HasCorrectPassword = data.Password == channelPassword;
     UserName           = userName;
 }
Пример #8
0
        public ChannelConnectionDataModel UpdateChannelConnection(IDbConnection connection, ChannelConnectionDataModel dataModel)
        {
            var ent = _channelConnRepo.ConvertToEntity(dataModel);
            var suc = _channelConnRepo.Update(connection, ent);

            if (!suc)
            {
                throw new NotImplementedException();
            }
            return(dataModel);
        }
        public GroupChannelOut JoinUserToGroupChannel(IDbConnection connection, int channelId, string password, int userId, Action <NameIdInt, ChannelConnectionUserOut> setChannelOwnerAndUserOut)
        {
            GroupChannelOut result    = null;
            var             groupType = (byte)ChannelTypes.Group;

            var channel = _channelRepo.GetChannelWithConnectedUsers(connection, channelId, new List <int>());

            if (channel == null)
            {
                throw new Exception(Error.ChannelNotExist);
            }
            if (channel.creatorId == userId)
            {
                throw new NotImplementedException("is creator user channel must be exist before");
            }

            var admin = new NameIdInt(channel.creatorId, channel.creatorName);

            if (channel.password != password)
            {
                setChannelOwnerAndUserOut(admin, null);
                throw new SecurityException(Error.NotPermitted);
            }
            var maxLimit          = (int)MaxLenghtConsts.GroupChannelsLimit;
            var channelConnection = channel.GetConnections().SingleOrDefault(i => i.userId == userId && i.channelType == groupType);

            ChannelConnectionDataModel targetChannelConnectionData;

            if (channelConnection == null)
            {
                var chConnCount = _channelConnRepo.GetCountConectionsForUser(connection, userId);
                var canAdd      = chConnCount <= maxLimit - 1;
                if (!canAdd)
                {
                    throw new Exception(Error.MaxChannelsLimit);
                }
                targetChannelConnectionData = new ChannelConnectionDataModel
                {
                    UserId      = userId,
                    Password    = password,
                    MessageRead = true,
                    MessageSend = true,
                    ChannelType = ChannelTypes.Group,
                    ChannelId   = channel.Id
                };
                var entyty = _channelConnRepo.ConvertToEntity(targetChannelConnectionData);
                entyty = _channelConnRepo.AddOrUpdate(connection, entyty);
                targetChannelConnectionData.Id = entyty.Id;
            }
            else
            {
                if (!channelConnection.messageRead)
                {
                    setChannelOwnerAndUserOut(admin, null);
                    throw new SecurityException(Error.YouAreBlockedInThisChannel);
                }
                // ReSharper disable once InvertIf
                if (channelConnection.password != password)
                {
                    channelConnection.password = password;
                    var updated = _channelConnRepo.Update(connection, channelConnection);
                    if (!updated)
                    {
                        throw new NotImplementedException();
                    }
                }

                targetChannelConnectionData = _channelConnRepo.ConvertToWorkModel(channelConnection);
            }

            var channelData = channel.ConvertToWorkModel();

            setChannelOwnerAndUserOut(admin, new ChannelConnectionUserOut(targetChannelConnectionData, "", password));
            var channelOut = new GroupChannelOut(channelData, userId);

            channelOut.SetMessages(connection, _channelMessageRepo);
            channelOut.SetBtnSend(targetChannelConnectionData.MessageSend);
            channelOut.SetComplexButtonView();
            result = channelOut;
            return(result);
        }
Пример #10
0
        /// <summary>
        /// </summary>
        /// <param name="toAllianceId"></param>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotExist</exception>
        /// <exception cref="ArgumentNullException">Error.ConnectionUserNotConnected</exception>
        /// <exception cref="Exception">Error.AllianceActionInBlockedState</exception>
        /// <returns></returns>
        public async Task <IPlanshetViewData> RequestAllianceAcceptJoinUserToAlliance(int toAllianceId)
        {
            var timeToDone = 0;

            try
            {
                return(await _dbProvider.TransactionAsync(async transaction =>
                {
                    var connection = transaction.Connection;
                    var joinedUser = _getCurrentUser(connection);  //cr
                    var gameUser = _gameUserService.GetGameUser(connection, joinedUser.UserId);
                    var curTime = UnixTime.UtcNow();
                    var leftTime = curTime - gameUser.LeaveAllianceTime;
                    if (leftTime < UserDataModel.JoinJoAlllianceBlockedTime)
                    {
                        timeToDone = UserDataModel.JoinJoAlllianceBlockedTime - leftTime;
                        throw new Exception(Error.AllianceActionInBlockedState);
                    }

                    var oldAllianceUserId = joinedUser.AllianceUserId;
                    var oldAllianceId = joinedUser.AllianceId;
                    var newAllianceUser =
                        _allianceService.LeaveFromNpcAndJoinToUserAlliance(connection, oldAllianceUserId,
                                                                           toAllianceId);
                    var newAlliance = _allianceService.GetAllianceById(connection, toAllianceId, false);

                    //todo обработать чаты
                    ChannelConnectionDataModel newChannelConnectionDataModel = null;
                    ChannelDataModel newChannelDataModel = null;
                    var oldChannelId = 0;
                    _channelService.OnUserChangeAlliance(connection, oldAllianceId, newAllianceUser, (chtConn, channel) =>
                    {
                        newChannelConnectionDataModel = chtConn;
                        newChannelDataModel = channel;
                    }, oldChId => { oldChannelId = oldChId; });

                    var npcAllianceId = (int)NpcAllianceId.Confederation;
                    var oldGroupName = await joinedUser.RemoveAllianceGroupNameAsync(Groups);
                    joinedUser.SetNewAllianceData(newAllianceUser.Id, newAlliance.Name, newAllianceUser.RoleId,
                                                  newAllianceUser.AllianceId);
                    var newGroupName = await joinedUser.AddOrReplaceAllianceGroupAsync(Groups);
                    var updHubUser = _hubCache.AddOrUpdateLocal(joinedUser, true);
                    _allianceNotifyOldAllianceUserLeft(joinedUser.UserId, npcAllianceId, oldGroupName);

                    var newAlliancePlanshet = _allianceSetNewPlanshetAndNotifyNewGroup(connection, updHubUser, newAllianceUser, newGroupName);

                    //todo  какое то оповещение менеджеров для обновления информации о юзерах в чатах
                    if (newChannelConnectionDataModel.MessageRead)
                    {
                        var newAllianceChannel = _channelService.GetAllianceChannelOut(transaction.Connection,
                                                                                       newChannelDataModel, newChannelConnectionDataModel);
                        await Clients.Client(updHubUser.ConnectionId).InvokeAsync("onUserChannelsUserChangeAlliance",
                                                                                  oldChannelId, newAllianceChannel, updHubUser);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                    return newAlliancePlanshet;
                }));
            }
            catch (Exception e)
            {
                if (timeToDone != 0)
                {
                    e.Data.Add("TimeToDone", timeToDone);
                    throw new HubException(e.Message, e);
                }
                throw new HubException(e.Message, e);
            }
        }
Пример #11
0
        private void _onUserChangeAlliance(IDbConnection connection, int oldAllianceId, AllianceUserDataModel newAllianceUserDataModel, Action <ChannelConnectionDataModel, ChannelDataModel> setNewChannelData, Action <int> setOldChannelId)
        {
            var typeChannel = (byte)ChannelTypes.Alliance;
            ChannelConnectionDataModel newChannelConnectionDataModel = null;
            ChannelDataModel           newChannelDataModel           = null;
            var channels = _channelRepo.GetChannels(connection, new List <int> {
                oldAllianceId, newAllianceUserDataModel.AllianceId
            }, typeChannel);

            if (!channels.Any())
            {
                throw new NotImplementedException("!channels.Any()");
            }
            //var channels = c.channel.Where(i =>
            //                 i.creatorId == oldAllianceId || i.creatorId == newAllianceUserDataModel.AllianceId &&
            //                 i.channelType == typeChannel).ToList();
            var oldChannel = channels.First(i => i.creatorId == oldAllianceId);
            var newChannel = channels.First(i => i.creatorId == newAllianceUserDataModel.AllianceId);

            setOldChannelId(oldChannel.Id);
            var oldConnection = _channelConnRepo.GetUserConnectedChannl(connection, oldChannel.Id, newAllianceUserDataModel.UserId, typeChannel);

            newChannelDataModel = newChannel.ConvertToWorkModel();
            var newRole = AllianceRoleHelper.GetByRoleId(newAllianceUserDataModel.RoleId);

            if (oldConnection != null)
            {
                oldConnection.password    = newChannel.password;
                oldConnection.channelId   = newChannel.Id;
                oldConnection.messageRead = newRole.MessageRead;
                oldConnection.messageSend = newRole.MessageSend;
                var updOldConnection = _channelConnRepo.Update(connection, oldConnection);
                if (!updOldConnection)
                {
                    throw new NotImplementedException();
                }

                newChannelConnectionDataModel = _channelConnRepo.ConvertToWorkModel(oldConnection);
            }
            else
            {
                var data = _channelConnRepo.AddOrUpdate(connection, new channel_connection
                {
                    userId      = newAllianceUserDataModel.UserId,
                    messageRead = newRole.MessageRead,
                    messageSend = newRole.MessageSend,
                    password    = newChannel.password,
                    channelType = newChannel.channelType,
                    channelId   = newChannel.Id
                });

                newChannelConnectionDataModel = _channelConnRepo.ConvertToWorkModel(data);
                setNewChannelData(newChannelConnectionDataModel, newChannelDataModel);
            }

            if (newChannelDataModel.ChannelConnections == null)
            {
                newChannelDataModel.ChannelConnections = new List <ChannelConnectionDataModel> {
                    newChannelConnectionDataModel
                };
            }
            else
            {
                newChannelDataModel.ChannelConnections.Add(newChannelConnectionDataModel);
            }
            setNewChannelData(newChannelConnectionDataModel, newChannelDataModel);
        }
Пример #12
0
        public AllianceChannelOut GetAllianceChannelOut(IDbConnection connection, ChannelDataModel allianceChannel, ChannelConnectionDataModel crUserChannelConnection, int skip = 0)
        {
            if (!crUserChannelConnection.MessageRead)
            {
                return(null);
            }
            AllianceChannelOut result = null;
            var allianceChannelOut    = new AllianceChannelOut(allianceChannel);

            allianceChannelOut.SetMessages(connection, _channelMessageRepo, skip);
            allianceChannelOut.SetComplexButtonView();
            allianceChannelOut.SetBtnSend(crUserChannelConnection.MessageSend);
            result = allianceChannelOut;
            return(result);
        }