def test_error_attacker_bomb_time_invalid(self): """ Attacker wants to set a bomb but his bomb quota for today is expired""" attacker = self.p1a attacker.can_set_after = Util.next_day() params = self.default_params with self.assertRaises(TimeError) as e: Bomb.handler(attacker, params) self.assertEqual(e.exception.message, "You cannot set bomb before time {}.".\ format(Util.utc_to_chi(attacker.can_set_after).strftime(\ "%m-%d %I:%M%p"))) self.assertEqual(len(self.bomb_queue), 0)
def test_invul_error_time_before_valid_time(self): """ MEDIC can only INVUL one person per 24 hour period """ medic = self.p1c medic.can_set_after = Util.next_day() medic.put() params = self.default_params with self.assertRaises(TimeError) as e: Invul.handler(medic, params) self.assertEqual(e.exception.message, "You cannot set invul before time {}."\ .format(Util.utc_to_chi(medic.can_set_after).strftime("%m-%d %I:%M%p"))) self.assertEqual(len(self.invul_queue), 0)
def bomb_worker(): ''' Get bomb id ''' req_key = request.form.get('id', "") bomb = Bomb.get_by_id(int(req_key)) ''' ERROR: no bomb found by id ''' if not bomb: logging.error("BOMB Worker: No bomb found by key {}".format(req_key)) raise Exception() ''' Bomb deprecated no action ''' if bomb.deprecated: logging.info("BOMB Worker: Bomb with key {} deprecated. No explosion".format(req_key)) return "BOMB Worker: Deprecated Bomb" ''' Trigger bomb ''' logging.info("BOMB: triggered at {} UTC {} Chicago".format( datetime.now(), Util.utc_to_chi(datetime.now().replace(tzinfo=pytz.utc)))) client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) attacker = Player.get_by_id(bomb.attacker) attacker.can_set_after = Util.next_day() attacker.put() bomb.trigger = True bomb_key = bomb.put() action = Action() action.attacker = bomb.attacker action.action = "BOMB" action.victim = "*" action.datetime = datetime.now() action.place = bomb.place action_key = action.put() response_num_list = [key.id() for key in Player.query(ndb.AND(Player.state=="ALIVE",\ Player.invul==False) ).fetch(keys_only=True)] response = "{} has been bombed at {}. If you were there, [REPLY {}] Y.".format( action.place, Util.utc_to_chi(action.datetime).strftime("%m-%d %I:%M%p"), action_key.id()) for response_number in response_num_list: logging.info("Making message {} for {} with num_list {}".format( response, response_number, response_num_list)) '''Make message''' outgoing_message = Message(From=SERVER_NUMBER, To=response_number, Body=response) outgoing_message.put() '''Send message''' client.messages.create( to=response_number, from_=SERVER_NUMBER, body=response) return "Bomb triggered at {}".format(bomb.place)
def invul_worker(): ''' Get invul id''' req_key = request.form.get("id", "") inv = Invul.get_by_id(int(req_key)) ''' No Inv found ''' if not inv: logging.error("INV worker: no INV found") raise Exception() '''Inv deprecated''' if inv.deprecated: logging.info("INV Worker: Inv deprecated. No action.") return "INVUL Worker: Deprecated" client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) '''Target dead. Report back to medic''' logging.info(inv) target_num = inv.target logging.info(target_num) target = Player.get_by_id(target_num) medic = Player.get_by_id(inv.medic) logging.info(medic) if not target: logging.error("INV worker: cannot find target {}".format(target_num)) return if target.state == "DEAD": logging.error("INV worker: Target {} has died. Cannot grant INVUL".format(target.codename)) response = "Your INVUL target has died. Cannot grant dead people INVUL." msg = Message(From=SERVER_NUMBER, To=inv.medic, Body=response) msg.put() client.messages.create( to=inv.medic, from_=SERVER_NUMBER, body=response) return "INVUL Worker" '''Target alive. If INVUL not yet in effect, trigger''' if not inv.in_effect: logging.info("INVUL worker: Triggering 8 hour INVUL for target {} at {}".format(target.codename, datetime.now())) inv.in_effect = True inv_key = inv.put() task = taskqueue.Task(url="/invul", params={"id": inv_key.id()}, eta=inv.end_time) task.add(queue_name="invul") logging.info("Task queue okay") logging.info(target) target.invul = True target.put() logging.info("target set okay") logging.info(medic) medic.can_set_after = Util.next_day() medic.put() logging.info("medic set okay") response = "You have been granted INVUL for 8 hour from {} to {}".\ format(Util.utc_to_chi(inv.start_time).strftime("%m-%d %I:%M%p"),\ Util.utc_to_chi(inv.end_time).strftime("%m-%d %I:%M%p")) msg = Message(From=SERVER_NUMBER, To=inv.target, Body=response) msg.put() client.messages.create( to=inv.target, from_=SERVER_NUMBER, body=response) logging.info("message set okay") return "INVUL Worker" else: logging.info("INVUL worker: END 8 hour INVUL for target {} at {}".format(target.codename, datetime.now())) inv.deprecated = True inv.put() target.invul = False target.put() response = "Your INVUL period has ended. You are no longer INVUL." msg = Message(From=SERVER_NUMBER, To=inv.target, Body=response) msg.put() client.messages.create( to=inv.target, from_=SERVER_NUMBER, body=response) return "INVUL Worker"
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() self.testbed.init_taskqueue_stub(root_path=".") self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME) # Setup fixture today = Util.next_day() - timedelta(days=1) # Make Team 1 and populate with player 1a, 1b, 1c. # p1a is ALIVE and DEMO # p1b is ALIVE, DISARM, and SNIPER # p1c is ALIVE, MEDIC self.team1 = Team(id = "Team1", to_kill="Team2", target_of="Team3") self.p1a = Player(id="+1", realname="player1a", codename="p1a",\ team="Team1", state="ALIVE", role="DEMO", can_set_after=today) self.team1.demo = "+1" self.p1a.put() self.p1b = Player(id="+2", realname="player1b", codename="p1b",\ team="Team1", state="ALIVE", disarm=True, role="SNIPER",\ can_set_after=today) self.team1.sniper="+2" self.p1b.put() self.p1c = Player(id="+3", realname="player1c", codename="p1c",\ team="Team1", state="ALIVE", role="MEDIC", can_set_after=today) self.team1.medic="+3" self.p1c.put() self.team1.put() # Make Team 2 and populate with player 2a, 2b, 2c. # p2a is ALIVE # p2b is DEAD # p2c is INVUL self.team2 = Team(id="Team2", to_kill="Team3", target_of="Team1") self.p2a = Player(id="+4", realname="player2a", codename="p2a",\ team="Team2", state="ALIVE", role="DEMO", can_set_after=today) self.team2.demo = "+4" self.p2a.put() self.p2b = Player(id="+5", realname="player2b", codename="p2b",\ team="Team2", state="DEAD", role="SNIPER", can_set_after=today) self.team2.sniper="+5" self.p2b.put() self.p2c = Player(id="+6", realname="player2c", codename="p2c",\ team="Team2", state="ALIVE", invul=True, role="MEDIC",\ can_set_after=today) self.team2.medic="+6" self.p2c.put() self.team2.put() # Make Team 3 and populate with player 3a, 3b, 3c. self.team3 = Team(id="Team3", to_kill="Team1", target_of="Team2") self.p3a = Player(id="+7", realname="player3a", codename="p3a",\ team="Team3", state="ALIVE", role="DEMO", can_set_after=today) self.team3.demo = "+7" self.p3a.put() self.p3b = Player(id="+8", realname="player3b", codename="p3b",\ team="Team3", state="ALIVE", role="SPY", can_set_after=today) self.team3.sniper="+8" self.p3b.put() self.p3c = Player(id="+9", realname="player3c", codename="p3c",\ team="Team3", state="ALIVE", role="MEDIC", can_set_after=today) self.team3.medic="+9" self.p3c.put() self.team3.put()
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().clear_cache() self.testbed.init_taskqueue_stub(root_path=".") self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME) # Setup fixture today = Util.next_day() - timedelta(days=1) # Make Team 1 and populate with player 1a, 1b, 1c. # p1a is ALIVE and DEMO # p1b is ALIVE, DISARM, and SNIPER # p1c is ALIVE, MEDIC self.team1 = Team(id="Team1", to_kill="Team2", target_of="Team3") self.p1a = Player( id="+1", realname="player1a", codename="p1a", team="Team1", state="ALIVE", role="DEMO", can_set_after=today ) self.team1.demo = "+1" self.p1a.put() self.p1b = Player( id="+2", realname="player1b", codename="p1b", team="Team1", state="ALIVE", disarm=True, role="SNIPER", can_set_after=today, ) self.team1.sniper = "+2" self.p1b.put() self.p1c = Player( id="+3", realname="player1c", codename="p1c", team="Team1", state="ALIVE", role="MEDIC", can_set_after=today ) self.team1.medic = "+3" self.p1c.put() self.team1.put() # Make Team 2 and populate with player 2a, 2b, 2c. # p2a is ALIVE # p2b is DEAD # p2c is INVUL self.team2 = Team(id="Team2", to_kill="Team3", target_of="Team1") self.p2a = Player( id="+4", realname="player2a", codename="p2a", team="Team2", state="ALIVE", role="DEMO", can_set_after=today ) self.team2.demo = "+4" self.p2a.put() self.p2b = Player( id="+5", realname="player2b", codename="p2b", team="Team2", state="DEAD", role="SNIPER", can_set_after=today ) self.team2.sniper = "+5" self.p2b.put() self.p2c = Player( id="+6", realname="player2c", codename="p2c", team="Team2", state="ALIVE", invul=True, role="MEDIC", can_set_after=today, ) self.team2.medic = "+6" self.p2c.put() self.team2.put() # Make Team 3 and populate with player 3a, 3b, 3c. self.team3 = Team(id="Team3", to_kill="Team1", target_of="Team2") self.p3a = Player( id="+7", realname="player3a", codename="p3a", team="Team3", state="ALIVE", role="DEMO", can_set_after=today ) self.team3.demo = "+7" self.p3a.put() self.p3b = Player( id="+8", realname="player3b", codename="p3b", team="Team3", state="ALIVE", role="SPY", can_set_after=today ) self.team3.sniper = "+8" self.p3b.put() self.p3c = Player( id="+9", realname="player3c", codename="p3c", team="Team3", state="ALIVE", role="MEDIC", can_set_after=today ) self.team3.medic = "+9" self.p3c.put() self.team3.put()