def run_updating_top_players(): print('run_updating_top_players') system_service = SystemService() global_settings = GlobalSettings() while True: try: system_service.update_top_players_cache(global_settings.number_top_players) time.sleep(global_settings.frequency_update_top_players * 60) except Exception as e: print(e)
def run_updating_cache_trade(): print('run_updating_cache_trade') system_service = SystemService() global_settings = GlobalSettings() while True: try: system_service.update_trade_cache() time.sleep(global_settings.frequency_update_trade * 60) except Exception as e: print(e)
def clear_country_attack_history(): print('clear_country_attack_history') global_settings = GlobalSettings.objects().first() while True: for country in Country.objects(): active_history_lst = [] for history in country.army.history_attacks: if (datetime.utcnow() - history.time).total_seconds()/60 < global_settings.pause_between_war: active_history_lst.append(history) country.army.history_attacks = active_history_lst country.save() time.sleep(3600 * 24)
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)
def redirect_feedback(request, user_id): try: user = User.objects(id=user_id).first() request_data = JSONParser().parse(request) global_settings = GlobalSettings.objects().first() if SystemService.verify_token(user_id, request_data['token']): if (datetime.utcnow() - user.date_last_feedback).total_seconds()/60 >= global_settings.feedback_pause: user.date_last_feedback = datetime.utcnow() SystemService().send_notification([ADMIN_EMAIL],EmailEvent.FEEDBACK, user.username, request_data['msg'], request_data['rating'], user.email) user.save() else: return HttpResponse('You can send only 1 feedback in 24 hours', status=status.HTTP_208_ALREADY_REPORTED) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
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)
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
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
def register_new_user(self, username: str, password: str, email: str, country_name: str, link_country_flag: str): country = SystemService().create_default_country( country_name, link_country_flag) try: country.save() except Exception as error: print(error) return False user = User(username=username, password=sha256(password.encode()).hexdigest(), email=email, country=country.pk) try: user.save() except Exception as error: print(error) country.delete() return False if GlobalSettings.objects().first().email_notification: SystemService().send_notification([email], EmailEvent.REGISTRATION, username, country_name, str(user.pk), link_country_flag) return True
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)
def run_updating_politics_cache(): print('run_updating_politics_cache') global_settings = GlobalSettings() while True: SystemService().update_politics_cache() time.sleep(global_settings.frequency_update_politics_cache * 60)