Example #1
0
	def set_fleets(self, evt):
		self.sizer.DeleteWindows()
		self.fleets = {}
		
		self.coord = evt.attr1
		x,y = evt.attr1
		for fleet in store.iter_objects_list('fleet', {'x':x, 'y':y}):
			self.add_fleet(fleet)

		for fleet in store.iter_objects_list('flying_fleet', {'x':x, 'y':y}):
			self.add_fleet(fleet)

		for fleet in store.iter_objects_list('alien_fleet', {'x':x, 'y':y}):
			self.add_alien_fleet(fleet)
Example #2
0
def save_csv_table(path, table, data_filter):
	writer = None
	f = os.path.join(path, '%s.csv'%(table,))
	for p in store.iter_objects_list(table, data_filter):
		if not writer:
			writer = csv_open(f, p.keys())
		writer.writerow({k:(v.encode('utf8') if (k in unicode_strings and v) else v) for k,v in p.items()})
Example #3
0
	def update(self):
		self.my_users.DeleteWindows()
		self.access_users.DeleteWindows()
		self.other_users.DeleteWindows()
		self.users = {}
		
		print config.users
		for user in store.iter_objects_list('user'):
			user_id = int(user['user_id'])
			#print user
			#login = unicode(user['login']) #.encode('utf-8')
			#print login
			
			sz = wx.BoxSizer(wx.HORIZONTAL)
			self.add_user(user, sz)
			
			if not 'login' in user or not user['login']:
				self.other_users.Add(sz)
			elif config.has_user(user['login']):
				self.my_users.Add(sz)
			else:
				self.access_users.Add(sz)
		
		if self.my_users.GetChildren():
			self.my_users.Insert(before=0, item=wx.StaticText(self, label='my'))

		if self.access_users.GetChildren():
			self.access_users.Insert(before=0, item=wx.StaticText(self, label='access'))

		if self.other_users.GetChildren():
			self.other_users.Insert(before=0, item=wx.StaticText(self, label='other'))
		
		self.sizer.Layout()
Example #4
0
	def add_alien_fleet(self, fleet):
		ft = fleet['turn']
		max_t = store.max_turn()
		label = ''
		if ft < max_t:
			label = '[-%d] '%(max_t - ft,)
			if max_t - ft > 100:
				# fleet info is too old
				return

		cp = wx.CollapsiblePane(self, label=label+'%s'%fleet['name'], style=wx.CP_DEFAULT_STYLE|wx.CP_NO_TLW_RESIZE)
		self.sizer.Add(cp)
		pane = cp.GetPane()
		
		u = store.get_user(fleet['user_id'])
		if u:
			owner_name = u['name']
		else:
			owner_name = '<unknown>'
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(wx.StaticText(pane, label='owner: %s'%(owner_name,)), 1, wx.EXPAND)		

		for unit in store.iter_objects_list('alien_unit', {'fleet_id':fleet['fleet_id']}):
			hbox = wx.BoxSizer(wx.HORIZONTAL)
			sizer.Add(hbox, 1, wx.EXPAND)
			
			carp = unit['carapace']
			if carp == 0:
				img = image.getBcImage(unit['class'])
			else:
				img = image.getCarapaceImage(carp, unit['color'] )
			
			if img:
				bitmap = wx.StaticBitmap(pane)
				bitmap.SetBitmap(img)
				hbox.Add(bitmap, 1, wx.EXPAND)
			else:
				print 'image not found for unit %s, carp %s, color %s'%(unit['unit_id'],  int(unit['carapace']), int(unit['color']) )

			name = get_unit_name(int(unit['carapace']))
			hbox.Add(wx.StaticText(pane, label=name), 1, wx.EXPAND)

		border = wx.BoxSizer()
		border.Add(sizer, 1, wx.EXPAND|wx.ALL)
		pane.SetSizer(border)
		
		self.sizer.Layout()
		
		self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged, cp)
		cp.Expand()
		
		cp.Bind(wx.EVT_LEFT_DOWN, self.onFleetSelect)
		self.fleets[cp] = fleet
