mirror of
https://github.com/evennia/evennia.git
synced 2026-03-19 06:16:31 +01:00
354 lines
No EOL
12 KiB
HTML
354 lines
No EOL
12 KiB
HTML
|
||
|
||
<!DOCTYPE html>
|
||
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
||
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
||
<head>
|
||
<meta charset="utf-8">
|
||
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<title>Command Cooldown — Evennia 1.0-dev documentation</title>
|
||
|
||
|
||
|
||
|
||
<link rel="shortcut icon" href="_static/favicon.ico"/>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
||
|
||
|
||
|
||
|
||
|
||
<link rel="index" title="Index"
|
||
href="genindex.html"/>
|
||
<link rel="search" title="Search" href="search.html"/>
|
||
<link rel="top" title="Evennia 1.0-dev documentation" href="index.html"/>
|
||
|
||
|
||
<script src="_static/js/modernizr.min.js"></script>
|
||
|
||
</head>
|
||
|
||
<body class="wy-body-for-nav" role="document">
|
||
|
||
|
||
<div class="wy-grid-for-nav">
|
||
|
||
|
||
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
||
<div class="wy-side-scroll">
|
||
<div class="wy-side-nav-search">
|
||
|
||
|
||
|
||
<a href="index.html" class="icon icon-home"> Evennia
|
||
|
||
|
||
|
||
</a>
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="search">
|
||
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
||
<input type="text" name="q" placeholder="Search docs" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
|
||
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
||
|
||
|
||
|
||
<!-- Local TOC -->
|
||
<div class="local-toc"><ul>
|
||
<li><a class="reference internal" href="#">Command Cooldown</a><ul>
|
||
<li><a class="reference internal" href="#non-persistent-cooldown">Non-persistent cooldown</a></li>
|
||
<li><a class="reference internal" href="#persistent-cooldown">Persistent cooldown</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</nav>
|
||
|
||
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
||
|
||
|
||
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
||
|
||
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
||
<a href="index.html">Evennia</a>
|
||
|
||
</nav>
|
||
|
||
|
||
|
||
<div class="wy-nav-content">
|
||
<div class="rst-content">
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div role="navigation" aria-label="breadcrumbs navigation">
|
||
|
||
<ul class="wy-breadcrumbs">
|
||
|
||
<li><a href="index.html">Docs</a> »</li>
|
||
|
||
<li>Command Cooldown</li>
|
||
|
||
|
||
<li class="wy-breadcrumbs-aside">
|
||
|
||
|
||
<a href="_sources/Command-Cooldown.md.txt" rel="nofollow"> View page source</a>
|
||
|
||
|
||
</li>
|
||
|
||
</ul>
|
||
|
||
|
||
<hr/>
|
||
</div>
|
||
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
|
||
<div itemprop="articleBody">
|
||
|
||
<div class="section" id="command-cooldown">
|
||
<h1>Command Cooldown<a class="headerlink" href="#command-cooldown" title="Permalink to this headline">¶</a></h1>
|
||
<p>Some types of games want to limit how often a command can be run. If a
|
||
character casts the spell <em>Firestorm</em>, you might not want them to spam that
|
||
command over and over. Or in an advanced combat system, a massive swing may
|
||
offer a chance of lots of damage at the cost of not being able to re-do it for
|
||
a while. Such effects are called <em>cooldowns</em>.</p>
|
||
<p>This page exemplifies a very resource-efficient way to do cooldowns. A more
|
||
‘active’ way is to use asynchronous delays as in the <a class="reference external" href="/Command-Duration.html#Blocking-Commands">command duration
|
||
tutorial</a>, the two might be useful to
|
||
combine if you want to echo some message to the user after the cooldown ends.</p>
|
||
<div class="section" id="non-persistent-cooldown">
|
||
<h2>Non-persistent cooldown<a class="headerlink" href="#non-persistent-cooldown" title="Permalink to this headline">¶</a></h2>
|
||
<p>This little recipe will limit how often a particular command can be run. Since
|
||
Commands are class instances, and those are cached in memory, a command
|
||
instance will remember things you store on it. So just store the current time
|
||
of execution! Next time the command is run, it just needs to check if it has
|
||
that time stored, and compare it with the current time to see if a desired
|
||
delay has passed.</p>
|
||
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16
|
||
17
|
||
18
|
||
19
|
||
20
|
||
21
|
||
22
|
||
23
|
||
24
|
||
25
|
||
26
|
||
27
|
||
28
|
||
29
|
||
30
|
||
31</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">time</span>
|
||
<span class="kn">from</span> <span class="nn">evennia</span> <span class="kn">import</span> <span class="n">default_cmds</span>
|
||
|
||
<span class="k">class</span> <span class="nc">CmdSpellFirestorm</span><span class="p">(</span><span class="n">default_cmds</span><span class="o">.</span><span class="n">MuxCommand</span><span class="p">):</span>
|
||
<span class="sd">"""</span>
|
||
<span class="sd"> Spell - Firestorm</span>
|
||
|
||
<span class="sd"> Usage: </span>
|
||
<span class="sd"> cast firestorm <target></span>
|
||
<span class="sd"> </span>
|
||
<span class="sd"> This will unleash a storm of flame. You can only release one </span>
|
||
<span class="sd"> firestorm every five minutes (assuming you have the mana). </span>
|
||
<span class="sd"> """</span>
|
||
<span class="n">key</span> <span class="o">=</span> <span class="s2">"cast firestorm"</span>
|
||
<span class="n">locks</span> <span class="o">=</span> <span class="s2">"cmd:isFireMage()"</span>
|
||
|
||
<span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="s2">"Implement the spell"</span>
|
||
|
||
<span class="c1"># check cooldown (5 minute cooldown)</span>
|
||
<span class="n">now</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
|
||
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s2">"lastcast"</span><span class="p">)</span> <span class="ow">and</span> \
|
||
<span class="n">now</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastcast</span> <span class="o"><</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">:</span>
|
||
<span class="n">message</span> <span class="o">=</span> <span class="s2">"You cannot cast this spell again yet."</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">caller</span><span class="o">.</span><span class="n">msg</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
|
||
<span class="k">return</span>
|
||
|
||
<span class="c1">#[the spell effect is implemented]</span>
|
||
|
||
<span class="c1"># if the spell was successfully cast, store the casting time</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">lastcast</span> <span class="o">=</span> <span class="n">now</span>
|
||
</pre></div>
|
||
</td></tr></table></div>
|
||
<p>We just check the <code class="docutils literal notranslate"><span class="pre">lastcast</span></code> flag, and update it if everything works out.
|
||
Simple and very effective since everything is just stored in memory. The
|
||
drawback of this simple scheme is that it’s non-persistent. If you do
|
||
<code class="docutils literal notranslate"><span class="pre">@reload</span></code>, the cache is cleaned and all such ongoing cooldowns will be
|
||
forgotten. It is also limited only to this one command, other commands cannot
|
||
(easily) check for this value.</p>
|
||
</div>
|
||
<div class="section" id="persistent-cooldown">
|
||
<h2>Persistent cooldown<a class="headerlink" href="#persistent-cooldown" title="Permalink to this headline">¶</a></h2>
|
||
<p>This is essentially the same mechanism as the simple one above, except we use
|
||
the database to store the information which means the cooldown will survive a
|
||
server reload/reboot. Since commands themselves have no representation in the
|
||
database, you need to use the caster for the storage.</p>
|
||
<div class="highlight-python notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||
2
|
||
3
|
||
4
|
||
5
|
||
6
|
||
7
|
||
8
|
||
9
|
||
10
|
||
11
|
||
12
|
||
13
|
||
14
|
||
15
|
||
16</pre></div></td><td class="code"><div class="highlight"><pre><span></span> <span class="c1"># inside the func() of CmdSpellFirestorm as above</span>
|
||
|
||
<span class="c1"># check cooldown (5 minute cooldown)</span>
|
||
|
||
<span class="n">now</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
|
||
<span class="n">lastcast</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">caller</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">firestorm_lastcast</span>
|
||
|
||
<span class="k">if</span> <span class="n">lastcast</span> <span class="ow">and</span> <span class="n">now</span> <span class="o">-</span> <span class="n">lastcast</span> <span class="o"><</span> <span class="mi">5</span> <span class="o">*</span> <span class="mi">60</span><span class="p">:</span>
|
||
<span class="n">message</span> <span class="o">=</span> <span class="s2">"You need to wait before casting this spell again."</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">caller</span><span class="o">.</span><span class="n">msg</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
|
||
<span class="k">return</span>
|
||
|
||
<span class="c1">#[the spell effect is implemented]</span>
|
||
|
||
<span class="c1"># if the spell was successfully cast, store the casting time</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">caller</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">firestorm_lastcast</span> <span class="o">=</span> <span class="n">now</span>
|
||
</pre></div>
|
||
</td></tr></table></div>
|
||
<p>Since we are storing as an <a class="reference internal" href="Attributes.html"><span class="doc">Attribute</span></a>, we need to identify the
|
||
variable as <code class="docutils literal notranslate"><span class="pre">firestorm_lastcast</span></code> so we are sure we get the right one (we’ll
|
||
likely have other skills with cooldowns after all). But this method of
|
||
using cooldowns also has the advantage of working <em>between</em> commands - you can
|
||
for example let all fire-related spells check the same cooldown to make sure
|
||
the casting of <em>Firestorm</em> blocks all fire-related spells for a while. Or, in
|
||
the case of taking that big swing with the sword, this could now block all
|
||
other types of attacks for a while before the warrior can recover.</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
<footer>
|
||
|
||
|
||
<hr/>
|
||
|
||
<div role="contentinfo">
|
||
<p>
|
||
© Copyright 2020, The Evennia developer community.
|
||
|
||
</p>
|
||
</div>
|
||
Built with <a href="http://sphinx-doc.org/">Sphinx</a> and ❤️ using a custom <a href="https://github.com/LinxiFan/Sphinx-theme">theme</a> based on <a href="https://readthedocs.org">Read the Docs</a>.
|
||
|
||
</footer>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</section>
|
||
|
||
</div>
|
||
|
||
|
||
|
||
|
||
|
||
<script type="text/javascript">
|
||
var DOCUMENTATION_OPTIONS = {
|
||
URL_ROOT:'./',
|
||
VERSION:'1.0-dev',
|
||
COLLAPSE_INDEX:false,
|
||
FILE_SUFFIX:'.html',
|
||
HAS_SOURCE: true,
|
||
SOURCELINK_SUFFIX: '.txt'
|
||
};
|
||
</script>
|
||
<script type="text/javascript" src="_static/jquery.js"></script>
|
||
<script type="text/javascript" src="_static/underscore.js"></script>
|
||
<script type="text/javascript" src="_static/doctools.js"></script>
|
||
<script type="text/javascript" src="_static/language_data.js"></script>
|
||
|
||
|
||
|
||
|
||
|
||
<script type="text/javascript" src="_static/js/theme.js"></script>
|
||
|
||
|
||
|
||
|
||
<script type="text/javascript">
|
||
jQuery(function () {
|
||
SphinxRtdTheme.StickyNav.enable();
|
||
});
|
||
</script>
|
||
|
||
|
||
</body>
|
||
</html> |