<li><aclass="reference internal"href="#activating-your-new-character-generation">Activating your new character generation</a></li>
<li><aclass="reference internal"href="#adding-a-no-capcha-recapcha-on-your-character-generation">Adding a no CAPCHA reCAPCHA on your character generation</a><ul>
<li><aclass="reference internal"href="#step-1-obtain-a-sitekey-and-secret-from-google">Step 1: Obtain a SiteKey and secret from Google</a></li>
<li><aclass="reference internal"href="#step-2-installing-and-configuring-the-dedicated-django-app">Step 2: installing and configuring the dedicated Django app</a></li>
<li><aclass="reference internal"href="#step-3-adding-the-capcha-to-our-form">Step 3: Adding the CAPCHA to our form</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<pclass="topless"><ahref="Web-Add-a-wiki.html"
title="previous chapter">Add a wiki on your website</a></p>
<p>This tutorial will create a simple web-based interface for generating a new in-game Character. Accounts will need to have first logged into the website (with their <codeclass="docutils literal notranslate"><spanclass="pre">AccountDB</span></code> account). Once finishing character generation the Character will be created immediately and the Accounts can then log into the game and play immediately (the Character will not require staff approval or anything like that). This guide does not go over how to create an AccountDB on the website with the right permissions to transfer to their web-created characters.</p>
<p>It is probably most useful to set <codeclass="docutils literal notranslate"><spanclass="pre">AUTO_CREATE_CHARACTER_WITH_ACCOUNT</span><spanclass="pre">=</span><spanclass="pre">False</span></code> so that all player characters can be created through this.</p>
<p>You should have some familiarity with how Django sets up its Model Template View framework. You need to understand what is happening in the basic <aclass="reference internal"href="Web-Character-View-Tutorial.html"><spanclass="doc std std-doc">Web Character View tutorial</span></a>. If you don’t understand the listed tutorial or have a grasp of Django basics, please look at the <aclass="reference external"href="https://docs.djangoproject.com/en/4.1/intro/">Django tutorial</a> to get a taste of what Django does, before throwing Evennia into the mix (Evennia shares its API and attributes with the website interface). This guide will outline the format of the models, views, urls, and html templates needed.</p>
<h2>Pictures<aclass="headerlink"href="#pictures"title="Permalink to this headline">¶</a></h2>
<p>Here are some screenshots of the simple app we will be making.</p>
<p>Index page, with no character application yet done:</p>
<hrclass="docutils"/>
<p><imgalt="Index page, with no character application yet done."src="https://lh3.googleusercontent.com/-57KuSWHXQ_M/VWcULN152tI/AAAAAAAAEZg/kINTmVlHf6M/w425-h189-no/webchargen_index2.gif"/></p>
<p>Having clicked the “create” link you get to create your character (here we will only have name and background, you can add whatever is needed to fit your game):</p>
<p><imgalt="Having entered an application."src="https://lh6.googleusercontent.com/-HlxvkvAimj4/VWcUKjFxEiI/AAAAAAAAEZo/gLppebr05JI/w321-h194-no/webchargen_index1.gif"/></p>
<p><imgalt="Detail view of character application."src="https://lh6.googleusercontent.com/-2m1UhSE7s_k/VWcUKfLRfII/AAAAAAAAEZc/UFmBOqVya4k/w267-h175-no/webchargen_detail.gif"/></p>
</section>
<hrclass="docutils"/>
<sectionid="installing-an-app">
<h2>Installing an App<aclass="headerlink"href="#installing-an-app"title="Permalink to this headline">¶</a></h2>
<p>Assuming your game is named “mygame”, navigate to your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/</span></code> directory, and type:</p>
<p>This will initialize a new Django app we choose to call “chargen” in <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/</span></code>. We put it under <codeclass="docutils literal notranslate"><spanclass="pre">web/</span></code> to keep all web stuff together, but you can organize however you like. It is directory containing some basic starting things Django needs.</p>
<p>Next, navigate to <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf/settings.py</span></code> and add or edit the following line to make Evennia (and Django) aware of our new app:</p>
<em>views</em> (the server-side website content generators), <em>urls</em> (how the web browser finds the pages) and <em>templates</em> (how the web page should be structured).</p>
<h3>Installing - Checkpoint:<aclass="headerlink"href="#installing-checkpoint"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><p>you should have a folder named <codeclass="docutils literal notranslate"><spanclass="pre">chargen</span></code> or whatever you chose in your mygame/web/ directory</p></li>
<li><p>you should have your application name added to your INSTALLED_APPS in <aclass="reference external"href="http://settings.py">settings.py</a></p></li>
</ul>
</section>
</section>
<sectionid="create-models">
<h2>Create Models<aclass="headerlink"href="#create-models"title="Permalink to this headline">¶</a></h2>
<p>Models are created in <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen/models.py</span></code>.</p>
<p>A <aclass="reference internal"href="../Concepts/Models.html"><spanclass="doc std std-doc">Django database model</span></a> is a Python class that describes the database storage of the
data you want to manage. Any data you choose to store is stored in the same database as the game and you have access to all the game’s objects here.</p>
<p>We need to define what a character application actually is. This will differ from game to game so for this tutorial we will define a simple character sheet with the following database fields:</p>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">app_id</span></code> (AutoField): Primary key for this character application sheet.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">char_name</span></code> (CharField): The new character’s name.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">date_applied</span></code> (DateTimeField): Date that this application was received.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">background</span></code> (TextField): Character story background.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">account_id</span></code> (IntegerField): Which account ID does this application belong to? This is an
AccountID from the AccountDB object.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">submitted</span></code> (BooleanField): <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>/<codeclass="docutils literal notranslate"><spanclass="pre">False</span></code> depending on if the application has been submitted yet.</p></li>
<p>You should consider how you are going to link your application to your account. For this tutorial, we are using the account_id attribute on our character application model in order to keep track of which characters are owned by which accounts. Since the account id is a primary key in Evennia, it is a good candidate, as you will never have two of the same IDs in Evennia. You can feel free to use anything else, but for the purposes of this guide, we are going to use account ID to join the character applications with the proper account.</p>
<li><p>you should have filled out <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen/models.py</span></code> with the model class shown above (eventually adding fields matching what you need for your game).</p></li>
<p><em>Views</em> are server-side constructs that make dynamic data available to a web page. We are going to add them to <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen.views.py</span></code>. Each view in our example represents the backbone of a
specific web page. We will use three views and three pages here:</p>
<ulclass="simple">
<li><p>The index (managing <codeclass="docutils literal notranslate"><spanclass="pre">index.html</span></code>). This is what you see when you navigate to
<li><p>The detail display sheet (manages <codeclass="docutils literal notranslate"><spanclass="pre">detail.html</span></code>). A page that passively displays the stats of a given Character.</p></li>
<li><p>Character creation sheet (manages <codeclass="docutils literal notranslate"><spanclass="pre">create.html</span></code>). This is the main form with fields to fill in.</p></li>
</ul>
<sectionid="index-view">
<h3><em>Index</em> view<aclass="headerlink"href="#index-view"title="Permalink to this headline">¶</a></h3>
<p>Let’s get started with the index first.</p>
<p>We’ll want characters to be able to see their created characters so let’s</p>
<spanclass="n">current_user</span><spanclass="o">=</span><spanclass="n">request</span><spanclass="o">.</span><spanclass="n">user</span><spanclass="c1"># current user logged in</span>
<spanclass="n">p_id</span><spanclass="o">=</span><spanclass="n">current_user</span><spanclass="o">.</span><spanclass="n">id</span><spanclass="c1"># the account id</span>
<spanclass="c1"># submitted Characters by this account</span>
<p>Our detail page will have pertinent character application information our users can see. Since this is a basic demonstration, our detail page will only show two fields:</p>
<p>We will use the account ID again just to double-check that whoever tries to check our character page is actually the account who owns the application.</p>
<p>Predictably, our <em>create</em> function will be the most complicated of the views, as it needs to accept information from the user, validate the information, and send the information to the server. Once the form content is validated will actually create a playable Character.</p>
<p>The form itself we will define first. In our simple example we are just looking for the Character’s name and background. This form we create in <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen/forms.py</span></code>:</p>
<spanclass="c1"># add the right locks for the character so the account can</span>
<spanclass="c1"># puppet it</span>
<spanclass="n">char</span><spanclass="o">.</span><spanclass="n">locks</span><spanclass="o">.</span><spanclass="n">add</span><spanclass="p">(</span><spanclass="s2">" or "</span><spanclass="o">.</span><spanclass="n">join</span><spanclass="p">([</span>
<spanclass="n">char</span><spanclass="o">.</span><spanclass="n">db</span><spanclass="o">.</span><spanclass="n">background</span><spanclass="o">=</span><spanclass="n">background</span><spanclass="c1"># set the character background</span>
<div><p>Note also that we basically create the character using the Evennia API, and we grab the proper permissions from the <codeclass="docutils literal notranslate"><spanclass="pre">AccountDB</span></code> object and copy them to the character object. We take the user permissions attribute and turn that list of strings into a string object in order for the create_object function to properly process the permissions.</p>
<p>Most importantly, the following attributes must be set on the created character object:</p>
<ulclass="simple">
<li><p>Evennia <aclass="reference internal"href="../Components/Permissions.html"><spanclass="doc std std-doc">permissions</span></a> (copied from the <codeclass="docutils literal notranslate"><spanclass="pre">AccountDB</span></code>).</p></li>
<li><p>The right <codeclass="docutils literal notranslate"><spanclass="pre">puppet</span></code><aclass="reference internal"href="../Components/Locks.html"><spanclass="doc std std-doc">locks</span></a> so the Account can actually play as this Character later.</p></li>
<li><p>The relevant Character <aclass="reference internal"href="../Components/Typeclasses.html"><spanclass="doc std std-doc">typeclass</span></a></p></li>
<li><p>Character name (key)</p></li>
<li><p>The Character’s home room location (<codeclass="docutils literal notranslate"><spanclass="pre">#2</span></code> by default)</p></li>
<p>Other attributes are strictly speaking optional, such as the <codeclass="docutils literal notranslate"><spanclass="pre">background</span></code> attribute on our character. It may be a good idea to decompose this function and create a separate _create_character function in order to set up your character object the account owns. But with the Evennia API, setting custom attributes is as easy as doing it in the meat of your Evennia game directory.</p>
<p>After all of this, our <codeclass="docutils literal notranslate"><spanclass="pre">views.py</span></code> file should look like something like this:</p>
<spanclass="n">current_user</span><spanclass="o">=</span><spanclass="n">request</span><spanclass="o">.</span><spanclass="n">user</span><spanclass="c1"># current user logged in</span>
<spanclass="n">p_id</span><spanclass="o">=</span><spanclass="n">current_user</span><spanclass="o">.</span><spanclass="n">id</span><spanclass="c1"># the account id</span>
<spanclass="c1"># submitted apps under this account</span>
<spanclass="c1"># add the right locks for the character so the account can</span>
<spanclass="c1"># puppet it</span>
<spanclass="n">char</span><spanclass="o">.</span><spanclass="n">locks</span><spanclass="o">.</span><spanclass="n">add</span><spanclass="p">(</span><spanclass="s2">" or "</span><spanclass="o">.</span><spanclass="n">join</span><spanclass="p">([</span>
<spanclass="n">char</span><spanclass="o">.</span><spanclass="n">db</span><spanclass="o">.</span><spanclass="n">background</span><spanclass="o">=</span><spanclass="n">background</span><spanclass="c1"># set the character background</span>
<h3>Create Views - Checkpoint:<aclass="headerlink"href="#create-views-checkpoint"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><p>you’ve defined a <codeclass="docutils literal notranslate"><spanclass="pre">views.py</span></code> that has an index, detail, and creating functions.</p></li>
<li><p>you’ve defined a <aclass="reference external"href="http://forms.py">forms.py</a> with the <codeclass="docutils literal notranslate"><spanclass="pre">AppForm</span></code> class needed by the <codeclass="docutils literal notranslate"><spanclass="pre">creating</span></code> function of <codeclass="docutils literal notranslate"><spanclass="pre">views.py</span></code>.</p></li>
<li><p>your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen</span></code> directory should now have a <codeclass="docutils literal notranslate"><spanclass="pre">views.py</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">forms.py</span></code> file</p></li>
</ul>
</section>
</section>
<sectionid="create-urls">
<h2>Create URLs<aclass="headerlink"href="#create-urls"title="Permalink to this headline">¶</a></h2>
<p>URL patterns helps redirect requests from the web browser to the right views. These patterns are created in <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen/urls.py</span></code>.</p>
<p>You could change the format as you desire. To make it more secure, you could remove app_id from the “detail” url, and instead just fetch the account’s applications using a unifying field like account_id to find all the character application objects to display.</p>
<p>To add this to our website, we must also update the main <codeclass="docutils literal notranslate"><spanclass="pre">mygame/website/urls.py</span></code> file; this will help tying our new chargen app in with the rest of the website. <codeclass="docutils literal notranslate"><spanclass="pre">urlpatterns</span></code> variable, and change it to include:</p>
<h3>URLs - Checkpoint:<aclass="headerlink"href="#urls-checkpoint"title="Permalink to this headline">¶</a></h3>
<ulclass="simple">
<li><p>You’ve created a <aclass="reference external"href="http://urls.py">urls.py</a> file in the <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen</span></code> directory</p></li>
<li><p>You have edited the main <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/urls.py</span></code> file to include urls to the <codeclass="docutils literal notranslate"><spanclass="pre">chargen</span></code> directory.</p></li>
</ul>
</section>
</section>
<sectionid="html-templates">
<h2>HTML Templates<aclass="headerlink"href="#html-templates"title="Permalink to this headline">¶</a></h2>
<p>So we have our url patterns, views, and models defined. Now we must define our HTML templates that the actual user will see and interact with. For this tutorial we us the basic <em>prosimii</em> template that comes with Evennia.</p>
<p>Take note that we use <codeclass="docutils literal notranslate"><spanclass="pre">user.is_authenticated</span></code> to make sure that the user cannot create a character without logging in.</p>
<p>These files will all go into the <codeclass="docutils literal notranslate"><spanclass="pre">/mygame/web/chargen/templates/chargen/</span></code> directory.</p>
<sectionid="index-html">
<h3>index.html<aclass="headerlink"href="#index-html"title="Permalink to this headline">¶</a></h3>
<p>This HTML template should hold a list of all the applications the account currently has active. For this demonstration, we will only list the applications that the account has submitted. You could easily adjust this to include saved applications, or other types of applications if you have different kinds.</p>
<p>Please refer back to <codeclass="docutils literal notranslate"><spanclass="pre">views.py</span></code> to see where we define the variables these templates make use of.</p>
<spanclass="p"><</span><spanclass="nt">p</span><spanclass="p">></span>You haven't submitted any character applications.<spanclass="p"></</span><spanclass="nt">p</span><spanclass="p">></span>
<p>This page should show a detailed character sheet of their application. This will only show their name and character background. You will likely want to extend this to show many more fields for your game. In a full-fledged character generation, you may want to extend the boolean attribute of submitted to allow accounts to save character applications and submit them later.</p>
<spanclass="p"><</span><spanclass="nt">p</span><spanclass="p">></span>You didn't submit this character.<spanclass="p"></</span><spanclass="nt">p</span><spanclass="p">></span>
<p>Our create HTML template will use the Django form we defined back in <aclass="reference external"href="http://views.py/forms.py">views.py/forms.py</a> to drive the majority of the application process. There will be a form input for every field we defined in <aclass="reference external"href="http://forms.py">forms.py</a>, which is handy. We have used POST as our method because we are sending information to the server that will update the database. As an alternative, GET would be much less secure. You can read up on documentation elsewhere on the web for GET vs. POST.</p>
<li><p>Create a <codeclass="docutils literal notranslate"><spanclass="pre">index.html</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">detail.html</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">create.html</span></code> template in your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/web/chargen/templates/chargen</span></code> directory</p></li>
<h2>Activating your new character generation<aclass="headerlink"href="#activating-your-new-character-generation"title="Permalink to this headline">¶</a></h2>
<p>After finishing this tutorial you should have edited or created the following files:</p>
<p>This will create and update the models. If you see any errors at this stage, read the traceback carefully, it should be relatively easy to figure out where the error is.</p>
<p>Login to the website (you need to have previously registered an Player account with the game to do this). Next you navigate to <codeclass="docutils literal notranslate"><spanclass="pre">http://yourwebsite.com/chargen</span></code> (if you are running locally this will be something like <codeclass="docutils literal notranslate"><spanclass="pre">http://localhost:4001/chargen</span></code> and you will see your new app in action.</p>
<p>This should hopefully give you a good starting point in figuring out how you’d like to approach your own web generation. The main difficulties are in setting the appropriate settings on your newly created character object. Thankfully, the Evennia API makes this easy.</p>
<h2>Adding a no CAPCHA reCAPCHA on your character generation<aclass="headerlink"href="#adding-a-no-capcha-recapcha-on-your-character-generation"title="Permalink to this headline">¶</a></h2>
<p>As sad as it is, if your server is open to the web, bots might come to visit and take advantage of your open form to create hundreds, thousands, millions of characters if you give them the opportunity. This section shows you how to use the <aclass="reference external"href="https://www.google.com/recaptcha/intro/invisible.html">No CAPCHA
reCAPCHA</a> designed by Google. Not only is it easy to use, it is user-friendly… for humans. A simple checkbox to check, except if Google has some suspicion, in which case you will have a more difficult test with an image and the usual text inside. It’s worth pointing out that, as long as Google doesn’t suspect you of being a robot, this is quite useful, not only for common users, but to screen-reader users, to which reading inside of an image is pretty difficult, if not impossible. And to top it all, it will be so easy to add in your website.</p>
<h3>Step 1: Obtain a SiteKey and secret from Google<aclass="headerlink"href="#step-1-obtain-a-sitekey-and-secret-from-google"title="Permalink to this headline">¶</a></h3>
<p>The first thing is to ask Google for a way to safely authenticate your website to their service. To do it, we need to create a site key and a secret. Go to <aclass="reference external"href="https://www.google.com/recaptcha/admin">https://www.google.com/recaptcha/admin</a> to create such a site key. It’s quite easy when you have a Google account.</p>
<p>When you have created your site key, save it safely. Also copy your secret key as well. You should find both information on the web page. Both would contain a lot of letters and figures.</p>
<h3>Step 2: installing and configuring the dedicated Django app<aclass="headerlink"href="#step-2-installing-and-configuring-the-dedicated-django-app"title="Permalink to this headline">¶</a></h3>
<p>Since Evennia runs on Django, the easiest way to add our CAPCHA and perform the proper check is to install the dedicated Django app. Quite easy:</p>
<p>And add it to the installed apps in your settings. In your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf/settings.py</span></code>, you might have something like this:</p>
<p>Finally we have to add the CAPCHA to our form. It will be pretty easy too. First, open your <codeclass="docutils literal notranslate"><spanclass="pre">web/chargen/forms.py</span></code> file. We’re going to add a new field, but hopefully, all the hard work has been done for us. Update at your convenience, You might end up with something like this:</p>
<p>And you should put it at the bottom of the page. Just before the closing body would be good, but for the time being, the base page doesn’t provide a footer block, so we’ll put it in the content block. Note that it’s not the best place, but it will work. In the end, your
<p>Reload and open <aclass="reference external"href="http://localhost:4001/chargen/create/">http://localhost:4001/chargen/create</a> and you should see your beautiful CAPCHA just before the “submit” button. Try not to check the checkbox to see what happens. And do the same while checking the checkbox!</p>