def run(self, event): output_lines = ["High scores:"] for game_name in self.high_scores: score = self.high_scores[game_name]["score"] player = self.high_scores[game_name]["player"] date = self.high_scores[game_name]["date"] output_lines.append("{}> Score: {}, Player: {}, Date: {}".format( game_name, score, player, Commons.format_unix_time(date))) return event.create_response("\n".join(output_lines))
def run(self, event): input_clean = event.command_args.strip().lower() url = "https://api.randomuser.me/0.6/?nat=gb&format=json" # Get api response json_dict = Commons.load_url_json(url) user_dict = json_dict["results"][0]["user"] # Construct response name = "{} {} {}".format( user_dict["name"]["title"], user_dict["name"]["first"], user_dict["name"]["last"], ).title() email = user_dict["email"] address = "{}, {}, {}".format( user_dict["location"]["street"].title(), user_dict["location"]["city"].title(), user_dict["location"]["postcode"], ) username = user_dict["username"] password = user_dict["password"] date_of_birth = Commons.format_unix_time(int(user_dict["dob"])) phone_home = user_dict["phone"] phone_mob = user_dict["cell"] national_insurance = user_dict["NINO"] pronoun = "he" if user_dict["gender"] == "male" else "she" pronoun_possessive = "his" if user_dict["gender"] == "male" else "her" if input_clean not in ["more", "full", "verbose", "all"]: output = "I have generated this person: Say hello to {}. {} was born at {}.".format( name, pronoun.title(), date_of_birth ) return event.create_response(output) output = ( "I have generated this person: Say hello to {}. " "{} was born at {} and lives at {}. " '{} uses the email {}, the username {} and usually uses the password "{}". ' "{} home number is {} but {} mobile number is {}. " "{} national insurance number is {}.".format( name, pronoun.title(), date_of_birth, address, pronoun.title(), email, username, password, pronoun_possessive.title(), phone_home, pronoun_possessive, phone_mob, pronoun_possessive.title(), national_insurance, ) ) return event.create_response(output)
def quit_game(self): """User has quit the game""" # check high scores is_high_score = self.check_high_score() if is_high_score: previous_score = self.high_scores_obj.get_high_score( self.HIGH_SCORE_NAME) previous_score_text = "(previous highscore was: {}, set by {} {}.)".format( previous_score["score"], previous_score["player"], Commons.format_unix_time(previous_score["date"]), ) self.update_high_score() # Create output return ("Sorry to see you quit, you had managed {} cards, " "which is a new highscore!{}".format( self.turns - 1, previous_score_text)) else: return "Sorry to see you quit, you had managed {} cards.".format( self.turns - 1)
def guess_lower(self): """User has guessed the next card is higher.""" last_card = self.last_card next_card = self.get_next_card() if next_card.to_int() < last_card.to_int(): output_string = ( "Your {} card is {}, which is lower! Congrats! Do you think the next card will " "be higher or lower?".format(Commons.ordinal(self.turns), next_card.to_string())) return output_string if next_card.to_int() == last_card.to_int(): output_string = ( "Your {} card is {}, which is the same (that's fine.) Do you think the next card will " "be higher or lower?".format(Commons.ordinal(self.turns), next_card.to_string())) return output_string if next_card.to_int() > last_card.to_int(): self.lost = True # high scores is_high_score = self.check_high_score() previous_score_text = "" if is_high_score: previous_score = self.high_scores_obj.get_high_score( self.HIGH_SCORE_NAME) previous_score_text = "(previous highscore was: {}, set by {} {}.)".format( previous_score["score"], previous_score["player"], Commons.format_unix_time(previous_score["date"]), ) self.update_high_score() # Output message output_string = "Your {} card is {}. Sorry, that's higher, you lose.".format( Commons.ordinal(self.turns), next_card.to_string()) if is_high_score: output_string += " You managed {} cards though, that's a new highscore!{}".format( self.turns - 1, previous_score_text) else: output_string += " You managed {} cards though.".format( str(self.turns - 1)) return output_string
def test_format_unix_time(unix, iso): assert Commons.format_unix_time(unix) == iso
def run(self, event): try: line = int(event.command_args) except ValueError: return event.create_response("Invalid timestamp") return event.create_response(Commons.format_unix_time(line) + ".")