mcc avatar mcc committed dc0ba23

Also must send sid and lastf. Fitness function now tested

Comments (0)

Files changed (2)

media/overlay/play/onLoad.lua

 	if session and session.real then
 		local keep_session = session
 		reset_session()
-		put = {version=1.0, pid=player_info.id, deaths=keep_session.deaths, totalf=keep_session.totalf, won=keep_session.won,}
+		put = {version=1.0, pid=player_info.id, deaths=keep_session.deaths, totalf=keep_session.totalf, won=keep_session.won, gid=keep_session.gid, sid=keep_session.sid, lastf=keep_session.lastf,}
 		if keep_session.deaths then
 			local rms = 0
 			for i,v in ipairs(keep_session.f) do
 	dosLoading()
 	xhr(um.level, function(t)
 		dos:clear()
+		pull(session, {gid=t.gid, sid=t.sid})
 		startWithSpec(Spec(t))
 	end, put)
 end
 	def __init__(self, gid, suppress = False):
 		self.gid = gid
 		self.sid = -1
+		self.checked = False
+		self.won = False
 		if not suppress:
 			self.board = Board()
 		
 		r.setAttribute("gid", str(self.gid))
 		r.setAttribute("sid", str(self.sid))
 		self.board.xml(doc,r)
+		
+	def report(self, post_obj):
+		self.checked = True
+		if post_obj.hasAttribute("won") and int(post_obj.getAttribute("won")):
+			self.won = True
+			self.deaths = int(post_obj.getAttribute("deaths"))
+			self.lastf = int(post_obj.getAttribute("lastf"))
 	
 class Genome:
 	def __init__(self, _id):
 		self.specs = []
 		basic = Spec(self.id)
 		self.issue_next = 0
+		self.generation_count = 1
 		for i in range(options.specs):
 			s = basic.clone().mutate()
 			s.sid = i
 			try:
 				old_specs = self.specs
 				new_specs = [] # Do a funny dance so we can recover if there's an exception.
-				winner1 = old_specs[0]
-				winner2 = old_specs[1]
+				def fitness(x,y): # resort by these criteria
+					if not (x.checked or y.checked):
+						return 0
+					if not (x.won or y.won):
+						return 0
+					if not x.checked or not x.won:
+						return 1
+					if not y.checked or not y.won:
+						return -1
+					if x.deaths != y.deaths:
+						return cmp(x.deaths, y.deaths)
+					return cmp(x.lastf, y.lastf) # Last ditch
+				sorted_old_specs = sorted(old_specs, cmp=fitness)
+				winner1 = sorted_old_specs[0]
+				winner2 = sorted_old_specs[1]
+				if options.debug > 1:
+					print("Recombining genome %d. Winning specs: %d, %d" % (self.id, winner1.sid, winner2.sid))
 				for i in range(len(old_specs)):
 					old_spec = old_specs[i]
-					s = winner1.clone().recombine(winner2).mutate()
+					if old_spec.checked:
+						s = winner1.clone().recombine(winner2).mutate()
+					else:
+						s = old_spec
+						if options.debug > 2:
+							print("Preserving spec %d" % (s.sid))
 					s.sid = i
 					new_specs.append(s)
 				self.specs = new_specs
+				self.generation_count = self.generation_count + 1
 			finally:
 				self.issue_next = 0
 		return s
+	
+	def report(self, post_obj):
+		sid = int(post_obj.getAttribute("sid"))
+		self.specs[sid].report(post_obj)
 
 class Manager:
 	def __init__(self):
 	
 	def any(self):
 		return randomfrom(self.tanks)
+		
+	def report(self, post_obj):
+		for a in ["gid","sid","deaths","lastf"]:
+			if not post_obj.hasAttribute(a):
+				return
+		try:
+			gid = int(post_obj.getAttribute("gid"))
+			self.tanks[gid].report(post_obj)
+		except ValueError, IndexError:
+			pass
 
 class LuaHandler(tornado.web.RequestHandler):
 	def result(self, doc, post_obj):
 					print("POST: %s" % post)
 				dom = xml.dom.minidom.parseString(post)
 				document = dom.documentElement
-				post_obj = document.firstChild
+				post_obj = document
 				
 			output = xml.dom.minidom.Document()
 			dc = self.result(output, post_obj)
 		
 class LevelHandler(LuaHandler):
 	def result(self, doc, post_obj):
+		if post_obj:
+			m.report(post_obj)
 		r = doc.createElement("level")
 		s = m.any()
 		s.any().xml(doc, r)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.