Пример #1
0
        public void HandleRequest(IRequest request)
        {
            using (var scope = Db.CreateTransaction())
            {
                var nick     = request.Data.GetOrDefault <string>(k.nick).Trim();
                var avatar   = request.Data.GetOrDefault <Dictionary <string, object> >(k.avatar);
                var raceID   = request.Data.GetOrDefault <int>(k.raceID);
                var majorID  = request.Data.GetOrDefault <int>(k.majorID);
                var schoolID = request.Data.GetOrDefault <int>(k.schoolID);
                var sparkID  = request.Data.GetOrDefault <int>(k.sparkID);

                var account = _accountManager.Repository.Get(request.Session.AccountId);
                if (account == null)
                {
                    throw new PerpetuumException(ErrorCodes.AccountNotFound);
                }

                Character.CheckNickAndThrowIfFailed(nick, request.Session.AccessLevel, account);

                //only 3 characters per account is allowed
                var activeCharactersCount = _accountManager.GetActiveCharactersCount(account);
                if (activeCharactersCount >= 3)
                {
                    throw new PerpetuumException(ErrorCodes.MaximumAmountOfCharactersReached);
                }

                if (account.FirstCharacterDate == null)
                {
                    account.FirstCharacterDate = DateTime.Now;
                    _accountManager.AddExtensionPoints(account, 40000); //TODO: starting EP - store in DB
                    _accountManager.Repository.Update(account);
                }

                var character = CreateNewCharacter();
                character.AccountId = account.Id;
                character.Nick      = nick;
                character.Avatar    = GenxyString.FromDictionary(avatar);
                character.IsDocked  = true;

                DockingBase        dockingBase;
                DefaultCorporation corporation;

                if (schoolID > 0)
                {
                    if (majorID == 0 || raceID == 0 || sparkID == 0)
                    {
                        throw new PerpetuumException(ErrorCodes.WTFErrorMedicalAttentionSuggested);
                    }

                    corporation = DefaultCorporation.GetBySchool(raceID, schoolID) ?? throw new PerpetuumException(ErrorCodes.CorporationNotExists);
                    dockingBase = corporation.GetDockingBase();
                    dockingBase.CreateStarterRobotForCharacter(character, true);

                    character.MajorId  = majorID;
                    character.RaceId   = raceID;
                    character.SchoolId = schoolID;
                    character.SparkId  = sparkID;
                    character.AddToWallet(TransactionType.CharacterCreate, 20000);

                    var extensions = character.GetDefaultExtensions();
                    character.SetExtensions(extensions);

                    var sparkToActivate = _sparkHelper.ConvertCharacterWizardSparkIdToSpark(sparkID);
                    _sparkHelper.ActivateSpark(character, sparkToActivate);
                }
                else
                {
                    // training
                    dockingBase = _dockingBaseHelper.GetTrainingDockingBase();
                    corporation = ((TrainingDockingBase)dockingBase).GetTrainingCorporation();
                    character.SetAllExtensionLevel(6);
                    dockingBase.CreateStarterRobotForCharacter(character);
                    character.AddToWallet(TransactionType.CharacterCreate, 10000000);
                }

                character.CurrentDockingBaseEid = dockingBase.Eid;
                character.DefaultCorporationEid = corporation.Eid;

                //add to default corp, only in sql
                corporation.AddNewCharacter(character);

                _accountManager.PackageGenerateAll(account);

                Transaction.Current.OnCommited(() =>
                {
                    _channelManager.JoinChannel(corporation.ChannelName, character);
                    Message.Builder.FromRequest(request).SetData(k.characterID, character.Id).Send();
                });

                scope.Complete();
            }
        }