Drop deprecation filters for pip and yamllint
[mudpy.git] / doc / source / coder.rst
1 =============
2  coder guide
3 =============
4
5 .. Copyright (c) 2004-2021 mudpy authors. Permission to use, copy,
6    modify, and distribute this software is granted under terms
7    provided in the LICENSE file distributed with this software.
8
9 This guide attempts to embody a rudimentary set of rules for developer
10 submissions of source code and documentation targeted for inclusion
11 within the mudpy project, as well as pointers to useful resources for
12 those attempting to obtain a greater understanding of the software.
13
14 source
15 ------
16
17 As with any project, the mudpy source code could always be better
18 documented, and contributions to that end are heartily welcomed.
19
20 version control system
21 ~~~~~~~~~~~~~~~~~~~~~~
22
23 Git_ is used for version control on the project, and the archive can
24 be browsed or cloned anonymously from the official
25 https://mudpy.org/code/mudpy repository location. For now, detailed
26 commits can be E-mailed to fungi@yuggoth.org, but there will most
27 likely be a developer mailing list for more open presentation and
28 discussion of patches eventually.
29
30 A :file:`ChangeLog` is generated automatically from repository
31 commit logs, and is included automatically in all sdist_ tarballs. It
32 can be regenerated easily by running :command:`tox -e dist` from the
33 top level directory of the Git repository in a working `developer
34 environment`_.
35
36 .. _Git: https://git-scm.com/
37 .. _sdist: https://packaging.python.org/glossary
38            /#term-source-distribution-or-sdist
39
40 developer environment
41 ~~~~~~~~~~~~~~~~~~~~~
42
43 Basic developer requirements are a POSIX Unix derivative (such as
44 Linux), a modern Python 3 interpreter (any of the minor revisions
45 mentioned in the ``metadata.classifier`` section of
46 :file:`setup.cfg`) and a recent release of the tox_ utility (at least
47 the ``tox.minversion`` mentioned in :file:`tox.ini`).
48
49 .. _tox: https://tox.readthedocs.io/
50
51 application program interface
52 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53
54 The :doc:`api` API documentation is maintained within docstrings in
55 the mudpy source code.
56
57 regression testing
58 ~~~~~~~~~~~~~~~~~~
59
60 All new commits are tested using an included
61 :mod:`mudpy.test.selftest` script, to help ensure the software is
62 continually usable. Any new features should be accompanied by
63 suitable regression tests so that their functionality can be
64 maintained properly through future releases. The selftest can be
65 invoked with :command:`tox -e py3` which will automatically start
66 the daemon with the :file:`mudpy/tests/fixtures/test_daemon.yaml`
67 test configuration.
68
69 style
70 -----
71
72 This project follows Guido van Rossum and Barry Warsaw's `Style
73 Guide`_ for Python Code (a.k.a. "PEP-8"). When in need of sample
74 code or other examples, any common source code file or text document
75 file distributed as part of mudpy should serve as a suitable
76 reference. Testing of all new patches with the flake8_ utility
77 should be performed by running :command:`tox -e flake8` from the
78 repository working directory to ensure adherence to preferred style
79 conventions.
80
81 .. _Style Guide: :pep:`0008`
82 .. _flake8: https://pypi.org/project/flake8
83
84 .. _demo:
85
86 test and demo walk-through
87 --------------------------
88
89 The included tox configuration provides testenv definitions for a
90 variety of analyzers, regression tests, documentation builds and
91 package generation. It also has a ``demo`` testenv which will run
92 the server using the provided :file:`etc/mudpy.yaml` and other
93 sample files. By default it listens on TCP port 4000 at the IPv6
94 loopback address, streams its logging to the terminal via stdout,
95 and grants administrative rights automatically to an account named
96 ``admin`` (once created).
97
98 Because all the dependencies besides the :command:`python3`
99 interpreter itself are available from PyPI, installing them should
100 be fairly similar across most GNU/Linux distributions. For example,
101 on Debian 10 (a.k.a. *Buster*) you need to expressly install the
102 ``pip`` and ``venv`` modules since they're packaged separately from
103 the rest of the Python standard library. Once that's done, you can
104 perform a local install of ``tox`` as a normal non-root user. We're
105 also going to install system packages for the ``git`` revision
106 control toolset and an extensible console-based MUD client called
107 ``tf5`` (TinyFugue version 5)::
108
109     sudo apt install git python3-pip python3-venv tf5
110     pip install --user tox
111     exit
112
113 The reason for exiting is that, if this is the first time you've
114 ever used pip's ``--user`` option, when you log back in your
115 ``~/.profile`` should see that there's now a ``~/.local/bin``
116 directory and add it to your ``$PATH`` environment variable
117 automatically from that point on. Next, retrieve the project source
118 code and switch your current working directory to where you've
119 cloned it::
120
121     git clone https://mudpy.org/code/mudpy
122     cd mudpy
123
124 Now you should be able to invoke any tox testenv you like. Just
125 running :command:`tox` without any additional options will go
126 through the default battery of checks and is a good way to make sure
127 everything is installed and working. Once you're ready to try out
128 the server interactively, launch it like this::
129
130     tox -e demo
131
132 Now in another terminal/session (because the one you've been using
133 is busy displaying the server's logs) connect using a MUD client
134 (such as :command:`tf5` which we installed above)::
135
136     tf5 ip6-localhost 4000
137
138 Log in as ``admin`` creating an account and then an avatar and
139 awaken it. Try out the :command:`help` command and make sure you see
140 some command words in red (you're using a color terminal, right?)
141 since those are admin-only commands and being able to see them
142 confirms you're an administrator. When you're ready to terminate the
143 service you can either give the :command:`halt` command in your MUD
144 client terminal or press the ``control`` and ``c`` keys together in
145 the terminal where you ran tox. To exit the tf5 MUD client, give it
146 the :command:`/quit` command.
147
148 miscellanea
149 -----------
150
151 This section is a collection of various coding-related discussions
152 and treatises, mostly here because there's not a better place, and
153 so they don't get lost in random E-mail threads.
154
155 avatar names
156 ~~~~~~~~~~~~
157
158 It comes up fairly often, so bears mentioning, **there is no
159 assumption avatar names will be globally unique**. This is part of
160 the reason the default :code:`choose_name` menu just runs
161 :func:`mudpy.misc.random_name`, to make impersonation a bit harder.
162 The idea is to make sure to be able to support realistic settings
163 where multiple people are often given the same names and don't
164 really have much choice as to what their parents decided to name
165 them at birth, but can still choose a name they like later (perhaps
166 through some cultural rite of passage quest, attaining a particular
167 guild rank, or just through a command they're allowed to run as soon
168 as they awaken that avatar for the first time).
169
170 It may be possible to force globally-unique avatar names, but the
171 need to treat avatars similarly to non-player characters (actors
172 which aren't associated with an account and may be driven by
173 scripted routines instead) means it may also be desirable to prevent
174 a user from choosing an avatar name which duplicates the name of any
175 existing actor (whether or not that actor is a user's avatar). This
176 would entail scanning the full dataset to identify actors with
177 similar names so they could be rejected or excluded for a new
178 avatar, but then raises the question as to what to do when some new
179 content adds NPCs with a name which is already in use.
180
181 The best way to side-step this challenge is to not rely on avatar
182 names for programmatic interaction and instead reference the
183 corresponding ID for an avatar's element in the universe contents.
184 IDs **are** already guaranteed to be globally unique, so there is no
185 ambiguity when using them (an avatar's element ID is constructed
186 from the owning account name plus an index integer). Exactly what
187 variables you have to work from will depend on the context where
188 your hypothetical routine is called.
189
190 Taking as an example, let's say what you want is to be able to have
191 an area's owner permit a specific avatar to pass through the portals
192 (doorways, gates, whatever) which connect it to other areas. This is
193 similar to how *guild houses* work in some classical MUDs. Here's
194 how I'd go about implementing it:
195
196 1. Extend the area element to have two new facets: *owners* (type
197    list) and *visitors* (type list). The first will contain
198    references to the element IDs for the avatars who are allowed to
199    alter the entries of both lists, while the second will be the
200    element IDs for avatars who are allowed to enter the area (maybe
201    also allow owners to enter an area so they don't need to be
202    duplicated in both lists).
203
204 2. Implement a command which allows someone to see the corresponding
205    ID for an element (alternatively make it an acquirable ability,
206    skill, spell, item, or however finding that information would
207    best fit into your setting). This is stored in the element's
208    :code:`.key` attribute, and you can play around with it on the
209    command line with :command:`evaluate` like this::
210
211     > look
212     Center Sample Location
213     This is the Center Sample Location. It is merely provided as an
214     example of what an area might look like.
215     [ Exits: down, east, north, south, up, west ]
216     A sample prop sits here.
217     Utso is here.
218
219     > evaluate [
220           a.key.split('_', 1)[1]
221           for a in actor.universe.contents[
222               actor.get('location')
223           ].contents.values()
224           if a.key.startswith('actor.avatar_')
225           and a.get('name') == 'Utso'
226       ][0]
227
228     'luser0_0'
229
230    [Put the command on one line, It's merely wrapped here for
231    readability.] In short, this list comprehension takes the
232    internal IDs for elements present in the calling actor's current
233    location, filtered by whether they're avatars and a specific
234    actor name, then splits the group and prefix off (relying on the
235    fact that it uses ``_`` as a prefix separator) and returns the
236    first result. In a non-prototype implementation, the command
237    would probably include a routine which looked something like:
238
239    .. code-block:: python
240
241     location_id = actor.get("location")
242     also_here = actor.universe.contents[location_id].contents
243     for who in also_here.values():
244         if (who.key.startswith("actor.avatar_")
245                 and who.get("name") == parameters[0]:
246             message += "Avatar ID: %s" % .key.split("_", 1)[1]
247             break
248
249    That's a rough approximation, and not terribly immersive, but
250    hopefully you get the idea. We could also improve the element
251    attributes with pre-populated backlink references for some of
252    these relationships to reduce code complexity.
253
254 3. Implement a command which allows a user to add and remove entries
255    in the owners and visitors lists for their current location,
256    which takes this avatar ID as a parameter.
257
258 4. Hook in the :meth:`mudpy.misc.Element.go_to` method to test the
259    owners and visitors lists for the area parameter to make sure
260    self is in one of those lists. Probably also stick in an override
261    to make sure that if the actor's owner account is flagged as
262    administrative then they're also allowed even if they're not on
263    the allowed list.
264
265 The resulting workflow this would enable is that after an avatar *A*
266 has been set as an owner for area *X* (perhaps by an admin or a
267 world builder), *A* when in another area *Y* at the same time as
268 some avatar *B* whom they would like to be allowed to visit *X*
269 could run the identification command from part #2 above, then they
270 would later travel to *X* and run the safe passage command from part
271 #3 adding *B* to the visitors list for *X*. After that, *B* is able
272 freely enter *X* until *A* runs a similar command to remove them
273 from the visitors list for *X* again.
274
275 To tackle the immersion problem, an optional substitution cipher
276 could be implemented to (reversibly) turn those IDs into something
277 more mystical-looking. Also the identification command (or ability,
278 skill, spell, tool, whatever) could be limited to only work when the
279 area you're running it in is flagged a certain way. Taking this
280 concept further, all sorts of elements could have access lists for
281 which avatars own them and which avatars are allowed to interact
282 with them (in which case maybe the term *visitor* for the latter is
283 too area-specific and it needs a slightly more general term?).
284
285 However, a far more immersive solution would be to get closable and
286 lockable elements (and inventory management) implemented, have a
287 means of crafting keys which unlock specific elements, and then when
288 creating an area you want to restrict to specific avatars, put
289 locked doors for all its portals and give keys for them to anyone
290 you want to be able to visit. Or implement non-avatar actors, then
291 create guards you can hire or summon or construct to police your
292 doorways and check whether visitors are on a list. That list itself
293 could even be a piece of paper or a book (a prop), created with
294 appropriate materials and some skill which allows the actor to write
295 and edit paper notes, given to the guard and held by it as an
296 inventory item.
297
298 custom commands
299 ~~~~~~~~~~~~~~~
300
301 Command definitions are split into metadata and procedure. The
302 metadata needs to be in an element like the basic ones shipped in
303 the :file:`share/command.yaml` file, and then a handler function
304 added to the :mod:`mudpy.command` module. There's not yet a plugin
305 layer to allow those to be added to a separate module. The function
306 name needs to match the element base name, or you have to add an
307 action facet to the element indicating the name of the function you
308 want it to call; the ``command.set`` element has an example of this,
309 so that we avoid shadowing Python's built-in :func:`set` function
310 and call :func:`mudpy.command.c_set` instead.
311
312 The :meth:`mudpy.misc.Element.set` method takes two parameters, the
313 name of the facet and the value to pass into it. An example of it in
314 action is :meth:`mudpy.misc.User.authenticate` where the user's
315 *administrator* facet is set to the value *True* if their username
316 is in the ``.mudpy.limit.admins`` list used to bootstrap
317 administrators:
318
319 .. code-block:: python
320     :emphasize-lines: 9
321
322     def authenticate(self):
323         """Flag the user as authenticated and disconnect duplicates."""
324         if self.state != "authenticated":
325             self.authenticated = True
326             log("User %s authenticated for account %s." % (
327                     self, self.account.subkey), 2)
328             if ("mudpy.limit" in universe.contents and self.account.subkey in
329                     universe.contents["mudpy.limit"].get("admins")):
330                 self.account.set("administrator", True)
331                 log("Account %s is an administrator." % (
332                         self.account.subkey), 2)