public IEnumerator UpdateAccountCoroutine(uLobby.IAccount account, uLobby.AccountUpdate update, uLobby.Request <uLobby.Account> request)
    {
        Request <AccountRecord> getAccountRequest;

        getAccountRequest = StorageLayerUtility.GetAccountRecord(account.id);
        yield return(getAccountRequest.WaitUntilDone()); if (StorageLayerUtility.RequestUtility.PropagateException(getAccountRequest, request))
        {
            yield break;
        }
        AccountRecord record = getAccountRequest.result;

        if (StorageLayerUtility.AccountUpdateUtility.isPasswordChanged(update))
        {
            record.passwordHash = SaltedPasswordHash.GenerateSaltedPasswordHash(SaltedPasswordHash.GeneratePasswordHash(StorageLayerUtility.AccountUpdateUtility.GetPassword(update) + record.name));
        }
        if (StorageLayerUtility.AccountUpdateUtility.IsDataChanged(update))
        {
            record.data = StorageLayerUtility.AccountUpdateUtility.GetData(update);
        }
        MySqlParameter[] paramsArray = new MySqlParameter[4];
        paramsArray[0] = new MySqlParameter("password", record.passwordHash.passwordHash);
        paramsArray[1] = new MySqlParameter("salt", record.passwordHash.salt);
        paramsArray[2] = new MySqlParameter("data", record.data);
        paramsArray[3] = new MySqlParameter("id", int.Parse(record.id.value));
        var operation = ExecuteNonQueryAsync.BeginInvoke(storageManager.connectionString, "UPDATE accounts SET password=@password,salt=@salt,data=@data WHERE id = @id", paramsArray, null, null);

        while (!operation.IsCompleted)
        {
            yield return(null);
        }
        Account updatedAccount = StorageLayerUtility.CreateAccount(record);

        StorageLayerUtility.RequestUtility.SetResult(request, updatedAccount);
    }
