mirror of
				https://github.com/more-tech4-magnum-opus/backend.git
				synced 2025-11-04 01:27:35 +03:00 
			
		
		
		
	Merge pull request #3 from more-tech4-magnum-opus/clans
add clan seasons
This commit is contained in:
		
						commit
						31a7b70c84
					
				| 
						 | 
				
			
			@ -1,7 +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, CreateSeasonApi
 | 
			
		||||
from users.api.views import (
 | 
			
		||||
    ListCreateUserApi,
 | 
			
		||||
    RetireUpdateDeleteUserApi,
 | 
			
		||||
| 
						 | 
				
			
			@ -88,4 +88,12 @@ urlpatterns = [
 | 
			
		|||
            ]
 | 
			
		||||
        ),
 | 
			
		||||
    ),
 | 
			
		||||
    path(
 | 
			
		||||
        'create_season/',
 | 
			
		||||
        include(
 | 
			
		||||
            [
 | 
			
		||||
                path("", CreateSeasonApi.as_view(), name='create new season')
 | 
			
		||||
            ]
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										56
									
								
								app/users/service.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								app/users/service.py
									
									
									
									
									
										Normal 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)
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
| 
						 | 
				
			
			@ -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'))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user