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. 14
      Account.cs
  2. 7
      Character.cs
  3. 25
      GMTicket.cs
  4. 4
      Pages/Account.cshtml
  5. 19
      Pages/Account.cshtml.cs
  6. 26
      Pages/Index.cshtml.cs
  7. 3
      Program.cs

14
Account.cs

@ -2,7 +2,8 @@ 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 class Account
{ {
@ -15,8 +16,10 @@ public class Account
public List<AccountAccess> Access { get; set; } public List<AccountAccess> Access { get; set; }
public Account AccountByID(int id, MySqlConnection conn) public static Account AccountByID(int id)
{ {
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "select username from account where id=@id"; string sql = "select username from account where id=@id";
MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlCommand cmd = new MySqlCommand(sql, conn);
@ -26,8 +29,7 @@ public class Account
{ {
try try
{ {
this.Username = rdr.GetString(0); return new Account(rdr.GetString(0));
return new Account(this.Username, conn);
} }
catch (Exception e) catch (Exception e)
@ -38,8 +40,10 @@ public class Account
return null; return null;
} }
public Account(string username, MySqlConnection conn) public Account(string username)
{ {
MySqlConnection conn = new MySqlConnection(Program.connStr);
conn.Open(); conn.Open();
string sql = "select id,username,email,last_ip,last_login from account where username=@username"; string sql = "select id,username,email,last_ip,last_login from account where username=@username";

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>

19
Pages/Account.cshtml.cs

@ -11,7 +11,6 @@ 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; }
@ -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;

26
Pages/Index.cshtml.cs

@ -8,7 +8,6 @@ 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>();
@ -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