def run_updating_players(): print('run_updating_players') system_service = SystemService() while True: try: system_service.update_players() except Exception as e: print(e)
def set_settings(request, user_id): try: user = User.objects(id=user_id).first() request_data = JSONParser().parse(request) if SystemService.verify_token(user_id,request_data['token']): SystemService().set_user_settings(user, request_data['setting_list']) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
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 calculate_war(request, user_id, defending_player_name): try: user_1, user_2 = User.objects(id=user_id).first(), User.objects(username=defending_player_name).first() request_data = JSONParser().parse(request) if SystemService.verify_token(user_id,request_data['token']): country_1, country_2 = Country.objects(id=user_1.country.id).first(), Country.objects(id=user_2.country.id).first() view_obj = GameService().calculate_war(country_1.name, country_2.name) if SystemService().get_user_settings(user_id)['attacks']: pass # send email to attacked player return HttpResponse(json.dumps(view_obj, default=lambda x: x.__dict__), status=status.HTTP_200_OK) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
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 logout(request, user_id): try: request_data = JSONParser().parse(request) if SystemService.verify_token(user_id, request_data['token']): UserService().logout(user_id) return HttpResponse({}, status=status.HTTP_200_OK) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def change_user_data(request, user_id): try: request_data = JSONParser().parse(request) if SystemService.verify_token(user_id, request_data['token']): UserService().change_user_data(user_id, request_data['country_name'], request_data['link_on_flag']) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
def edit_army(request, user_id): try: user = User.objects(id=user_id).first() request_data = JSONParser().parse(request) if SystemService.verify_token(user_id,request_data['token']): country = Country.objects(id=user.country.id).first() GameService().edit_army(country, request_data['name_unit'], int(request_data['new_number'])) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
def cancel_law(request, user_id): try: user = User.objects(id=user_id).first() request_data = JSONParser().parse(request) if SystemService.verify_token(user_id,request_data['token']): country = Country.objects(id=user.country.id).first() GameService().cancel_politics_law(country, request_data['name_law']) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
def delete_account(request, user_id, password): try: request_data = JSONParser().parse(request) if SystemService.verify_token(user_id, request_data['token']): deleted = UserService().delete_user_account(user_id, password) if not deleted: return HttpResponse({'Incorrect password'}, status=status.HTTP_400_BAD_REQUEST) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
def find_player(request, user_id, player_name): try: request_data = JSONParser().parse(request) if SystemService.verify_token(user_id, request_data['token']): request_users = list(filter(lambda x: player_name in x.username, User.objects())) if request_users: view_list = [PlayerViewService().get_player(user.username) for user in request_users] return HttpResponse(json.dumps(view_list, default=lambda x: x.__dict__), status=status.HTTP_200_OK) return HttpResponse({}, status=status.HTTP_404_NOT_FOUND) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def set_law(request, user_id): try: user = User.objects(id=user_id).first() request_data = JSONParser().parse(request) if SystemService.verify_token(user_id,request_data['token']): country = Country.objects(id=user.country.id).first() names_political_laws = ['Isolation', 'Free medicine', 'Free housing', 'Free education'] if request_data['name_law'] in names_political_laws: GameService().set_politics_law(country, request_data['name_law']) else: GameService().set_conscript_law(country, request_data['name_law']) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return HttpResponse({}, status=status.HTTP_200_OK)
def get_view(request, user_id, name_view): try: request_data = JSONParser().parse(request) if SystemService.verify_token(user_id, request_data['token']): view_obj = None if name_view == 'BasicStatistic': view_obj = CountryViewService().get_basic_statistic(user_id) elif name_view == 'Budget': view_obj = CountryViewService().get_budget(user_id) elif name_view == 'Technologies': view_obj = CountryViewService().get_technologies(user_id) elif name_view == 'Industry': view_obj = CountryViewService().get_industry(user_id) elif name_view == 'Warehouses': view_obj = CountryViewService().get_warehouses(user_id) elif name_view == 'Politics': view_obj = CountryViewService().get_cache_politics_laws(user_id) elif name_view == 'Population': view_obj = CountryViewService().get_population(user_id) elif name_view == 'Trade': view_obj = CountryViewService().get_cache_trade(user_id) elif name_view == 'Army': view_obj = CountryViewService().get_army(user_id) elif name_view == 'News': view_obj = NewsViewService().get_news() elif name_view == 'Settings': view_obj = SystemService().get_user_settings(user_id) elif name_view == 'Account': view_obj = PlayerViewService().get_account(user_id) elif name_view == 'TopPlayers': view_obj = PlayerViewService().get_view_page(user_id) elif name_view == 'FQA': view_obj = FQAViewService().get_FQA() return HttpResponse(json.dumps(view_obj, default=lambda x: x.__dict__), status=status.HTTP_200_OK) except Exception as error: return HttpResponse(error, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
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 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 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 test_token_invalid_signature_error(self): with self.assertRaises(InvalidSignatureError): SystemService.verify_token(self.user.id, 'random token')
def test_correct_token(self): self.user.reload() self.assertTrue( SystemService.verify_token(self.user.id, self.user.token))
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)
def test_token_unknown_user_error(self): with self.assertRaises(UnknownUserError): SystemService.verify_token('604cb3cd6a900b4b4f95091d', self.user.token)