Example #5
0
	def __init__(self, parent, only_my_users = True):
		wx.Window.__init__(self, parent, wx.ID_ANY)
		sizer = wx.BoxSizer(wx.VERTICAL)
		
		self.users = {}
		for user in store.iter_objects_list('user'):
			if 'login' in user and user['login']:
				checkbox = wx.CheckBox(self, label=user['name'])
				checkbox.Bind(wx.EVT_CHECKBOX, self.userChanged)
				self.users[int(user['user_id'])] = checkbox
				checkbox.SetValue(False)
				sizer.Add(checkbox)
			
		self.SetSizer(sizer)
		
		self.Layout()
Example #6
0
def save_user_data(user_id, path):
	util.assureDirExist(path)
	log.info('saving user %s %s'%(user_id, path))
	
	user_filter = {'user_id':user_id}
	save_csv_table(path, 'user', user_filter)
	save_csv_table(path, 'open_planet', user_filter)
	save_csv_table(path, 'race', user_filter)
	save_csv_table(path, 'diplomacy', user_filter)
	save_csv_table(path, 'hw', user_filter)

	save_csv_table(path, 'flying_fleet', user_filter)
	save_csv_table(path, 'alien_flying_fleet', user_filter)
	save_csv_table(path, 'user_planet', user_filter)
	save_csv_table(path, 'fleet', user_filter)
	save_csv_table(path, 'proto', user_filter)
	
	save_csv_table(path, 'action', user_filter)
	
	fleet_unit_writer = csv_open( os.path.join(path, 'fleet_unit.csv'), store.keys('fleet_unit'))
	unit_writer = csv_open( os.path.join(path, 'unit.csv'), store.keys('unit'))
	for fleet in store.iter_objects_list('fleet', {'user_id':user_id}):
		for fleet_unit in store.iter_objects_list('fleet_unit', {'fleet_id':fleet['fleet_id']}):
			fleet_unit_writer.writerow(fleet_unit)
			unit_writer.writerow( store.get_object('unit', {'unit_id':fleet_unit['unit_id']}) )

	for fleet in store.iter_objects_list('flying_fleet', {'user_id':user_id}):
		for fleet_unit in store.iter_objects_list('fleet_unit', {'fleet_id':fleet['fleet_id']}):
			fleet_unit_writer.writerow(fleet_unit)
			unit_writer.writerow( store.get_object('unit', {'unit_id':fleet_unit['unit_id']}) )
			
	garrison_unit_writer = csv_open(os.path.join(path, 'garrison_unit.csv'), store.keys('garrison_unit'))
	garrison_queue_unit_writer = csv_open(os.path.join(path, 'garrison_queue_unit.csv'), store.keys('garrison_queue_unit'))
	#save_csv_table(path, 'garrison_queue_unit')
	for planet in store.iter_objects_list('user_planet', {'user_id':user_id}):
		for garrison_unit in store.iter_objects_list('garrison_unit', {'x':planet['x'], 'y':planet['y']}):
			garrison_unit_writer.writerow(garrison_unit)
			unit_writer.writerow( store.get_object('unit', {'unit_id':garrison_unit['unit_id']}) )
			
		for queue_unit in store.iter_objects_list('garrison_queue_unit', {'x':planet['x'], 'y':planet['y']}):
			garrison_queue_unit_writer.writerow( queue_unit )
	
	proto_actions_writer = csv_open(os.path.join(path, 'proto_action.csv'), store.keys('proto_action'))
	for proto in store.iter_objects_list('proto', {'user_id':user_id}):
		proto_actions_writer.writerows(store.get_objects_list('proto_action', {'proto_id':proto['proto_id']}))
