2019-01-23 23:53:13 +01:00
|
|
|
from django import forms
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2019-01-23 21:19:50 +01:00
|
|
|
from django.contrib.auth.models import User
|
2019-01-20 16:48:54 +01:00
|
|
|
from django.shortcuts import render, render_to_response
|
2019-01-23 23:53:13 +01:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2019-01-20 16:48:54 +01:00
|
|
|
from django.views import generic
|
2019-01-20 22:24:01 +01:00
|
|
|
from django.views.generic.edit import CreateView, FormView
|
2019-01-20 16:48:54 +01:00
|
|
|
# Create your views here.
|
2019-01-23 23:53:13 +01:00
|
|
|
from accounts.forms import CreateCharacterForm
|
|
|
|
from accounts.models import Accounts, Players, Towns
|
|
|
|
from accounts.utils import LoginHelper, VocationHelper
|
2019-01-20 16:48:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RegisterAccountView(CreateView):
|
|
|
|
model = Accounts
|
|
|
|
fields = ['name', 'password', 'email']
|
|
|
|
template_name = 'register_account.html'
|
|
|
|
|
2019-02-10 14:17:26 +01:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super(RegisterAccountView, self).get_form(form_class)
|
|
|
|
form.fields['password'].widget = forms.PasswordInput()
|
|
|
|
return form
|
|
|
|
|
2019-01-20 16:48:54 +01:00
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
|
2019-01-23 21:19:50 +01:00
|
|
|
# create django user
|
|
|
|
u, created = User.objects.get_or_create(
|
|
|
|
username=self.object.name,
|
|
|
|
email=self.object.email,
|
|
|
|
)
|
|
|
|
if created:
|
|
|
|
u.set_password(self.object.password)
|
|
|
|
u.save()
|
|
|
|
# create tibia account with sha1 hashed password
|
|
|
|
hashed_password = LoginHelper.hash_password(self.object.password)
|
|
|
|
self.object.password = hashed_password
|
|
|
|
self.object.linked_django_user = u
|
|
|
|
self.object.save()
|
2019-01-20 16:48:54 +01:00
|
|
|
return render_to_response(self.template_name, self.get_context_data())
|
|
|
|
|
|
|
|
class IndexView(generic.ListView):
|
|
|
|
template_name = 'index.html'
|
|
|
|
context_object_name = 'best_players'
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2019-01-23 23:53:13 +01:00
|
|
|
return Players.objects.filter(group_id=1).order_by('-level').values('name', 'level')[:10]
|
|
|
|
|
|
|
|
class PlayerView(LoginRequiredMixin, generic.ListView):
|
|
|
|
template_name = 'players.html'
|
|
|
|
context_object_name = 'players'
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
players = Players.objects.filter(account__linked_django_user=self.request.user)\
|
|
|
|
.values('name', 'level', 'vocation')
|
|
|
|
return players
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PlayerView, self).get_context_data(**kwargs)
|
|
|
|
players = context['players']
|
|
|
|
context['players'] = [{
|
|
|
|
'name': p['name'],
|
|
|
|
'level': p['level'],
|
|
|
|
'vocation': VocationHelper.vocation_to_string(p['vocation'])}
|
|
|
|
for p in players]
|
|
|
|
return context
|
|
|
|
|
|
|
|
class CreateCharView(LoginRequiredMixin, FormView):
|
|
|
|
form_class = CreateCharacterForm
|
|
|
|
template_name = 'add_character.html'
|
|
|
|
success_url = reverse_lazy('accounts:characters')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2019-01-27 13:39:49 +01:00
|
|
|
rookgaard = Towns.objects.get(name='Rookgaard Tutorial Island')
|
2019-01-23 23:53:13 +01:00
|
|
|
start_health = 150
|
|
|
|
start_mana = 55
|
|
|
|
start_cap = 400
|
|
|
|
user = self.request.user
|
|
|
|
account = Accounts.objects.get(linked_django_user=user)
|
|
|
|
player = Players.objects.create(
|
|
|
|
name=form.cleaned_data['name'],
|
|
|
|
level=1,
|
2019-01-27 13:39:49 +01:00
|
|
|
#vocation=int(form.cleaned_data['vocation']),
|
|
|
|
vocation=0,
|
2019-01-23 23:53:13 +01:00
|
|
|
sex=int(form.cleaned_data['sex']),
|
|
|
|
posx=rookgaard.posx,
|
|
|
|
posy=rookgaard.posy,
|
|
|
|
posz=rookgaard.posz,
|
|
|
|
health=start_health,
|
|
|
|
healthmax=start_health,
|
|
|
|
mana=start_mana,
|
|
|
|
manamax=start_mana,
|
|
|
|
cap=start_cap,
|
|
|
|
account_id=account.id,
|
2019-01-24 12:28:38 +01:00
|
|
|
town_id=rookgaard.id,
|
2019-01-23 23:53:13 +01:00
|
|
|
)
|
|
|
|
return super(CreateCharView, self).form_valid(form)
|
|
|
|
|