Пример #1
0
        public async Task <IHttpActionResult> Create(CreateChannelBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            ChannelDataRepository repository = new ChannelDataRepository();

            if (await repository.FindAsync(model.Name) == null)
            {
                ChannelDataModel channelDataModel = new ChannelDataModel();

                channelDataModel.Id   = model.Id;
                channelDataModel.Name = model.Name;

                repository.Add(channelDataModel);

                using (UserDataRepository db = new UserDataRepository())
                {
                    db.UpdateVisitedChannels(model.Owner, model.Name);
                }

                return(Ok());
            }

            return(GetErrorResult(new IdentityResult("Choose another ID")));
        }
Пример #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);
        }
Пример #3
0
 public bool DeleteAllianceChannel(IDbConnection connection, ChannelDataModel oldAllianceChannel)
 {
     if (oldAllianceChannel == null)
     {
         throw new NullReferenceException(Error.ChannelNotExist);
     }
     return(DeleteChannel(connection, oldAllianceChannel.Id));
 }
 protected BaseChannelOut(ChannelDataModel data)
 {
     ChannelId   = data.Id;
     ChannelType = data.ChannelType;
     ChannelName = data.ChannelName;
     DateCreate  = data.DateCreate;
     CreatorId   = data.CreatorId;
     CreatorName = data.CreatorName;
     ChannelIcon = data.ChannelIcon;
     PerPage     = GetPerPage(ChannelType);
 }
Пример #5
0
 public static ChannelConnectionDataModel CreateModelByChannel(this ChannelDataModel channel, int userId, bool messageRead, bool messageSend)
 {
     return(new ChannelConnectionDataModel
     {
         UserId = userId,
         ChannelId = channel.Id,
         Password = channel.Password,
         MessageRead = messageRead,
         MessageSend = messageSend,
         ChannelType = channel.ChannelType
     });
 }
        /// <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;
            }));
        }
Пример #7
0
 public static ChannelConnectionDataModel CreateModelByAllianceChannel(this ChannelDataModel allianceChannel, AllianceUserDataModel allianceUser, AllianceRoleDataModel role)
 {
     return(new ChannelConnectionDataModel
     {
         UserId = allianceUser.UserId,
         ChannelId = allianceChannel.Id,
         Password = allianceChannel.Password,
         MessageRead = role.MessageRead,
         MessageSend = role.MessageSend,
         ChannelType = allianceChannel.ChannelType
     });
 }
        public GroupChannelOut CreateGroupChannel(IDbConnection connection, ChannelDataModel baseData)
        {
            GroupChannelOut result              = null;
            var             groupType           = (byte)ChannelTypes.Group;
            var             maxChannelsFromUser = 1;

            var channels = _channelRepo.GetPreCreateChannels(connection, groupType, baseData.CreatorId, baseData.ChannelName).ToList();

            if (channels.Any())
            {
                var existChannelByName = channels.SingleOrDefault(i => i.channelName == baseData.ChannelName);
                if (existChannelByName != null)
                {
                    throw new Exception(Error.ChannelNameNotValid);
                }
                if (channels.Count > maxChannelsFromUser)
                {
                    throw new Exception(Error.UserGroupChannelLimit);
                }
            }


            var userChannel  = CreateChannel(connection, baseData, baseData.CreatorId);
            var startMessage = CreateMessage(connection, new ChannelMessageDataModel
            {
                UserId     = userChannel.CreatorId,
                DateCreate = userChannel.DateCreate,
                ChannelId  = userChannel.Id,
                UserName   = userChannel.CreatorName,
                Message    = "Welcome to " + userChannel.ChannelName + "!",
                UserIcon   = userChannel.ChannelIcon,
            });


            var userChannelOut = new GroupChannelOut(userChannel, userChannel.CreatorId)
            {
                Messages = new Dictionary <long, ChannelMessageDataModel> {
                    { startMessage.Id, startMessage }
                }
            };

            userChannelOut.SetUsersIfCanMansge(connection, _channelConnRepo);
            userChannelOut.SetBtnSend(true);
            userChannelOut.SetComplexButtonView();

            result = userChannelOut;

            return(result);
        }
Пример #9
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);
        }
Пример #10
0
        public ChannelDataModel CreateChannel(IDbConnection connection, ChannelDataModel newChannelModel, int creatorUserId)
        {
            if (newChannelModel.DateCreate == 0)
            {
                newChannelModel.DateCreate = UnixTime.UtcNow();
            }
            var channel = _channelRepo.AddOrUpdateeModel(connection, newChannelModel);

            if (channel == null)
            {
                throw new NullReferenceException(Error.ChannelNotExist);
            }
            channel.ChannelConnections = new List <ChannelConnectionDataModel> {
                AddUserToConnectionChannel(connection, channel, creatorUserId, true, true, channel.Password)
            };
            return(channel);
        }