Example #7
0
File: map.py Project: bogolt/dclord
	def drawPlanets(self, dc, rect):
		#cond = ['owner_id is not null'] 
		ax,bx = self.offset_pos[0], self.offset_pos[0]+self.screen_size[0]
		ay,by = self.offset_pos[1], self.offset_pos[1]+self.screen_size[1]

		inhabitedOnly = int(config.options['filter']['inhabited_planets'])==1
		ownedOnly = int(config.options['filter']['owned_planets'])==1

		if not inhabitedOnly and not ownedOnly:
			for p in store.iter_planets_size((ax,bx,ay,by)):
				self.drawPlanet(dc, p)
			
		for p in store.iter_planets((ax,bx,ay,by), inhabited = inhabitedOnly, owned = ownedOnly):
			self.drawPlanet(dc, p)
			#if self.draw_geo:
			#	self.drawPlanetGeo(dc, p)
			
		if self.selected_user_id:
			for p in store.iter_objects_list('open_planet', {'user_id':self.selected_user_id}):
				self.draw_open_planet(dc, p)
Example #8
0
def load_common_data(path):	

	for data in iter_csv_table(path, 'user'):
		# it will not load user specific info otherwise
		data['turn'] = '0'
		store.update_data('user', ['user_id'], data)
		
	for data in iter_csv_table(path, 'planet'):
		store.update_planet(data)

	for data in iter_csv_table(path, 'alien_fleet'):
		store.update_data('alien_fleet', ['fleet_id'], data)
	
	store.normalize_fleets()
		
	#load_csv_table(path, 'planet_geo')
	load_csv_table(path, 'alien_unit')
	
	for planet in store.iter_objects_list('planet'):
		load_geo_size_at((int(planet['x']), int(planet['y'])))
		
	store.remove_duplicate_planets()
	store.normalize_user_planets()
Example #9
0
File: map.py Project: bogolt/dclord
	def drawFleets(self, dc, rect):
		self.fleets = {}
		for p in store.iter_objects_list('fleet', {}, rect):
			self.drawFleet(dc, p)
		for p in store.iter_objects_list('flying_fleet', {}, rect):
			self.drawFlyingFleet(dc, p)
Example #10
0
	def onExploreGeoAll(self, _):
		'upload pending events on server'
		
		explore_owned_planets = True
		
		out_dir = os.path.join(util.getTempDir(), config.options['data']['raw-dir'])
		util.assureDirClean(out_dir)
		for acc in config.accounts():
			if not 'id' in acc:
				continue
			user_id = int(acc['id'])
				
			log.info('requesting user %s info'%(acc['login'],))
			# find if there is any explore-capable fleets over unexplored planets
			# or simply tell explore to every unit =))) game server will do the rest
			
			#1. find all fleets above empty planets
			
			# get fleet position, check if planet geo is unknown
			fleet_planet = {}
			pl = {}
			
			for fleet in store.iter_objects_list('fleet', {'user_id':acc['id']} ):
				#print 'got fleet %s'%(fleet,)
				coord = get_coord(fleet)
				
				if coord in pl:
					pl[coord].add(fleet['fleet_id'])
					continue

				planet = store.get_object('planet', {'x':coord[0], 'y':coord[1]})
				#check holes and stars
				if not planet:
					continue
				
				# check if already explored
				if 'o' in planet and planet['o']:
					continue
				
				if not coord in pl:
					pl[coord] = set()
				pl[ coord ].add(fleet['fleet_id'])
				#print 'Add to exploration list planet %s'%(planet,)
			
			acts = {}
			
			# get all fleet units, check if explore capable
			for coord, planet_fleets in pl.iteritems():
				for fleet_id in planet_fleets:
					for unit in store.get_fleet_units(fleet_id):
						#print '%s %s unit %s'%(coord, fleet_id, unit)
						# ok unit
						bc = unit['proto_id']
						
						#for proto in db.prototypes(['id=%s'%(bc,)]):
						#	print 'proto %s'%(proto,)

						#type 1 probably geo explore
						action_geo_explore = store.get_object('proto_action',{'proto_id':bc, 'proto_action_id':request.RequestMaker.GEO_EXPLORE})
						if action_geo_explore:
							acts[coord] = unit['unit_id']
							break
					# no need to request more then one explore of a single planet
					if coord in acts:
						break

			self.pending_actions.user_id = int(acc['id'])
			#self.pendingActions[int(acc['id'])] = actions
			#hw_planet = db.getUserHw(acc['id'])
			#actions.createNewFleet(hw_planet, 'a_new_shiny_fleet')
			
			at_least_one = False
			for coord, unit_id in acts.iteritems():
				fleet_unit = store.get_object('fleet_unit', {'unit_id':unit_id})
				self.actions.add_action(action.ActionStore(user_id, unit_id, fleet_unit['fleet_id'], coord, action.Action.GEO_EXPLORE))
				#self.pending_actions.explore_planet( coord, unit_id )
				at_least_one = True
			
			if at_least_one:
				self.perform_actions()