Example #2
0
    // UpdateAccountCoroutine
    IEnumerator IAccountOperations.UpdateAccountCoroutine(IAccount account, AccountUpdate update, Request <Account> request)
    {
        Request <AccountRecord> getAccountRequest;

        getAccountRequest = StorageLayerUtility.GetAccountRecord(account.id);
        yield return(getAccountRequest.WaitUntilDone()); if (StorageLayerUtility.RequestUtility.PropagateException(getAccountRequest, request))
        {
            yield break;
        }
        AccountRecord record = getAccountRequest.result;

        if (StorageLayerUtility.AccountUpdateUtility.isPasswordChanged(update))
        {
            record.passwordHash = SaltedPasswordHash.GenerateSaltedPasswordHash(SaltedPasswordHash.GeneratePasswordHash(StorageLayerUtility.AccountUpdateUtility.GetPassword(update) + record.name));
        }

        if (StorageLayerUtility.AccountUpdateUtility.IsDataChanged(update))
        {
            record.data = StorageLayerUtility.AccountUpdateUtility.GetData(update);
        }

        Account updatedAccount = StorageLayerUtility.CreateAccount(record);

        StorageLayerUtility.RequestUtility.SetResult(request, updatedAccount);
        yield break;
    }
 // ToAccountRecord
 public AccountRecord ToAccountRecord()
 {
     return(StorageLayerUtility.CreateAccountRecord(
                name,
                new SaltedPasswordHash(
                    System.Convert.FromBase64String(passwordHash),
                    System.Convert.FromBase64String(salt)
                    ),
                new AccountID(id),
                null
                ));
 }
    public IEnumerator GetFriendListRecordCoroutine(uLobby.AccountID accountID, uLobby.Request <uLobby.FriendListRecord> request)
    {
        FriendListRecord friendList = StorageLayerUtility.FriendListRecordUtility.CreateFriendListRecord();
        //read friend list
        var operation = ExecuteDataReaderAsync.BeginInvoke(storageManager.connectionString, "SELECT accounts.id,accounts.name,password,LENGTH(password),salt,LENGTH(salt),data,LENGTH(data) FROM accounts INNER JOIN friends ON accounts.id = friends.friend WHERE owner = @id", new MySqlParameter[] { new MySqlParameter("id", int.Parse(accountID.value)) }, null, null);

        while (!operation.IsCompleted)
        {
            yield return(null);
        }
        var dr = ExecuteDataReaderAsync.EndInvoke(operation);

        while (dr.Read())
        {
            AccountRecord accountRecord = ReadAccountRecordFromDataReader(dr);
            Account       account       = StorageLayerUtility.CreateAccount(accountRecord);
            StorageLayerUtility.FriendListRecordUtility.AddFriend(friendList, StorageLayerUtility.CreateFriendInfo(account));
        }
        dr.Close();
        //read the original account owning the list.
        operation = ExecuteDataReaderAsync.BeginInvoke(storageManager.connectionString, "SELECT id,name,password,LENGTH(password),salt,LENGTH(salt),data,LENGTH(data) FROM accounts WHERE id = @id", new MySqlParameter[] { new MySqlParameter("id", int.Parse(accountID.value)) }, null, null);
        while (!operation.IsCompleted)
        {
            yield return(null);
        }
        dr = ExecuteDataReaderAsync.EndInvoke(operation);
        dr.Read();
        Account originalAccount = StorageLayerUtility.CreateAccount(ReadAccountRecordFromDataReader(dr));

        dr.Close();

        //read invitations
        operation = ExecuteDataReaderAsync.BeginInvoke(storageManager.connectionString, "SELECT accounts.id,name,password,LENGTH(password),salt,LENGTH(salt),data,LENGTH(data) FROM accounts INNER JOIN invitations ON accounts.id = invitations.sender WHERE invitations.receiver = @id", new MySqlParameter[] { new MySqlParameter("id", int.Parse(accountID.value)) }, null, null);
        while (!operation.IsCompleted)
        {
            yield return(null);
        }
        dr = ExecuteDataReaderAsync.EndInvoke(operation);
        while (dr.Read())
        {
            Account account = StorageLayerUtility.CreateAccount(ReadAccountRecordFromDataReader(dr));
            StorageLayerUtility.FriendListRecordUtility.AddFriendInvitation(friendList, StorageLayerUtility.CreateFriendInvitation(account, originalAccount));
        }
        dr.Close();
        StorageLayerUtility.RequestUtility.SetResult(request, friendList);
        yield break;
    }
