예제 #1
0
	def __init__(self, entity_id, entity_type, kind, x, y):
		super(Character, self).__init__(entity_id, entity_type, kind, x, y)
		
		self.orientation = random_orientation()
		self.attackers = {}
		self.target = None
예제 #2
0
	def handle_message(self, message_data):
		logger.info('Recieved: %r', message_data)
		msg = deserialise_message(message_data)
		
		if type(msg) is HelloMessage:
			if self.has_entered_game and not self.is_dead:
				self.connection.close('Cannot handshake twice!')
			else:
				# we can accept this hello.
				
				# TODO: implement sanitiser.
				if msg.player_name == '':
					self.name = 'Leeroy Jenkins'
				else:
					self.name = msg.player_name[:16]
				
				self.kind = ENTITIES['WARRIOR']
				self.equip_armor(msg.armor_kind)
				self.equip_weapon(msg.weapon_kind)
				self.orientation = random_orientation()
				self.update_hit_points()
				self.update_position()
				
				self.server.add_player(self)
				#self.server.
				
				# send welcome message
				self.send(WelcomeMessage(self))
				self.has_entered_game = True
				self.is_dead = False
		elif self.has_entered_game:
			if type(msg) is ChatMessage:
				# ignore the player_id sent by the client
				
				# TODO: broadcast this to nearby clients.
				logger.info('<%s> %s', self.name, msg.message)
			elif type(msg) is ZoneMessage:
				self.on_zone()
			elif type(msg) in (MoveMessage, LootMoveMessage):
				if self.world.is_valid_position(msg.x, msg.y):
					logger.info('%s moved to %r, %r', self.name, msg.x, msg.y)
					self.set_position(msg.x, msg.y)
					
					if type(msg) is MoveMessage:
						self.clear_target()
						
						self.broadcast(MoveMessage(self))
						self.on_move(msg.x, msg.y)
					elif type(msg) is LootMoveMessage and message.target_id in self.world.entities:
						item = self.world.entities[message.target_id]
					
						self.broadcast(LootMoveMessage(self))
						self.on_loot_move(msg.x, msg.y)
				else:
					logger.warn('%s tried to move to invalid position %r, %r', self.name, msg.x, msg.y)
			elif type(msg) is CheckMessage:
				# TODO: handle this properly
				
				# lookup checkpoint on the map.
				if msg.checkpoint_id in self.world.map.checkpoints:
					self.last_checkpoint = self.world.map.checkpoints[msg.checkpoint_id]
					logger.info('%s checkpoint is %r', self.name, self.last_checkpoint)
				else:
					logger.warn('Ignoring bad checkpoint ID %r from %s', msg.checkpoint_id, self.name)

					
			else:
				logger.warn('Ignoring unknown message type %d (%s) during has_entered_game', msg.message_type, msg.message_type_label())
		else:
			logger.warn('Ignoring message type %d (%s) during !has_entered_game', msg.message_type, msg.message_type_label())