Don't like ads? PRO users don't see any ads ;-)
Guest

customplaylist.py

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 10.16 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #
  2.  
  3. # Custom Playlist Plugin for BigBrotherBot(B3) (www.bigbrotherbot.net)
  4.  
  5. # Copyright (C) 2011 Phyxious
  6.  
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or modify
  10.  
  11. # it under the terms of the GNU General Public License as published by
  12.  
  13. # the Free Software Foundation; either version 2 of the License, or
  14.  
  15. # (at your option) any later version.
  16.  
  17.  
  18.  
  19. # This program is distributed in the hope that it will be useful,
  20.  
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  
  25. # GNU General Public License for more details.
  26.  
  27. #
  28.  
  29. # You should have received a copy of the GNU General Public License
  30.  
  31. # along with this program; if not, write to the Free Software
  32.  
  33. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  34.  
  35. #
  36.  
  37. # Changelog:
  38.  
  39. #
  40.  
  41. # 1.0.0 - Initial realease
  42.  
  43.  
  44.  
  45. __version__ = '1.0.0'
  46.  
  47. __author__ = 'Phyxious'
  48.  
  49.  
  50.  
  51. import threading
  52.  
  53. import b3
  54.  
  55. import b3.plugin
  56.  
  57. import b3.events
  58.  
  59.  
  60.  
  61. class CustomplaylistPlugin(b3.plugin.Plugin):
  62.  
  63.     _play_lists = {18: {
  64.  
  65.                             0: {'tdm':1, 'dm':2, 'ctf':3, 'sd':4, 'koth':5, 'dom':6, 'sab':7, 'dem':8},         # softcore
  66.  
  67.                                                 1: {'tdm':9, 'dm':10, 'ctf':11, 'sd':12, 'koth':13, 'dom':14, 'sab':15, 'dem':16},  # hardcore
  68.  
  69.                                                 2: {'tdm':17, 'dm':18, 'ctf':19, 'sd':20, 'koth':21, 'dom':22, 'sab':23, 'dem':24}, # barebones
  70.  
  71.                                           },
  72.  
  73.                                   12: {
  74.  
  75.                                         0: {'tdm':32, 'dm':33, 'ctf':34, 'sd':35, 'koth':36, 'dom':37, 'sab':38, 'dem':39}, # softcore
  76.  
  77.                                                 1: {'tdm':41, 'dm':42, 'ctf':43, 'sd':44, 'koth':45, 'dom':46, 'sab':47, 'dem':48}, # hardcore
  78.  
  79.                                                 2: {'tdm':50, 'dm':51, 'ctf':52, 'sd':53, 'koth':54, 'dom':55, 'sab':56},           # barebones
  80.  
  81.                                            }
  82.  
  83.                                  }
  84.  
  85.                  
  86.  
  87.     _map_list = {
  88.  
  89.                                 'mp_array' : 'array',
  90.  
  91.                 'mp_cracked' : 'cracked',
  92.  
  93.                 'mp_crisis' : 'crisis',
  94.  
  95.                 'mp_firingrange' : 'firing range',
  96.  
  97.                 'mp_duga' : 'grid',
  98.  
  99.                 'mp_hanoi' : 'hanoi',
  100.  
  101.                 'mp_cairo' : 'havana',
  102.  
  103.                 'mp_havoc' : 'jungle',
  104.  
  105.                 'mp_cosmodrome' : 'launch',
  106.  
  107.                 'mp_nuked' : 'nuketown',
  108.  
  109.                 'mp_radiation' : 'radiation',
  110.  
  111.                 'mp_mountain' : 'summit',
  112.  
  113.                 'mp_villa' : 'villa',
  114.  
  115.                 'mp_russianbase' : 'wmd',
  116.  
  117.                 'mp_berlinwall2' : 'berlin wall',
  118.  
  119.                 'mp_discovery' : 'discovery',
  120.  
  121.                 'mp_kowloon' : 'kowloon',
  122.  
  123.                 'mp_stadium' : 'stadium',
  124.  
  125.                 'mp_gridlock' : 'convoy',
  126.  
  127.                 'mp_hotel' : 'hotel',
  128.  
  129.                 'mp_outskirts' : 'stockpile',
  130.  
  131.                 'mp_zoo' : 'zoo',
  132.  
  133.                 'mp_area51' : 'hangar 18',
  134.  
  135.                 'mp_drivein' : 'drive-in',
  136.  
  137.                 'mp_silo' : 'silo',
  138.  
  139.                 'mp_golfcourse' : 'hazard'
  140.  
  141.                                 }
  142.  
  143.                
  144.  
  145.     _custom_playlist = {}
  146.  
  147.     _slot_num = 18
  148.  
  149.     _game_mode = 0
  150.  
  151.     _default_playlist = None
  152.  
  153.     _next_gametype = None
  154.  
  155.     _next_map = None
  156.  
  157.     _list_counter = 1
  158.  
  159.     _rotation = None
  160.  
  161.    
  162.  
  163.     def onStartup(self):
  164.  
  165.         """\
  166.  
  167.         Initialize plugin settings
  168.  
  169.         """
  170.  
  171.        
  172.  
  173.         # get the admin plugin so we can register commands
  174.  
  175.         self._adminPlugin = self.console.getPlugin('admin')
  176.  
  177.         if not self._adminPlugin:
  178.  
  179.             # something is wrong, can't start without admin plugin
  180.  
  181.             self.error('Could not find admin plugin')
  182.  
  183.             return false
  184.  
  185.            
  186.  
  187.         # Register commands
  188.  
  189.         if 'commands' in self.config.sections():
  190.  
  191.             for cmd in self.config.options('commands'):
  192.  
  193.                 level = self.config.getint('commands', cmd)
  194.  
  195.                 sp = cmd.split('-')
  196.  
  197.                 alias = None
  198.  
  199.                 if len(sp) == 2:
  200.  
  201.                     cmd, alias = sp
  202.  
  203.                    
  204.  
  205.                 func = self.getCmd(cmd)
  206.  
  207.                 if func:
  208.  
  209.                     self.debug('CUSTOMPLAYLIST: Registering cmd %s' % cmd)
  210.  
  211.                     self._adminPlugin.registerCommand(self, cmd, level, func, alias)
  212.  
  213.                    
  214.  
  215.         # Register events
  216.  
  217.         self.verbose('Registering events')
  218.  
  219.         self.registerEvent(b3.events.EVT_GAME_MAP_CHANGE)
  220.  
  221.        
  222.  
  223.         # Deugging purposes
  224.  
  225.         self.debug('Started')
  226.  
  227.        
  228.  
  229.     def onLoadConfig(self):
  230.  
  231.         self.verbose('Loading config')
  232.  
  233.        
  234.  
  235.         # Get command levels
  236.  
  237.         self.cmd_change_rotation_minlevel = self.config.getint('commands', 'changerotation')
  238.  
  239.         self.debug('CUSTOMPLAYLIST: Change rotation min level is: %d' % self.cmd_change_rotation_minlevel)
  240.  
  241.        
  242.  
  243.         self.cmd_get_rotation_minlevel = self.config.getint('commands', 'getrotation')
  244.  
  245.         self.debug('CUSTOMPLAYLIST: Get Rotation min level is: %d' % self.cmd_get_rotation_minlevel)
  246.  
  247.        
  248.  
  249.         # Get slot number for game server
  250.  
  251.         self._slot_num = self.config.getint('config_settings', 'slot_num')
  252.  
  253.         if self._slot_num not in (12, 18):
  254.  
  255.             self.error('CUSTOMPLAYLIST: Incorrect number of slots %d, set to default of 18.' % self._slot_num)
  256.  
  257.             self._slot_num = 18
  258.  
  259.        
  260.  
  261.         # Get game mode for server
  262.  
  263.         self._game_mode = self.config.getint('config_settings', 'game_mode')
  264.  
  265.         if self._game_mode not in (0, 1, 2):
  266.  
  267.             self.error('CUSTOMPLAYLIST: Incorrect game mode %d, set to default of softcore.' % self._game_mode)
  268.  
  269.             self._game_mode = 0
  270.  
  271.            
  272.  
  273.         # Get default playlist
  274.  
  275.         self._default_playlist = self.config.get('config_settings', 'default_playlist')
  276.  
  277.        
  278.  
  279.         # Get custom playlist order from config file
  280.  
  281.         if self._default_playlist in self.config.sections():
  282.  
  283.             for customList in self.config.options(self._default_playlist):
  284.  
  285.                 self._custom_playlist[int(customList)] = self.config.get(self._default_playlist, customList)
  286.  
  287.                
  288.  
  289.         # Debugging purposes
  290.  
  291.         self.debug('CUSTOMPLAYLIST: %s' % self._custom_playlist)
  292.  
  293.        
  294.  
  295.     def onEvent(self, event):
  296.  
  297.         """\
  298.  
  299.         Handle intercepted events
  300.  
  301.         """
  302.  
  303.         if event.type == b3.events.EVT_GAME_MAP_CHANGE:
  304.  
  305.             self.debug('CUSTOMPLAYLIST: Game Map Change Event Fired...')
  306.  
  307.             # wait 30 sec and set the next map
  308.  
  309.             timer = threading.Timer(30, self.setRotation)
  310.  
  311.             timer.start()
  312.  
  313.            
  314.  
  315.     def getCmd(self, cmd):
  316.  
  317.         cmd = 'cmd_%s' % cmd
  318.  
  319.         if hasattr(self, cmd):
  320.  
  321.             func = getattr(self, cmd)
  322.  
  323.             return func
  324.  
  325.            
  326.  
  327.         return None
  328.  
  329.        
  330.  
  331.     # Rotate through custom playlist to get gametype and map
  332.  
  333.     def nextRotation(self):
  334.  
  335.         # Make sure the count is within the playlist number, if not reset to 1
  336.  
  337.         if self._list_counter not in self._custom_playlist.keys():
  338.  
  339.             self._list_counter = 1
  340.  
  341.            
  342.  
  343.         self._rotation = self._custom_playlist[self._list_counter].split(',')
  344.  
  345.         # Check to make sure there is a vaild gametype/map
  346.  
  347.         if self._rotation[0] in self._play_lists[self._slot_num][self._game_mode] \
  348.  
  349.             and self._rotation[1] in self._map_list.keys():
  350.  
  351.                 self.debug('CUSTOMPLAYLIST: The selected playlist gametype/map of %s,%s, is valid' % (self._rotation[0], self._rotation[1]))
  352.  
  353.                 self._next_gametype = self._play_lists[self._slot_num][self._game_mode][self._rotation[0]]
  354.  
  355.                 self._next_map = self._rotation[1]
  356.  
  357.                
  358.  
  359.                 self.getRotation()
  360.  
  361.                 # Move counter up for next gametype/map
  362.  
  363.                 self._list_counter += 1
  364.  
  365.         else:
  366.  
  367.             self.error('CUSTOMPLAYLIST: There was an error in the playlist config file, gametype is: %s and map is: %s.' % (rotation[0], rotation[1]))
  368.  
  369.             # update counter to move on to next in the playlist and recall nextRotation function
  370.  
  371.             self._list_counter += 1
  372.  
  373.             self.nextRotation()
  374.  
  375.            
  376.  
  377.     def setRotation(self):
  378.  
  379.         # Call nextRotation to get the next gametype/map in the rotation
  380.  
  381.         self.nextRotation()
  382.  
  383.        
  384.  
  385.         # Set playlist on server to change the gametype
  386.  
  387.         self.debug('CUSTOMPLAYLIST: setadmindvar playlist %s' % self._next_gametype)
  388.  
  389.         self.console.write('setadmindvar playlist %s' % self._next_gametype)
  390.  
  391.        
  392.  
  393.         # Exclude all maps except for the next one
  394.  
  395.         exclude = self._map_list.keys()
  396.  
  397.         exclude.remove(self._next_map)
  398.  
  399.        
  400.  
  401.         self.debug('CUSTOMPLAYLIST: setadmindvar playlist_excludeMap %s' % ' '.join(exclude))
  402.  
  403.         self.console.write('setadmindvar playlist_excludeMap %s' % ' '.join(exclude))
  404.  
  405.        
  406.  
  407.         self.debug('CUSTOMPLAYLIST: setadmindvar playlist_excludeGametypeMap ""')
  408.  
  409.         self.console.write ('setadmindvar playlist_excludeGametypeMap ""')
  410.  
  411.        
  412.  
  413.     def cmd_changerotation(self, data, client=None, cmd=None):
  414.  
  415.         """\
  416.  
  417.         Change current rotation
  418.  
  419.         """
  420.  
  421.        
  422.  
  423.         self.debug('CUSTOMPLAYLIST: Calling cmd_changerotation')
  424.  
  425.         # Check to make sure its an int
  426.  
  427.         if data != int(data):
  428.  
  429.             data = int(data)
  430.  
  431.         # Make sure within the range of the current playlist
  432.  
  433.         if data in self._custom_playlist.keys():
  434.  
  435.             self._list_counter = data
  436.  
  437.             self.setRotation()
  438.  
  439.         else:
  440.  
  441.             client.message('ERROR: %d is not a number in the current playlist' % data)
  442.  
  443.            
  444.  
  445.     def cmd_getrotation(self, data=None, client=None, cmd=None):
  446.  
  447.         """\
  448.  
  449.         Get next rotation and print to console
  450.  
  451.         """
  452.  
  453.        
  454.  
  455.         self.getRotation()
  456.  
  457.        
  458.  
  459.     def getRotation(self):
  460.  
  461.         if self._rotation[0] == 'koth':
  462.  
  463.             gt = 'HQ'
  464.  
  465.         else:
  466.  
  467.             gt = self._rotation[0].swapcase()
  468.  
  469.            
  470.  
  471.         self.console.say('^1Nextmap ^7is: ^3%s' % (gt, self._map_list[self._next_map].capitalize()))