Popuplib.construct

From EventScripts Community Encyclopedia



Method: construct

Module: popuplib
Class: (no class)
EventScripts version: 2.0

Method Overview

Table of Contents

Contents


Description

Creates a new easymenu type popup that is populated with items automatically based on the construct parameter, refined using flags.

Popuplib comes with one construct type built-in, "players", which accepts all the same flags as playerlib.getPlayerList does, including custom registered filters. Selecting a menu item from this construct will have the selected userid returned as choice parameter on the menuselect function. More construct types can be registered by script addons, see below.

The idea of construct popups is that you only need to create it once, usually in script loading method, and define which kind of content it will hold, for example "players". After that you can just simply send it to users and the contents will be automatically kept up to date with no further editing from the script's side needed.

The code for players construct is below (edited for generality):

def ConstructPlayerList(popupid, flags, menuselectfb):
    '''
    Constructs a playerlist
    '''
    def updatelisting(p):
        ''' Internal method that is called when the popup is displayed,
        the purpose is to update the menu contents, only if needed '''
        
        playerlist = playerlib.getPlayerList(flags)
        useridlist = [int(x) for x in playerlist]
        if useridlist != p.construct_check:
            # player listing has changed since last update, redo the options:
            p.construct_check = useridlist
            langs = p.options.keys()
            mainopt = p.options[p.editlang]
            mainopt.clear()
            for player in playerlist:
                p.addoption(int(player), player.attributes['name'])
            for lang in langs:
                p.options[lang] = mainopt
            # return True as mark that the contents were changed
            return True
        else:
            # no modifications done, return False to let popuplib optimize speed
            return False
    # Creating the easymenu and returning it
    thenewmenu = popuplib.easymenu(popupid, None, menuselectfb)
    thenewmenu.cachemode = "construct"   # IMPORTANT !
    thenewmenu.construct = updatelisting # This is the function used to update
    thenewmenu.construct_check = None    # This is the variable used to hold last state
    return thenewmenu # return the new menu to caller
 
# register the construct to popuplib so it can be used from popuplib.construct:
popuplib.registerConstruct('players', ConstructPlayerList)
  1. The first parameter is the construct name, the second one is the above function that builds it</python>

Arguments

popupname
the global popup name that identifies this popup
construct
specifies which kind of content this menu has (for example "players")
flags
specifies flags for the construct (for example "#all" when using "players" type)
function
a Python function or script block that acts as menuselectfb


Examples

This example creates two player menus using construct, one with all players and another with only alive players. Anyone can call those menus up with !all or !alive in chat.

import es
import popuplib
 
menus = {}
 
def load():
    menus['!all'] = popuplib.construct('all_players', 'players', '#all', player_selected)
    menus['!alive'] = popuplib.construct('alive_players', 'players', '#alive', player_selected)
 
def es_player_chat(event_var):
    text = event_var['text']
    if text in menus:
        menus[text].send(event_var['userid'])
 
def player_selected(userid, choice, popupid):
    whodid = es.getplayername(userid)
    target = es.getplayername(choice)
    es.msg('%s selected %s from %s'%(whodid, target, popupid))

Notes


See Also

popuplib.Popup_easymenu

blog comments powered by Disqus