mercredi 26 août 2020

Is there anyway to run a game irrespective of reloding page in django

I need a way to run this number game irrespective of request. It should accept the forms submitted from this same page. I found that js clock and backend clock are not synchronized so I have started reloading this game tab for each second. So that time modules are creating newly after every refresh. And another problem is when a new user visits this page it will create time modules newly which ruin this game. So please suggest me a better way to do this.

views.py

class NumberSection(ListView):
    template_name = "gamepage.html"        
    def get(self, *args, **kwargs):
        context = {}        

        silver_created=False
        if not silver_created:
            silver_end,silver_gold_buffer,silver_gold_new,silver_gold_correct,silver_gold_start = create_time_modules()
            self.silver = True
            silver_game = NumbersGame.objects.create(mode='silver')
            self.silver_game_id = silver_game.id
            context['silver_status'] = 'accepted'
            silver_created =True
        if silver_created:                        
            if silver_end<=datetime.now():
                self.silver = False
                context['silver_status'] = 'blocked'

            if silver_gold_buffer<=datetime.now():
                pass
            if silver_gold_new<=datetime.now():
                silver_created = False
            while True:
                silver_x = silver_gold_correct - silver_gold_start                
                context["silver_minutes"]=(silver_x.seconds//60)%60
                context["silver_seconds"]=silver_x.seconds%60
        return render(self.request,self.template_name,context=context)

    def post(self, *Args, **kwargs):
        name = self.request.POST.get("choosen")
        amt = self.request.POST.get("total")
        if name == 'silver' and self.silver:
            hist = History.objects.create(user=self.request.self.user.userprofile,investment=amt,color_num_selected=name)
            hist.id_made = self.silver_game_id
            hist.save()
        return redirect('core:next')



Aucun commentaire:

Enregistrer un commentaire