Exemplo n.º 1
0
        public void ParticipateInRoundWithExistingAccount(long clientId, long roundId, long walletId, long accountId, bool joinNearest, string language, bool?usePass)
        {
            Logger.Trace("Participate in round {0}. Client {4}, account {2}, wallet {1}, {3}use pass",
                         roundId, walletId, accountId, !usePass.HasValue || !usePass.Value ? "don't " : "", clientId);

            return(InvokeOperations.InvokeOperation(() =>
            {
                try
                {
                    var clientData = clientService.GetClient(clientId);
                    if (!clientData.IsSuccess)
                    {
                        throw new OperationException(clientData.Error, clientData.Code);
                    }

                    if (tournamentRepository.CheckRoundParticipation(roundId, clientId))
                    {
                        throw new OperationException("Already participant", ResultCode.TournamentAlreadyParticipate);
                    }
                    var round = tournamentRepository.GetRound(roundId);
                    var account = tournamentRepository.GetParticipant(accountId);
                    if (account.client_account_id != clientId)
                    {
                        throw new Exception("Wrong client id");
                    }
                    if (round.AccountType != account.account_type_id)
                    {
                        throw new Exception(String.Format("Wrong account type. was {0} expected {1}", account.account_type_id, round.AccountType));
                    }

                    var tournament = tournamentRepository.GetTournament(round.Round.tournament_id);
                    if (tournament.entrance_only_by_pass && (!usePass.HasValue || !usePass.Value))
                    {
                        throw new OperationException("Entrance only by pass", ResultCode.TournamentWelcomeNotAllowed);
                    }
                    if (tournament.authorized_only)
                    {
                        var clientStatusesData = clientService.GetClientStatuses(clientId);
                        if (!clientStatusesData.IsSuccess)
                        {
                            throw new OperationException(clientStatusesData.Error, clientStatusesData.Code);
                        }

                        if (!clientStatusesData.Result.Has(ClientStatuses.IsApproved))
                        {
                            throw new OperationException("Client is not approved", ResultCode.TournamentAuthorizedOnly);
                        }
                    }
                    //Check freeroll prizes and bans
                    if (tournament.type_id == (int)TournamentsType.FreeRoll)
                    {
                        if (clientData.Result.ClientStatuses.Has(ClientStatuses.IsPromotionBlocked))
                        {
                            throw new OperationException("Participation in round is not available", ResultCode.SiteOperationNotAvailable);
                        }

                        if (tournamentRepository.CheckClientTournamentPrizes(clientId, tournament.id_name))
                        {
                            throw new OperationException("Already has prize", ResultCode.TournamentAlreadyWon);
                        }
                        if (tournamentRepository.CheckClientBans(clientId, tournament.type_id))
                        {
                            throw new OperationException("Banned", ResultCode.TournamentBanned);
                        }
                    }
                    else if (tournament.type_id == (int)TournamentsType.Special)
                    {
                        if (tournamentRepository.CheckSpecialTournamentParticipation(clientId, tournament.id_name))
                        {
                            throw new OperationException("Already participant", ResultCode.TournamentChallengeAlreadyParticipate);
                        }
                    }

                    if (joinNearest)
                    {
                        round = tournamentRepository.GetNearestRound(tournament.id_name);
                    }
                    else if (round.Round.is_started && !tournament.entry_after_start)
                    {
                        throw new OperationException("Round is already started", ResultCode.TournamentRoundAlreadyStarted);
                    }

                    var participants = tournamentRepository.GetRoundParticipants(roundId).Count();
                    if (tournament.participants_number != 0)
                    {
                        if (participants >= tournament.participants_number)
                        {
                            throw new OperationException("Round is full", ResultCode.TournamentRoundIsFull);
                        }
                    }
                    if (CheckRoundsIntersection(tournamentRepository.GetAccountActiveRounds(accountId), round.Round.date_start, round.Round.date_end))
                    {
                        throw new OperationException("Rounds intersection", ResultCode.TournamentRoundsIntersection);
                    }

                    PayForTournament(tournament, round, walletId, clientId, language, clientData.Result.Email, usePass);
                    accountService.ChangeAccountBalance(accountId, 0, "");

                    tournamentRepository.AddRoundParticipation(accountId, roundId, walletId);
                    if (round.Round.is_started)
                    {
                        var resMove = accountService.ChangeAccountGroup(accountId, tournament.group_name);
                        if (!resMove.IsSuccess)
                        {
                            throw new OperationException(resMove.Error, (ResultCode)resMove.Code);
                        }
                        if (tournament.leverage != null)
                        {
                            var leverageRes = accountService.ChangeAccountLeverage(accountId, (int)tournament.leverage);
                            if (!leverageRes.IsSuccess)
                            {
                                throw new OperationException(leverageRes.Error, (ResultCode)leverageRes.Code);
                            }
                        }
                        var flagResult = accountService.ChangeReadOnlyFlag(accountId, false);
                        if (!flagResult.IsSuccess)
                        {
                            throw new OperationException(flagResult.Error, (ResultCode)flagResult.Code);
                        }
                        var setBalanceResult = accountService.SetAccountsBalance(new[] { accountId }, tournament.base_deposit,
                                                                                 new[] { string.Format(MT4CommentHelper.GetMt4CommentTournament(MT4CommentHelper.MT4CommentType.Deposit, tournament.id_name, round.Round.id, accountId)) });
                        if (!setBalanceResult.IsSuccess)
                        {
                            throw new OperationException(setBalanceResult.Error, (ResultCode)setBalanceResult.Code);
                        }
                    }
                    tournamentProcessor.ConstructRoundRating(round.Round);

                    participants = tournamentRepository.GetRoundParticipants(roundId).Count();
                    if (!round.Round.is_started && participants == tournament.participants_number)
                    {
                        tournamentProcessor.StartRound(round.Round);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error("Error at participate in round {0}. Client {4}, account {2}, wallet {1}, {3}use pass. {5}",
                                 roundId, walletId, accountId, !usePass.HasValue || !usePass.Value ? "don't " : "", clientId, ex.ToString());
                    throw;
                }
            }));
        }
Exemplo n.º 2
0
        public ParticipateAccount ParticipateInRoundWithNewAccount(long clientId, long roundId, long walletId, string nickname, string language, bool joinNearest, bool?usePass)
        {
            Logger.Trace("Participate in round {1}. Client {0}, nickname {3}, wallet {2}, {4}use pass",
                         clientId, roundId, walletId, nickname, !usePass.HasValue || !usePass.Value ? "don't " : "");

            return(InvokeOperations.InvokeOperation(() =>
            {
                try
                {
                    var clientData = clientService.GetClient(clientId);
                    if (!clientData.IsSuccess)
                    {
                        throw new OperationException(clientData.Error, clientData.Code);
                    }

                    if (tournamentRepository.CheckRoundParticipation(roundId, clientId))
                    {
                        throw new OperationException("Already participant", ResultCode.TournamentAlreadyParticipate);
                    }
                    var round = tournamentRepository.GetRound(roundId);

                    var tournament = tournamentRepository.GetTournament(round.Round.tournament_id);
                    if (tournament.entrance_only_by_pass && (!usePass.HasValue || !usePass.Value))
                    {
                        throw new OperationException("Entrance only by pass", ResultCode.TournamentWelcomeNotAllowed);
                    }
                    if (tournament.authorized_only)
                    {
                        var clientStatusesData = clientService.GetClientStatuses(clientId);
                        if (!clientStatusesData.IsSuccess)
                        {
                            throw new OperationException(clientStatusesData.Error, clientStatusesData.Code);
                        }

                        if (!clientStatusesData.Result.Has(ClientStatuses.IsApproved))
                        {
                            throw new OperationException("Client is not approved", ResultCode.TournamentAuthorizedOnly);
                        }
                    }
                    //Check freeroll prizes
                    if (tournament.type_id == (int)TournamentsType.FreeRoll)
                    {
                        if (clientData.Result.ClientStatuses.Has(ClientStatuses.IsPromotionBlocked))
                        {
                            throw new OperationException("Participation in round is not available", ResultCode.SiteOperationNotAvailable);
                        }

                        if (tournamentRepository.CheckClientTournamentPrizes(clientId, tournament.id_name))
                        {
                            throw new OperationException("Already has prize", ResultCode.TournamentAlreadyWon);
                        }
                        if (tournamentRepository.CheckClientBans(clientId, tournament.type_id))
                        {
                            throw new OperationException("Banned", ResultCode.TournamentBanned);
                        }
                    }
                    else if (tournament.type_id == (int)TournamentsType.Special)
                    {
                        if (tournamentRepository.CheckSpecialTournamentParticipation(clientId, tournament.id_name))
                        {
                            throw new OperationException("Already participant", ResultCode.TournamentChallengeAlreadyParticipate);
                        }
                    }

                    if (joinNearest)
                    {
                        round = tournamentRepository.GetNearestRound(tournament.id_name);
                    }
                    else if (round.Round.is_started && !tournament.entry_after_start)
                    {
                        throw new OperationException("Round is already started", ResultCode.TournamentRoundAlreadyStarted);
                    }

                    var participants = tournamentRepository.GetRoundParticipants(roundId).Count();
                    if (tournament.participants_number != 0)
                    {
                        if (participants >= tournament.participants_number)
                        {
                            throw new OperationException("Round is full", ResultCode.TournamentRoundIsFull);
                        }
                    }

                    var payment = tournament.isdemo ? 0 : round.Round.base_deposit + walletId == -1 ? 0 : round.Round.entry_fee;

                    var res = accountService.CreateTradingAccount(clientId, "USD", (AccountService.AccountType)round.AccountType, nickname, language);
                    if (!res.IsSuccess)
                    {
                        throw new OperationException(String.Format("Create trading account error: {0}", res.Error), res.Code);
                    }
                    var resRole = accountService.ChangeAccountRole(res.Result.AccountId, AccountRole.Tournament);
                    if (!resRole.IsSuccess)
                    {
                        throw new OperationException(String.Format("Change account role error: {0}", res.Error), res.Code);
                    }
                    var resAcc = accountService.GetMt4AccountInfo(res.Result.AccountId);
                    if (!resAcc.IsSuccess)
                    {
                        throw new OperationException(String.Format("Get account error: {0}", res.Error), res.Code);
                    }
                    var acc = resAcc.Result;

                    PayForTournament(tournament, round, walletId, clientId, language, clientData.Result.Email, usePass);

                    var client = clientService.GetClient(clientId);
                    var isBot = client.IsSuccess && client.Result.IsUnreal;

                    var account = new ParticipantModel
                    {
                        AccountTypeId = (long)acc.AccountTypeId,
                        Avatar = acc.Avatar,
                        Nickname = nickname,
                        ClientAccountId = acc.ClientId,
                        Description = acc.Description,
                        Id = acc.AccountId,
                        Login = acc.Login,
                        IsBot = isBot
                    };
                    tournamentRepository.AddParticipant(account);
                    tournamentRepository.AddRoundParticipation(account.Id, round.Round.id, walletId);
                    if (round.Round.is_started)
                    {
                        var resMove = accountService.ChangeAccountGroup(res.Result.AccountId, tournament.group_name);
                        if (!resMove.IsSuccess)
                        {
                            throw new OperationException(resMove.Error, resMove.Code);
                        }
                        if (tournament.leverage != null)
                        {
                            var leverageRes = accountService.ChangeAccountLeverage(account.Id, (int)tournament.leverage);
                            if (!leverageRes.IsSuccess)
                            {
                                throw new OperationException(leverageRes.Error, leverageRes.Code);
                            }
                        }
                        var flagResult = accountService.ChangeReadOnlyFlag(account.Id, false);
                        if (!flagResult.IsSuccess)
                        {
                            throw new OperationException(flagResult.Error, flagResult.Code);
                        }
                        var setBalanceResult = accountService.SetAccountsBalance(new[] { account.Id }, tournament.base_deposit,
                                                                                 new[] { string.Format(MT4CommentHelper.GetMt4CommentTournament(MT4CommentHelper.MT4CommentType.Deposit, tournament.id_name, round.Round.id, account.Id)) });
                        if (!setBalanceResult.IsSuccess)
                        {
                            throw new OperationException(setBalanceResult.Error, setBalanceResult.Code);
                        }
                    }
                    tournamentProcessor.ConstructRoundRating(round.Round);

                    participants = tournamentRepository.GetRoundParticipants(roundId).Count();
                    if (!round.Round.is_started && participants == tournament.participants_number)
                    {
                        tournamentProcessor.StartRound(round.Round);
                    }

                    return new ParticipateAccount
                    {
                        Login = (int)res.Result.Login,
                        Password = res.Result.Password,
                        ServerName = res.Result.ServerName
                    };
                }
                catch (Exception ex)
                {
                    Logger.Trace("Error at participate in round {1}. Client {0}, nickname {3}, wallet {2}, {4}use pass. {5}",
                                 clientId, roundId, walletId, nickname, !usePass.HasValue || !usePass.Value ? "don't" : "", ex.ToString());
                    throw;
                }
            }));
        }