Ejemplo n.º 1
0
    def check_dead_players(self):
        # check for dead player
        dead_players = {}
        info_to_pop = []
        for client in self.clients:
            for username, info in client.game_dead_players.items():
                if not username in dead_players.keys():
                    dead_players[username] = {
                        'count': 1,
                        'killer': info['killer']
                    }
                else:
                    dead_players[username]['count'] += 1
                # reduce lifetime of infos
                client.game_dead_players[username]['life'] -= 1
                if client.game_dead_players[username]['life'] == 0:
                    info_to_pop.append([client, username])

        for client, username in info_to_pop:
            client.game_dead_players.pop(username)

        # if every player send death -> confirm death
        for username, info in dead_players.items():
            if info['count'] == self.n_clients:
                for client in self.clients:
                    killer = info['killer']
                    send(client.conn, f'env|dead|{username}|{killer}')
                    try:
                        client.game_dead_players.pop(username)
                    except:
                        pass
Ejemplo n.º 2
0
    def check_hit_player(self):
        # check for hit player
        hit_players = {}
        info_to_pop = []
        for client in self.clients:
            for username, info in client.game_hit_players.items():
                if not username in hit_players.keys():
                    hit_players[username] = {
                        'count': 1,
                        'damage': info['damage'],
                        'shooter': info['shooter']
                    }
                else:
                    hit_players[username]['count'] += 1
                # reduce lifetime of infos
                client.game_hit_players[username]['life'] -= 1
                if client.game_hit_players[username]['life'] == 0:
                    info_to_pop.append([client, username])

        for client, username in info_to_pop:
            client.game_hit_players.pop(username)

        # if every player send death -> confirm death
        for username, info in hit_players.items():
            if info['count'] == self.n_clients:
                for client in self.clients:
                    damage = info['damage']
                    shooter = info['shooter']
                    send(client.conn, f'env|hit|{username}|{damage}|{shooter}')
                    try:
                        client.game_hit_players.pop(username)
                    except:
                        pass
Ejemplo n.º 3
0
 def quit_game(self, username):
     index = self.usernames.index(username)
     self.in_games[index] = False
     # send to client that one quit game
     for client in self.clients:
         if client.username != username:
             send(client.conn, f'env|quitgame|{username}')
Ejemplo n.º 4
0
    def end_turn(self):
        """
        Ask to end the turn

        Returns:
            None
        """
        comm.send(self, ['turn'])
Ejemplo n.º 5
0
    def roll_dice(self):
        """
        Ask for a dice roll

        Returns:
            None
        """
        comm.send(self, ['dices'])
Ejemplo n.º 6
0
 def del_friend(self, username):
     # update data
     other_index = Data.users[Data.users['username'] == username].index[0]
     Data.users.loc[self.index, 'friends'].remove(other_index)
     Data.users.loc[other_index, 'friends'].remove(self.index)
     if Interaction.is_connected(username):
         other_client = Interaction.get_client(username)
         send(other_client.conn, f'delfr|{self.username}')
Ejemplo n.º 7
0
    def reveal(self):
        """
        Ask to reveal the character

        Returns:
            None
        """
        comm.send(self, ['reveal'])
Ejemplo n.º 8
0
 def getClassInstance(self, className):
     sock = self.connect()
     # Request instance of TestClass above
     comm.send(sock, "getClassInstance")
     # After a "getClassInstance" request, we must provide a class name
     comm.send(sock, className)
     # Then we receive back the remote instance of that class
     classInstance = comm.recv(sock)
     sock.close()
     return classInstance
Ejemplo n.º 9
0
    def send_token(self, i):
        """
        Send the coordinates of the token i

        Args:
            i (int):

        Returns:
            None
        """
        comm.send(self, ['token', i, self.game.tokens[i].center])
Ejemplo n.º 10
0
    def draw(self, i):
        """
        Ask to draw a card from pile i

        Args:
            i (int):

        Returns:
            None
        """
        comm.send(self, ['draw', i])
Ejemplo n.º 11
0
    def send_vision(self, i_vision, i_player):
        """
        Send the vision i_vision to the player i_player

        Args:
            i_vision (int):
            i_player (int):

        Returns:
            None
        """
        comm.send(self, ['vision', i_vision, i_player])
Ejemplo n.º 12
0
    def disconn_friend(cls, disc_client):

        friends, _ = cls.get_connected_friends(disc_client)

        # send to all connected friends that the new client is disconnected
        for friend in friends:
            send(friend.conn, f'fr|disconn|{disc_client.username}')
        
        # remove client from Interaction
        try:
            cls.clients.remove(disc_client)
        except: pass
