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
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
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}')
def end_turn(self): """ Ask to end the turn Returns: None """ comm.send(self, ['turn'])
def roll_dice(self): """ Ask for a dice roll Returns: None """ comm.send(self, ['dices'])
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}')
def reveal(self): """ Ask to reveal the character Returns: None """ comm.send(self, ['reveal'])
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
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])
def draw(self, i): """ Ask to draw a card from pile i Args: i (int): Returns: None """ comm.send(self, ['draw', i])
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])
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
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
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')
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')
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)
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()
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
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
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()
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}')
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
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)
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
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()
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()
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}')
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')
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)
def take_equipment(self, i_player, i_equipment): comm.send(self, ['take', i_player, i_equipment])
def triggerVibration(): jsonStr = '{ "type":"vibrate"}' comm.send(jsonStr)