private List <IOfficerOut> _getOfficerOutList(List <IUserOfficerOut> userOfficers) { var outOfficers = new List <IOfficerOut>(); if (userOfficers == null) { return(outOfficers); } foreach (var baseOfficer in OfficerHelper.GetOfficers()) { var type = baseOfficer.Type; var users = userOfficers.Where(i => i.Type == type).ToList(); if (users.Count > 2) { throw new NotImplementedException("users.Count > 2"); } var elected = users.Single(i => i.Elected); IUserOfficerOut app = null; var appUserOut = users.SingleOrDefault(i => !i.Elected); if (appUserOut != null) { app = appUserOut; } else if (type != OfficerTypes.President) { app = new UserOfficerOut(false, type); } var userOfficer = new OfficerOut(baseOfficer, elected, app); outOfficers.Add(userOfficer); } return(outOfficers.OrderBy(i => i.Type).ToList()); }
public OfficerOut(IOfficerBase officerBase, IUserOfficerOut elected, IUserOfficerOut appointed) { if (elected == null) { throw new ArgumentNullException(nameof(elected), Error.NoData); } Type = elected.Type; Elected = elected; Appointed = appointed; Translate = officerBase.Translate; Stats = officerBase.Stats; if (Appointed == null) { return; } if (Elected.Type != Appointed.Type) { throw new NotImplementedException("Elected.Type != Appointed.Type"); } }
public IUserOfficerOut SetNewOfficerByPresident(IDbConnection connection, IUserOfficerOut newOfficer, int presidentOfficerId, int presidentUserId) { var dataRestored = false; if (_weekOfficersStorage == null || _weekOfficersStorage.IsEmpty) { dataRestored = true; GetOfficers(connection, false); } if (newOfficer.Type == OfficerTypes.President) { throw new SecurityException(Error.NotPermitted); } if (newOfficer.Elected) { throw new Exception(Error.InputDataIncorrect); } IOfficerOut presidentOut; // ReSharper disable once PossibleNullReferenceException var hasPresident = _weekOfficersStorage.TryGetValue(OfficerTypes.President, out presidentOut); if (!hasPresident) { if (dataRestored) { throw new ArgumentNullException(nameof(presidentOut)); } GetOfficers(connection, false); hasPresident = _weekOfficersStorage.TryGetValue(OfficerTypes.President, out presidentOut); if (!hasPresident) { throw new ArgumentNullException(nameof(presidentOut)); } dataRestored = true; } if (presidentOut.Elected.Id != presidentOfficerId || presidentOut.Elected.UserId != presidentUserId) { throw new SecurityException(Error.NotPermitted); } var president = presidentOut.Elected; IOfficerOut officerOut; var hasOfficerOut = _weekOfficersStorage.TryGetValue(newOfficer.Type, out officerOut); if (!hasOfficerOut || officerOut.Appointed == null) { if (dataRestored) { if (!hasOfficerOut) { throw new ArgumentNullException(nameof(officerOut)); } if (officerOut.Appointed == null) { throw new NotImplementedException("officerOut.Appointed == null"); } } GetOfficers(connection, false); hasOfficerOut = _weekOfficersStorage.TryGetValue(newOfficer.Type, out officerOut); if (!hasOfficerOut) { throw new ArgumentNullException(nameof(officerOut)); } if (officerOut.Appointed == null) { throw new NotImplementedException("officerOut.Appointed == null"); } dataRestored = true; } if (officerOut.Appointed.Id != 0) { throw new Exception(Error.OfficerIsAlreadyExist); } var chek = _officerRepository.GetTopOfficerByUserId(connection, newOfficer.UserId); if (chek != null) { throw new Exception(Error.OfficerIsAlreadyExist); } var allianceUserTableName = _provider.GetTableName(nameof(alliance_user)); var allianceTableName = _provider.GetTableName(nameof(alliance_user)); var userTableName = _provider.GetTableName(nameof(user)); var sqlAdvancdData = $"SELECT TOP 4 " + @"u.Id as userId, " + @"u.avatarUrls as userAvatar, " + @"u.nickname as userName, " + @"u.pvpPoint as userPvpPoint, " + @"a.Id as allianceId, " + @"a.name as allianceName, " + @"a.images as allianceLabel " + $"FROM {allianceUserTableName} as au " + $"JOIN {allianceTableName} as a ON a.Id = au.allianceId " + $"JOIN {userTableName} as u ON u.Id = au.userId " + $"WHERE au.userId = {newOfficer.UserId}"; var targetData = _provider.Text <dynamic>(connection, sqlAdvancdData).Select(i => new WinnerCandidat(i)) .FirstOrDefault(); if (targetData == null) { throw new NotImplementedException(); } newOfficer.AllianceLabel = Label.GetFileUrls(targetData.AllianceLabel); newOfficer.AllianceName = targetData.AllianceName; newOfficer.AllianceId = targetData.AllianceId; newOfficer.AppointedUserId = president.UserId; newOfficer.UserAvatar = Avatar.GetFileUrls(targetData.UserAvatar); newOfficer.UserName = targetData.UserName; // todo set bonus to alliance var newOfficerData = _officerRepository.AddOrUpdate(connection, new c_officer { userId = newOfficer.UserId, allianceId = newOfficer.AllianceId, elected = false, officerType = (byte)newOfficer.Type, dateEnd = president.DateEnd, dateStart = UnixTime.UtcNow(), appointedUserId = president.UserId }); newOfficer.Id = newOfficerData.Id; officerOut.Appointed = newOfficer; var suc = _weekOfficersStorage.AddOrUpdateSimple(officerOut.Type, officerOut); if (suc == default(IOfficerOut)) { throw new NotImplementedException("Oficer not set in local instance"); } return(newOfficer); }