Beispiel #1
0
        public Task SetAccountActiveCharacterAsync(Account account, Character character)
        {
            return(Task.Run(() =>
            {
                try
                {
                    using (var db = _ContextFactory.Create())
                    {
                        //Find out if we have a entry for the last active character in the database.
                        AccountLastUsedCharacterEntity activeCharEntity = db.LastUsedCharacters.FirstOrDefault(lastChar =>
                                                                                                               lastChar.Account.AccountId == account.AccountId);

                        //An entity does exist. Simply update the character key
                        if (activeCharEntity != null)
                        {
                            //The ids do match, so lets just return because no further action is required.
                            if (activeCharEntity.CharacterId == character.CharacterId)
                            {
                                return;
                            }

                            activeCharEntity.CharacterId = character.CharacterId;
                        }

                        //No last used character entity found, create a new one.
                        else
                        {
                            db.LastUsedCharacters.Add(new AccountLastUsedCharacterEntity
                            {
                                AccountId = account.AccountId,
                                CharacterId = character.CharacterId
                            });
                        }

                        //We have made changes, lets write them back to the database.
                        db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    string msg =
                        $"Something went wrong while setting the active character(last used character) of account '{account.UserName}' to character '{character.Name}'";
                    _Log.Error(msg);
                    throw new DatabaseRequestException(msg, e);
                }
            }));
        }
Beispiel #2
0
        public Task <ActiveCharacterResult> GetAccountActiveCharacterAsync(Account account)
        {
            if (account == null)
            {
                throw new ArgumentNullException(nameof(account));
            }

            Task <ActiveCharacterResult> task = new Task <ActiveCharacterResult>(() =>
            {
                try
                {
                    using (var db = _ContextFactory.Create())
                    {
                        AccountLastUsedCharacterEntity lastUsedEntity = db.LastUsedCharacters.FirstOrDefault(lc => lc.AccountId == account.AccountId);
                        if (lastUsedEntity != null)
                        {
                            if (!db.CharacterOwnerships.Any(os =>
                                                            os.OwnerId == account.AccountId && os.CharacterId == lastUsedEntity.CharacterId))
                            {
                                //We found a last used character but it is not owned by the given account(ownership was removed since setting the active character). Remove the database entity.
                                db.LastUsedCharacters.Remove(lastUsedEntity);
                                return(new NoActiveCharacterFound(account));
                            }

                            //Last used character was found and it is still owned by the given account.
                            return(new ActiveCharacterFound(account, lastUsedEntity.CharacterId));
                        }

                        // No last used character found.
                        return(new NoActiveCharacterFound(account));
                    }
                }
                catch (Exception e)
                {
                    string msg =
                        $"Something went wrong while requesting the last used character of the account '{account.UserName}' from the database";
                    _Log.Error(msg);
                    throw new DatabaseRequestException(msg, e);
                }
            });

            task.Start();
            return(task);
        }