Ejemplo n.º 13
0
 def add_client(self, new_client):
     # send to the new client all the connect players
     usernames = self.usernames.copy()
     usernames = '|'.join(usernames)
     send(new_client.conn, f'env|conn|{usernames}')
     # send to the other client that there is a new one
     for client in self.clients:
         send(client.conn, f'env|conn|{new_client.username}')
     self.clients.append(new_client)
     new_client.team = 0
     self.usernames.append(new_client.username)
     self.n_clients += 1
Ejemplo n.º 14
0
def left_way(frame, x_l_b, x_r_l, y_l_t, y_l_b, x_sli_r_l, x_r_b, y_r_t, y_r_b,
             x4_top_left, x2_top_right, y2_top_right, y3_bottom_left):
    check_slight_left = computation(frame, x_l_b, x_r_l, y_l_t, y_l_b,
                                    LeftRight_validity)
    check_slight_right = computation(frame, x_sli_r_l, x_r_b, y_r_t, y_r_b,
                                     LeftRight_validity)
    check_straight = computation(frame, x4_top_left, x2_top_right,
                                 y2_top_right, y3_bottom_left,
                                 straight_validity)

    #print check_slight_right

    if check_slight_left == 1:
        #print "L"
        print 'IMAGE PROCESSING : left'
        comm.send('left')

    elif check_slight_left == 0 and check_straight == 1:
        #print "S"
        print 'IMAGE PROCESSING : straight'
        comm.send('straight')

    elif check_slight_left == 0 and check_straight == 0 and check_slight_right == 1:
        #print "R"
        print 'IMAGE PROCESSING : right'
        comm.send('right')

    else:
        #STOP
        print 'IMAGE PROCESSING : stop'
        comm.send('stopt')
Ejemplo n.º 15
0
def straight_way(frame, x_l_b, x_r_l, y_l_t, y_l_b, x_sli_r_l, x_r_b, y_r_t,
                 y_r_b, x4_top_left, x2_top_right, y2_top_right,
                 y3_bottom_left):
    check_slight_left = computation(frame, x_l_b, x_r_l, y_l_t, y_l_b,
                                    LeftRight_validity)
    check_slight_right = computation(frame, x_sli_r_l, x_r_b, y_r_t, y_r_b,
                                     LeftRight_validity)
    check_straight = computation(frame, x4_top_left, x2_top_right,
                                 y2_top_right, y3_bottom_left,
                                 straight_validity)
    if check_slight_left == 0 and check_straight == 1 and check_slight_right == 1:
        #print "R" needs to be slight right
        print 'IMAGE PROCESSING : right'
        comm.send('right')

    elif check_slight_left == 1 and check_straight == 1 and check_slight_right == 0:
        #print "L"
        print 'IMAGE PROCESSING : left'
        comm.send('left')
    elif check_slight_left == 1 and check_straight == 1 and check_slight_right == 1:
        print 'IMAGE PROCESSING : straight'
        comm.send('straight')

    else:
        print 'IMAGE PROCESSING : STOP'
        comm.send('stop')
Ejemplo n.º 16
0
def main():
    commutils.set_comm(create_comm())
    comm = commutils.comm()

    if commutils.procid() == 0:
        while True:
            msg = commutils.time()
            comm.send(msg, dest=1, tag=1)
            time.sleep(0.5)

    elif commutils.procid() == 1:
        while True:
            comm.recv(source=0, tag=1)
            time.sleep(0.5)
Ejemplo n.º 17
0
    def run_in_game(self, msgs):
        # check that there is still somebody in game
        try:
            self.in_games.index(True)
        except:
            self.in_game = False
            self.reset_game()

        # send msgs to clients
        msg = self.build_frame_msg(msgs)
        if msg != 'env':
            for client in self.clients:
                send(client.conn, msg)

        self.check_hit_player()
        self.check_dead_players()
        self.check_items()
Ejemplo n.º 18
0
    def handeln_team(self, msg):
        if msg[0] == 'create':
            # create a new team
            # send to the clients that one created a team
            for client in self.clients:
                send(client.conn, f'env|team|create')

        elif msg[0] == 'change':
            username = msg[1]
            team_idx = int(msg[2])
            # send to the other clients that one changes team
            for client in self.clients:
                if client.username != username:
                    send(client.conn, f'env|team|change|{username}|{team_idx}')
                else:
                    # send the team of the user
                    client.team = team_idx