Пример #11
0
        public ChannelDataModel GetPrivateChannel(IDbConnection connection, int firstUser, int secodUser)
        {
            ChannelDataModel result = null;
            var chCon = _channelConnRepo.GetPrivateChannelConnections(connection, firstUser, secodUser)?.ToList();

            if (chCon == null || !chCon.Any())
            {
                return(result);
            }
            var channel = chCon.First(i => i.HasChannel() && i.GetChannel().creatorId == i.userId).GetChannel().ConvertToWorkModel();

            channel.ChannelConnections = new List <ChannelConnectionDataModel>();
            foreach (var con in chCon)
            {
                channel.ChannelConnections.Add(_channelConnRepo.ConvertToWorkModel(con));
            }
            result = channel;
            return(result);
        }
Пример #12
0
        public PrivateChannelOut GetPrivateChannelOut(IDbConnection connection, ChannelDataModel data, int currentUserId, int skip = 0)
        {
            PrivateChannelOut result = null;
            var pChOut = new PrivateChannelOut(data);

            if (data.ChannelConnections != null)
            {
                var count = data.ChannelConnections.Count;
                switch (count)
                {
                case 2:
                    break;

                case 1:

                    var ignoreId = data.ChannelConnections[0].UserId;
                    var others   = _channelConnRepo.GetOtherChannlConnecttions(connection, data.Id, ignoreId, (byte)ChannelTypes.Private).ToList();
                    if (others.Count != 1)
                    {
                        throw new ArgumentException(nameof(others), @" others.Count!=1 ");
                    }
                    data.ChannelConnections.Add(_channelConnRepo.ConvertToWorkModel(others[0]));
                    break;

                default:
                    throw new NotImplementedException("channel connection count is wrong");
                }
            }
            else
            {
                data.ChannelConnections = _channelConnRepo
                                          .ConvertToWorkModel(_channelConnRepo.GetByChannelId(connection, data.Id).ToList())
                                          .ToList();
            }

            var curConn = data.ChannelConnections.Single(i => i.UserId == currentUserId);

            pChOut.SetBtnSend(curConn.MessageSend);
            pChOut.SetMessages(connection, _channelMessageRepo, skip);
            pChOut.SetComplexButtonView();
            result = pChOut;
            return(result);
        }
        public async Task <GroupChannelOut> UserChannelsCreateGroupChannel(ChannelDataModel newChannelData)
        {
            return(await _contextActionAsync(async connection =>
            {
                newChannelData.ChannelName.ValidateChannelName();
                newChannelData.ChannelIcon.ValidateIcon();
                var cr = _getCurrentUser(connection);
                newChannelData.DateCreate = UnixTime.UtcNow();
                newChannelData.CreatorId = cr.UserId;
                newChannelData.CreatorName = cr.Name;
                newChannelData.ChannelName = newChannelData.ChannelName.ToUpper();
                newChannelData.ChannelType = ChannelTypes.Group;

                var channelOut = _channelService.CreateGroupChannel(connection, newChannelData);

                await cr.AddOrReplaceGroupChannelGroupNameAsync(Groups, channelOut.ChannelId, channelOut.ChannelName);
                var updHubUser = _hubCache.AddOrUpdateLocal(cr, true);
                await Clients.Client(updHubUser.ConnectionId).InvokeAsync("updateConnectionUser", updHubUser);
                return channelOut;
            }));
        }
        /// <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 AllianceChannelOut(ChannelDataModel data) : base(data)
 {
 }
 public GroupChannelOut(ChannelDataModel data, int currentUserId) : base(data)
 {
     _data     = data;
     CanManage = data.CreatorId == currentUserId;
     IsPublic  = data.Password == "";
 }
        public const int PRIVATE_PER_PAGE = 3; //BASE_PER_PAGE;


        public PrivateChannelOut(ChannelDataModel data) : base(data)
        {
        }
Пример #18
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);
            }
        }
 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);
     }
 }
Пример #20
0
 public static ChannelConnectionDataModel CreateModelByAllianceChannel(this ChannelDataModel allianceChannel, AllianceUserDataModel allianceUser)
 {
     return(allianceChannel.CreateModelByAllianceChannel(allianceUser, AllianceRoleHelper.GetByRoleId(allianceUser.RoleId)));
 }
Пример #21
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);
        }
Пример #22
0
        public ChannelConnectionDataModel AddUserToConnectionChannel(IDbConnection connection, ChannelDataModel channel, int userId, bool messageRead, bool messageSend, string channelPassword)
        {
            if (channel == null)
            {
                throw new NullReferenceException(Error.ChannelNotExist);
            }
            if (channel.Password != channelPassword)
            {
                throw new SecurityException(Error.PasswordIncorrect);
            }
            var model = channel.CreateModelByChannel(userId, messageRead, messageSend);
            var ent   = _channelConnRepo.ConvertToEntity(model);
            var data  = _channelConnRepo.AddOpUpdateChannelConnectionByChannelIdAndUserId(connection, ent);

            return(_channelConnRepo.ConvertToWorkModel(data));
        }