Browse Source

Cleanup SQL Connections and fix tickets

master
Gregory Rudolph 3 years ago
parent
commit
a31a1795c3
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 194
      Account.cs
  2. 7
      Character.cs
  3. 25
      GMTicket.cs
  4. 4
      Pages/Account.cshtml
  5. 21
      Pages/Account.cshtml.cs
  6. 36
      Pages/Index.cshtml.cs
  7. 3
      Program.cs

194
Account.cs

@ -2,120 +2,124 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using MySql.Data.MySqlClient; using MySql.Data.MySqlClient;
namespace NightmareCoreWeb2 { namespace NightmareCoreWeb2
public class Account
{ {
public UInt32 Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string LastIP { get; set; }
public DateTime LastLogin { get; set; }
public List<Character> Characters { get; set; }
public List<AccountAccess> Access { get; set; }
public Account AccountByID(int id, MySqlConnection conn) public class Account
{ {
conn.Open(); public UInt32 Id { get; set; }
string sql = "select username from account where id=@id"; public string Username { get; set; }
MySqlCommand cmd = new MySqlCommand(sql, conn); public string Email { get; set; }
cmd.Parameters.AddWithValue("id", id); public string LastIP { get; set; }
MySqlDataReader rdr = cmd.ExecuteReader(); public DateTime LastLogin { get; set; }
while (rdr.Read()) public List<Character> Characters { get; set; }
public List<AccountAccess> Access { get; set; }
public static Account AccountByID(int id)
{ {
try
{
this.Username = rdr.GetString(0);
return new Account(this.Username, conn);
} MySqlConnection conn = new MySqlConnection(Program.connStr);
catch (Exception e) conn.Open();
string sql = "select username from account where id=@id";
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("id", id);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{ {
Console.WriteLine(e); try
{
return new Account(rdr.GetString(0));
}
catch (Exception e)
{
Console.WriteLine(e);
}
} }
return null;
} }
return null;
}
public Account(string username, MySqlConnection conn) public Account(string username)
{ {
conn.Open();
string sql = "select id,username,email,last_ip,last_login from account where username=@username"; MySqlConnection conn = new MySqlConnection(Program.connStr);
MySqlCommand cmd = new MySqlCommand(sql, conn); conn.Open();
cmd.Parameters.AddWithValue("username", username);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) string sql = "select id,username,email,last_ip,last_login from account where username=@username";
{ MySqlCommand cmd = new MySqlCommand(sql, conn);
try cmd.Parameters.AddWithValue("username", username);
{ MySqlDataReader rdr = cmd.ExecuteReader();
this.Id = rdr.GetUInt32(0);
this.Username = rdr.GetString(1); while (rdr.Read())
this.Email = rdr.GetString(2);
this.LastIP = rdr.GetString(3);
this.LastLogin = rdr.GetDateTime(4);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
rdr.Close();
sql = "select guid,username,name,level,race,class from characters.characters join auth.account on characters.characters.account = auth.account.id where characters.characters.account=@id";
cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("id", this.Id);
rdr = cmd.ExecuteReader();
this.Characters = new List<Character>();
while (rdr.Read())
{
try
{ {
Character c = new Character(); try
c.guid = (int)rdr.GetUInt32(0); {
c.Username = rdr.GetString(1); this.Id = rdr.GetUInt32(0);
c.Name = rdr.GetString(2); this.Username = rdr.GetString(1);
c.Level = rdr.GetByte(3); this.Email = rdr.GetString(2);
c.Race = rdr.GetByte(4); this.LastIP = rdr.GetString(3);
c.Class = rdr.GetByte(5); this.LastLogin = rdr.GetDateTime(4);
this.Characters.Add(c); }
catch (Exception e)
{
Console.WriteLine(e);
}
} }
catch (Exception e) rdr.Close();
sql = "select guid,username,name,level,race,class from characters.characters join auth.account on characters.characters.account = auth.account.id where characters.characters.account=@id";
cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("id", this.Id);
rdr = cmd.ExecuteReader();
this.Characters = new List<Character>();
while (rdr.Read())
{ {
Console.WriteLine(e); try
{
Character c = new Character();
c.guid = (int)rdr.GetUInt32(0);
c.Username = rdr.GetString(1);
c.Name = rdr.GetString(2);
c.Level = rdr.GetByte(3);
c.Race = rdr.GetByte(4);
c.Class = rdr.GetByte(5);
this.Characters.Add(c);
}
catch (Exception e)
{
Console.WriteLine(e);
}
} }
} rdr.Close();
rdr.Close();
sql = "select SecurityLevel,RealmID from account_access where AccountID=@id"; sql = "select SecurityLevel,RealmID from account_access where AccountID=@id";
cmd = new MySqlCommand(sql, conn); cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("id", this.Id); cmd.Parameters.AddWithValue("id", this.Id);
rdr = cmd.ExecuteReader(); rdr = cmd.ExecuteReader();
this.Access = new List<AccountAccess>(); this.Access = new List<AccountAccess>();
while (rdr.Read()) while (rdr.Read())
{
try
{
AccountAccess acctA = new AccountAccess();
acctA.SecurityLevel = rdr.GetByte(0);
acctA.RealmID = rdr.GetInt32(1);
this.Access.Add(acctA);
}
catch (Exception e)
{ {
Console.WriteLine(e); try
{
AccountAccess acctA = new AccountAccess();
acctA.SecurityLevel = rdr.GetByte(0);
acctA.RealmID = rdr.GetInt32(1);
this.Access.Add(acctA);
}
catch (Exception e)
{
Console.WriteLine(e);
}
} }
rdr.Close();
conn.Close();
} }
rdr.Close();
conn.Close();
} }
public class AccountAccess
} {
public class AccountAccess public int SecurityLevel { get; set; }
{ public int RealmID { get; set; }
public int SecurityLevel { get; set; } }
public int RealmID { get; set; }
}
} }

7
Character.cs

@ -66,8 +66,10 @@ namespace NightmareCoreWeb2
} }
public Character() { } public Character() { }
public Character(int guid, MySqlConnection conn) public Character(int guid)
{ {
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "select username,name,level,race,class,at_login from characters.characters join auth.account on characters.characters.account = auth.account.id where characters.characters.guid=@id"; string sql = "select username,name,level,race,class,at_login from characters.characters join auth.account on characters.characters.account = auth.account.id where characters.characters.guid=@id";
@ -95,7 +97,8 @@ namespace NightmareCoreWeb2
conn.Close(); conn.Close();
} }
public void SetAtLogin(MySqlConnection conn) { public void SetAtLogin() {
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "update characters.characters set at_login=@loginOpts where guid=@guid"; string sql = "update characters.characters set at_login=@loginOpts where guid=@guid";
MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlCommand cmd = new MySqlCommand(sql, conn);

25
GMTicket.cs

@ -10,7 +10,7 @@ namespace NightmareCoreWeb2
public class GMTicket public class GMTicket
{ {
public int Id { get; set; } public int Id { get; set; }
public Account Account { get; set; } public Account OpenedBy { get; set; }
public string CharacterName { get; set; } public string CharacterName { get; set; }
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
public DateTime LastModifiedTime { get; set; } public DateTime LastModifiedTime { get; set; }
@ -19,19 +19,20 @@ namespace NightmareCoreWeb2
public string Description { get; set; } public string Description { get; set; }
public static List<GMTicket> GetAllTickets(MySqlConnection conn) public static List<GMTicket> GetAllTickets()
{ {
List<GMTicket> ret = new List<GMTicket>(); List<GMTicket> ret = new List<GMTicket>();
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "select id from gm_ticket"; string sql = "select id from characters.gm_ticket";
MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader(); MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read()) while (rdr.Read())
{ {
try try
{ {
GMTicket ticket = new GMTicket(rdr.GetInt32(0), conn); GMTicket ticket = new GMTicket(rdr.GetInt32(0));
ret.Add(ticket); ret.Add(ticket);
} }
catch (Exception e) catch (Exception e)
@ -42,12 +43,14 @@ namespace NightmareCoreWeb2
return ret; return ret;
} }
public GMTicket(int id, MySqlConnection conn) public GMTicket(int id)
{ {
this.Id = id; this.Id = id;
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "select id,playerGuid,name,description,createTime,lastModifiedTime,closedBy,assignedTo from gm_ticket where id=@id"; string sql = "select playerGuid,name,createTime,lastModifiedTime,closedBy,assignedTo,description from characters.gm_ticket where id=@id";
MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.Parameters.AddWithValue("id", id); cmd.Parameters.AddWithValue("id", id);
MySqlDataReader rdr = cmd.ExecuteReader(); MySqlDataReader rdr = cmd.ExecuteReader();
@ -56,17 +59,17 @@ namespace NightmareCoreWeb2
{ {
try try
{ {
this.Account = Account.AccountByID(rdr.GetInt32(0), conn); this.OpenedBy = new Account(new Character(rdr.GetInt32(0)).Username);
this.CharacterName = rdr.GetString(1); this.CharacterName = rdr.GetString(1);
this.CreateTime = rdr.GetDateTime(2); this.CreateTime = DateTimeOffset.FromUnixTimeSeconds(rdr.GetInt32(2)).UtcDateTime;
this.LastModifiedTime = rdr.GetDateTime(3); this.LastModifiedTime = DateTimeOffset.FromUnixTimeSeconds(rdr.GetInt32(3)).UtcDateTime;
if (rdr.GetInt32(4) != 0) if (rdr.GetInt32(4) != 0)
{ {
this.ClosedBy = Account.AccountByID(rdr.GetInt32(4), conn); this.ClosedBy = Account.AccountByID(rdr.GetInt32(4));
} }
if (rdr.GetInt32(5) != 0) if (rdr.GetInt32(5) != 0)
{ {
this.AssignedTo = Account.AccountByID(rdr.GetInt32(5), conn); this.AssignedTo = Account.AccountByID(rdr.GetInt32(5));
} }
this.Description = rdr.GetString(6); this.Description = rdr.GetString(6);
} }

4
Pages/Account.cshtml

@ -44,12 +44,12 @@
{ {
@foreach (var ticket in Model.Tickets) @foreach (var ticket in Model.Tickets)
{ {
if (ticket.ClosedBy == null) @if (ticket.ClosedBy == null)
{ {
<div class="col-md-4"> <div class="col-md-4">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<h6>@ticket.CharacterName</h6> <a href="/?handler=Account&name=@ticket.OpenedBy.Username">@ticket.CharacterName</a>
</div> </div>
<div class="card-body"> <div class="card-body">
<p class="card-text">@ticket.Description</p> <p class="card-text">@ticket.Description</p>

21
Pages/Account.cshtml.cs

@ -11,8 +11,7 @@ namespace NightmareCoreWeb2.Pages
{ {
public class AccountModel : PageModel public class AccountModel : PageModel
{ {
string connStr = $"SslMode=None;server={Program.MysqlServer};user={Program.MysqlUser};database={Program.MysqlDatabase};port={Program.MysqlPort};password={Program.MysqlPassword}"; public string UserEmail { get; set; }
public string UserEmail { get; set; }
public string UserPassword { get; set; } public string UserPassword { get; set; }
public string CharacterListType { get; set; } public string CharacterListType { get; set; }
public string AuthToken { get; set; } public string AuthToken { get; set; }
@ -29,22 +28,22 @@ namespace NightmareCoreWeb2.Pages
public AccountModel(ILogger<AccountModel> logger) public AccountModel(ILogger<AccountModel> logger)
{ {
conn = new MySqlConnection(connStr); conn = new MySqlConnection(Program.connStr);
_logger = logger; _logger = logger;
} }
public void OnGetAccount(string name) public void OnGetAccount(string name)
{ {
Account a = new Account(name, conn); Account a = new Account(name);
//AuthToken = "OK"; AuthToken = "OK";
UserAccount = a; UserAccount = a;
OnlineCharacters = a.Characters; OnlineCharacters = a.Characters;
foreach (var access in a.Access) foreach (var access in a.Access)
{ {
if (access.RealmID == -1 && access.RealmID >= 1) if (access.RealmID == -1 && access.SecurityLevel >= 1)
{ {
this.IsGM = true; this.IsGM = true;
this.Tickets = GMTicket.GetAllTickets(conn); this.Tickets = GMTicket.GetAllTickets();
} }
} }
ViewData["Title"] = a.Username; ViewData["Title"] = a.Username;
@ -52,12 +51,12 @@ namespace NightmareCoreWeb2.Pages
} }
public void OnGetCharacterAction(int guid, int action) public void OnGetCharacterAction(int guid, int action)
{ {
Character c = new Character(guid, conn); Character c = new Character(guid);
if ((c.AtLogin & Character.AtLoginOptions.AT_LOGIN_FIRST) == 0) if ((c.AtLogin & Character.AtLoginOptions.AT_LOGIN_FIRST) == 0)
{ {
c.AtLogin |= (Character.AtLoginOptions)action; c.AtLogin |= (Character.AtLoginOptions)action;
} }
c.SetAtLogin(conn); c.SetAtLogin();
} }
public void OnGet() public void OnGet()
@ -68,7 +67,7 @@ namespace NightmareCoreWeb2.Pages
Username = Request.Cookies["Username"]; Username = Request.Cookies["Username"];
if (!string.IsNullOrEmpty(Username)) if (!string.IsNullOrEmpty(Username))
{ {
Account a = new Account(Username, conn); Account a = new Account(Username);
AuthToken = "OK"; AuthToken = "OK";
UserAccount = a; UserAccount = a;
OnlineCharacters = a.Characters; OnlineCharacters = a.Characters;
@ -77,7 +76,7 @@ namespace NightmareCoreWeb2.Pages
if (access.RealmID == -1 && access.RealmID >= 1) if (access.RealmID == -1 && access.RealmID >= 1)
{ {
this.IsGM = true; this.IsGM = true;
this.Tickets = GMTicket.GetAllTickets(conn); this.Tickets = GMTicket.GetAllTickets();
} }
} }
ViewData["Title"] = a.Username; ViewData["Title"] = a.Username;

36
Pages/Index.cshtml.cs

@ -8,15 +8,14 @@ namespace NightmareCoreWeb2.Pages
{ {
public class IndexModel : PageModel public class IndexModel : PageModel
{ {
string connStr = $"SslMode=None;server={Program.MysqlServer};user={Program.MysqlUser};database={Program.MysqlDatabase};port={Program.MysqlPort};password={Program.MysqlPassword}";
public List<Character> OnlineCharacters = new List<Character>(); public List<Character> OnlineCharacters = new List<Character>();
public Dictionary<string, string> Realms = new Dictionary<string, string>(); public Dictionary<string, string> Realms = new Dictionary<string, string>();
public string ActivateEmail {get; set;} public string ActivateEmail { get; set; }
public string ActivatePassword {get; set;} public string ActivatePassword { get; set; }
public string ActivateToken {get; set;} public string ActivateToken { get; set; }
public string RequestTokenEmail {get; set;} public string RequestTokenEmail { get; set; }
public string CharacterListType {get; set;} public string CharacterListType { get; set; }
private MySqlConnection conn; private MySqlConnection conn;
private readonly ILogger<IndexModel> _logger; private readonly ILogger<IndexModel> _logger;
@ -25,7 +24,7 @@ namespace NightmareCoreWeb2.Pages
{ {
_logger = logger; _logger = logger;
conn = new MySqlConnection(connStr); conn = new MySqlConnection(Program.connStr);
try try
{ {
conn.Open(); conn.Open();
@ -64,32 +63,39 @@ namespace NightmareCoreWeb2.Pages
} }
public void OnGet() { public void OnGet()
{
ViewData["Title"] = "WotDN"; ViewData["Title"] = "WotDN";
} }
public void OnGetAccount(string name) { public void OnGetAccount(string name)
Account a = new Account(name, conn); {
Account a = new Account(name);
ViewData["Title"] = name; ViewData["Title"] = name;
CharacterListType = $"{name}'s Characters"; CharacterListType = $"{name}'s Characters";
OnlineCharacters = a.Characters; OnlineCharacters = a.Characters;
} }
public void OnPostActivateAccount() { public void OnPostActivateAccount()
{
ActivateEmail = Request.Form["ActivateEmail"]; ActivateEmail = Request.Form["ActivateEmail"];
ActivatePassword = Request.Form["ActivatePassword"]; ActivatePassword = Request.Form["ActivatePassword"];
ActivateToken = Request.Form["ActivateToken"]; ActivateToken = Request.Form["ActivateToken"];
} }
public void OnPostRequestToken() { public void OnPostRequestToken()
{
RequestTokenEmail = Request.Form["RequestTokenEmail"]; RequestTokenEmail = Request.Form["RequestTokenEmail"];
} }
public bool RequestToken() { public bool RequestToken()
{
return false; return false;
} }
public bool CreateAccount() { public bool CreateAccount()
{
return false; return false;
} }
public bool IsTokenValid(string username, string token) { public bool IsTokenValid(string username, string token)
{
return false; return false;
} }
} }

3
Program.cs

@ -18,6 +18,7 @@ namespace NightmareCoreWeb2
public static string MysqlDatabase; public static string MysqlDatabase;
public static string MysqlPort; public static string MysqlPort;
public static string MysqlPassword; public static string MysqlPassword;
public static string connStr;
public static void Main(string[] args) public static void Main(string[] args)
{ {
using (StreamReader r = new StreamReader("config.json")) using (StreamReader r = new StreamReader("config.json"))
@ -29,6 +30,8 @@ namespace NightmareCoreWeb2
Program.MysqlDatabase = config.MysqlDatabase; Program.MysqlDatabase = config.MysqlDatabase;
Program.MysqlPassword = config.MysqlPassword; Program.MysqlPassword = config.MysqlPassword;
Program.MysqlPort = config.MysqlPort; Program.MysqlPort = config.MysqlPort;
connStr = $"SslMode=None;server={Program.MysqlServer};user={Program.MysqlUser};database={Program.MysqlDatabase};port={Program.MysqlPort};password={Program.MysqlPassword}";
} }
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();

Loading…
Cancel
Save