Mineplex2018-withcommit/Website/LOC.Website.Common/Models/AccountAdministrator.cs

918 lines
38 KiB
C#
Raw Normal View History

2013-08-27 17:14:08 +02:00
namespace LOC.Website.Common.Models
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Core;
using Core.Data;
using Core.Model.Account;
using Core.Model.Sales;
using Core.Model.Server.GameServer;
2013-08-27 17:14:08 +02:00
using Core.Model.Server.PvpServer;
using Core.Tokens;
using Core.Tokens.Client;
using Data;
using LOC.Website.Common.Contexts;
using System.Data.Entity.Infrastructure;
2014-09-02 05:03:08 +02:00
using System.Transactions;
using System.Runtime.CompilerServices;
2013-08-27 17:14:08 +02:00
public class AccountAdministrator : IAccountAdministrator
{
private readonly INautilusRepositoryFactory _repositoryFactory;
private readonly IGameServerMonitor _gameServerMonitor;
private readonly ILogger _logger;
2013-08-27 17:14:08 +02:00
private static ConditionalWeakTable<string, object> _accountLocks = new ConditionalWeakTable<string, object>();
2013-08-27 17:14:08 +02:00
public AccountAdministrator(INautilusRepositoryFactory nautilusRepositoryFactory, ILogger logger)
2013-08-27 17:14:08 +02:00
{
_repositoryFactory = nautilusRepositoryFactory;
_gameServerMonitor = GameServerMonitor.Instance;
_logger = logger;
2013-08-27 17:14:08 +02:00
}
public List<String> GetAccountNames()
{
using (var repository = _repositoryFactory.CreateRepository())
{
return repository.GetAll<Account>().Select(x => x.Name).ToList();
}
}
public List<Account> GetAllAccountsMatching()
{
using (var repository = _repositoryFactory.CreateRepository())
{
return repository.GetAll<Account>().Include(x => x.Rank).ToList();
}
}
public List<Account> GetAllAccountsMatching(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
return repository.GetAll<Account>().Where(c => c.Name == name).Include(x => x.Rank).ToList();
2013-08-27 17:14:08 +02:00
}
}
public List<String> GetAllAccountNamesMatching(string name)
{
var accounts = new List<String>();
2013-08-27 17:14:08 +02:00
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == name).OrderByDescending(y => y.LastLogin).FirstOrDefault();
if (account != null)
accounts.Add(account.Name);
2013-08-27 17:14:08 +02:00
}
return accounts;
2013-08-27 17:14:08 +02:00
}
/*
2015-05-13 09:00:37 +02:00
public List<AccountTask> GetTasksByCount(SearchConf searchConf)
2015-05-13 03:59:22 +02:00
{
var tasks = new List<AccountTask>();
try
2015-05-13 03:59:22 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
2015-05-13 03:59:22 +02:00
{
var gameTasks = repository.GetAll<GameTask>().Where(x => x.GameTaskId > searchConf.IdIndex).OrderBy(x => x.GameTaskId).Take(searchConf.Count).Include(x => x.Account).ToList();
2015-05-13 03:59:22 +02:00
foreach (var task in gameTasks)
{
AccountTask accountTask = new AccountTask();
accountTask.Id = task.GameTaskId;
accountTask.Task = task.TaskName;
accountTask.UUID = task.Account.Uuid;
tasks.Add(accountTask);
}
2015-05-13 03:59:22 +02:00
}
}
catch (Exception ex)
{
_logger.Log("ERROR", ex.Message + " : " + ex.StackTrace);
}
2015-05-13 03:59:22 +02:00
return tasks;
}
*/
2015-05-13 03:59:22 +02:00
private object getAccountLock(string name)
2013-08-27 17:14:08 +02:00
{
object lockObject = null;
if (!_accountLocks.TryGetValue(name, out lockObject))
2013-08-27 17:14:08 +02:00
{
lockObject = new object();
_accountLocks.Add(name, lockObject);
}
2013-08-27 17:14:08 +02:00
return lockObject;
}
public Account Login(LoginRequestToken loginToken)
{
lock (getAccountLock(loginToken.Name))
{
using (var repository = _repositoryFactory.CreateRepository())
2013-08-27 17:14:08 +02:00
{
var account = repository.Where<Account>(x => x.Uuid == loginToken.Uuid).FirstOrDefault();
if (account == default(Account))
account = CreateAccount(loginToken, repository);
account.LoadNavigationProperties(repository.Context);
account.LastLogin = DateTime.Now.Ticks;
if (String.IsNullOrEmpty(account.Uuid))
account.Uuid = loginToken.Uuid;
// Expire punishments
if (account.Punishments != null)
2013-08-27 17:14:08 +02:00
{
foreach (var expiredPunishment in account.Punishments.Where(x => x.Active && (x.Duration - 0d) > 0 && TimeUtil.GetCurrentMilliseconds() > (x.Time + (x.Duration * 3600000))))
{
expiredPunishment.Active = false;
}
2013-08-27 17:14:08 +02:00
}
// Insert UUID if not there
if (String.IsNullOrEmpty(account.Uuid))
{
if (!String.IsNullOrEmpty(loginToken.Uuid))
account.Uuid = loginToken.Uuid;
}
2014-04-20 05:53:55 +02:00
// Update account name if changed
if (!String.Equals(account.Name, loginToken.Name))
{
account.Name = loginToken.Name;
var oldAccount = repository.Where<Account>(x => x.Name == loginToken.Name).FirstOrDefault();
if (oldAccount != null && oldAccount != default(Account))
{
}
}
/*
// Expire ranks
if ((account.Rank.Name == "ULTRA" || account.Rank.Name == "HERO") && !account.RankPerm && DateTime.Now.CompareTo(account.RankExpire) >= 0)
{
account.Rank = repository.Where<Rank>(x => x.Name == "ALL").First();
repository.Attach(account.Rank);
}
* */
2013-08-27 17:14:08 +02:00
repository.Edit(account);
repository.CommitChanges();
2013-08-27 17:14:08 +02:00
return account;
}
2013-08-27 17:14:08 +02:00
}
}
public void Logout(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = GetAccountByName(name, repository);
if (account.Logins.Any())
{
account.LastLogin = account.Logins.OrderBy(x => x.Time).Last().Time;
account.TotalPlayingTime += DateTime.Now.Subtract(TimeSpan.FromTicks(account.LastLogin)).Ticks;
}
repository.CommitChanges();
_gameServerMonitor.PlayerLoggedOut(account);
}
}
public Account GetAccountByName(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
return GetAccountByName(name, repository);
}
}
public Account GetAccountById(int id)
{
using (var repository = _repositoryFactory.CreateRepository())
{
return GetAccountByName(repository.GetAll<Account>().First(x => x.AccountId == id).Name, repository);
}
}
public Account CreateAccount(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Add(new Account
{
Gems = 1200,
Coins = 0,
2013-08-27 17:14:08 +02:00
Name = name,
Rank = repository.Where<Rank>(x => x.RankId == 1).First(),
LastVote = DateTime.Today.Subtract(TimeSpan.FromDays(5)),
RankExpire = DateTime.Now
});
repository.CommitChanges();
return account;
}
}
public void UpdateAccountUUIDs(List<AccountNameToken> tokens)
{
try
{
using (var repository = _repositoryFactory.CreateRepository())
{
foreach (AccountNameToken token in tokens)
{
var account = GetAccountByName(token.Name);
account.Uuid = token.UUID;
repository.Context.Database.ExecuteSqlCommand("UPDATE dbo.Accounts SET Uuid = {0} WHERE Name = {1}", token.UUID, token.Name);
}
repository.CommitChanges();
}
}
catch (Exception exception)
{
_logger.Log("Error", exception.Message);
}
}
public List<AccountNameToken> GetAccounts(AccountBatchToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
return repository.GetAll<Account>().OrderBy(x => x.AccountId).Skip(token.Start).Take(token.End - token.Start).Select(x => new AccountNameToken { Name = x.Name }).ToList();
}
}
2013-08-27 17:14:08 +02:00
public bool GemReward(GemRewardToken token)
2013-08-27 17:14:08 +02:00
{
lock (getAccountLock(token.Name))
2013-08-27 17:14:08 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).OrderByDescending(x => x.LastLogin).FirstOrDefault();
if (account == null)
return false;
token.OriginalBalance = account.Gems;
account.Gems += token.Amount;
if (!token.Source.Contains("Earned") && !token.Source.Contains("Tutorial") && !token.Source.Contains("Parkour"))
{
var gemTransaction = new GemTransaction
{
Source = token.Source,
Account = account,
Amount = token.Amount,
Date = (long)TimeUtil.GetCurrentMilliseconds()
};
repository.Add<GemTransaction>(gemTransaction);
}
repository.Edit(account);
repository.CommitChanges();
}
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).OrderByDescending(x => x.LastLogin).FirstOrDefault();
if (account == null)
return false;
if (account.Gems != token.OriginalBalance + token.Amount)
return false;
}
2013-08-27 17:14:08 +02:00
}
return true;
2013-08-27 17:14:08 +02:00
}
public bool CoinReward(GemRewardToken token)
{
lock (getAccountLock(token.Name))
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).OrderByDescending(x => x.LastLogin).FirstOrDefault();
if (account == null)
return false;
token.OriginalBalance = account.Coins;
account.Coins += token.Amount;
if (!token.Source.Contains("Earned") && !token.Source.Contains("Tutorial") && !token.Source.Contains("Parkour"))
{
var coinTransaction = new CoinTransaction
{
Source = token.Source,
Account = account,
Amount = token.Amount,
Date = (long)TimeUtil.GetCurrentMilliseconds()
};
repository.Add<CoinTransaction>(coinTransaction);
}
repository.Edit(account);
repository.CommitChanges();
}
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Name).OrderByDescending(x => x.LastLogin).FirstOrDefault();
if (account == null)
return false;
if (account.Coins != token.OriginalBalance + token.Amount)
return false;
}
}
return true;
}
2013-08-27 17:14:08 +02:00
public PunishmentResponse Punish(PunishToken punish)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == punish.Target).OrderByDescending(x => x.LastLogin).Include(x => x.Rank).FirstOrDefault();
2013-08-27 17:14:08 +02:00
if (account == null)
return PunishmentResponse.AccountDoesNotExist;
if (!String.Equals(punish.Admin, "Mineplex Enjin Server"))
{
var punisher =
repository.Where<Account>(x => x.Name == punish.Admin).OrderByDescending(x => x.LastLogin).Include(x => x.Rank).FirstOrDefault();
2013-08-27 17:14:08 +02:00
if (punisher == null)
return PunishmentResponse.NotPunished;
}
2013-08-27 17:14:08 +02:00
var punishment = new Punishment
{
UserId = account.AccountId,
Admin = punish.Admin,
Category = punish.Category,
Sentence = punish.Sentence,
Time = (long)TimeUtil.GetCurrentMilliseconds(),
2013-08-27 17:14:08 +02:00
Reason = punish.Reason,
Duration = punish.Duration,
Severity = punish.Severity,
Active = true
};
if (account.Punishments == null)
account.Punishments = new List<Punishment>();
account.Punishments.Add(punishment);
repository.Edit(account);
repository.CommitChanges();
}
return PunishmentResponse.Punished;
}
public PunishmentResponse RemovePunishment(RemovePunishmentToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Target).OrderByDescending(x => x.LastLogin).Include(x => x.Punishments).FirstOrDefault();
2013-08-27 17:14:08 +02:00
if (account == null)
return PunishmentResponse.AccountDoesNotExist;
if (account.Punishments == null || account.Punishments.Count == 0)
return PunishmentResponse.NotPunished;
var activePunishment = account.Punishments.FirstOrDefault(x => x.PunishmentId == token.PunishmentId && x.Active);
if (activePunishment == null)
return PunishmentResponse.NotPunished;
var punishment = repository.Where<Punishment>(x => x.UserId == account.AccountId && x.PunishmentId == token.PunishmentId && x.Active).First();
punishment.Active = false;
punishment.Removed = true;
punishment.RemoveAdmin = token.Admin;
punishment.RemoveTime = (long)TimeUtil.GetCurrentMilliseconds();
2013-08-27 17:14:08 +02:00
punishment.RemoveReason = token.Reason;
repository.Edit(punishment);
repository.CommitChanges();
}
return PunishmentResponse.PunishmentRemoved;
}
public string PurchaseGameSalesPackage(PurchaseToken token)
{
try
2013-08-27 17:14:08 +02:00
{
lock (getAccountLock(token.AccountName))
2013-08-27 17:14:08 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account =
repository.Where<Account>(x => x.Name == token.AccountName).OrderByDescending(x => x.LastLogin)
2013-08-27 17:14:08 +02:00
.Include(x => x.PvpTransactions)
.First();
var salesPackage =
repository.Where<GameSalesPackage>(x => x.GameSalesPackageId == token.SalesPackageId)
.FirstOrDefault();
if (account == null || salesPackage == null)
return TransactionResponse.Failed.ToString();
if (account.Gems < salesPackage.Gems)
return TransactionResponse.InsufficientFunds.ToString();
var accountTransaction = new GameTransaction
{
Account = account,
GameSalesPackageId = salesPackage.GameSalesPackageId,
Gems = salesPackage.Gems,
};
repository.Attach(account);
repository.Edit(account);
if (account.PvpTransactions == null)
account.PvpTransactions = new List<GameTransaction> { accountTransaction };
2013-08-27 17:14:08 +02:00
else
{
account.PvpTransactions.Add(accountTransaction);
}
account.Gems -= salesPackage.Gems;
repository.CommitChanges();
return TransactionResponse.Success.ToString();
}
}
}
catch (Exception exception)
{
return TransactionResponse.Failed.ToString() + ":" + exception.Message;
2013-08-27 17:14:08 +02:00
}
}
public bool AccountExists(string name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
return repository.Any<Account>(x => x.Name == name);
}
}
public void SaveCustomBuild(CustomBuildToken token)
{
lock (getAccountLock(token.PlayerName))
2013-08-27 17:14:08 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account =
repository.Where<Account>(x => x.Name == token.PlayerName).OrderByDescending(x => x.LastLogin).Include(x => x.CustomBuilds).First();
2013-08-27 17:14:08 +02:00
var customBuild =
account.CustomBuilds.FirstOrDefault(
x => String.Equals(x.PvpClass, token.PvpClass) && x.CustomBuildNumber == token.CustomBuildNumber);
2013-08-27 17:14:08 +02:00
if (customBuild == null)
{
customBuild = repository.Add(token.GetCustomBuild());
account.CustomBuilds.Add(customBuild);
}
else
{
token.UpdateCustomBuild(customBuild);
repository.Edit(customBuild);
}
2013-08-27 17:14:08 +02:00
if (customBuild.Active)
2013-08-27 17:14:08 +02:00
{
foreach (
var otherClassBuild in
account.CustomBuilds.Where(
x =>
String.Equals(x.PvpClass, token.PvpClass) && x.CustomBuildNumber != customBuild.CustomBuildNumber)
.ToList())
{
otherClassBuild.Active = false;
repository.Edit(otherClassBuild);
}
2013-08-27 17:14:08 +02:00
}
repository.Edit(account);
repository.CommitChanges();
}
2013-08-27 17:14:08 +02:00
}
}
public void Ignore(string accountName, string ignoredPlayer)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == accountName).OrderByDescending(x => x.LastLogin).First();
2013-08-27 17:14:08 +02:00
account.IgnoredPlayers.Add(ignoredPlayer);
repository.CommitChanges();
}
}
public void RemoveIgnore(string accountName, string ignoredPlayer)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == accountName).OrderByDescending(x => x.LastLogin).First();
2013-08-27 17:14:08 +02:00
account.IgnoredPlayers.Remove(ignoredPlayer);
repository.CommitChanges();
}
}
public string PurchaseUnknownSalesPackage(UnknownPurchaseToken token)
{
try
2013-08-27 17:14:08 +02:00
{
lock (getAccountLock(token.AccountName))
2013-08-27 17:14:08 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account =
repository.Where<Account>(x => x.Name == token.AccountName)
.OrderByDescending(x => x.LastLogin)
2013-08-27 17:14:08 +02:00
.Include(x => x.AccountTransactions)
.First();
if (account == null)
return TransactionResponse.Failed.ToString();
if (token.CoinPurchase ? account.Coins < token.Cost : account.Gems < token.Cost)
2013-08-27 17:14:08 +02:00
return TransactionResponse.InsufficientFunds.ToString();
var accountTransaction = new AccountTransaction
{
Account = account,
SalesPackageName = token.SalesPackageName,
Date = (long)TimeUtil.GetCurrentMilliseconds(),
Gems = token.CoinPurchase ? 0 : token.Cost,
Coins = token.CoinPurchase ? token.Cost : 0
2013-08-27 17:14:08 +02:00
};
repository.Attach(account);
repository.Edit(account);
if (account.AccountTransactions == null)
account.AccountTransactions = new List<AccountTransaction> { accountTransaction };
else
{
account.AccountTransactions.Add(accountTransaction);
}
if (token.CoinPurchase)
account.Coins -= token.Cost;
else
account.Gems -= token.Cost;
2013-08-27 17:14:08 +02:00
repository.CommitChanges();
return TransactionResponse.Success.ToString();
}
}
}
catch (Exception exception)
{
return TransactionResponse.Failed.ToString() + ":" + exception.Message;
2013-08-27 17:14:08 +02:00
}
}
public bool ApplyKits(String name)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => String.Equals(x.Name, name)).OrderByDescending(x => x.LastLogin).Include(x => x.Rank).FirstOrDefault();
account.LoadNavigationProperties(repository.Context);
addAccountTransaction(repository, account, "Bacon Brawl Bebe Piggles", 0, 0);
addAccountTransaction(repository, account, "Bacon Brawl `Pig`", 0, 0);
addAccountTransaction(repository, account, "A Barbarians Life Barbarian Archer", 0, 0);
addAccountTransaction(repository, account, "A Barbarians Life Bomber", 0, 0);
addAccountTransaction(repository, account, "The Bridges Archer", 0, 0);
addAccountTransaction(repository, account, "The Bridges Bomber", 0, 0);
addAccountTransaction(repository, account, "The Bridges Brawler", 0, 0);
addAccountTransaction(repository, account, "The Bridges Miner", 0, 0);
addAccountTransaction(repository, account, "Castle Siege Castle Assassin", 0, 0);
addAccountTransaction(repository, account, "Castle Siege Castle Brawler", 0, 0);
addAccountTransaction(repository, account, "Castle Siege Castle Knight", 0, 0);
addAccountTransaction(repository, account, "Castle Siege Undead Archer", 0, 0);
addAccountTransaction(repository, account, "Castle Siege Undead Zombie", 0, 0);
addAccountTransaction(repository, account, "Death Tag Runner Archer", 0, 0);
addAccountTransaction(repository, account, "Death Tag Runner Traitor", 0, 0);
addAccountTransaction(repository, account, "Dragon Escape Disruptor", 0, 0);
addAccountTransaction(repository, account, "Dragon Escape Warper", 0, 0);
addAccountTransaction(repository, account, "Dragons Marksman", 0, 0);
addAccountTransaction(repository, account, "Dragons Pyrotechnic", 0, 0);
addAccountTransaction(repository, account, "Block Hunt Instant Hider", 0, 0);
addAccountTransaction(repository, account, "Block Hunt Shocking Hider", 0, 0);
addAccountTransaction(repository, account, "Block Hunt Radar Hunter", 0, 0);
addAccountTransaction(repository, account, "Block Hunt TNT Hunter", 0, 0);
addAccountTransaction(repository, account, "Super Paintball Machine Gun", 0, 0);
addAccountTransaction(repository, account, "Super Paintball Shotgun", 0, 0);
addAccountTransaction(repository, account, "One in the Quiver Brawler", 0, 0);
addAccountTransaction(repository, account, "One in the Quiver Enchanter", 0, 0);
addAccountTransaction(repository, account, "Runner Archer", 0, 0);
addAccountTransaction(repository, account, "Runner Frosty", 0, 0);
addAccountTransaction(repository, account, "Sheep Quest Archer", 0, 0);
addAccountTransaction(repository, account, "Sheep Quest Brute", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Blaze", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Chicken", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Mad Cow", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Creeper", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Enderman", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Undead Knight", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Magma Cube", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Pig", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Skeletal Horse", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Sky Squid", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Snowman", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Witch", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Wither", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Wither Skeleton", 0, 0);
addAccountTransaction(repository, account, "Super Smash Mobs Wolf", 0, 0);
addAccountTransaction(repository, account, "Snake Super Snake", 0, 0);
addAccountTransaction(repository, account, "Snake Other Snake", 0, 0);
addAccountTransaction(repository, account, "Sneaky Assassins Ranged Assassin", 0, 0);
addAccountTransaction(repository, account, "Sneaky Assassins Revealer", 0, 0);
addAccountTransaction(repository, account, "Super Spleef Archer", 0, 0);
addAccountTransaction(repository, account, "Super Spleef Brawler", 0, 0);
addAccountTransaction(repository, account, "Squid Shooter Squid Blaster", 0, 0);
addAccountTransaction(repository, account, "Squid Shooter Squid Sniper", 0, 0);
addAccountTransaction(repository, account, "Survival Games Archer", 0, 0);
addAccountTransaction(repository, account, "Survival Games Assassin", 0, 0);
addAccountTransaction(repository, account, "Survival Games Beastmaster", 0, 0);
addAccountTransaction(repository, account, "Survival Games Bomber", 0, 0);
addAccountTransaction(repository, account, "Survival Games Brawler", 0, 0);
addAccountTransaction(repository, account, "Survival Games Necromancer", 0, 0);
addAccountTransaction(repository, account, "Turf Wars Infiltrator", 0, 0);
addAccountTransaction(repository, account, "Turf Wars Shredder", 0, 0);
addAccountTransaction(repository, account, "Zombie Survival Survivor Archer", 0, 0);
addAccountTransaction(repository, account, "Zombie Survival Survivor Rogue", 0, 0);
repository.CommitChanges();
}
return true;
}
2013-08-27 17:14:08 +02:00
public string UpdateRank(RankUpdateToken token)
{
Rank rank = null;
var expire = DateTime.Now.AddMonths(1).AddMilliseconds(-DateTime.Now.Millisecond);
try
2013-08-27 17:14:08 +02:00
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => String.Equals(x.Name, token.Name)).OrderByDescending(x => x.LastLogin).Include(x => x.Rank).FirstOrDefault();
rank = repository.Where<Rank>(x => String.Equals(x.Name, token.Rank)).FirstOrDefault();
2013-08-27 17:14:08 +02:00
if (account == null)
return "ALL";
2015-05-13 23:33:31 +02:00
if (account.Rank.Name == "LT" || account.Rank.Name == "OWNER")
return account.Rank.Name;
2013-08-27 17:14:08 +02:00
if (rank == null)
return account.Rank.ToString();
2014-09-02 05:03:08 +02:00
account.Rank = rank;
account.RankExpire = expire;
account.RankPerm = token.Perm;
2014-09-02 05:03:08 +02:00
repository.Edit(account);
repository.CommitChanges();
_logger.Log("INFO", "TOKEN " + token.Name + "'s rank has been updated to " + token.Rank + " " + (token.Perm ? "Permanently" : "Monthly") + "." + " Rank expire : " + account.RankExpire.ToString());
2014-09-02 05:03:08 +02:00
}
2013-08-27 17:14:08 +02:00
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => String.Equals(x.Name, token.Name)).OrderByDescending(x => x.LastLogin).Include(x => x.Rank).FirstOrDefault();
if (token.Retries >= 3)
_logger.Log("ERROR", "Applying UpdateRank, retried 3 times and something didn't stick.");
else if (!account.Rank.Name.Equals(token.Rank) || account.RankPerm != token.Perm || account.RankExpire.Equals(expire))
{
token.Retries++;
UpdateRank(token);
}
}
}
catch (Exception ex)
{
_logger.Log("ERROR", "Applying UpdateRank : " + String.Join("; ", ex.InnerException.InnerException.Message));
2013-08-27 17:14:08 +02:00
}
return rank == null ? token.Rank : rank.Name.ToString();
2013-08-27 17:14:08 +02:00
}
public void RemoveBan(RemovePunishmentToken token)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Name == token.Target).OrderByDescending(x => x.LastLogin).Include(x => x.Punishments).FirstOrDefault();
2013-08-27 17:14:08 +02:00
if (account == null)
return;
if (account.Punishments == null || account.Punishments.Count == 0)
return;
var activePunishments = account.Punishments.Where(x => x.Active);
if (!activePunishments.Any())
return;
foreach (Punishment punishment in activePunishments)
{
punishment.Active = false;
punishment.Removed = true;
punishment.RemoveAdmin = token.Admin;
punishment.RemoveTime = DateTime.Now.Ticks;
punishment.RemoveReason = token.Reason;
repository.Edit(punishment);
}
repository.CommitChanges();
}
}
public void ApplySalesPackage(SalesPackage salesPackage, int accountId, decimal gross, decimal fee)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.AccountId == accountId).Include(x => x.Transactions).First();
var accountTransaction = new LOC.Core.Model.Sales.Transaction {Account = account, SalesPackage = salesPackage, Fee = fee, Profit = (gross - fee), Time = DateTime.Now};
2013-08-27 17:14:08 +02:00
repository.Attach(salesPackage);
repository.Attach(account);
repository.Edit(account);
if (account.Transactions == null)
account.Transactions = new List<LOC.Core.Model.Sales.Transaction>();
2013-08-27 17:14:08 +02:00
account.Transactions.Add(accountTransaction);
account.Gems += salesPackage.Gems;
account.Donated = true;
account.RankPerm = salesPackage.RankPerm;
if (salesPackage.Rank.RankId != 1 && !salesPackage.RankPerm)
{
account.Rank = salesPackage.Rank;
account.RankExpire = DateTime.Now.AddDays(salesPackage.Length);
}
repository.CommitChanges();
}
}
protected Account GetAccountByName(string name, IRepository repository)
{
var account = repository.Where<Account>(x => x.Name == name).OrderByDescending(y => y.LastLogin).FirstOrDefault();
account.LoadNavigationProperties(repository.Context);
return account;
2013-08-27 17:14:08 +02:00
}
protected Account CreateAccount(LoginRequestToken loginToken, IRepository repository)
{
var newAccount = new Account
{
Name = loginToken.Name,
2014-04-20 05:53:55 +02:00
Uuid = loginToken.Uuid,
2013-08-27 17:14:08 +02:00
Rank = repository.Where<Rank>(x => x.RankId == 1).First(),
Gems = 0,
Coins = 0,
Transactions = new List<LOC.Core.Model.Sales.Transaction>(),
2013-08-27 17:14:08 +02:00
PvpTransactions = new List<GameTransaction>(),
IpAddresses = new List<LoginAddress>(),
MacAddresses = new List<MacAddress>(),
Logins = new List<Login>(),
CustomBuilds = new List<CustomBuild>(),
LastVote = DateTime.Today.Subtract(TimeSpan.FromDays(5)),
RankExpire = DateTime.Now
};
newAccount = repository.Add(newAccount);
repository.CommitChanges();
newAccount = repository.Where<Account>(x => x.AccountId == newAccount.AccountId)
.Include(x => x.Rank)
.Include(x => x.Punishments)
.Include(x => x.Clan)
.Include(x => x.ClanRole)
.Include(x => x.CustomBuilds)
.Include(x => x.FishCatches)
.Include(x => x.PvpTransactions)
.Include(x => x.Pets)
.First();
return newAccount;
}
protected LoginAddress CreateIpAddress(LoginRequestToken loginToken, IRepository repository)
{
var newLoginAddress = new LoginAddress { Address = loginToken.IpAddress };
newLoginAddress = repository.Add(newLoginAddress);
repository.CommitChanges();
return newLoginAddress;
}
protected void CreateMacAddress(LoginRequestToken loginToken, IRepository repository)
{
var newMacAddress = new MacAddress { Address = loginToken.MacAddress, Accounts = new List<Account>() };
repository.Add(newMacAddress);
repository.CommitChanges();
}
2014-08-21 22:55:09 +02:00
public ClientToken GetAccountByUUID(string uuid)
{
using (var repository = _repositoryFactory.CreateRepository())
{
var account = repository.Where<Account>(x => x.Uuid == uuid).FirstOrDefault();
if (account == null)
return null;
account.LoadNavigationProperties(repository.Context);
2014-08-21 22:55:09 +02:00
ClientToken clientToken = null;
clientToken = new ClientToken(account);
foreach (var trans in repository.Where<CoinTransaction>(x => x.Account.AccountId == account.AccountId).ToList())
{
clientToken.DonorToken.CoinRewards.Add(new CoinTransactionToken(trans));
}
return clientToken;
}
}
private void addAccountTransaction(IRepository repository, Account account, string salesPackageName, int gems, int coins)
{
var accountTransaction = new AccountTransaction
{
Account = account,
SalesPackageName = salesPackageName,
Date = (long)TimeUtil.GetCurrentMilliseconds(),
Gems = gems,
Coins = coins
};
if (account.AccountTransactions == null)
account.AccountTransactions = new List<AccountTransaction> { accountTransaction };
else
{
account.AccountTransactions.Add(accountTransaction);
}
}
2013-08-27 17:14:08 +02:00
}
}