Example #11
0
	def onFlyHomeScouts(self, _):
				
		for acc in config.accounts():
			if not 'id' in acc:
				continue
			user_id = int(acc['id'])
			self.pending_actions.user_id = user_id
			#print 'fly home scouts for user %s %s'%(user_id, acc['login'])
							
			# fly scouts back to base
			
			fleets = []
			fleet_flt = ['owner_id=%s'%(user_id,)]
			
			fleet_name = None #unicode('Fleet')
			# can also filter fleets by names
			#TODO: beware escapes
			if fleet_name:
				fleet_flt.append( 'name="%s"'%(fleet_name,) ) 
			for fleet in store.iter_objects_list('fleet', {'user_id':user_id}):
				#print 'found fleet %s'%(fleet,)
				# if fleet over empty planet - jump back home
				coord = get_coord(fleet)
				coord_filter = {'x':coord[0], 'y':coord[1]}
				planet = store.get_object('planet', coord_filter )
				#TODO: allow jump on all open-planets ( not only owned by user )
				coord_filter['user_id'] = user_id
				user_open_planet = store.get_object('open_planet', coord_filter )
				if user_open_planet:
					continue

				#print 'fleet %s not at home'%(fleet['id'],)
				units = []
				for unit in store.get_fleet_units(fleet['fleet_id']):
					#print 'fleet %s has unit %s'%(fleet['id'], unit)
					units.append(unit)
				
				# not a scout fleet if more then one unit in fleet
				# if zero units - don't care about empty fleet as well
				if len(units) != 1:
					#print 'fleet %s has %s units, while required 1'%(fleet['id'], len(units))
					continue

				if int(units[0]['unit_id']) in self.manual_control_units:
					continue

				proto = store.get_object('proto', {'proto_id':units[0]['proto_id']})
				if proto['carapace'] != CARAPACE_PROBE:
					#print 'fleet %s unit %s is not a probe'%(fleet['id'], units[0])
					continue

				#jump back
				#print 'fleet %s %s needs to get home'%(coord, fleet)
				fleets.append( (coord, fleet) )					

			if not fleets:
				#print 'no scout fleets found not at home'
				continue
			
			coords = []
			for planet in store.iter_objects_list('open_planet', {'user_id':user_id}):
				coord = get_coord(planet)
				coords.append( coord )
				#print 'possible home planet %s'%(coord,)
				
			if coords == None or fleets == []:
				#print 'oops %s %s'%(coords, fleets)
				continue
			
			#print 'looking for best route for %s fleets' %(len(fleets,),)
			for coord, fleet in fleets:
				#find closest planet
				closest_planet = coords[0]
				min_distance = util.distance(coords[0], coord)
				for c in coords:
					if util.distance(coord, c) < min_distance:
						min_distance = util.distance(coord, c)
						closest_planet = c
				
				# ok, found then jump
				#print 'Jump (%s) %s'%(closest_planet, fleet)
				
				self.actions.add_action( action.ActionJump(user_id, fleet['fleet_id'], closest_planet ))