Example #5
0
    // GetAccountCoroutine
    IEnumerator IAccountOperations.GetAccountCoroutine(AccountID accountID, string accountName, bool exceptionIfInvalid, Request <Account> request)
    {
        foreach (var account in accounts)
        {
            if (account.id == accountID || (accountID == null && accountName == account.name))
            {
                StorageLayerUtility.RequestUtility.SetResult(request, StorageLayerUtility.CreateAccount(account));
                yield break;
            }
        }

        StorageLayerUtility.RequestUtility.SetResult(request, null);
        if (exceptionIfInvalid)
        {
            StorageLayerUtility.RequestUtility.ThrowException(request, (StorageLayerUtility.Exceptions.CreateAccountException("Account with id " + accountID + " and name " + accountName + " does not exist.")));
        }

        yield break;
    }
    public IEnumerator GetAccountCoroutine(uLobby.AccountID accountID, string accountName, bool exceptionIfInvalid, uLobby.Request <uLobby.Account> request)
    {
        MySqlDataReader dr;

        if (accountID == null)
        {
            var operation = ExecuteDataReaderAsync.BeginInvoke(storageManager.connectionString, "SELECT id,name,password,LENGTH(password),salt,LENGTH(salt),data,LENGTH(data) FROM accounts WHERE name = @name;", new MySqlParameter[] { new MySqlParameter("name", accountName) }, null, null);
            while (!operation.IsCompleted)
            {
                yield return(null);
            }
            dr = ExecuteDataReaderAsync.EndInvoke(operation);
        }
        else
        {
            var operation = ExecuteDataReaderAsync.BeginInvoke(storageManager.connectionString, "SELECT id,name,password,LENGTH(password),salt,LENGTH(salt),data,LENGTH(data) FROM accounts WHERE id = @id;", new MySqlParameter[] { new MySqlParameter("id", int.Parse(accountID.value)) }, null, null);
            while (!operation.IsCompleted)
            {
                yield return(null);
            }
            dr = ExecuteDataReaderAsync.EndInvoke(operation);
        }
        if (dr.HasRows)
        {
            dr.Read();
            Account account = StorageLayerUtility.CreateAccount(ReadAccountRecordFromDataReader(dr));
            StorageLayerUtility.RequestUtility.SetResult(request, account);
        }
        else
        {
            StorageLayerUtility.RequestUtility.SetResult(request, null);
            if (exceptionIfInvalid)
            {
                StorageLayerUtility.RequestUtility.ThrowException(request, StorageLayerUtility.Exceptions.CreateAccountException("Account with id " + accountID + " and name " + accountName + " does not exist."));
            }
        }
        dr.Close();
    }
    /// <summary>
    /// This method gets a data reader which it's Read method is called and assumes the following structure
    /// (name,password,password length,salt,salt length,data,data length)
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    protected AccountRecord ReadAccountRecordFromDataReader(MySqlDataReader dr)
    {
        int passwordLength = dr.GetInt32(3);
        int saltLength     = dr.GetInt32(5);
        int dataLength     = dr.GetInt32(7);

        byte[] passwordBuffer = new byte[passwordLength];
        byte[] saltBuffer     = new byte[saltLength];
        byte[] dataBuffer     = (dataLength != 0) ? new byte[dataLength] : null;

        if (dataBuffer != null)
        {
            dr.GetBytes(dr.GetOrdinal("data"), 0, dataBuffer, 0, dataBuffer.Length);
        }
        dr.GetBytes(dr.GetOrdinal("password"), 0, passwordBuffer, 0, passwordBuffer.Length);
        dr.GetBytes(dr.GetOrdinal("salt"), 0, saltBuffer, 0, saltBuffer.Length);
        AccountRecord record = StorageLayerUtility.CreateAccountRecord(dr.GetString("name"),
                                                                       new uLobby.SaltedPasswordHash(passwordBuffer, saltBuffer),
                                                                       new uLobby.AccountID(dr.GetInt32("id").ToString()),
                                                                       dataBuffer);

        return(record);
    }
Example #8
0
    // UpdateAccountCoroutine
    IEnumerator IAccountOperations.UpdateAccountCoroutine(IAccount account, AccountUpdate update, Request <Account> request)
    {
        // Get account record
        Request <AccountRecord> getAccountRequest;

        getAccountRequest = StorageLayerUtility.GetAccountRecord(account.id);
        yield return(getAccountRequest.WaitUntilDone());

        // Did it cause an exception?
        if (StorageLayerUtility.RequestUtility.PropagateException(getAccountRequest, request))
        {
            yield break;
        }

        AccountRecord record = getAccountRequest.result;

        // Password change
        if (StorageLayerUtility.AccountUpdateUtility.isPasswordChanged(update))
        {
            record.passwordHash = SaltedPasswordHash.GenerateSaltedPasswordHash(SaltedPasswordHash.GeneratePasswordHash(StorageLayerUtility.AccountUpdateUtility.GetPassword(update) + record.name));
        }

        Account updatedAccount = StorageLayerUtility.CreateAccount(record);

        // Save in accounts database
        yield return(GameDB.instance.StartCoroutine(GameDB.Set <GameAccount>(
                                                        "Accounts",
                                                        updatedAccount.id.value,
                                                        new GameAccount(record),
                                                        data => {
            if (data != null)
            {
                StorageLayerUtility.RequestUtility.SetResult(request, updatedAccount);
            }
        }
                                                        )));
    }
 // ToAccount
 public Account ToAccount()
 {
     return(StorageLayerUtility.CreateAccount(ToAccountRecord()));
 }