<s> import threading <EOL> import time <EOL> import logging <EOL> import logging . handlers <EOL> from shinymud . lib . db import DB <EOL> from shinymud . data . config import * <EOL> class World ( object ) : <EOL> _instance = None <EOL> @ classmethod <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> if not cls . _instance : <EOL> cls . _instance = super ( World , cls ) . __new__ ( <EOL> cls ) <EOL> return cls . _instance <EOL> def __init__ ( self , conn = None ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> return <EOL> self . _initialized = True <EOL> self . configure_logs ( ) <EOL> self . player_list = { } <EOL> self . player_delete = [ ] <EOL> self . battles = { } <EOL> self . battles_delete = [ ] <EOL> self . player_list_lock = threading . Lock ( ) <EOL> self . shutdown_flag = False <EOL> self . areas = { } <EOL> self . db = DB ( self . log , conn = conn ) <EOL> self . default_location = None <EOL> self . currency_name = CURRENCY <EOL> self . login_greeting = '<STR_LIT>' <EOL> self . uptime = time . time ( ) <EOL> self . active_npcs = [ ] <EOL> try : <EOL> greet_file = open ( ROOT_DIR + '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> except Exception , e : <EOL> self . log . error ( '<STR_LIT>' + str ( e ) ) <EOL> else : <EOL> text = greet_file . read ( ) <EOL> self . login_greeting = text . split ( '<STR_LIT:\n>' ) <EOL> finally : <EOL> greet_file . close ( ) <EOL> if not self . login_greeting : <EOL> self . login_greeting = '<STR_LIT>' % GAME_NAME <EOL> @ classmethod <EOL> def get_world ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . _instance <EOL> def configure_logs ( self ) : <EOL> format = "<STR_LIT>" <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> shiny_handler = logging . handlers . RotatingFileHandler ( <EOL> SHINYMUD_LOGFILE , '<STR_LIT:a>' , SHINYMUD_MAXBYTES , SHINYMUD_NUMFILES ) <EOL> shiny_handler . setFormatter ( logging . Formatter ( format ) ) <EOL> self . log . addHandler ( shiny_handler ) <EOL> self . log . setLevel ( SHINYMUD_LOGLEVEL ) <EOL> self . play_log = logging . getLogger ( '<STR_LIT>' ) <EOL> social_handler = logging . handlers . RotatingFileHandler ( <EOL> SOCIAL_LOGFILE , '<STR_LIT:a>' , SOCIAL_MAXBYTES , SOCIAL_NUMFILES ) <EOL> social_handler . setFormatter ( logging . Formatter ( "<STR_LIT>" ) ) <EOL> self . play_log . addHandler ( social_handler ) <EOL> self . play_log . setLevel ( SOCIAL_LOGLEVEL ) <EOL> def cleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> for player in self . player_delete : <EOL> del self . player_list [ player ] <EOL> self . player_delete = [ ] <EOL> for battle in self . battles_delete : <EOL> del self . battles [ battle ] <EOL> self . battles_delete = [ ] <EOL> def start_turning ( self ) : <EOL> while not self . shutdown_flag : <EOL> start = time . time ( ) <EOL> for i in reversed ( xrange ( len ( self . active_npcs ) ) ) : <EOL> if not self . active_npcs [ i ] . do_tick ( ) : <EOL> del self . active_npcs [ i ] <EOL> self . player_list_lock . acquire ( ) <EOL> list_keys = self . player_list . keys ( ) <EOL> for key in list_keys : <EOL> self . player_list [ key ] . do_tick ( ) <EOL> self . cleanup ( ) <EOL> list_keys = self . player_list . keys ( ) <EOL> for key in list_keys : <EOL> self . player_list [ key ] . send_output ( ) <EOL> self . player_list_lock . release ( ) <EOL> for key in self . battles . keys ( ) : <EOL> self . battles [ key ] . perform_round ( ) <EOL> for area in self . areas . values ( ) : <EOL> if area . times_visited_since_reset > <NUM_LIT:0> : <EOL> now = time . time ( ) <EOL> if ( now - area . time_of_last_reset ) >= RESET_INTERVAL : <EOL> area . reset ( ) <EOL> self . log . info ( '<STR_LIT>' % area . name ) <EOL> finish = time . time ( ) - start <EOL> if finish >= <NUM_LIT:1> : <EOL> self . log . critical ( '<STR_LIT>' ) <EOL> elif finish < <NUM_LIT> : <EOL> time . sleep ( <NUM_LIT> - finish ) <EOL> self . listening = False <EOL> def has_location ( self , area_name , room_id ) : <EOL> """<STR_LIT>""" <EOL> if area_name in self . areas : <EOL> room = self . areas . get ( area_name ) . get_room ( str ( room_id ) ) <EOL> if room : <EOL> return True <EOL> return False <EOL> def get_location ( self , area_name , room_id ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_location ( area_name , room_id ) : <EOL> return None <EOL> room = self . areas . get ( area_name ) . get_room ( str ( room_id ) ) <EOL> return room <EOL> def area_add ( self , area ) : <EOL> self . areas [ area . name ] = area <EOL> def list_areas ( self ) : <EOL> names = [ '<STR_LIT>' % ( key , value . title ) for key , value in self . areas . items ( ) ] <EOL> area_list = '<STR_LIT>' . center ( <NUM_LIT:50> , '<STR_LIT:->' ) + '<STR_LIT:\n>' <EOL> if names : <EOL> area_list += '<STR_LIT:\n>' . join ( names ) <EOL> else : <EOL> area_list += '<STR_LIT>' <EOL> area_list += '<STR_LIT:\n>' + '<STR_LIT:->' . center ( <NUM_LIT:50> , '<STR_LIT:->' ) <EOL> return area_list <EOL> def area_exists ( self , area_name ) : <EOL> if area_name in self . areas : <EOL> return True <EOL> return False <EOL> def get_area ( self , area_name ) : <EOL> if self . area_exists ( area_name ) : <EOL> return self . areas [ area_name ] <EOL> return None <EOL> def destroy_area ( self , area_name , playername ) : <EOL> """<STR_LIT>""" <EOL> self . log . warning ( '<STR_LIT>' % ( playername , area_name ) ) <EOL> area = self . get_area ( area_name ) <EOL> if not area : <EOL> return '<STR_LIT>' % area_name <EOL> if self . default_location and self . default_location . area == area : <EOL> self . default_location = None <EOL> for player in self . player_list . values ( ) : <EOL> if player . location and ( player . location . area . name == area . name ) : <EOL> player . update_output ( '<STR_LIT>' ) <EOL> player . location . remove_char ( player ) <EOL> player . location = self . default_location <EOL> if player . mode and ( player . mode . name == '<STR_LIT>' ) : <EOL> if player . mode . edit_area and ( player . mode . edit_area . name == area_name ) : <EOL> player . mode . edit_area = None <EOL> player . mode . edit_object = None <EOL> if playername != player . name : <EOL> player . update_output ( '<STR_LIT>' % <EOL> playername . capitalize ( ) ) <EOL> if player . last_mode and ( player . last_mode . name == '<STR_LIT>' ) : <EOL> if player . last_mode . edit_area and ( player . last_mode . edit_area . name == area_name ) : <EOL> player . last_mode . edit_area = None <EOL> player . last_mode . edit_object = None <EOL> if playername != player . name : <EOL> player . update_output ( '<STR_LIT>' % <EOL> playername . capitalize ( ) ) <EOL> item_keys = area . items . keys ( ) <EOL> for item in item_keys : <EOL> self . log . debug ( area . destroy_item ( item ) ) <EOL> script_keys = area . scripts . keys ( ) <EOL> for script in script_keys : <EOL> self . log . debug ( area . destroy_script ( script ) ) <EOL> npc_keys = area . npcs . keys ( ) <EOL> for npc in npc_keys : <EOL> self . log . debug ( area . destroy_npc ( npc ) ) <EOL> room_keys = area . rooms . keys ( ) <EOL> self . log . debug ( '<STR_LIT>' ) <EOL> for room in room_keys : <EOL> self . log . debug ( area . destroy_room ( room ) ) <EOL> self . log . debug ( '<STR_LIT>' ) <EOL> area . destruct ( ) <EOL> del self . areas [ area . name ] <EOL> area . name = None <EOL> self . log . info ( '<STR_LIT>' % ( playername , area_name ) ) <EOL> return '<STR_LIT>' % area_name <EOL> def tell_players ( self , message , exclude_list = [ ] , color = wecho_color ) : <EOL> """<STR_LIT>""" <EOL> message = color + message + clear_fcolor <EOL> for player in self . player_list . values ( ) : <EOL> if player . name in exclude_list : <EOL> pass <EOL> elif player . mode and player . mode . name != '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> player . update_output ( message ) <EOL> def has_player ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in self . player_list : <EOL> return True <EOL> return False <EOL> def player_exists ( self , player_name ) : <EOL> """<STR_LIT>""" <EOL> row = self . db . select ( '<STR_LIT>' , [ player_name ] ) <EOL> if row : <EOL> return True <EOL> return False <EOL> def get_player ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . player_list . get ( name ) <EOL> def player_add ( self , player ) : <EOL> key = player . name <EOL> if isinstance ( key , basestring ) : <EOL> key = key . lower ( ) <EOL> self . player_list [ key ] = player <EOL> def player_remove ( self , playername ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( playername , basestring ) : <EOL> playername = playername . lower ( ) <EOL> self . player_delete . append ( playername ) <EOL> def battle_add ( self , battle ) : <EOL> if not self . battles : <EOL> battle . id = <NUM_LIT:1> <EOL> else : <EOL> battle . id = max ( self . battles . keys ( ) ) + <NUM_LIT:1> <EOL> self . battles [ battle . id ] = battle <EOL> self . log . debug ( "<STR_LIT>" % str ( self . battles ) ) <EOL> def battle_remove ( self , battle_id ) : <EOL> if battle_id in self . battles : <EOL> self . battles_delete . append ( battle_id ) <EOL> def npc_subscribe ( self , npc ) : <EOL> """<STR_LIT>""" <EOL> self . active_npcs . append ( npc ) </s>
