Ejemplo n.º 1
0
    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;
    }
Ejemplo n.º 2
0
    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;
    }
Ejemplo n.º 3
0
 public static PresenceStatus GetPresence(Account account)
 {
   return (account.IpAddress != null && account.Port != null)
     ? PresenceStatus.Online
     : PresenceStatus.Offline;
 }
Ejemplo n.º 4
0
    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();
    }
Ejemplo n.º 5
0
 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);
     }
   });
 }