def hunt_status(self, game_id = None): if game_id is None: user, role, game, all_tasks = get_session_info() game_id = game.id gap, dummy1, dummy2 = self.get_gap(game_id) delta = self.get_delta(game_id) solved_cnt = self.get_solved(game_id) if delta <= gap and solved_cnt > 0: user, role, game, all_tasks = get_session_info() if role.role == "mrx": return {"hunt_active":True} else: loc = cherrypy.request.db.query(Location).join(Game)\ .join(Role, Role.user_id == Location.user_id)\ .filter(Role.game_id == Game.id)\ .filter(Game.id == game_id)\ .filter(Role.role == "mrx").order_by(Location.time.desc()).all()[0] return {"hunt_active":True, "lat":loc.lat, "lng":loc.lng, "time":str(loc.time)} else: return {"hunt_active":False}
def role(self): user, role, game, all_tasks = get_session_info() if role is None: return {"status":"fail"} else: return {"status":"success", "role":role.role}
def gap(self, game_id = None): if game_id is None: user, role, game, all_tasks = get_session_info() game_id = game.id gap, next_gap, next_gap_time = self.get_gap(game_id) return {"gap":gap}
def take(self, id): try: user, role, game, all_tasks = get_session_info() active_tasks = filter(lambda x: x[0].status == "active" or x[0].status == "requested", all_tasks) if len(active_tasks) > 0: return {"status":"fail","msg":"Only one task can be active at a time"} task, crime, point = filter(lambda x: x[1].id == int(id), all_tasks)[0] if task.status != "pending": return {"status":"fail", "msg":"Wrong task status %s. Should be pending" % task.status} task.status = "requested" task.request_time = datetime.now() prev_crimes = filter(lambda x: x[0].status in ("cancelled", "completed"), all_tasks) if len(prev_crimes) == 0: prev_crime = None else: prev_crime =sorted(prev_crimes, key = lambda x: x[0].request_time)[-1][1] seconds_to_walk = self.calculateTimeToWalk(prev_crime, crime) #TODO actually calculate task.walk_time = timedelta(seconds=seconds_to_walk) cherrypy.request.db.add(task) cherrypy.request.db.commit() return {"status":"success", "remaining": seconds_to_walk} except: cherrypy.request.db.rollback() raise
def get_gap(self, game_id): user, role, game, all_tasks = get_session_info() gap = 0 if game.status == "active": eta = (datetime.now() - game.detective_start).seconds gap += eta/(60*45) # +1 every 45 minutes return gap, None, None
def cancel(self, id): user, role, game, all_tasks = get_session_info() task, crime, point = filter(lambda x: x[1].id == int(id), all_tasks)[0] if task.status not in ("requested","active"): return {"status":"fail", "msg":"Wrong task status %s. Should be active" % task.status} task.status = "cancelled" cherrypy.request.db.add(task) cherrypy.request.db.commit() return {"status":"success"}
def mrx_pos(self): user, role, game, all_tasks = get_session_info() gap, next_gap, next_gap_time = self.get_gap(game.id) delta = self.get_delta(game.id) if delta > gap: return {"status":"declined", "msg":"too big delta"} else: loc = cherrypy.request.db.query(Location).join(Game).join(Role)\ .filter(Game.id == game.id).filter(Location.user_id == Role.user_id)\ .filter(Role.role == "mrx").order_by(Location.time.desc()).one() return {"status":"success", "lat":loc.lat, "lng":loc.lng, "time":loc.time}
def send_location(self, lat, lng): user, role, game, all_tasks = get_session_info() location = Location( user_id = user.id, game_id = game.id, lat = lat, lng = lng, time = datetime.now() ) cherrypy.request.db.add(location) cherrypy.request.db.commit() return {"status":"success"}
def index(self): user, role, game, all_tasks = get_session_info() if game is None: return "No active or scheduled games" if game.status == "active": tmpl = env.get_template('newindex.html') elif game.status == "mrx_active": if role.role == "mrx": tmpl = env.get_template('newindex.html') else: tmpl = env.get_template('waitformrx.html') else: tmpl = env.get_template('gameover.html') return tmpl.render()
def submit_mrx_code(self, code): user, role, game, all_tasks = get_session_info() if role.role != "detective": return {"status":"fail", "msg":"only detectives can submit code"} if game.status != "active": return {"status":"fail", "msg":"game is not active"} if code.lower() == game.code.lower(): game.status = 'finished' cherrypy.request.db.add(game) cherrypy.request.db.commit() return {"status":"success"} else: return {"status":"fail", "msg":"wrong code"}
def answer(self, id, answer): user, role, game, all_tasks = get_session_info() logging.error("%s Answering %s = %s" % (user.username, id, answer)) task, crime, point = filter(lambda x: x[1].id == int(id), all_tasks)[0] if task.status not in ("active"): return {"status":"fail", "msg":"Wrong task status %s. Should be active" % task.status} correct_answers = map(lambda x: x.lower().strip(), point.answer) if answer.lower().strip() in correct_answers: task.status = "completed" if role.role == "mrx": crime.status = "commited" crime.commit_time = datetime.now() det_task = cherrypy.request.db.query(Task)\ .join(Crime, Crime.det_task_id == Task.id)\ .filter(Crime.id == crime.id).one() det_task.status = "pending" cherrypy.request.db.add(det_task) commited_cnt = cherrypy.request.db.query(Crime).filter(Crime.game_id == game.id)\ .filter(or_(Crime.status == "commited", Crime.status == "solved")).count() if game.status == "mrx_active" and commited_cnt >= START_HANDICAP: game.status = "active" game.detective_start = datetime.now() else: crime.status = "solved" cherrypy.request.db.add(task) cherrypy.request.db.add(crime) cherrypy.request.db.add(game) cherrypy.request.db.commit() return {"status":"success"} else: return {"status":"fail", "msg":"Wrong answer"}
def list(self): user, role, game, all_tasks = get_session_info() ret = [] if role.role == "mrx": for task, crime, point in all_tasks: ret.append(self.make_one_task(crime,task,point)) elif role.role == "detective": if game.status == "active": for task, crime, point in all_tasks: if crime.status not in ("commited", "solved"): continue if crime.status == "commited" and \ crime.commit_time + CRIME_EXPOSURE_TIME > datetime.now(): continue ret.append(self.make_one_task(crime,task,point)) cherrypy.request.db.commit() return ret
def game_status(self): user, role, game, all_tasks = get_session_info() if game is None or (game.status == "finished" and (datetime.now() - game.detective_start).seconds > 60*60*5): return {"game_status":"no"} ret = {} if game.status == "mrx_active": ret["remaining"] = game.duration.seconds elif game.detective_start + game.duration > datetime.now(): remaining = (game.detective_start + game.duration - datetime.now()).seconds ret["remaining"] = remaining else: game.status = "finished" ret["game_status"] = game.status cherrypy.request.db.add(game) cherrypy.request.db.commit() return ret
def delta(self, game_id = None): if game_id is None: user, role, game, all_tasks = get_session_info() game_id = game.id return {"delta":self.get_delta(game_id)}