def sendKeyStatus(self, key, status): packet = struct.pack("=BBB", 0x43, key, status) try: connection.send(packet) except: if e.errno == 32: raise DisconnectException()
def data_transfer(): me_data = me.make_data_package() connection.send(me_data, OTHER_HOST, OTHER_PORT) # the send code enemy_data = server.receive() # the receive code enemy.count = int(enemy_data[:4]) enemy.name = enemy_data[4:]
def sendMouse(self, position=(0.0, 0.0), velocity=(0.0, 0.0)): packet = struct.pack("=Bffff", 0x44, position[0], position[1], velocity[0], velocity[1]) try: connection.send(packet) except socket.error as e: if e.errno == 32: raise DisconnectException()
def data_transfer(): me_data = player.make_data_package() connection.send(me_data, OTHER_HOST, OTHER_PORT) # the send code enemy_data = server.receive() # the receive code player2.rect.centerx = int(enemy_data[:4]) player2.rect.centery = int(enemy_data[4:])
def sendQuitRequest(self): quit_header = { 'cmd': 'QUIT', 'info': { 'player_id': self.player.getPlayerId() } } send(self.connection, quit_header, self.server) reply, address = receive(self.connection, self.packet_size) print reply
def sendStartRequest(self): start_game_header = { 'cmd': 'START', 'info': { 'player_id': self.player.getPlayerId() } } send(self.connection, start_game_header, self.server) reply, address = receive(self.connection, self.packet_size) print reply
def sendReadyRequest( self ): ready_header = { 'cmd': 'READY', 'info': { 'player_id': self.player.getPlayerId() } } send( self.connection, ready_header, self.server ) reply, address = receive( self.connection, self.packet_size ) print reply
def sendReadyRequest(self): ready_header = { 'cmd': 'READY', 'info': { 'player_id': self.player.getPlayerId() } } send(self.connection, ready_header, self.server) reply, address = receive(self.connection, self.packet_size) print reply
def sendStartRequest( self ): start_game_header = { 'cmd': 'START', 'info': { 'player_id': self.player.getPlayerId() } } send( self.connection, start_game_header, self.server ) reply, address = receive( self.connection, self.packet_size ) print reply
def sendQuitRequest( self ): quit_header = { 'cmd': 'QUIT', 'info': { 'player_id': self.player.getPlayerId() } } send( self.connection, quit_header, self.server ) reply, address = receive( self.connection, self.packet_size ) print reply
def sendJoinRequest( self ): join_header = { 'cmd': 'JOIN', 'info': { 'name': self.player.getNick() } } send( self.connection, join_header, self.server ) reply, address = receive( self.connection, self.packet_size ) self.player.setPlayerId( reply['self']['id'] )
def sendSnakeDataRequest(self): snake_header = { 'cmd': 'SNAKEDATA', 'info': { 'player_id': self.player.getPlayerId(), 'data': self.constructor } } send(self.connection, snake_header, self.server) reply, address = receive(self.connection, self.packet_size) print reply
def sendSnakeDataRequest( self ): snake_header = { 'cmd': 'SNAKEDATA', 'info': { 'player_id': self.player.getPlayerId(), 'data': self.constructor } } send( self.connection, snake_header, self.server ) reply, address = receive( self.connection, self.packet_size ) print reply
def receiveJoinRequest(self, request, address): self.players[address] = { 'id': len(self.players) + 1, 'name': request['info']['name'], 'ready': False, 'playing': False } reply = { 'self': { 'id': self.players[address]['id'] }, 'other_players': "" } send(self.listener, reply, address) self.players[address]['player'] = Player(self.players[address]['name'], self.players[address]['id'])
def receiveStartRequest(self, request, address): for addr in self.players: if not self.players[addr]['ready']: reply = { 'response': 'ERROR', 'info': 'All the players are not yet ready to play.' } send(self.listener, reply, address) return False if address in self.players and self.players[address]['id'] == 1: time.sleep(10) self.sendData() else: reply = { 'response': 'ERROR', 'info': 'Only the player who joined first can start the game.' } send(self.listener, reply, address)
def login(self, user, serverpw, userpw): packet = struct.pack("=B16s16s16s", 0x42, user, serverpw, userpw) connection.send(packet)
def sendData(self): for address in self.players: self.players[address]['playing'] = True send(self.listener, reply, address)
def sendName( self ): send( self.connection, self.player.getNick(), self.server )
def sendName(self): send(self.connection, self.player.getNick(), self.server)
class TrickplayInspector(QWidget): def __init__(self, parent=None, f=0): """ UI Element property inspector made up of two QTreeViews """ QWidget.__init__(self, parent) self.ui = Ui_TrickplayInspector() self.ui.setupUi(self) self.ui.refresh.setEnabled(False) self.ui.search.setEnabled(False) # Ignore signals while updating elements internally self.preventChanges = False # Searches find multiple items with the same name #self.lastSearchedText = None #self.lastSearchedItem = None # Models self.inspectorModel = TrickplayElementModel(self) self.propertyModel = TrickplayPropertyModel() self.ui.inspector.setModel(self.inspectorModel) self.ui.property.setModel(self.propertyModel) self.ui.lineEdit.setPlaceholderText("Search by GID or Name") self.setHeaders(self.inspectorModel, ['UI Element', 'Name']) self.setHeaders(self.propertyModel, ['Property', 'Value']) # QTreeView selectionChanged signal doesn't seem to work here... # Use the selection model instead QObject.connect( self.ui.inspector.selectionModel(), SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.selectionChanged) # For changing checkboxes (visibility) QObject.connect( self.inspectorModel, SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)"), self.inspectorDataChanged) # For changing UI Element properties QObject.connect( self.propertyModel, SIGNAL("dataChanged(const QModelIndex&,const QModelIndex&)"), self.propertyDataChanged) QObject.connect(self.ui.refresh, SIGNAL("clicked()"), self.refresh) QObject.connect(self.ui.search, SIGNAL("clicked()"), self.userSearch) QObject.connect(self.ui.lineEdit, SIGNAL('returnPressed()'), self.userSearch) def refresh(self): """ Fill the inspector with Trickplay UI element data """ self.preventChanges = True # Reselect gid of last item selected gid = None try: index = self.selected(self.ui.inspector) item = self.inspectorModel.itemFromIndex(index) gid = item['gid'] except: gid = 1 # Get all new data self.inspectorModel.empty() self.inspectorModel.fill() self.propertyModel.empty() self.preventChanges = False # Find the last item after getting new data so that # both trees reflect the changes result = self.search(gid, 'gid') if result: self.selectItem(result) def setHeaders(self, model, headers): """ Set headers for a given model """ model.setHorizontalHeaderLabels(headers) def selected(self, view=None): """ Return the selected index from the view given or None """ view = view or self.ui.inspector try: i = view.selectionModel().selection() return i.indexes()[0] except: return None def userSearch(self): """ Perform a search and select the item found TODO: If search is pressed multiple times with the same string, then search for the next item matching the search """ text = self.ui.lineEdit.text() # Search by gid if possible, otherwise name property = None try: text = int(text) property = 'gid' except: property = 'name' #item = None #if self.lastSearchedText == text: # item = self.lastSearchedItem result = self.search(text, property) if result: print('Found', result['gid'], result['name']) #self.lastSearchedText = text #self.lastSearchedItem = item self.selectItem(result) else: print('UI Element not found') def search(self, value, property, start=None): """ Search for a node by one of its properties """ return self.inspectorModel.search(property, value, start) def selectItem(self, item): """ Select a row of the inspector model (as the result of a search) """ topLeft = item.index() bottomRight = item.partner().index() self.ui.inspector.scrollTo(topLeft, 3) self.ui.inspector.selectionModel().select( QItemSelection(topLeft, bottomRight), QItemSelectionModel.SelectCurrent) # "selectionChanged(QItemSelection, QItemSelection)" def selectionChanged(self, selected, deselected): """ Re-populate the property view every time a new UI element is selected in the inspector view. """ print(selected, deselected) if not self.preventChanges: self.preventChanges = True index = self.selected(self.ui.inspector) item = self.inspectorModel.itemFromIndex(index) data = item.TPJSON() print "-----------" print data print "-----------" self.propertyModel.fill(data) self.preventChanges = False # "dataChanged(const QModelIndex &, const QModelIndex &)" def inspectorDataChanged(self, topLeft, bottomRight): """ Change UI Element visibility using checkboxes """ if not self.preventChanges: self.preventChanges = True item = topLeft.model().itemFromIndex(topLeft) # Only nodes in the first column have checkboxes if 0 == item.column(): checkState = bool(item.checkState()) if self.sendData(item['gid'], 'is_visible', checkState): item['is_visible'] = checkState self.propertyModel.fill(item.TPJSON()) self.preventChanges = False # "dataChanged(const QModelIndex &, const QModelIndex &)" def propertyDataChanged(self, topLeft, bottomRight): """ Change UI Element properties """ if not self.preventChanges: self.preventChanges = True model = topLeft.model() item = model.itemFromIndex(topLeft) gid = item['gid'] # For example, if changing { 'size' : { 'w' : 100 , 'h' : 200 } }, # then subtitle would be 'size' and title would be 'w' or 'h' title = model.title(item) subtitle = model.subtitle(item) value = model.prepareData(item) if self.sendData(gid, subtitle + title, value): model.updateData(item) else: model.revertData(item) print('Error >> Unable to send data to Trickplay') self.preventChanges = False def sendData(self, gid, property, value): """ Send changed properties to Trickplay device """ try: property, value = modelToData(property, value) except BadDataException, (e): print("Error >> Invalid data entered", e.value) return False print('Sending:', gid, property, value) return connection.send({'gid': gid, 'properties': {property: value}})
def data_transfer(): me_data = main.quiz.loop connection.send(me_data, OTHER_HOST, OTHER_PORT) enemy_data = server.receive()
def send(self, s): x = s.encode('utf-8') try: connection.send(self.sock, self.active_sock, x, len(x)) except TypeError: print('TypeError:\n\t{}\t{}'.format(x, type(x)))
lh.landmark2id["PINKY_TIP"]].x isRight = index_x > pinky_x # mediapipe comes with an build in handiness detection, that can be used instead of the custom check #hand_label = results.multi_handedness[hand_idx].classification[0].label #isRight = True if hand_label == "Right" else False handdata.Hand = hd.HandData.HandType.RightHand if isRight else hd.HandData.HandType.LeftHand for hand_idx in range(0, 21): landmark = handdata.Landmarks.add() landmark.X = hand_landmarks.landmark[hand_idx].x landmark.Y = hand_landmarks.landmark[hand_idx].y landmark.Z = hand_landmarks.landmark[hand_idx].z landmark.Type = hand_idx mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS) connection.send(message.SerializeToString()) if SHOW_WINDOW: cv2.imshow('MediaPipe Hands', image) if cv2.waitKey(5) & 0xFF == 27: break finally: hands.close() cap.release() connection.close()
def sendJoinRequest(self): join_header = {'cmd': 'JOIN', 'info': {'name': self.player.getNick()}} send(self.connection, join_header, self.server) reply, address = receive(self.connection, self.packet_size) self.player.setPlayerId(reply['self']['id'])