diff --git a/app/conf/api.py b/app/conf/api.py index db397cd..cd6e7e6 100644 --- a/app/conf/api.py +++ b/app/conf/api.py @@ -1,8 +1,8 @@ from django.urls import path, include from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView -from events.api.views import ListCreateEventApi, RetireUpdateDeleteEventApi from marketplace.api.views import ListCreateProductApi, RetireUpdateDestroyProductApi +from users.api.views import ListCreateUserApi, CreateSeasonApi from users.api.views import ( ListCreateUserApi, RetireUpdateDeleteUserApi, @@ -102,4 +102,12 @@ urlpatterns = [ ] ), ), + path( + 'create_season/', + include( + [ + path("", CreateSeasonApi.as_view(), name='create new season') + ] + ) + ) ] diff --git a/app/users/api/serializers.py b/app/users/api/serializers.py index d534f55..5cc984d 100644 --- a/app/users/api/serializers.py +++ b/app/users/api/serializers.py @@ -1,5 +1,6 @@ from rest_framework import serializers - +from ..service import create_season +from users.models import User from users.models import User, Department, Stream, Command @@ -31,6 +32,13 @@ class UserSerializer(serializers.ModelSerializer): return user +class CreateSeasonSerializer(serializers.Serializer): + created = serializers.BooleanField(read_only=True) + def create(self, *args, **kwargs): + create_season() + return {'created': True} + + class CommandSerializer(serializers.ModelSerializer): workers = UserSerializer(many=True) diff --git a/app/users/api/views.py b/app/users/api/views.py index 817a1d1..78a19f6 100644 --- a/app/users/api/views.py +++ b/app/users/api/views.py @@ -3,6 +3,8 @@ from rest_framework.generics import get_object_or_404 from rest_framework.permissions import IsAuthenticated from common.permissions import IsAdmin +from users.api.serializers import UserSerializer, CreateSeasonSerializer +from users.models import User from users.api.serializers import ( UserSerializer, DepartmentSerializer, @@ -18,6 +20,10 @@ class ListCreateUserApi(generics.ListCreateAPIView): queryset = User.objects.all() +class CreateSeasonApi(generics.CreateAPIView): + serializer_class = CreateSeasonSerializer + #permission_classes = [IsAuthenticated, IsAdmin] + class RetireUpdateDeleteUserApi(generics.RetrieveUpdateDestroyAPIView): def get_object(self): user = get_object_or_404( diff --git a/app/users/models.py b/app/users/models.py index e904b01..2571d4e 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -20,10 +20,11 @@ class User(AbstractUser): type = models.CharField( max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER ) + salary = models.IntegerField(default=0) + clan = models.ForeignKey(Clan, on_delete=models.SET_NULL, null=True) command = models.ForeignKey( "users.Command", related_name="workers", on_delete=models.CASCADE ) - salary = models.IntegerField(default=0, validators=[MinValueValidator(0)]) respect = models.IntegerField(default=0, validators=[MinValueValidator(0)]) wallet_private_key = models.CharField(max_length=96, unique=True) diff --git a/app/users/service.py b/app/users/service.py new file mode 100644 index 0000000..082b561 --- /dev/null +++ b/app/users/service.py @@ -0,0 +1,56 @@ +from random import shuffle +from .models import Clan, User +from utils.blockchain import transfer_rubbles +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.user_set.all())) > mx_value: + mx_value = sum(map(lambda user: user.respect, clan.user_set)) + mx_clan = clan + for user in mx_clan.user_set.all(): + transfer_rubbles("46d3684932f300a7fcdc8cc73cfa3057b5f61695c6e0299a5cb551f645e4cb9c", user.wallet_public_key, 100) + Clan.objects.all().delete() + + +def create_chat(clan: Clan): + user_list = list( + map(lambda user: user.telegram, clan.user_set.all()) + ) + if len(user_list): + r.post('https://tender-badgers-eat-178-71-165-37.loca.lt/create-chat', data=json.dumps( + { + 'chat_name': clan.name, + 'users': user_list + } + ), + headers={ + 'accept': 'application/json', + 'Content-Type': 'application/json' + } + ) + + +def create_season(): + if len(Clan.objects.all()): + end_season() + + users = list(User.objects.all()) + shuffle(users) + clan = None + for index, user in enumerate(users): + print(index, len(users)) + if (index % 10 == 0) or (index == len(users)-1): + if clan is not None: + create_chat(clan) + clan = Clan.objects.create() + user.clan = clan + user.save() + if len(users) % 10 != 0: + create_chat(clan) + + \ No newline at end of file diff --git a/app/utils/blockchain.py b/app/utils/blockchain.py index 944a64e..8cca582 100644 --- a/app/utils/blockchain.py +++ b/app/utils/blockchain.py @@ -128,5 +128,3 @@ def transfer_nft(my_private_wallet_key: str, transfer_publick_key: str, token_id def get_balance(my_public_wallet_key: str) -> WalletBalance: res = r.get(URL+f'/v1/wallets/{my_public_wallet_key}/balance') return WalletBalance(matic=res.json()['maticAmount'], coins=res.json()['coinsAmount']) - -print(get_balance('0x1a63208e5b82588320a8C24b2c595Ba5d5cbfF3f'))