Example #12
0
	def onMakeScoutFleets(self, _):		
		# get all planets
		# get harrison units able to scout
		# create fleets
		# put units to fleets
		
		# get all scouting fleets ( available to jump ( on my planets ) )
		# get unexplored planets
		# send nearest fleets to these planets
		
		# load size-map, use it to scout biggest first ( N > 70, descending )
		
		# get all scouting fleets ( on other planets )
		# geo-explore
		# send them back to nearest home planet
		
		#command_type, 
		#move_command = ('move_to', 
		
		#move_commands = [((x,y), fleet_id)]
	
		carapace = 11 # probe/zond
		fleet_name = 'scout:geo'
		turn = db.getTurn()
		
		for acc in config.accounts():
			if not 'id' in acc:
				continue
			user_id = int(acc['id'])
			
			#if user_id < 601140:
			#	continue

			probes_types = store.get_objects_list('proto', {'carapace':carapace, 'user_id':user_id})
			#any_class = 'class in (%s)'%(','.join([str(cls) for cls in units_classes]),)
			#print 'testing user %s with class %s'%(user_id, any_class)
			probe_ids = [str(proto['proto_id']) for proto in probes_types]
			
			self.pending_actions.user_id = user_id
			pending_units = []
			
			for planet in store.iter_objects_list('user_planet', {'user_id':user_id}):
				coord = get_coord(planet)
				#print 'checking harrison for planet %s'%(planet,)
				for unit in store.get_garrison_units(coord, value_in=('proto_id', probe_ids)):
					#print 'found unit %s on planet %s'%(unit, planet,)
					action_create = action.ActionCreateFleet(user_id, fleet_name, coord)
					self.actions.add_action(action_create)
					fleet_id = action_create.fleet_id
					
					self.actions.add_action(action.ActionUnitMove(user_id, fleet_id, unit['unit_id']))
					
					#self.actions.add_action(action.Action('unit_move', user_id, {'planet':coord, 'unit_id':unit['unit_id'], 'fleet_id':fleet_id}))
					#self.actions.add_action(action.Action('unit_move', user_id, {'planet':coord, 'unit_id':unit['unit_id'], 'fleet_id':fleet_id}))
					
					#self.pending_actions.createNewFleet(coord, fleet_name)
					#pending_units.append( (self.pending_actions.get_action_id(), coord, unit['id'] ) )
					
					#print 'found unit %s on planet %s'%(unit, coord )
			
			if len(pending_units) == 0:
				continue
