def buylist(nick, message, is_whisper, match): if not is_whisper: return # Support for 4144's shop (Sell list) data = '\302\202S1' for id_, (price, amount) in buying.iteritems(): index = get_item_index(id_) if index > 0: _, curr_amount = mapserv.player_inventory[index] amount -= curr_amount try: can_afford = mapserv.player_money / price except ZeroDivisionError: can_afford = 10000000 amount = min(can_afford, amount) if amount <= 0: continue data += encode_str(id_, 2) data += encode_str(price, 4) data += encode_str(amount, 3) whisper(nick, data)
def buylist(nick, message, is_whisper, match): if not is_whisper: return # Support for 4144's shop (Sell list) data = "\302\202S1" for id_, (price, amount) in buying.iteritems(): index = get_item_index(id_) if index > 0: _, curr_amount = mapserv.player_inventory[index] amount -= curr_amount try: can_afford = mapserv.player_money / price except ZeroDivisionError: can_afford = 10000000 amount = min(can_afford, amount) if amount <= 0: continue data += encode_str(id_, 2) data += encode_str(price, 4) data += encode_str(amount, 3) whisper(nick, data)
def get_block(self): """ download a file block from the server """ print('* Getting a block from the server:') self.soc.send(ut.encode_str(str(self.p_port), ut.data_unit)) # send1-1: port of client recv_buffer = ut.recvall(self.soc, ut.data_unit) self.idx_clt = int( ut.decode_str(recv_buffer)) # receive1-2: index of client print('Index of current peer:', self.idx_clt) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.num_peer = int( ut.decode_str(recv_buffer)) # receive1-3: number of peers print('Number of peers:', self.num_peer) self.info_pool = [None] * self.num_peer recv_buffer = ut.recvall( self.soc, ut.data_unit) # receive1-4: information of all peers info_clts = ut.decode_str(recv_buffer).split('|') for i in range(self.num_peer): info_clt = info_clts[i].split(';') # (ip, port) self.info_pool[i] = (info_clt[0], int(info_clt[1])) print('Information of peers:', self.info_pool) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.file_len = int( ut.decode_str(recv_buffer)) # receive1-5: file length print('Length of the file:', self.file_len) self.block_name = self.downfile_folder + '/' + str(self.idx_clt) print('Name of the block:', self.block_name) recv_buffer = ut.recvall(self.soc, ut.data_unit) self.block_len = int( ut.decode_str(recv_buffer)) # receive1-6: block length print('Length of the block:', self.block_len) sys.stdout.flush() # receive a block of file received_size = 0 local_file = open(self.block_name, 'wb') while received_size != self.block_len: received_data = self.soc.recv( ut.data_unit) # receive1-7: file block # print('received:', len(received_data)) local_file.write(received_data) received_size += len(received_data) local_file.close() print('Downloading of block finished!\n') sys.stdout.flush() self.has_received = [False] * self.num_peer self.has_received[self.idx_clt] = True
def selllist(nick, message, is_whisper, match): if not is_whisper: return # Support for 4144's shop (Sell list) data = '\302\202B1' for id_, (price, amount) in selling.iteritems(): index = get_item_index(id_) if index < 0: continue _, curr_amount = mapserv.player_inventory[index] amount = min(curr_amount, amount) data += encode_str(id_, 2) data += encode_str(price, 4) data += encode_str(amount, 3) whisper(nick, data)
def selllist(nick, message, is_whisper, match): if not is_whisper: return # Support for 4144's shop (Sell list) data = "\302\202B1" for id_, (price, amount) in selling.iteritems(): index = get_item_index(id_) if index < 0: continue _, curr_amount = mapserv.player_inventory[index] amount = min(curr_amount, amount) data += encode_str(id_, 2) data += encode_str(price, 4) data += encode_str(amount, 3) whisper(nick, data)
def invlists(max_items=1000): inventory = OrderedDict() for id_, amount in mapserv.player_inventory.values(): inventory[id_] = inventory.setdefault(id_, 0) + amount lists = [] data = '\302\202B1' i = 0 for id_, amount in inventory.items(): i += 1 if i > max_items: i = 0 lists.append(data) data = '\302\202B1' data += encode_str(id_, 2) data += encode_str(1, 4) data += encode_str(amount, 3) lists.append(data) return lists
def send_file(self, clt, addr, file_name): """ send files """ data_file = open(file_name, 'rb') file_len = len(data_file.read()) clt.send(ut.encode_str(str(file_len), ut.data_unit)) # send1: file length send_size = 0 data_file.seek(0) while file_len != send_size: data_trans = data_file.read(ut.data_unit) clt.send(data_trans) # send2: file send_size += len(data_trans) # print('Transmission progress: %s:%s' % (send_size, file_len)) print('Transmission completed!\n') sys.stdout.flush() data_file.close() clt.close()
def send_block(self, clt): """ send file block to other peers """ clt.send(ut.encode_str(str(self.block_len), ut.data_unit)) # send2-1: block length # send the block of file data_block = open(self.block_name, 'rb') send_size = 0 data_block.seek(0) while send_size != self.block_len: data_trans = data_block.read(ut.data_unit) clt.send(data_trans) # send2-2: file block send_size += len(data_trans) print('Transmission of block %d to another peer completed!\n' % self.idx_clt) sys.stdout.flush() data_block.close() clt.close()
def send_file(self, clt, addr, idx_clt, file_name): """ send blocks of files """ recv_buffer = ut.recvall(clt, ut.data_unit) # receive1-1: port of client clt_port = int(ut.decode_str(recv_buffer)) self.info_pool[idx_clt] = (addr[0], clt_port) while True: if len(self.clt_pool ) == self.num_peer and None not in self.info_pool: clt.send(ut.encode_str( str(idx_clt), ut.data_unit)) # send1-2: index of client clt.send(ut.encode_str(str( self.num_peer), ut.data_unit)) # send1-3: number of peers all_clt_info = '' for i in range(self.num_peer): ip_i = self.info_pool[i][0] port_i = self.info_pool[i][1] all_clt_info = all_clt_info + ip_i + ';' + str( port_i) + '|' all_clt_info = all_clt_info[:-1] clt.send(ut.encode_str( all_clt_info, ut.data_unit)) # send1-4: information of all clients data_file = open(file_name, 'rb') file_len = len(data_file.read()) clt.send(ut.encode_str(str(file_len), ut.data_unit)) # send1-5: file length if idx_clt == len(self.clt_pool) - 1: block_len = file_len - math.ceil( file_len / self.num_peer) * idx_clt else: block_len = math.ceil(file_len / self.num_peer) clt.send(ut.encode_str(str(block_len), ut.data_unit)) # send1-6: block length # send a block of file send_size = 0 data_file.seek(idx_clt * math.ceil(file_len / self.num_peer), 0) while send_size != block_len: if send_size + ut.data_unit <= block_len: data_trans = data_file.read(ut.data_unit) clt.send(data_trans) # send1-7: file block send_size += len(data_trans) # print('send size', send_size) # print('Transmission progress: %d:%d' % (send_size, block_len)) else: data_trans = data_file.read(block_len - send_size) clt.send(data_trans) # send1-7: file block send_size += len(data_trans) # print('send size', send_size) # print('Transmission progress: %d:%d' % (send_size, block_len)) print('Transmission of block %d completed!\n' % idx_clt) sys.stdout.flush() data_file.close() clt.close() return