Пример #1
0
 def test_good_bomb(self):
     """ Good attacker set good bomb. """
     attacker = self.p1a
     later = Util.utc_to_chi(datetime.now())
     params = self.default_params
     Bomb.handler(attacker, params)
     self.assertEqual(len(self.taskqueue_stub.get_filtered_tasks(\
             queue_names='bomb')), 1)
Пример #2
0
 def test_error_role_is_not_demo(self):
     """ Attacker is not role DEMO, Error."""
     attacker = self.p1b
     params = self.default_params
     with self.assertRaises(MeError) as e:
         Bomb.handler(attacker, params)
     self.assertEqual(e.exception.message, "You are not DEMO")
     self.assertEqual(len(self.bomb_queue), 0)
Пример #3
0
 def test_error_attacker_is_disarm(self):
     """ Attacker is diarmed trying to set a bomb: use p2a as attacker """
     attacker = self.p1a
     attacker.disarm = True
     attacker.put()
     params = self.default_params
     with self.assertRaises(MeError) as e:
         Bomb.handler(attacker, params)
     self.assertEqual(e.exception.message, "You are DISARM")
     self.assertEqual(len(self.bomb_queue), 0)
Пример #4
0
 def test_error_attacker_is_dead(self):
     """ Attacker is DEAD, Error. """
     attacker = self.p1a
     attacker.state = "DEAD"
     attacker.put()
     params = self.default_params
     with self.assertRaises(MeError) as e:
         Bomb.handler(attacker, params)
     self.assertEqual(e.exception.message, "You are DEAD")
     self.assertEqual(len(self.bomb_queue), 0)
Пример #5
0
 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)
Пример #6
0
 def test_error_time_is_before_now(self):
     """ Attacker wants to set a bomb back in time """
     attacker = self.p1a
     now = Util.utc_to_chi(datetime.now()) - timedelta(hours=1)
     params = ["Here", str(now.month), str(now.day),\
             str(now.hour), str(now.minute)]
     with self.assertRaises(TimeError) as e:
         Bomb.handler(attacker, params)
     self.assertEqual(e.exception.message, "You cannot set {} before time {}."\
             .format(now.strftime("%m-%d %I:%M%p"),\
             Util.utc_to_chi(datetime.now()).strftime("%m-%d %I:%M%p")))
     self.assertEqual(len(self.bomb_queue), 0)
Пример #7
0
 def test_bomb_reply_Y(self):
     """ Bomb reply Y response """
     ret = Bomb.reply_handler(self.action, "Y", self.p2a)
     self.assertEqual(len(ret), 1)
     self.assertEqual(ret[0][0], "*")
     self.assertEqual(ret[0][1], "p2a has been killed")
     self.assertEqual(self.p2a.state, "DEAD")
Пример #8
0
    def inner_handler(cls, message):
        """ Return [(number, msg),...]"""
        action, params = CommandHandler.get_command(message.Body)
        attacker = Util.get_attacker(message.From)

        if action == "KILL":
            return Kill.handler(attacker, params)
        elif action[1:] == "REPLY":
            ref = params.pop(0)[:-1]
            return Reply.handler(ref, params, attacker)
        elif action == "BOMB":
            return Bomb.handler(attacker, params)
        elif action == "INVUL":
            return Invul.handler(attacker, params)
        elif action == "DISARM":
            return Disarm.handler(attacker, params)
        elif action == "SNIPE":
            if message.From != WEI_HAN:
                raise CommandError(action)
            sniper = Player.query(Player.codename == params[0]).get()
            if sniper == None:
                raise DbError(params[0])
            return Snipe.handler(sniper, params[1])
        elif action == "?":
            msg = "Guide for SAMSU Assassins:\n"
            msg += "KILL <target codename>\n"
            msg += "BOMB <mm> <dd> <hour> <min> <place>\n"
            msg += "INVUL <target codename> <mm> <dd> <hour> <min>\n"
            msg += "DISARM <target codename>\n"
            msg += "SNIPE - send message and picture to {}\n".format(WEI_HAN)
            msg += "REPLY - [REPLY <number>] Y or [REPLY <number>] N\n"
            msg += "If you receive an UNKNOWN ERROR or don't get a message that you expect, contact Wei Han at 312-731-0539."
            return [(attacker.key.id(), msg)]
        else:
            raise CommandError(action)
Пример #9
0
    def handler(cls, ref, params, From):
        logging.debug("REPLY {}".format(ref))
        lookup = Action.get_by_id(int(ref))
        if not lookup:
            raise ReplyError("ref num", ref)

        response = params[0]
        if response != "Y" and response != "y" and response != "N" and response != "n":
            raise ReplyError(response, ref)

        if lookup.action == "DISARM":
            return Disarm.reply_handler(lookup, response)
        else:
            output_msg = []
            if lookup.action == "KILL":
                output_msg += Kill.reply_handler(lookup, response)
            elif lookup.action == "BOMB":
                output_msg += Bomb.reply_handler(lookup, response, From)
            """ Generate push if necessary """
            output_msg += Team.push(Team.get_by_id(From.team))
            return output_msg
        raise ReplyError(response, ref)
Пример #10
0
 def test_bomb_reply_n(self):
     """ Bomb reply n response """
     ret = Bomb.reply_handler(self.action, "n", self.p2a)
     self.assertEqual(len(ret), 0)
     self.assertEqual(self.p2a.state, "ALIVE")
Пример #11
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)