Пример #1
0
	def __init__(self,hostname, port):
		self.hostname = hostname
		self.port = port
		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

		

		try:
			self.sock.connect((hostname, port))
			print('Connected to: ' + str(hostname) + ':' + str(port))
			print('Waiting for other clients...')
			print('Exchanging init information...')

			# RECEIVE SERVER INIT MESSAGE and RESPOND WITH CLIENT INIT MESSAGE -------------------------------------------------------------------

			cli_init = init_message.InitMessageFromClient()
			cli_init = socket_utilities.convert_to_bytes(cli_init)
			socket_utilities.send_data(self.sock, cli_init)

			ser_init = socket_utilities.recv_data(self.sock)
			ser_init = socket_utilities.convert_to_object(ser_init)

			# STARTING NORMAL GAME FLOW -----------------------------------------------------------------------------------------------------------
			while True:
				data = self.listen_for_update()
				if data == b'END':
					print('Simulation ended successfully!')
					return
				elif data == b'START':
					print("Simulation running...")
				self.send_response()
		except socket.error as e:
			print(e)

			return
Пример #2
0
    def receive_client_inputs(self):
        #print('waiting for client inputs...')
        inputs = []

        for c in self.connections:
            #print("receiveing from: " + str(self.connections.index(c)))
            temp = socket_utilities.recv_data(c)
            temp = socket_utilities.convert_to_object(temp)
            #temp = c.recv(1024).decode('utf-8')
            inputs.append(temp)

        return inputs
Пример #3
0
    def listen_for_update(self):
        #print('Listening for update...')
        data = b''
        #temp = self.sock.recv(1024)

        data = socket_utilities.recv_data(self.sock)
        #while len(temp.decode('utf-8')) > 0:
        #data += temp
        #break
        #temp = self.sock.recv(1024)

        return data
Пример #4
0
    def __init__(self, hostname, port, n_clients, n_iterations, sim_name):
        #Create history object for this simulation
        self.gamehistory = game_history.GameHistory(sim_name)
        self.gamedata = game_data.GameData()
        #Program Process

        #Initialize Server with Values

        print("Started game at " + hostname + ':' + str(port))
        self.hostname = hostname
        self.port = port

        #Creating and binding a server socket to a port
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            self.sock.bind((hostname, port))
        except socket.error as e:
            return

        #Waiting for and connecting to clients
        print('Waiting for ' + str(n_clients) + ' clients...')
        self.sock.listen(n_clients)

        #Accepting connections and saving them
        for c in range(0, n_clients):
            conn, addr = self.sock.accept()
            self.connections.append(conn)
            print('Client ' + str(len(self.connections) - 1) +
                  ' Connected at: ' + str(addr[0]) + ':' + str(addr[1]))

            self.gamedata.players.append(
                game_data.Player(c, float(random.randint(-8000, 8000)),
                                 float(random.randint(-8000, 8000)),
                                 float(random.randint(0, 360))))

            print('\tExchanging init information...')

            cli_init = socket_utilities.recv_data(conn)
            cli_init = socket_utilities.convert_to_object(cli_init)

            ser_init = init_message.InitMessageFromServer(c)
            ser_init = socket_utilities.convert_to_bytes(ser_init)
            socket_utilities.send_data(conn, ser_init)

            print('\tFinished exchanging init information...\n')

        #Starting Game Process
        print('Starting ' + str(n_iterations) + ' iterations...')
        start = time.time()
        for c in self.connections:
            #c.send('START'.encode())
            socket_utilities.send_data(c, 'START'.encode())

        for i in range(0, n_iterations):
            if self.gamedata.game_over:
                break
            self.broadcast_game_data()
            given = self.receive_client_inputs()
            self.process_game_data(given)

        if (not self.gamedata.game_over):
            self.gamedata.game_over = True
            print("The game ended in a draw!")

        #Saves last bit of data
        if not self.gamehistory.is_empty():
            self.gamehistory.save_to_file()

        #Closes client connections
        self.close_connections()

        #print(self.gamedatahistory)
        print('Simulation ending...')
        print('Elapsed time: ' + str(time.time() - start))