public static bool InsertAccount(Account account) { var result = true; var connection = new MySqlConnection(DatabaseConfig.ConnectionString); OpenMySqlConnection(connection); var cmd = CreateStoredProcedureCommand( connection, "InsertAccount", new Dictionary<string, object> { { "@Name", account.UserName }, { "@IpAddress", account.IpAddress }, { "@Port", account.Port }, }); try { var rdr = cmd.ExecuteReader(); rdr.Close(); } catch (MySqlException ex) { if (ex.Message == $"Duplicate entry '{account.UserName}' for key 'Name'") { result = false; } else { throw; } } connection.Close(); return result; }
public static Account GetAccountByName(string accountName) { var connection = new MySqlConnection(DatabaseConfig.ConnectionString); OpenMySqlConnection(connection); var cmd = CreateStoredProcedureCommand( connection, "GetAccountByName", new Dictionary<string, object> { { "@Name", accountName } }); MySqlDataReader rdr = cmd.ExecuteReader(); Account account = null; if (rdr.Read()) { account = new Account { Id = rdr[0] as long? ?? 0, UserName = rdr[1] as string, IpAddress = rdr[2] as string, Port = rdr[3] as int? ?? 0 }; } rdr.Close(); connection.Close(); return account; }
public static PresenceStatus GetPresence(Account account) { return (account.IpAddress != null && account.Port != null) ? PresenceStatus.Online : PresenceStatus.Offline; }
public static void SetIpAndPort(Account account) { var connection = new MySqlConnection(DatabaseConfig.ConnectionString); OpenMySqlConnection(connection); var cmd = CreateStoredProcedureCommand( connection, "SetIpAndPort", new Dictionary<string, object> { { "@Id", account.Id }, { "@IpAddress", account.IpAddress }, { "@Port", account.Port} }); var rdr = cmd.ExecuteReader(); rdr.Close(); }
public void NotifyFriendsOfPresenceChange(Account account, IEnumerable<Account> friends) { Parallel.ForEach(friends, friend => { if (ServiceHelpers.GetPresence(friend) == PresenceStatus.Online) { IEvent evnt = new PresenceChangeEvent { Type = EventType.PresenceChange, Timestamp = DateTime.Now, AccountName = account.UserName, PresenceStatus = ServiceHelpers.GetPresence(account) }; // ReSharper disable once PossibleInvalidOperationException EventManager.SendEventToClient(friend.IpAddress, friend.Port.Value, evnt); } }); }