Server Help

ASSS Questions - timers and per arena data with python

Anonymous - Tue Sep 28, 2004 4:53 pm
Post subject: timers and per arena data with python
How do you do timers and perarena/player data with python?

Also I notice in the kill points example you posted you did:
from asss import *
How is this different from doing:
import asss
which I saw in the prizelist.py example.

One last thing, how do you find out what a target is, for example finding out if a command was sent privately or to the whole arena.
Grelminar - Wed Sep 29, 2004 1:41 am
Post subject:
First, the python docs for import. The basic difference is that "import asss" loads the asss module, and puts one entry in the current namespace, named "asss". The contents of the module are available through that entry, as in "asss.get_interface". "from asss import *" loads the module and puts all of the module's contents in the current namespace. So you can just use "get_interface". The first way is a little cleaner, and avoids namespace conflicts, the second is when you're being lazy.

Targets are converted to the most convient python object. If the command was sent as a pub message, the target will be the arena. If it was sent to a specific player, it will be a player. If it was a freq, it will be a (arena, freq) tuple. Now, that doesn't really help you, because all you have is an object, and you don't know what type it is. Python provides a built-in function "type" which will give you the type of an object. But that doesn't help very much either, because you have nothing to compare it to. And that's my fault. So I'm going to expose the Player and Arena types so that you can say "if type(target) == asss.PlayerType: ...". I'll probably even create functions is_player and is_arena, to wrap that logic.

There's currently no way to do timers in python. It should be easy to add; I'll do it soon.

Per-arena and per-player data is simple: just use fields of the player and arena objects. No allocation or registration required. Note that it's easy to get namespace collisions, so you might want to prefix your field names with your module name, or something similar. E.g., use "p.prizelist_prizes = ..." instead of "p.prizes = ..."
Grelminar - Wed Sep 29, 2004 2:29 am
Post subject:
Um, I was wrong, there is definitely a way to do timers in python. The code's been there for over a year, but apparently I forgot about it, because "timers in python" was on my todo list icon_smile.gif. Just do:
Code: Show/Hide
def mytimer():
    # stuff here

tmr1 = asss.set_timer(mytimer, 500)


Return a false value (other than None), or lose the reference to tmr1, to stop being called.
Smong - Thu Sep 30, 2004 7:06 am
Post subject:
So there is no way of sending a parameter to the timer?
Code: Show/Hide
def mytimer(params):
    arena = params
    chat.SendArenaMessage(arena, "blah")

Grelminar - Thu Sep 30, 2004 11:49 am
Post subject:
Python actually supports nested functions and lexical scope, which together give you closures, so there's no more need for the manual closure paramter that gets used in C.

Code: Show/Hide
def make_message_sender(interval, arena):
    def send_message():
        chat.SendArenaMessage(arena, "blah")
    return asss.set_timer(send_message, interval)


Some Python users will tell you to wrap your closure data in a class and define a __call__ method, but I think that's dumb. And there's another technique you can use to achieve the same effect, which is more or less deprecated. All three will work.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group