backend/app/users/services.py

51 lines
1.4 KiB
Python
Raw Normal View History

2022-10-08 17:14:19 +03:00
from random import shuffle
2022-10-08 20:13:21 +03:00
from django.conf import settings
from blockchain.services import transact_from_admin
2022-10-08 17:14:19 +03:00
from .models import Clan, User
import requests as r
import json
def end_season():
mx_value = -1
mx_clan = None
for clan in Clan.objects.all():
if sum(map(lambda user: user.respect, clan.users.all())) > mx_value:
mx_value = sum(map(lambda user: user.respect, clan.users.all()))
2022-10-08 17:14:19 +03:00
mx_clan = clan
for user in mx_clan.users.all():
transact_from_admin(user, 100)
2022-10-08 17:14:19 +03:00
Clan.objects.all().delete()
for user in User.objects.filter(type=User.WorkerType.WORKER):
if user.salary > 0:
transact_from_admin(user, user.salary)
2022-10-08 17:14:19 +03:00
def create_chat(clan: Clan):
user_list = list(map(lambda user: user.telegram, clan.users.all()))
2022-10-08 17:14:19 +03:00
if len(user_list):
2022-10-08 20:13:21 +03:00
r.post(
f"{settings.TELEGRAM_API}/create-chat",
data=json.dumps({"chat_name": clan.name, "users": user_list}),
headers={"accept": "application/json", "Content-Type": "application/json"},
2022-10-08 17:14:19 +03:00
)
def create_season():
if len(Clan.objects.all()):
end_season()
2022-10-08 20:13:21 +03:00
users = list(User.objects.filter(type=User.WorkerType.WORKER))
2022-10-08 17:14:19 +03:00
shuffle(users)
clan = None
for index, user in enumerate(users):
if index % 5 == 0:
2022-10-08 17:14:19 +03:00
clan = Clan.objects.create()
user.clan = clan
user.save()
for clan in Clan.objects.all():
2022-10-08 17:14:19 +03:00
create_chat(clan)