def test_valid_kill_no_team_push(selfi, mock_action_get_by_id, mock_kill_handler): """ Valid kill, but team not killed """ ref = "1" action = Action() action.action = "KILL" mock_action_get_by_id.return_value = action mock_kill_handler.return_value = None Reply.handler(ref, ["Y"], Player()) mock_kill_handler.assert_called_once_with(action, "Y")
def test_disarm(self, mock_action_get_by_id, mock_disarm_handler): """ Test disarm """ ref = "1" action = Action() action.action = "DISARM" mock_action_get_by_id.return_value = action mock_disarm_handler.return_value = None # Don't care about response because handled by other unit tests. Reply.handler(ref, ["Y"], Player()) mock_disarm_handler.assert_called_once_with(action, "Y")
def setUp(self): super(TestKill, self).setUp() action = Action() action.attacker = "+1" action.action = "KILL" action.victim = "+4" action.datetime = datetime.now() action.put() self.action = action
def setUp(self): super(TestDisarm, self).setUp() action = Action() action.attacker = "+4" action.action = "DISARM" action.victim = "+1" action.datetime = datetime.now() action.put() self.action = action
def setUp(self): super(TestBomb, self).setUp() test_dt = Util.utc_to_chi(datetime.now()) + timedelta(minutes=1) self.default_params = ["Here", str(test_dt.month), str(test_dt.day),\ str(test_dt.hour), str(test_dt.minute)] action = Action() action.attacker = "+1" action.action = "BOMB" action.victim = "*" action.datetime = datetime.now() action.place = "Place" action.put() self.action = action
def test_invalid_response(self, mock_action_get_by_id): """ Response is not Y, y, N, n """ ref = "1" mock_action_get_by_id.return_value = Action() with self.assertRaises(ReplyError) as e: Reply.handler(ref, ["F"], Player()) self.assertEquals(e.exception.message, "Reply {} is invalid. Ref num: {}".\ format("F", ref))
def handler(cls, attacker, params): if len(params) == 0: raise CommandError("Parameter {}".format(params)) victim = Util.get_victim(params[0]) Disarm.validate_disarm(attacker, victim) action = Action() action.attacker = attacker.key.id() action.action = "DISARM" action.victim = victim.key.id() action.datetime = datetime.now() action.need_validation = True action_key = action.put() return [(action.victim, "{} claimed to have disarm you. " "[REPLY {}] Y/N.".format(attacker.realname, action_key.id()))]
def handler(cls, attacker, params): logging.info("KILL start") victim = Util.get_victim(params[0]) Kill.validate_kill(attacker, victim) action = Action() action.attacker = attacker.key.id() action.action = "KILL" action.victim = victim.key.id() action.datetime = datetime.now() action.need_validation = True action_key = action.put() logging.info("KILL finish") return [(action.victim, "{} claimed to have killed you. " "[REPLY {}] Y/N.".format(attacker.realname, action_key.id()))]
def handler(cls, attacker, victim_codename): logging.info("SNIPE start.") victim = Util.get_victim(victim_codename) logging.info("Attacker {}".format(attacker)) logging.info("Victim {}".format(victim)) """ validation """ outgoing = [] try: Kill.validate_kill(attacker, victim) if attacker.role != "SNIPER": raise MeError("not SNIPER") except (TeamError, MeError, TargetError) as message: outgoing.append((WEI_HAN, message.message)) outgoing.append((attacker.key.id(), message.message)) return outgoing except: message = "[ERR] Unknown Error in SNIPE" outgoing.append((WEI_HAN, message)) outgoing.append((attacker.key.id(), message)) return outgoing action = Action() action.attacker = attacker.key.id() action.action = "SNIPE" action.victim = victim.key.id() action.datetime = datetime.now() action_key = action.put() attacker = Player.get_by_id(action.attacker) attacker.killed.append(str(action_key.id())) attacker.put() victim.state = "DEAD" victim.killed_by = str(action_key.id()) victim.put() message = "{} has been SNIPED.".format(victim_codename) outgoing.append((victim.key.id(), "You have been SNIPED. (Ref {}).".\ format(action_key.id()))) outgoing.append(("*", message)) outgoing += Team.push(Team.get_by_id(victim.team)) return outgoing
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)
def handler(cls, attacker, params): logging.info("KILL start") victim = Util.get_victim(params[0]) Kill.validate_kill(attacker, victim) action = Action() action.attacker = attacker.key.id() action.action = "KILL" action.victim = victim.key.id() action.datetime = datetime.now() action.need_validation = True action_key = action.put() logging.info("KILL finish") return [ ( action.victim, "{} claimed to have killed you. " "[REPLY {}] Y/N.".format(attacker.realname, action_key.id()), ) ]
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 reply_handler(cls, action, response, from_): # Deep copy bomb action_c = Action() action_c.attacker = action.attacker action_c.action = action.action action_c.victim = from_.key.id() action_c.datetime = datetime.now() action_c.place = action.place if response == "Y" or response == "y": action_c.need_validation = False action_c.incorrect_kill = False action_c_key = action_c.put() victim = Player.get_by_id(action_c.victim) victim.state = "DEAD" victim.killed_by = str(action_c_key.id()) victim.put() attacker = Player.get_by_id(action_c.attacker) attacker.killed.append(str(action_c_key.id())) attacker.put() return [("*", "{} has been killed".format(victim.codename))] else: action_c.need_validation = True action_c.incorrect_kill = True return []
from view.user import UserView from controller.user import UserController from model.user import User from controller.persistence import PersistenceController from model.persistence import Persistence from controller.actions import ActionController from model.actions import Action persistence_model = Persistence() persistence = PersistenceController(persistence_model) actions_model = Action(persistence) actions_controller = ActionController(actions_model) user_model = User(persistence, actions_controller) user_controller = UserController(user_model) userview = UserView(user_controller) def welcome(): return input('''Bienvenido, escoje una opción: 1.- Log in 2.- Register s.- Salir ''') option = welcome() if option == '2': userview.register() if option == '1':