ace/Source/ACE.Database/AuthenticationDatabase.cs

181 lines
5.7 KiB
C#
Raw Permalink Normal View History

using System.Linq;
using System.Threading;
using Microsoft.EntityFrameworkCore;
using log4net;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using ACE.Database.Models.Auth;
using ACE.Entity.Enum;
using System.Collections.Generic;
using System;
using System.Net;
namespace ACE.Database
{
public class AuthenticationDatabase
{
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public bool Exists(bool retryUntilFound)
{
var config = Common.ConfigManager.Config.MySql.Authentication;
for (; ; )
{
using (var context = new AuthDbContext())
{
if (((RelationalDatabaseCreator)context.Database.GetService<IDatabaseCreator>()).Exists())
{
log.InfoFormat("[DATABASE] Successfully connected to {0} database on {1}:{2}.", config.Database, config.Host, config.Port);
return true;
}
}
log.Error($"[DATABASE] Attempting to reconnect to {config.Database} database on {config.Host}:{config.Port} in 5 seconds...");
if (retryUntilFound)
Thread.Sleep(5000);
else
return false;
}
}
PlayerManager (#1103) * PlayerManager initial checkin * OfflinePlayer initial checkin * More progress * Comment out Console.WriteLine debug messages * Add property dictionaries to OfflinePlayer * IPlayer initial checkin * Add allegiance vars to IPlayer * Couple comments * More progress * Add more to IPlayer * line endings fix * More progress. Compiles. Friends list works again. * Friend status updates now work (again) * PlayerManager save OfflinePlayers every 1hr * Bye bye legacy AllPlayers * using cleanup * Allegience switch to PlayerManager progress * More Allegiance progress * More progress * More allegiance progress * IPlayer adds * Migrate inversefriends to PlayerManager * Switch WorldManager GetPlayerByGuid over to PlayerManager * WorldManager Find(ObjectGuid characterGuid) removed. Use PlayerManager instead * WorldManager FindByPlayerName(string name) removed. Use PlayerManager instead * PlayerManager thread safety switched to ReaderWriterLockSlim * More progress * ServerStatus added TotalAccountsCreated, TotalCharactersCreated * No more Player access/management stuff in WorldManager. It's all in PlayerManager now * Player.IsOnline is no more. * Friends AppearOnline fix * Handle dead session better to switch player from online to offline * Remove adding the + to names in ChatChannels. Name property should be changed to not include + * serverstatus total accounts and characters on same line * Allegience initial fix, still more bugs though * PlayerManager minor cleanup uint should be faster key than ObjectGuid Changed the dictionaries to private. We don't want anyone outside of this class referencing them. * Thread safety added to LScape.get_landcell
2018-12-02 13:41:16 -06:00
public int GetAccountCount()
{
using (var context = new AuthDbContext())
return context.Account.Count();
}
/// <exception cref="MySqlException">Account with name already exists.</exception>
public Account CreateAccount(string name, string password, AccessLevel accessLevel, IPAddress address)
{
var account = new Account();
account.AccountName = name;
account.SetPassword(password);
account.SetSaltForBCrypt();
account.AccessLevel = (uint)accessLevel;
account.CreateTime = DateTime.UtcNow;
account.CreateIP = address.GetAddressBytes();
using (var context = new AuthDbContext())
{
context.Account.Add(account);
context.SaveChanges();
}
return account;
}
/// <summary>
/// Will return null if the accountId was not found.
/// </summary>
public Account GetAccountById(uint accountId)
{
using (var context = new AuthDbContext())
{
return context.Account
.AsNoTracking()
.FirstOrDefault(r => r.AccountId == accountId);
}
}
/// <summary>
/// Will return null if the accountName was not found.
/// </summary>
public Account GetAccountByName(string accountName)
{
using (var context = new AuthDbContext())
{
return context.Account
.AsNoTracking()
.FirstOrDefault(r => r.AccountName == accountName);
}
}
/// <summary>
/// id will be 0 if the accountName was not found.
/// </summary>
public uint GetAccountIdByName(string accountName)
{
using (var context = new AuthDbContext())
{
var result = context.Account
.AsNoTracking()
.FirstOrDefault(r => r.AccountName == accountName);
return (result != null) ? result.AccountId : 0;
}
}
public void UpdateAccount(Account account)
{
using (var context = new AuthDbContext())
{
context.Entry(account).State = EntityState.Modified;
context.SaveChanges();
}
}
public bool UpdateAccountAccessLevel(uint accountId, AccessLevel accessLevel)
{
using (var context = new AuthDbContext())
{
var account = context.Account
.First(r => r.AccountId == accountId);
if (account == null)
return false;
account.AccessLevel = (uint)accessLevel;
context.SaveChanges();
}
return true;
}
public List<string> GetListofAccountsByAccessLevel(AccessLevel accessLevel)
{
using (var context = new AuthDbContext())
{
var results = context.Account
.AsNoTracking()
.Where(r => r.AccessLevel == Convert.ToUInt32(accessLevel)).ToList();
var result = new List<string>();
foreach (var account in results)
result.Add(account.AccountName);
return result;
}
}
public List<string> GetListofBannedAccounts()
{
using (var context = new AuthDbContext())
{
var results = context.Account
.AsNoTracking()
.Where(r => r.BanExpireTime > DateTime.UtcNow).ToList();
var result = new List<string>();
foreach (var account in results)
{
var bannedbyAccount = account.BannedByAccountId.Value > 0 ? $"account {GetAccountById(account.BannedByAccountId.Value).AccountName}" : "CONSOLE";
result.Add($"{account.AccountName} -- banned by {bannedbyAccount} until server time {account.BanExpireTime.Value.ToLocalTime():MMM dd yyyy h:mmtt}{(!string.IsNullOrWhiteSpace(account.BanReason) ? $" -- Reason: {account.BanReason}" : "")}");
}
return result;
}
}
}
}