Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. # -*- coding:Utf-8 -*-
  2. import es
  3. import cmdlib
  4.  
  5. esInfo = es.AddonInfo()
  6. esInfo.author = "DoCky, edited by wcs community"
  7. esInfo.version = "1.101pEdit"
  8. esInfo.name = "PlayerViewLib"
  9. esInfo.basename = "playerviewlib"
  10. esInfo.url = ""
  11. esInfo.description = "Allow you to know what entity a player is aiming"
  12.  
  13. version = es.ServerVar("playerviewlib", "", " Created by DoCky -")
  14. version.set(esInfo.version)
  15. version.makepublic()
  16.  
  17.  
  18. # players dict
  19. players = {}
  20.  
  21. def _command(args):
  22.     # exec internal i_entity method
  23.     players[args[0]].i_entity(args[1])
  24. # register server command
  25. cmdlib.registerServerCommand('_playerview_cmd', _command, 'playerview: internal command')
  26.  
  27. class PlayerView(object):
  28.     """ PlayerView object """
  29.     def __init__(self, userid):
  30.         self.userid = str(userid)
  31.         self.entities = {}
  32.  
  33.     def i_entities(self):
  34.         # clear dict
  35.         self.entities.clear()
  36.         # for entities in map
  37.         for index, attribute in es.createentityindexlist().items():
  38.             # store "index" = "classname", "targetname"
  39.             self.entities[index] = (attribute["classname"], es.entitygetvalue(index, "targetname"))
  40.  
  41.     def i_entity(self, target):
  42.         # default index value
  43.         entity = -1
  44.         # targetname to find
  45.         targetname = "_playerview_%s_%s" % (target, self.userid)
  46.  
  47.         # for index, info in entity list
  48.         for index, info in self.entities.items():
  49.             # check if targetname is corresponding
  50.             if es.entitygetvalue(index, "targetname") == targetname:
  51.                 entity = index
  52.                 # restore last targetname
  53.                 es.entitysetvalue(index, "targetname", info[1])
  54.                 break
  55.             # remove player from dict
  56.         del players[self.userid]
  57.  
  58.         # if asked value is entity
  59.         if target == "entity":
  60.             # try to execute function with "userid" and "index" as arguments
  61.             es.ServerVar('wcs_userid').set(self.userid)
  62.             es.ServerVar('wcs_index').set(entity)
  63.  
  64.         # if asked value is player
  65.         elif target == "player":
  66.             # try to execute function with "userid" and "playerid" as arguments
  67.             player = es.getuserid(es.gethandlefromindex(index))
  68.             es.ServerVar('wcs_userid').set(self.userid)
  69.             es.ServerVar('wcs_player').set(player)
  70.  
  71.  
  72.     def i_command(self):
  73.         if not es.exists("userid", self.userid): es.dbgmsg(0, "playerview: userid <%s> was not found" % self.userid)
  74.         elif not players.has_key(self.userid):
  75.             # refresh entity list
  76.             self.i_entities()
  77.             # add object in players dict
  78.             players[self.userid] = self
  79.  
  80.     def entity(self):
  81.         # check user, refresh entity list, add object in players dict
  82.         self.i_command()
  83.         # set entity targetname, exec internal server command
  84.     es.server.insertcmd("es_xentsetname %s _playerview_entity_%s;_playerview_cmd %s entity" % (self.userid, self.userid, self.userid))
  85.  
  86.  
  87.     def player(self):
  88.         # check user, refresh entity list, add object in players dict
  89.         self.i_command()
  90.         # set entity targetname, exec internal server command
  91.         es.server.insertcmd("es_xentsetname %s _playerview_player_%s;_playerview_cmd %s player" % (self.userid, self.userid, self.userid))
  92.  
  93.  
  94.  
  95.  
  96. def entity(userid, callback):
  97.     # playerviewlib.entity(<userid>, <callback>)
  98.     PlayerView(userid, callback).entity()
  99.  
  100. def player(userid, callback):
  101.     # playerviewlib.player(<userid>, <callback>)
  102.     PlayerView(userid, callback).player()
  103.  
  104. def es_map_start(event_var):
  105.     # clear players dict
  106.     players.clear()
  107. # registering es_map_start event
  108. es.addons.registerForEvent(__import__(__name__), "es_map_start", es_map_start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement