From ada4fe8deb75b4185dbb6c7da10876845912ef53 Mon Sep 17 00:00:00 2001 From: ilia Date: Sat, 8 Oct 2022 17:14:19 +0300 Subject: [PATCH] add clan seasons --- app/conf/api.py | 11 +++- app/users/api/serializers.py | 9 +++- app/users/api/views.py | 7 ++- app/users/migrations/0004_alter_user_clan.py | 19 +++++++ app/users/models.py | 2 +- app/users/service.py | 56 ++++++++++++++++++++ app/utils/blockchain.py | 2 - 7 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 app/users/migrations/0004_alter_user_clan.py create mode 100644 app/users/service.py diff --git a/app/conf/api.py b/app/conf/api.py index 3df281e..0c6be6c 100644 --- a/app/conf/api.py +++ b/app/conf/api.py @@ -1,8 +1,7 @@ from django.urls import path, include from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView - from marketplace.api.views import ListCreateProductApi, RetireUpdateDestroyProductApi -from users.api.views import ListCreateUserApi +from users.api.views import ListCreateUserApi, CreateSeasonApi urlpatterns = [ path( @@ -39,4 +38,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 1da84b7..b40a86a 100644 --- a/app/users/api/serializers.py +++ b/app/users/api/serializers.py @@ -1,5 +1,5 @@ from rest_framework import serializers - +from ..service import create_season from users.models import User @@ -26,3 +26,10 @@ class UserSerializer(serializers.ModelSerializer): **validated_data, username=validated_data["telegram"] ) return user + + +class CreateSeasonSerializer(serializers.Serializer): + created = serializers.BooleanField(read_only=True) + def create(self, *args, **kwargs): + create_season() + return {'created': True} diff --git a/app/users/api/views.py b/app/users/api/views.py index b00e165..d775bc5 100644 --- a/app/users/api/views.py +++ b/app/users/api/views.py @@ -2,7 +2,7 @@ from rest_framework import generics from rest_framework.permissions import IsAuthenticated from common.permissions import IsAdmin -from users.api.serializers import UserSerializer +from users.api.serializers import UserSerializer, CreateSeasonSerializer from users.models import User @@ -10,3 +10,8 @@ class ListCreateUserApi(generics.ListCreateAPIView): serializer_class = UserSerializer permission_classes = [IsAuthenticated, IsAdmin] queryset = User.objects.all() + + +class CreateSeasonApi(generics.CreateAPIView): + serializer_class = CreateSeasonSerializer + #permission_classes = [IsAuthenticated, IsAdmin] diff --git a/app/users/migrations/0004_alter_user_clan.py b/app/users/migrations/0004_alter_user_clan.py new file mode 100644 index 0000000..9d78fe3 --- /dev/null +++ b/app/users/migrations/0004_alter_user_clan.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0.8 on 2022-10-08 12:34 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0003_user_name_user_respect_user_telegram_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='user', + name='clan', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='users.clan'), + ), + ] diff --git a/app/users/models.py b/app/users/models.py index a8ddfc3..683589b 100644 --- a/app/users/models.py +++ b/app/users/models.py @@ -31,7 +31,7 @@ class User(AbstractUser): max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER ) salary = models.IntegerField(default=0) - clan = models.ForeignKey(Clan, on_delete=models.CASCADE, null=True) + clan = models.ForeignKey(Clan, on_delete=models.SET_NULL, null=True) 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, null=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'))