def handle_events():
    global loaded_indicator
    global load_match_progress_timer
    
    if loaded_indicator == False:
        load()
        loaded_indicator = True
    
    #loading_match_label.draw(gamestate.screen)
    
    #load_match_progress_timer += gamestate.clock.get_time()
    
    #if load_match_progress_timer > 3000:
    #    loading_match_label.set_text(loading_match_label.text + ".")
    #    load_match_progress_timer = 0
    
    #if loading_match_label.text == LOADING_MATCH_TEXT + "....":
    #    loading_match_label.set_text(LOADING_MATCH_TEXT)
    
    if versusclient.listener.server_mode == versusserver.ServerModes.MATCH:
        
        unload()
        gamestate.mode = gamestate.Modes.ONLINEVERSUSMODE
    
    versusclient.get_network_messages()
    versusclient.listener.Pump()
def unload():
    global loaded
    global exit_button
    global start_match_label
    global ip_address_input
    global player_status_ui_dictionary
    global join_match_button
    global assigned_positions
    global spectate_button
    global local_player_position
    global local_player_container_created
    
    exit_button = None
    loaded = False
    start_match_label = None
    network_message_label = None
    join_match_button = None
    assigned_positions = None
    spectate_button = None
    player_status_ui_dictionary = None
    local_player_position = None
    local_player_container_created = False
    
    if versusclient.connected():
        unregister_network_callbacks()
        
        #clean up any remaining messages to the client
        versusclient.get_network_messages()
        versusclient.listener.Pump()
        
        versusclient.listener.close()
        versusclient.unload()
        print("listener closed")
    
    if gamestate.hosting:
        #clean up any remaining messages to the server
        versusserver.server.Pump()
        
        versusserver.server.close()
        versusserver.server = None
        print("server closed")
        
        versusserver.broadcast.close()
        versusserver.broadcast = None
        print("server broadcasting closed")
        
        gamestate.hosting = False
 def handle_network_events(self):
     if versusclient.get_connection_status() != versusclient.ConnectionStatus.DISCONNECTED:
         
         if versusclient.listener.server_mode == versusserver.ServerModes.MOVESET_SELECT:
             print("match exited")
             if self.exiting == False and self.exit_indicator == False:
                 self.exit(False)
             
             gamestate.mode = gamestate.Modes.ONLINEVERSUSMOVESETSELECT
         
         versusclient.get_network_messages()
         versusclient.listener.Pump()
         
     else:
         print("disconnected")
         if self.exiting == False and self.exit_indicator == False:
             self.exit()
         
         gamestate.mode = gamestate.Modes.ONLINEMENUPAGE
def handle_events():
    global loaded
    global exit_button
    global start_match_label
    global player_status_ui_dictionary
    global join_match_button
    global local_player_container_created
    global local_player_position
    
    if loaded == False:
        load()
    
    if pygame.MOUSEBUTTONDOWN in wotsuievents.event_types:
        if exit_button.contains(wotsuievents.mouse_pos):
            exit_button.handle_selected()
        
        if start_match_label.active:
            if start_match_label.contains(wotsuievents.mouse_pos):
                start_match_label.handle_selected()
        
        if join_match_button.active:
            if join_match_button.contains(wotsuievents.mouse_pos):
                join_match_button.handle_selected()
        
        if spectate_button.active:
            if spectate_button.contains(wotsuievents.mouse_pos):
                spectate_button.handle_selected()
        
    if pygame.MOUSEBUTTONUP in wotsuievents.event_types:
        if exit_button.selected:
            exit_button.handle_deselected()
            
            if exit_button.contains(wotsuievents.mouse_pos):
                gamestate.mode = gamestate.Modes.ONLINEMENUPAGE
                unload()
        
        elif start_match_label.selected:
            if start_match_label.contains(wotsuievents.mouse_pos):
                start_match_label.handle_deselected()
                versusclient.listener.load_match_data()
        
        #TODO - inactivate join if selected and same for spectate
        elif join_match_button.selected:
            if join_match_button.contains(wotsuievents.mouse_pos):
                versusclient.listener.join_match()
                join_match_button.handle_deselected()
                join_match_button.inactivate()
                
                spectate_button.activate()
        
        elif spectate_button.selected:
            if spectate_button.contains(wotsuievents.mouse_pos):
                versusclient.listener.spectate_match()
                spectate_button.handle_deselected()
                spectate_button.inactivate()
                join_match_button.activate()
    
    if loaded:
        
        if versusclient.dummies_only() and start_match_label.active == False:
            start_match_label.activate()
            
        
        if local_player_container_created:
            players_ready = True
            
            local_player_position = versusclient.get_local_player_position()
            
            if player_status_ui_dictionary[local_player_position].player_ready() and \
            not versusclient.listener.player_positions_ready_dictionary[local_player_position]:
                
                versusclient.listener.player_ready()
            
            for player_status_ui in player_status_ui_dictionary.values():
                if hasattr(player_status_ui, "player_ready"):
                    players_ready = (getattr(player_status_ui, "player_ready")() and players_ready)
                else:
                    players_ready = False
            
            if players_ready:
                if start_match_label.active == False:
                    start_match_label.activate()
            else:
                if start_match_label.active:
                    start_match_label.inactivate()
        
        for player_status_ui in player_status_ui_dictionary.values():
            player_status_ui.handle_events()
            player_status_ui.draw(gamestate.screen)
        
        join_match_button.draw(gamestate.screen)
        exit_button.draw(gamestate.screen)
        start_match_label.draw(gamestate.screen)
        spectate_button.draw(gamestate.screen)
        
        #Network Dependent Event Handling
        if (versusclient.listener.connection_status == 
        versusclient.ConnectionStatus.DISCONNECTED):
            
            unload()
            gamestate.mode = gamestate.Modes.ONLINEMENUPAGE
        
        if versusclient.connected() or gamestate.hosting:
            if not versusclient.local_player_is_in_match():
                if not join_match_button.active:
                    join_match_button.activate()
                
                if spectate_button.active:
                    spectate_button.inactivate()
                
            else:
                
                if not spectate_button.active:
                    spectate_button.activate()
                
                if join_match_button.active:
                    join_match_button.inactivate()
            
            update_player_data()
            
            if (versusclient.listener.server_mode == 
            versusserver.ServerModes.LOADING_MATCH_DATA) or \
            (versusclient.listener.server_mode == versusserver.ServerModes.MATCH):
                gamestate.mode = gamestate.Modes.ONLINEMATCHLOADER
            
            versusclient.get_network_messages()
            versusclient.listener.Pump()