def logout(self, user_id: str):
     user = User.objects(id=user_id).first()
     if user:
         user.isAuth = False
         user.token = ''
         user.save()
         return True
     else:
         raise UnknownUserError(str(user_id))
Ejemplo n.º 2
0
    def get_industry(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()
        industry_dict = {}
        farms_list = []
        mines_list = []
        factories_list = []
        game_service = GameService()

        farms_modifiers = game_service.get_industry_modifiers(country, 'farms')
        mines_modifiers = game_service.get_industry_modifiers(country, 'mines')
        factories_modifiers = game_service.get_industry_modifiers(
            country, 'factories')

        for farm in country.farms:
            industrial_card_view = IndustrialCardView(
                farm.name, farm.link_img, round(farm.production_speed, 2),
                round(
                    farm.active_number * farm.production_speed *
                    farms_modifiers, 2), farm.price_build, farm.workers,
                farm.number, farm.active_number, farm.number * farm.workers,
                [])
            farms_list.append(industrial_card_view)

        industry_dict['farms'] = farms_list

        for mine in country.mines:
            industrial_card_view = IndustrialCardView(
                mine.name, mine.link_img, round(mine.production_speed, 2),
                round(
                    mine.active_number * mine.production_speed *
                    mines_modifiers, 2), mine.price_build, mine.workers,
                mine.number, mine.active_number, mine.number * mine.workers,
                [])
            mines_list.append(industrial_card_view)

        industry_dict['mines'] = mines_list

        for factory in country.factories + country.military_factories:
            industrial_card_view = IndustrialCardView(
                factory.name, factory.link_img,
                round(factory.production_speed, 2),
                round(factory.active_number * factory.production_speed *
                      factories_modifiers), factory.price_build,
                factory.workers, factory.number, factory.active_number,
                factory.number * factory.workers, [
                    TableRowGoodsView(item.link_img, item.name, item.value)
                    for item in factory.need_goods
                ])
            factories_list.append(industrial_card_view)

        industry_dict['factories'] = factories_list[:18]
        industry_dict['military_factories'] = factories_list[18:]
        finish = time.time()
        return industry_dict
Ejemplo n.º 3
0
 def get_cache_politics_laws(self, user_id: str):
     user = User.objects(id=user_id).first()
     country = Country.objects(id=user.country.id).first()
     cache = Cache.objects().first()
     if cache.politics != '':
         politics_view = json.loads(cache.politics)
         politics_view['selected_laws'] = country.adopted_laws
         return politics_view
     else:
         return self.get_politics_laws(user_id)
 def delete_user_account(self, user_id: str, password: str):
     if User.objects(id=user_id).count() == 1 and User.objects(
             id=user_id).first().password == sha256(
                 password.encode()).hexdigest():
         try:
             user = User.objects(id=user_id).first()
             user_email = user.email
             country_pk = user.country.id
             country = Country.objects(id=country_pk).first()
             country.delete()
             user.delete()
             if GlobalSettings.objects().first().email_notification:
                 SystemService().send_notification([user_email],
                                                   EmailEvent.DELETE,
                                                   user.username)
             return True
         except Exception as e:
             print(e)
             return False
     return False
Ejemplo n.º 5
0
 def get_account(self, user_id: str):
     user = User.objects(id=user_id).first()
     country = Country.objects(id=user.country.id).first()
     game_service = GameService()
     days_in_game = (datetime.utcnow() - user.date_registration).days
     date_reg = user.date_registration.strftime('%d.%m.%Y')
     account_view = AccountView(
         user_id, country.link_img, country.name,
         game_service.get_total_profit(country),
         game_service.get_economic_place(country.name),
         game_service.get_army_place(country.name), user.username,
         user.email, date_reg, days_in_game)
     return account_view
Ejemplo n.º 6
0
 def get_view_page(self, user_id: str):
     user = User.objects(id=user_id).first()
     country = Country.objects(id=user.country.id).first()
     economic_place = GameService().get_economic_place(country.name)
     army_place = GameService().get_army_place(country.name)
     cache = Cache.objects().first()
     if cache.top_players != '':
         top_players = json.loads(cache.top_players)
     else:
         global_settings = GlobalSettings.objects().first()
         top_players = self.get_top_players(
             global_settings.number_top_players)
     return TopPlayersPage(economic_place, army_place, top_players)
Ejemplo n.º 7
0
    def get_trade(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        goods = Trade.objects
        target_country = Country.objects(id=user.country.id).first()
        countries = Country.objects

        trade_cards_view_list = []
        production_data = GameService().get_table_world_place_production()

        for item in goods:
            data_top_producer = []
            if len(production_data[item.name]) > 2:
                data_top_producer_buffer = production_data[item.name][:3]
            else:
                data_top_producer_buffer = production_data[item.name]

            for producer in data_top_producer_buffer:
                producer_view = TableRowProducerView(
                    countries.filter(name=producer[0]).first().link_img,
                    producer[0], producer[1])
                data_top_producer.append(producer_view)

            data_top_producer.sort(key=lambda x: x.number, reverse=True)
            warehouse = next(
                filter(lambda x: x.goods.name == item.name,
                       target_country.warehouses), None)
            price_list = [
                round(history.value, 2) for history in item.history_price
            ]

            if not price_list:
                price_list = [round(item.price_now, 2)]

            chart_price_goods = ChartPriceGoods(price_list, [
                history.time.strftime("%H:%M:%S")
                for history in item.history_price
            ], item.name,
                                                max(price_list) * 1.2,
                                                min(price_list) * 0.9)

            trade_card_view = TradeCardView(item.name,
                                            warehouse.goods.link_img,
                                            round(item.price_now),
                                            round(warehouse.goods.value,
                                                  2), warehouse.capacity,
                                            data_top_producer,
                                            chart_price_goods)
            trade_cards_view_list.append(trade_card_view)
        finish = time.time()
        return trade_cards_view_list
Ejemplo n.º 8
0
 def get_cache_trade(self, user_id: str):
     cache = Cache.objects().first()
     if cache.trade != '':
         trade_view = json.loads(cache.trade)
         user = User.objects(id=user_id).first()
         target_country = Country.objects(id=user.country.id).first()
         for trade_card in trade_view:
             warehouse = next(
                 filter(lambda x: x.goods.name == trade_card['name'],
                        target_country.warehouses), None)
             trade_card['warehouse_has'] = round(warehouse.goods.value, 2)
             trade_card['warehouse_capacity'] = warehouse.capacity
         return trade_view
     else:
         return self.get_trade(user_id)
Ejemplo n.º 9
0
def run_check_warehouses():
    print('run_check_warehouses')
    global_settings = GlobalSettings.objects().first()
    if global_settings.email_notification:
        while True:
            try:
                users = User.objects()
                for user in users:
                    if user.settings['warehouse overflow or empty']:
                        country = Country.objects(id=user.country).first()
                        for warehouse in country.warehouses:
                            if warehouse.filling_speed != 0 and (datetime.utcnow() - country.date_last_warehouse_notification).total_seconds()/60 >= global_settings.frequency_email_notification and (warehouse.goods.value <= 0 or warehouse.goods.value >= warehouse.capacity):
                                SystemService().send_notification([user.email],EmailEvent.WAREHOUSE)
                                break
                time.sleep(global_settings.frequency_check_warehouses * 60)
            except Exception as e:
                print(e)
Ejemplo n.º 10
0
    def get_warehouses(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()

        warehouses_list = []
        for warehouse in country.warehouses:
            warehouse_card_view = WarehouseCardView(
                warehouse.goods.name, warehouse.goods.link_img,
                round(warehouse.goods.value, 2), warehouse.capacity,
                round(warehouse.filling_speed, 2), warehouse.level,
                warehouse.max_level, warehouse.price_upgrade,
                warehouse.added_capacity, warehouse.increase_price)
            warehouses_list.append(warehouse_card_view)

        finish = time.time()
        return warehouses_list
Ejemplo n.º 11
0
    def get_technologies(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()

        technologies_view_list = []
        for technology in country.technologies:
            technology_view = TechnologyView(
                technology.name, technology.price_upgrade, technology.level,
                technology.max_level,
                technology.level * technology.modifiers[0].value, [
                    ModifierView(mod.value, mod.address_to)
                    for mod in technology.modifiers
                ])
            technologies_view_list.append(technology_view)

        finish = time.time()
        return technologies_view_list
 def login(self, username: str, password: str):
     user = User.objects(username=username,
                         password=sha256(
                             password.encode()).hexdigest()).first()
     if user:
         token = tokenlib.make_token(
             {
                 "user_id": str(user.id),
                 'username': username,
                 'password': password,
                 'random_value': random.randint(0, 1000000)
             },
             secret=SECRET_KEY)
         user.isAuth = True
         user.token = token
         user.date_last_login = timezone.now()
         user.save()
         return token
     else:
         raise UnknownUserError(username)
Ejemplo n.º 13
0
 def get_politics_laws(self, user_id: str):
     user = User.objects(id=user_id).first()
     country = Country.objects(id=user.country.id).first()
     conscription_laws = []
     pop_laws = []
     for law in Law.objects():
         if 'Conscript law:' in law.name:
             conscription_percent = re.split(' ', law.description)[-1]
             conscription_laws.append(
                 LawView(law.name, law.description, [
                     ModifierView(mod.value, mod.address_to)
                     for mod in law.modifiers
                 ], conscription_percent))
         else:
             pop_laws.append(
                 LawView(law.name, f' ({law.description})', [
                     ModifierView(mod.value, mod.address_to)
                     for mod in law.modifiers
                 ]))
     return PoliticsView(country.adopted_laws, conscription_laws, pop_laws)
 def change_user_data(self,
                      user_id: str,
                      new_country_name: str = None,
                      new_country_flag: str = None):
     user = User.objects(id=user_id).first()
     if user is not None:
         country = Country.objects(id=user.country.id).first()
         country.name = new_country_name if new_country_name is not None and Country.objects(
             name=new_country_name).count() == 0 else country.name
         country.link_img = new_country_flag if new_country_flag is not None else country.link_img
         country.save()
         user.country = country.to_dbref()
         user.save()
         if GlobalSettings.objects().first().email_notification:
             SystemService().send_notification([user.email],
                                               EmailEvent.CHANGE_DATA,
                                               user.username, country.name,
                                               country.link_img)
         return True
     return False
Ejemplo n.º 15
0
    def get_player(self, username: str):
        start = time.time()
        user = User.objects(username=username).first()
        if user is not None:
            country = Country.objects(id=user.country.id).first()

            player_view = PlayerView(
                country.link_img, country.name, user.username,
                GameService().get_economic_place(country.name),
                GameService().get_army_place(country.name),
                country.budget.money, country.population.total_population,
                sum([farm.number for farm in country.farms]),
                sum([mine.number for mine in country.mines]),
                sum([
                    sum([factory.number for factory in country.factories]),
                    sum([
                        factory.number
                        for factory in country.military_factories
                    ])
                ]), country.population.solders, country.army.units)
            finish = time.time()
            return player_view
Ejemplo n.º 16
0
    def get_population(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()
        modifiers_view_list = []
        population_modifiers = 0  # -> 1(100%)

        for mod in country.population.modifiers:
            modifiers_view_list.append(
                ModifierView(round(mod.value, 2), mod.address_from))
            population_modifiers += mod.value
        modifiers_view_list.append(
            ModifierView(country.population.basic_percent_growth_rate,
                         'basic percent'))

        percent_total_progress = round(
            (population_modifiers +
             country.population.basic_percent_growth_rate), 2)

        pie_chart_labels = [
            'Farmers', 'Miners', 'Workers', 'Solders', 'Free', 'Others'
        ]
        pie_chart_data = [
            country.population.farmers, country.population.miners,
            country.population.factory_workers, country.population.solders,
            country.population.free_people,
            country.population.total_population *
            (country.population.min_percent_others / 100)
        ]

        population_view = PopulationView(
            country.population.total_population, country.population.farmers,
            country.population.miners, country.population.factory_workers,
            country.population.solders, country.population.free_people,
            country.population.others, percent_total_progress,
            modifiers_view_list, pie_chart_data, pie_chart_labels)
        finish = time.time()
        return population_view
Ejemplo n.º 17
0
def run_check_news():
    print('run_check_news')
    global_settings = GlobalSettings.objects().first()
    number_of_news = len(News.objects())
    if global_settings.email_notification:
        while True:
            try:
                news_len_lst = len(News.objects())

                if news_len_lst < number_of_news:
                    number_of_news = news_len_lst

                if news_len_lst > number_of_news:
                    number_of_news = news_len_lst
                    users = User.objects()
                    to_emails_lst = []
                    for user in users:
                        if user.settings['news']:
                           to_emails_lst.append(user.email)
                    SystemService().send_notification(to_emails_lst, EmailEvent.NEWS)

                time.sleep(global_settings.frequency_check_news * 60)
            except Exception as e:
                print(e)
Ejemplo n.º 18
0
    def get_budget(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()

        pop_taxes_profit = int(GameService().get_pop_taxes_profit(country))
        army_taxes_profit = int(GameService().get_army_taxes_profit(country))
        farms_taxes_profit = int(GameService().get_farms_taxes_profit(country))
        mines_taxes_profit = int(GameService().get_mines_taxes_profit(country))
        factories_taxes_profit = int(
            GameService().get_factories_taxes_profit(country))

        taxes_profit = pop_taxes_profit + farms_taxes_profit + mines_taxes_profit + factories_taxes_profit
        farms_profit = int(GameService().get_farms_production_profit(country))
        mines_profit = int(GameService().get_mines_production_profit(country))
        factories_profit = int(
            GameService().get_factories_production_profit(country))
        total_profit = taxes_profit + farms_profit + mines_profit + factories_profit

        pop_tax_card = TaxesCard(
            'population taxes', country.budget.population_taxes,
            pop_taxes_profit, [
                ModifierView(round(mod.value, 2), mod.address_from)
                for mod in country.population.modifiers
            ])
        army_views_modifiers = [
            ModifierView(round(mod.value, 2), 'to attack, ' + mod.address_from)
            for mod in country.army.attack_modifiers
        ]
        army_views_modifiers.extend([
            ModifierView(round(mod.value, 2),
                         'to defence, ' + mod.address_from)
            for mod in country.army.defence_modifiers
        ])
        army_tax_card = TaxesCard('army taxes', country.budget.military_taxes,
                                  army_taxes_profit, army_views_modifiers)
        farms_tax_card = TaxesCard(
            'farms taxes', country.budget.farms_taxes, farms_taxes_profit, [
                ModifierView(round(mod.value, 2), mod.address_from)
                for mod in country.industry_modifiers
                if mod.address_to == 'farms' or mod.address_to == 'industry'
            ])
        mines_tax_card = TaxesCard(
            'mines taxes', country.budget.mines_taxes, mines_taxes_profit, [
                ModifierView(round(mod.value, 2), mod.address_from)
                for mod in country.industry_modifiers
                if mod.address_to == 'mines' or mod.address_to == 'industry'
            ])
        factories_tax_card = TaxesCard(
            'factories taxes', country.budget.factories_taxes,
            factories_taxes_profit, [
                ModifierView(round(mod.value, 2), mod.address_from)
                for mod in country.industry_modifiers if
                mod.address_to == 'factories' or mod.address_to == 'industry'
            ])

        budget_view = BudgetView(int(country.budget.money), taxes_profit,
                                 farms_profit, mines_profit, factories_profit,
                                 country.budget.military_expenses,
                                 total_profit, pop_tax_card, army_tax_card,
                                 farms_tax_card, mines_tax_card,
                                 factories_tax_card)

        finish = time.time()
        return budget_view
Ejemplo n.º 19
0
    def get_basic_statistic(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()

        total_profit = int(GameService().get_total_profit(country))
        economic_place = GameService().get_economic_place(country.name)
        army_place = GameService().get_army_place(country.name)

        money_history = [
            int(history.value) for history in country.budget.budget_history
        ]
        x_money_data = [
            str(history.time) for history in country.budget.budget_history
        ]
        chart_money = ChartPopulationData(money_history, x_money_data)

        profit_data = [
            int(history.value) for history in country.budget.profit_history
        ]
        expenses_data = [
            int(history.value) for history in country.budget.expenses_history
        ]
        x_budget_data = [
            str(history.time) for history in country.budget.profit_history
        ]
        max_chart_budget_y_axis_value = 0
        if profit_data and expenses_data:
            max_chart_budget_y_axis_value = max(max(profit_data),
                                                max(expenses_data))
        chart_budget = ChartBudgetData(profit_data, expenses_data,
                                       x_budget_data,
                                       max_chart_budget_y_axis_value)

        pop_data = [
            round(history.value, 1)
            for history in country.population.population_history
        ]
        x_pop_data = [
            str(history.time)
            for history in country.population.population_history
        ]
        chart_pop = ChartPopulationData(pop_data, x_pop_data)

        chart_profit_data = [
            round(GameService().get_pop_taxes_profit(country) /
                  (total_profit + country.budget.military_expenses) * 100),
            round(GameService().get_mines_profit(country) /
                  (total_profit + country.budget.military_expenses) * 100),
            round(GameService().get_farms_profit(country) /
                  (total_profit + country.budget.military_expenses) * 100),
            round(GameService().get_factories_profit(country) /
                  (total_profit + country.budget.military_expenses) * 100)
        ]

        chart_profit = ChartProfitData(chart_profit_data,
                                       ['Taxes', 'Mines', 'Farms', 'Industry'],
                                       round(max(chart_profit_data)))

        goods_data = GameService().get_goods_data(country)
        chart_farms_goods_data = ChartGoodsData(goods_data['farms']['values'],
                                                goods_data['farms']['names'])
        chart_mines_goods_data = ChartGoodsData(goods_data['mines']['values'],
                                                goods_data['mines']['names'])
        chart_factories_goods_data = ChartGoodsData(
            goods_data['factories']['values'],
            goods_data['factories']['names'])
        chart_military_factories_goods_data = ChartGoodsData(
            goods_data['military factories']['values'],
            goods_data['military factories']['names'])

        table_data = GameService().get_table_world_place_production()
        industry = []
        industry.extend(country.farms)
        industry.extend(country.mines)
        industry.extend(country.factories)
        industry.extend(country.military_factories)

        goods_table = []
        for building in industry:
            name_goods = GameService().get_name_goods_from_building(
                building.name)
            target_country = next(
                filter(lambda x: x[0] == country.name, table_data[name_goods]))
            word_place = table_data[name_goods].index(target_country) + 1
            goods_table.append(
                TableRowDataView(building.link_img, name_goods,
                                 building.number, word_place))

        farm_goods_table = goods_table[:len(country.farms)]
        mine_goods_table = goods_table[len(country.farms):len(country.farms) +
                                       len(country.mines)]
        industrial_goods_table = goods_table[
            len(country.farms) + len(country.mines):len(country.farms) +
            len(country.mines) + len(country.factories)]
        military_goods_table = goods_table[len(country.farms) +
                                           len(country.mines) +
                                           len(country.factories):]

        bsv = BasicStatisticView(
            country.link_img, country.name,
            country.population.total_population, country.budget.money,
            total_profit, economic_place, army_place, country.army.victories,
            country.army.losses, chart_money, chart_budget, chart_pop,
            chart_profit, chart_farms_goods_data, chart_mines_goods_data,
            chart_factories_goods_data, chart_military_factories_goods_data,
            farm_goods_table, mine_goods_table, industrial_goods_table,
            military_goods_table)
        finish = time.time()
        return bsv
Ejemplo n.º 20
0
    def get_army(self, user_id: str):
        start = time.time()
        user = User.objects(id=user_id).first()
        country = Country.objects(id=user.country.id).first()
        army = country.army.units
        army_units = ArmyUnit.objects()
        army_view_list = []
        sum_attack_modifiers = GameService().get_army_modifiers(
            country, 'attack')
        sum_defence_modifiers = GameService().get_army_modifiers(
            country, 'defence')

        for name_unit in army:
            if name_unit == 'Infantry':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Infantry equipment',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            elif name_unit == 'Artillery':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Artillery',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            elif name_unit == 'Anti-tank gun':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Anti-tank gun',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            elif name_unit == 'Air defense':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Air defense',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            elif name_unit == 'Tank':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Tanks',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            elif name_unit == 'Aviation':
                warehouse = next(
                    filter(lambda x: x.goods.name == 'Aviation',
                           country.warehouses), None)
                weapons = warehouse.goods.value
                storage_capacity = warehouse.capacity

            unit = army_units.filter(name=name_unit).first()
            unit_characteristic_view_list = [
                UnitCharacteristicView(
                    item.unit_name, item.attack_value * sum_attack_modifiers,
                    item.defence_value * sum_defence_modifiers)
                for item in unit.unit_characteristic.values()
            ]

            modifiers = [
                ModifierView(mod.value, mod.address_from + ' TO ATTACK')
                for mod in country.army.attack_modifiers
            ]
            modifiers.extend([
                ModifierView(mod.value, mod.address_from + ' TO DEFENCE')
                for mod in country.army.defence_modifiers
            ])

            army_card_view = ArmyCardView(
                name_unit, unit.link_img, army[name_unit], unit.need_peoples,
                unit.maintenance_price,
                unit.maintenance_price * army[name_unit],
                country.army.reserve_military_manpower, round(weapons, 2),
                storage_capacity, modifiers, unit_characteristic_view_list)
            army_view_list.append(army_card_view)
        finish = time.time()
        return army_view_list
Ejemplo n.º 21
0
 def tearDownClass(cls):
     user = User.objects(username='******').first()
     UserService().delete_user_account(user.id,user.password)
Ejemplo n.º 22
0
 def setUp(self):
     self.user = User.objects(username='******').first()
Ejemplo n.º 23
0
 def get_top_players(self, number: int):
     view_list = [self.get_player(user.username) for user in User.objects()]
     if number > len(view_list):
         number = len(view_list)
     return sorted(view_list, key=lambda x: x.economic_place)[:number]
Ejemplo n.º 24
0
 def setUp(self):
     self.user = User.objects(username='******').first()
     UserService().login(self.user.username, self.user.password)