Exemplo n.º 1
0
 def create_server(self,s_name,field_xy):
     if global_state == 0 or shut_down_level != 0:
         return
     ChooseType.button4.config(state="disabled")
     # addition game field size check
     GameWindow.vSer.set(s_name)
     m_split=field_xy.split('X')
     try:
         x_size = int(m_split[0])
     except:
         x_size = 10  
     try:
         y_size = int(m_split[1])
     except:
         y_size = 10                    
     if (x_size < 2 or x_size > MAX_GF_SIZE):
         print 'The gamefield\'s dimensions size should be between 3 and '+str(MAX_GF_SIZE)+' !'
         x_size = 10             
     if (y_size < 2 or y_size > MAX_GF_SIZE):
         print 'The gamefield\'s dimensions size should be between 3 and '+str(MAX_GF_SIZE)+' !'
         y_size = 10
     # Start the world
     CreateServer.local_world = engine.World(x_size,y_size)
     et = engine.engine_thread(ENGINE_SLEEP, CreateServer.local_world)
     et.start()
     THREADS.append(et)       
     # Initialize the server
     server_sock = server.init_server()
     set_globvar(server_sock.getsockname())
     st = server.server_thread(s_name, server_sock, CreateServer.local_world)        
     st.start() # Start the server thread
     THREADS.append(st)     
     # Initialize the broadcaster
     bc_sock = server.init_broadcaster()
     # Start broadcasting thread
     bt = server.announce_bc_thread(bc_sock, BROADCASTING_PORT, s_name, CreateServer.local_world)
     bt.start()
     THREADS.append(bt)
Exemplo n.º 2
0
def main():
    # First screen in GUI. If user selects to be a server, initialize the server and the world.
    mode = raw_input('Welcome! Start server: S, Join game: Enter: ').upper()
    # This will be the second screen in the GUI
    if mode == 'S':
        # Get Server name
        s_name = raw_input('Enter server name: ')
        # Get game field size
        x_size = 7 # For the time being doesn't matter
        y_size = 7
        # Start the world
        world = engine.World(x_size,y_size)
        et = engine.engine_thread(0.5, world)
        et.start()
        THREADS.append(et)
        
        # Initialize the server
        server_sock = server.init_server()
        st = server.server_thread(s_name, server_sock, world)        
        st.start() # Start the server thread
        THREADS.append(st)
        
        # Initialize the broadcaster
        bc_sock = server.init_broadcaster()
        # Start broadcasting thread
        bt = server.announce_bc_thread(bc_sock, BROADCASTING_PORT, s_name)
        bt.start()
        THREADS.append(bt)
        # This will be the third screen in the GUI
        # Join a local game  
        pl_name = raw_input('Enter your name: ')
        pl_class = raw_input('Enter your class! Frog: FROG, Fly: FLY, Spectator:S: ')
        print 'Joining a local game!'
        # This is the fourth screen in the GUI
        runtime_game_handler(pl_name, pl_class, world)
    else:    
        # This will be the third screen in the GUI
        # Join a remote game  
        pl_name = raw_input('Enter your name: ')
        pl_class = raw_input('Enter your class! Frog: FROG, Fly: FLY, Spectator:S: ')
        print 'Joining a remote game!'
        # Start the listening thread.
        refresh = True
        while refresh:
            print 'Refreshing the server list...'
            client_term.listen_broadcasts(BROADCASTING_PORT, SERVER_LIST, LISTENING_TIMEOUT)
            nr = 1
            for server_el in SERVER_LIST:
                print str(nr)+". Name: "+str(server_el[0])+".\n   IP: "+str(server_el[1])+".\n   Timestamp: "+str(time.asctime())
            answer = raw_input("Want to refresh the list again? Y = Yes, N = No: ").upper()
            if answer == 'N':
                refresh = False
                
        answer = 0
        while answer < 1 or answer > len(SERVER_LIST):
            reply = raw_input('Choose the server you with to connect to using the appropriate list index (1,2,3...): ')
            answer = int(reply)
        print 'Thank you and have a great game!'        
        server_id = SERVER_LIST[int(answer) - 1]
        print server_id
        # This is the fourth screen in the GUI
        client_term.client_task(server_id[1],pl_name,pl_class)