Answer the question
In order to leave comments, you need to log in
How to correctly update the data displayed by context'om in a timely manner?
All ku.
There is such a cbv
class StartFight(FightMixin,HeroEquipInfo, UserInfo, View):
def post(self, request, hero_unique_id, *args,**kwargs):
uid = request.user.id
logic_data = super(StartFight, self)
user_stat_list = logic_data.UserStat(uid)
context = {}
context = user_stat_list
it_stat = logic_data.GetItemStat(uid)
user_hero_power = it_stat['total_power']
en_list = UserActiveHero.objects.filter(hero_power__lte=user_hero_power)
context['enemy'] = en_list
fight_resultat = logic_data.fight_result(uid,hero_unique_id,user_stat_list)
context['fight_res'] = fight_resultat
return render(request,'fight.html',context)
class FightMixin(object):
def fight_result(self, uid, hero_unique_id, user_stat_list, *args, **kwargs):
#Статы юзера
user_hero = UserActiveHero.objects.filter(user_hero_id=uid)
#Достаем статы противника
enemy_hero = UserActiveHero.objects.filter(hero_unique_id=hero_unique_id)
if enemy_hero and user_hero:
#ОПЫТ СЕРЕБРО И ТП
silver_win_reward = 120
silver_loose_reward = 50
xp_win_reward = 25
xp_loose_reward = 10
if user_stat_list['xp_buff']:#:
xp_win_reward = xp_win_reward * 2
xp_loose_reward = xp_loose_reward * 2
fight_result_list ={'w_l':None,'xp':None,'silver':None,'drop_portal':None,'fight_color':None,'energy':None}
for i in enemy_hero:
enemy_power = i.hero_power
for u in user_hero:
user_power = u.hero_power
# ПОБЕДА
if user_power > enemy_power and user_stat_list['energy'] >=10:
#user win
fight_result_list['energy'] = True
fight_result_list['w_l'] = 'Победа!'
fight_result_list['fight_color'] = '#207C1C'
fight_result_list['xp'] = xp_win_reward
fight_result_list['silver'] = silver_win_reward
fight_result_list['drop_portal'] = False
#Добавляем победные статы
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+250,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
totalWin=F('totalWin')+1)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+20)
return fight_result_list
#НИЧЬЯ
elif user_power == enemy_power and user_stat_list['energy'] >=10:
res = random.randint(1,4)
if res == 1 or res == 2:
#user win
fight_result_list['energy'] = True
fight_result_list['w_l'] = 'Победа!'
fight_result_list['fight_color'] = '#207C1C'
fight_result_list['xp'] = xp_win_reward
fight_result_list['silver'] = silver_win_reward
fight_result_list['drop_portal'] = False
#Добавляем победные статы
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_win_reward,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
totalWin=F('totalWin')+1)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+xp_win_reward)
return fight_result_list
else: #Поражение
fight_result_list['energy'] = True
fight_result_list['w_l'] = 'Поражение'
fight_result_list['fight_color'] = '#CB4A3F'
fight_result_list['xp'] = xp_loose_reward
fight_result_list['silver'] = silver_loose_reward
fight_result_list['drop_portal'] = False
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_win_reward,energy=F('energy')-10,
)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+xp_win_reward)
return fight_result_list
#ПОРАЖЕНИЕ
elif user_power < enemy_power and user_stat_list['energy'] >=10:
fight_result_list['energy'] = True
fight_result_list['w_l'] = 'Поражение'
fight_result_list['fight_color'] = '#CB4A3F'
fight_result_list['xp'] = xp_loose_reward
fight_result_list['silver'] = silver_loose_reward
fight_result_list['drop_portal'] = False
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)
# вернуть лист со статусом и наградами
return fight_result_list
elif user_stat_list['energy'] <10:
fight_result_list['energy'] = False
return fight_result_list
else:
return False
elif user_power < enemy_power and user_stat_list['energy'] >=10:
fight_result_list['energy'] = True
fight_result_list['w_l'] = 'Поражение'
fight_result_list['fight_color'] = '#CB4A3F'
fight_result_list['xp'] = xp_loose_reward
fight_result_list['silver'] = silver_loose_reward
fight_result_list['drop_portal'] = False
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)
# вернуть лист со статусом и наградами
return fight_result_list
UserAttribute.objects.filter(user_id=uid).update(silver=F('silver')+silver_loose_reward,xp=F('xp')+xp_loose_reward,energy=F('energy')-10,)
UserActiveHero.objects.filter(user_hero_id=uid).update(hero_xp=F('hero_xp')+silver_loose_reward)
Answer the question
In order to leave comments, you need to log in
Use instead of QuerySet.update() to change object attributes and MyModel().save() (if I didn't misunderstand the question)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question