Ejemplo n.º 19
0
 def processCommand(self, clientsock, clientaddr):
     # Comm.recv just sits and waits until it hears SOMETHING from the client
     commandMessage = comm.recv(clientsock)
     if commandMessage == "getClassInstance":
         # If that's "Get Class" it should be immediately followed by a class definition
         className = comm.recv(clientsock)
         # Get an instance of that class (it should already exist, but if it doesn't GetClassInstance will try to create it)
         classInstance = self.getClassInstance(className)
         # Then return the instance to the client
         comm.send(clientsock, classInstance)
     elif commandMessage == "saveClassInstance":
         # If that's "Save Class" it should be immediately followed by a remote system path, then the class instance
         modulePaths = comm.recv(clientsock)
         classInstance = comm.recv(clientsock)
         # Save that instance (overwriting any existing instances of the same class)
         self.saveClassInstance(modulePaths, classInstance)
     elif commandMessage == "break":
         self.dying = True
Ejemplo n.º 20
0
 def __init__(self, clients, usernames):
     self.clients = list(clients)
     for client in self.clients:
         client.team = 0
     self.usernames = list(usernames)
     self.n_clients = len(self.clients)
     self.in_games = [False for _ in range(self.n_clients)]
     self.ready_players = []  # players who have chosen there weapon
     self.n_player_game_sess = 0  # players who pushed the play button
     # send to clients that there in an env
     for client in self.clients:
         usernames = self.usernames.copy()
         usernames.remove(client.username)
         usernames = '|'.join(usernames)
         send(client.conn, f'env|conn|{usernames}')
     # start thread for run func
     run_thread = threading.Thread(target=self.run)
     run_thread.start()
Ejemplo n.º 21
0
 def processCommand(self, clientsock, clientaddr):
     # Comm.recv just sits and waits until it hears SOMETHING from the client
     commandMessage = comm.recv(clientsock)
     if commandMessage == "getClassInstance":
         # If that's "Get Class" it should be immediately followed by a class definition
         className = comm.recv(clientsock)
         # Get an instance of that class (it should already exist, but if it doesn't GetClassInstance will try to create it)
         classInstance = self.getClassInstance(className)
         # Then return the instance to the client
         comm.send(clientsock, classInstance)
     elif commandMessage == "saveClassInstance":
         # If that's "Save Class" it should be immediately followed by a remote system path, then the class instance
         modulePaths = comm.recv(clientsock)
         classInstance = comm.recv(clientsock)
         # Save that instance (overwriting any existing instances of the same class)
         self.saveClassInstance(modulePaths, classInstance)
     elif commandMessage == "break":
         self.dying = True
Ejemplo n.º 22
0
 def quit(self, username):
     client = Interaction.get_client(username)
     # update client attributes
     client.in_env = False
     client.env = None
     send(client.conn, f'env|stop')
     # send to friends: out env
     Interaction.inform_outenv(client.username)
     # update env attributes
     self.clients.remove(client)
     self.usernames.remove(username)
     self.n_clients -= 1
     # check if there is still someone
     if self.n_clients == 0:
         self.active = False
     else:
         # inform other user in env
         for client in self.clients:
             send(client.conn, f'env|quit|{username}')
Ejemplo n.º 23
0
    def check_items(self):
        to_pop = []
        count_info = {}  # count to see if each players send the info
        for client in self.clients:
            for mix in client.game_items.keys():
                # check type of item
                if 'health' in mix:
                    if not mix in count_info.keys():
                        count_info[mix] = {
                            'count': 1,
                            'pos_idx': client.game_items[mix]['pos_idx']
                        }
                    else:
                        count_info[mix]['count'] += 1
                # reduce lifetime of infos
                client.game_items[mix]['life'] -= 1
                if client.game_items[mix]['life'] == 0:
                    to_pop.append([client, mix])

        for client, mix in to_pop:
            client.game_items.pop(mix)
            send(
                client.conn, f'env|item|{mix}|0|0'
            )  # second zero means nothing (but has to be there for in the case where it's confirmed)

        # if every players send item -> confirm item
        for mix, info in count_info.items():
            if info['count'] == self.n_clients:
                # set new pos idx
                available_idx = [0, 1, 2]
                available_idx.remove(info['pos_idx'])
                new_pos_idx = choice(available_idx)
                for client in self.clients:
                    send(client.conn,
                         f'env|item|{mix}|1|{new_pos_idx}')  # 1 for confirmed
                    try:
                        client.game_items.pop(mix)
                    except:
                        pass
Ejemplo n.º 24
0
 def sign(self, username, password):
     select_name_index = Data.users[Data.users['username'] ==
                                    username].index
     if len(select_name_index) != 0:
         send(self.conn, 'taken')
         return
     else:
         Data.users = Data.users.append(
             {
                 'username': username,
                 'password': password.strip(),
                 'friends': [],
                 'demands': [],
                 'kills': 0,
                 'deaths': 0,
                 'played': 0
             },
             ignore_index=True)
         store_user_data()
         send(self.conn, 'signed')
         sleep(
             .1)  # let the time to the client to start the separated thread
         self.on_log(username)
