add clan seasons

This commit is contained in:
ilia 2022-10-08 17:14:19 +03:00
parent 779648b535
commit ada4fe8deb
7 changed files with 99 additions and 7 deletions

View File

@ -1,8 +1,7 @@
from django.urls import path, include from django.urls import path, include
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from marketplace.api.views import ListCreateProductApi, RetireUpdateDestroyProductApi from marketplace.api.views import ListCreateProductApi, RetireUpdateDestroyProductApi
from users.api.views import ListCreateUserApi from users.api.views import ListCreateUserApi, CreateSeasonApi
urlpatterns = [ urlpatterns = [
path( path(
@ -39,4 +38,12 @@ urlpatterns = [
] ]
), ),
), ),
path(
'create_season/',
include(
[
path("", CreateSeasonApi.as_view(), name='create new season')
]
)
)
] ]

View File

@ -1,5 +1,5 @@
from rest_framework import serializers from rest_framework import serializers
from ..service import create_season
from users.models import User from users.models import User
@ -26,3 +26,10 @@ class UserSerializer(serializers.ModelSerializer):
**validated_data, username=validated_data["telegram"] **validated_data, username=validated_data["telegram"]
) )
return user return user
class CreateSeasonSerializer(serializers.Serializer):
created = serializers.BooleanField(read_only=True)
def create(self, *args, **kwargs):
create_season()
return {'created': True}

View File

@ -2,7 +2,7 @@ from rest_framework import generics
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from common.permissions import IsAdmin from common.permissions import IsAdmin
from users.api.serializers import UserSerializer from users.api.serializers import UserSerializer, CreateSeasonSerializer
from users.models import User from users.models import User
@ -10,3 +10,8 @@ class ListCreateUserApi(generics.ListCreateAPIView):
serializer_class = UserSerializer serializer_class = UserSerializer
permission_classes = [IsAuthenticated, IsAdmin] permission_classes = [IsAuthenticated, IsAdmin]
queryset = User.objects.all() queryset = User.objects.all()
class CreateSeasonApi(generics.CreateAPIView):
serializer_class = CreateSeasonSerializer
#permission_classes = [IsAuthenticated, IsAdmin]

View File

@ -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'),
),
]

View File

@ -31,7 +31,7 @@ class User(AbstractUser):
max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER max_length=6, choices=WorkerType.choices, default=WorkerType.WORKER
) )
salary = models.IntegerField(default=0) 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)]) salary = models.IntegerField(default=0, validators=[MinValueValidator(0)])
respect = 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) wallet_private_key = models.CharField(max_length=96, unique=True, null=True)

56
app/users/service.py Normal file
View File

@ -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)

View File

@ -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: def get_balance(my_public_wallet_key: str) -> WalletBalance:
res = r.get(URL+f'/v1/wallets/{my_public_wallet_key}/balance') res = r.get(URL+f'/v1/wallets/{my_public_wallet_key}/balance')
return WalletBalance(matic=res.json()['maticAmount'], coins=res.json()['coinsAmount']) return WalletBalance(matic=res.json()['maticAmount'], coins=res.json()['coinsAmount'])
print(get_balance('0x1a63208e5b82588320a8C24b2c595Ba5d5cbfF3f'))