Example #13
0
	def onSendScouts(self, _):
		min_size = 70
		max_size = 99
		
		#helps avoid flying on the same planet with different accounts
		friend_geo_scout_ids = []
		for user in db.users():
			friend_geo_scout_ids.append(str(user['id']))
		
		for acc in config.accounts():
			user_id = int(acc['id'])

			if self.command_selected_user and user_id != self.map.selected_user_id:
				continue

			self.pending_actions.user_id = user_id
			#log.info('send scouts %s size %s'%(acc['login'], min_size))
					
			# find units that can geo-explore
			# find the ones that are already in fleets in one of our planets
			
			fly_range = 0.0
			ready_scout_fleets = {}
			# get all fleets over our planets
			for planet in store.iter_objects_list('open_planet', {'user_id':user_id}):
				#print 'open planet %s'%(planet,)
				coord = get_coord(planet)
				for fleet in store.iter_objects_list('fleet', {'user_id':user_id, 'x':coord[0], 'y':coord[1]}):
					if value_in(self.exclude_fleet_names, fleet['name']):
						continue
					units = store.get_fleet_units(fleet['fleet_id'])
					if len(units) != 1:
						#print 'fleet %s has wrong units count ( != 1 ) %s, skipping it'%(fleet, units)						
						continue
					unit = units[0]
					if int(unit['unit_id']) in self.manual_control_units:
						#print 'unit %s reserved for manual control, skipping it'%(unit,)
						continue

					if not self.is_geo_scout(unit):
						#print 'unit %s is not geo-scout, skipping it'%(unit,)
						continue
					#fly_range = max(fly_range, )
					#print 'unit %s on planet %s for fleet %s is geo-scout'%(unit, coord, fleet)
					# ok, this is geo-scout, single unit in fleet, on our planet
					#ready_scout_fleets.append((coord, fleet))
					_,r = store.get_fleet_speed_range(fleet['fleet_id'])
					fly_range = max(fly_range, r)
					ready_scout_fleets.setdefault(coord, []).append((fleet, r))
					
			
			# get possible planets to explore in nearest distance
			for coord in ready_scout_fleets.keys():
				#print 'load geo size centered at %s with range %s'%(coord, int(fly_range))
				save_load.load_geo_size_center(coord, int(fly_range))
			
			# jump to nearest/biggest unexplored planet
			exclude = set()
			for coord, fleets in ready_scout_fleets.iteritems():
				max_fly_range = 0
				for f,r in fleets:
					max_fly_range = max(max_fly_range, r)
				
				possible_planets = []
				#s<=99 - skip stars
				#dx = lt[0]-fly_range, lt[0]+fly_range
				#dy = lt[1]-fly_range, lt[1]+fly_range
				for p in store.iter_planets_size(pos=coord, fly_range=max_fly_range, size_min=min_size, bigger_first = True):
					if not (p['s']>=min_size and p['s']<=max_size):
						#print 'planet %s not fit the size'%(p,)
						continue
					dest = get_coord(p)
					if dest in exclude:
						continue
					dist = util.distance(dest, coord)
					if dist > fly_range:
						#print 'planet %s is too far away'%(p,)
						continue
						
					planet = db.get_planet(dest)
					if planet and 'o' in planet and planet['o']:
						#print 'planet %s already explored'%(p,)
						continue
					
					has_flying_geo_scouts = False
					
					# check if currently there is some explore fleet on planet, or explore fleet already fly there
					already_has_scout = False
					for fleet in store.iter_objects_list('fleet', {'x':dest[0], 'y':dest[1]}, controlled = True):
						if self.is_geo_scout_fleet(fleet['fleet_id']):
							already_has_scout = True
							break
					if already_has_scout:
						#print 'planet %s has scount fleet on it'%(p,)
						continue
						
					already_fly_geo_scouts = False
					for fleet in store.iter_objects_list('flying_fleet', {'x':dest[0], 'y':dest[1]}, controlled = True):
						if self.is_geo_scout_fleet(fleet['fleet_id']):
							already_fly_geo_scouts = True
							#self.actions.add_action( action.ActionGeoExplore(user_id, ':dest, 'fleet_id':fleet['fleet_id']}))
							break
					if already_fly_geo_scouts:
						#print 'planet %s has flying scount fleet'%(p,)
						continue

					possible_planets.append( (dist, dest) )
					#print 'add possible planet %s'%(dest,)

				for fleet, fleet_range in fleets:
					#print 'check planets for fleet %s'%(fleet,)
					for dist, planet in sorted(possible_planets):
						if dist > fleet_range:
							#print 'planet %s too scary'%(p,)
							continue
						# ok fly to it
						self.actions.add_action( action.ActionJump(user_id, fleet['fleet_id'], planet ))
						#self.pending_actions.fleetMove(fleet['id'], planet)
						exclude.add( planet )
						#print 'jump %s from %s to %s'%(fleet, coord, planet)
						possible_planets.remove( (dist, planet ) )
						break
Example #14
0
	def is_geo_scout(self, unit):
		for act in store.iter_objects_list('proto_action', {'proto_id':unit['proto_id']}):
			#print 'proto action %s'%(act,)
			if act['proto_action_id']== request.RequestMaker.GEO_EXPLORE:
				return True
		return False