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
|
|
|
|
|
2022-10-09 02:24:31 +03:00
|
|
|
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():
|
2022-10-09 00:42:30 +03:00
|
|
|
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
|
2022-10-09 00:42:30 +03:00
|
|
|
for user in mx_clan.users.all():
|
2022-10-09 02:24:31 +03:00
|
|
|
transact_from_admin(user, 100)
|
2022-10-08 17:14:19 +03:00
|
|
|
Clan.objects.all().delete()
|
|
|
|
|
2022-10-09 02:24:31 +03:00
|
|
|
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):
|
2022-10-09 00:42:30 +03:00
|
|
|
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
|
|
|
|
2022-10-09 00:42:30 +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):
|
2022-10-09 00:42:30 +03:00
|
|
|
if index % 5 == 0:
|
2022-10-08 17:14:19 +03:00
|
|
|
clan = Clan.objects.create()
|
|
|
|
user.clan = clan
|
|
|
|
user.save()
|
2022-10-09 00:42:30 +03:00
|
|
|
for clan in Clan.objects.all():
|
2022-10-08 17:14:19 +03:00
|
|
|
create_chat(clan)
|