Ejemplo n.º 25
0
    def body(self):
        sleep(0.03)

        msgs = self.get_frame_msgs()

        if self.in_game:
            self.run_in_game(msgs)

        elif self.in_game_session:
            for username, current_msgs in msgs.items():
                if current_msgs:
                    if current_msgs[0] == 'ready':
                        weapon = current_msgs[1]
                        char = current_msgs[2]
                        team = current_msgs[3]
                        for client in self.clients:
                            if username != client.username:
                                send(
                                    client.conn,
                                    f'env|ready|{username}|{weapon}|{char}|{team}'
                                )
                        d = {
                            'username': username,
                            'weapon': weapon,
                            'char': char,
                            'team': team
                        }
                        self.ready_players.append(d)

            if len(self.ready_players) == len(self.clients):
                self.in_games = [True for _ in range(self.n_clients)]
                self.in_game = True
        else:
            if self.n_player_game_sess == len(self.clients):
                self.send_play_msg()
                self.in_game_session = True
Ejemplo n.º 26
0
    def save(self, classInstance):
        if not classInstance.__class__.__name__ in self.classInstances.keys():
            self.classInstances[
                classInstance.__class__.__name__] = classInstance

        # Inform the server we have updates to the local class
        sock = self.connect()
        comm.send(sock, "saveClassInstance")
        # After a "Save Class" request, we must provide modulePaths (for importing modules) and then the class instance
        comm.send(sock, self.modulePaths)
        comm.send(sock, classInstance)
        sock.close()
Ejemplo n.º 27
0
    def run(cls):
        while 1:
            sleep(.2)
            
            # check that clients are connected
            for client in cls.clients:
                if not client.connected:
                    cls.clients.remove(client)

            # collect messages from clients
            for client in cls.clients:
                if client.chat_current_msg:
                    content = (client.username, client.chat_current_msg)
                    cls.current_contents.append(content)
                    client.chat_current_msg = None

            # send messages to clients
            for client in cls.clients:
                for content in cls.current_contents:
                    if content[0] != client.username:
                        send(client.conn, 'chat|' + content[0] + '|' + content[1])
            
            # empties the messages list
            cls.current_contents = []
    
            # collect fr demand from clients
            for client in cls.clients:
                if client.current_demand:
                    # check for valid username
                    if Data.is_valid_username(client.current_demand):
                        cls.current_demands[client.username] = client.current_demand
                        # send to the client that the username is valid
                        send(client.conn, 'dfrv|1')
                    else:
                        # send to the client that the username is not valid
                        send(client.conn, 'dfrv|0')
                    client.current_demand = None
            
            # send demands to clients
            for requester, requested in cls.current_demands.items():
                # update data
                index_requester = Data.users[Data.users['username'] == requester].index[0]
                Data.users[Data.users['username'] == requested]['demands'].values[0].append(index_requester)

                if cls.is_connected(requested):
                    current_client = cls.get_client(requested)
                    current_client.look_for_fr_request()
            
            # empties the demands dictionnary
            cls.current_demands = {}

            store_user_data()
Ejemplo n.º 28
0
    def manage_friends(cls, new_client):

        friends, client_friends = cls.get_connected_friends(new_client)
        
        # send to all connected friends that the new client is connected
        for friend in friends:
            send(friend.conn, f'fr|conn|{new_client.username}')
        
        # send to new client all connected friends
        for i, is_conn in client_friends.items():
            username = Data.users.loc[i,'username']
            if is_conn:
                send(new_client.conn, f'fr|conn|{username}')
            else:
                send(new_client.conn, f'fr|disconn|{username}')
Ejemplo n.º 29
0
 def log(self, username, password):
     # check for valide username and password
     select_name_index = Data.users[Data.users['username'] ==
                                    username].index
     if len(select_name_index) == 0:
         send(self.conn, 'false')
         return
     index = select_name_index[0]
     if Data.users.iloc[index, 1] == password.strip():
         send(self.conn, 'logged')
         sleep(
             .1)  # let the time to the client to start the separated thread
         self.on_log(username)
     else:
         send(self.conn, 'false')
Ejemplo n.º 30
0
def update_servo_pos(conn):
	state = controller.get_state()
	new_pos = int((-1 * math.degrees( math.asin( state['trigger'] ) )) + 90)
	print new_pos
	comm.send(conn,new_pos)
Ejemplo n.º 31
0
 def take_equipment(self, i_player, i_equipment):
     comm.send(self, ['take', i_player, i_equipment])
Ejemplo n.º 32
0
def triggerVibration():
    jsonStr = '{ "type":"vibrate"}'
    comm.send(jsonStr)