def kahio_correct_guess(u_id, channel_id, new_message): """ If the correct message was guessed the user will be appended to the list an error will be raised if the user should already have the answer Parameters: u_id(int): The u_id of the user guessing channel_id(int): The channel_id of channel guess is occuring new_message(message dict): The message dict created in the message_send function Returns: message_id(int): The message_id of the message added to the channel """ #Adds the information that this user guessed correctly and gets the message to #Display instead of the correct guess new_message["message"] = data.correct_kahio_guess(u_id, channel_id, new_message["time_created"]) #Changes the user the message is being sent from to the user that started the KAHIO game new_message["u_id"] = data.return_kahio_starter(channel_id) #Adds the message data.add_message(new_message, channel_id) return { "message_id": new_message["message_id"], }
def kahio_guess(u_id, channel_id, new_message): """ Will check if the message is the correct answer or not If the correct message was guessed the user will be appended to the list, otherwise the incorrect message will just be treated as a regular guess, an error will be raised if the user should already have the answer Parameters: u_id(int): The u_id of the user guessing channel_id(int): The channel_id of channel guess is occuring new_message(message dict): The message dict created in the message_send function Returns: message_id(int): The message_id of the message added to the channel """ #Checks the user has not already guessed the message validation.check_kahio_user_has_answer(channel_id, u_id) #Gets the answer check_message = validation.check_kahio_answer(new_message["message"]) #Converts the user input question input into correct answer staging answer = data.return_kahio_answer(channel_id) #Checks if this guess is correct if check_message == answer: return kahio_correct_guess(u_id, channel_id, new_message) #Displays the guess since it was incorrect data.add_message(new_message, channel_id) return { "message_id": new_message["message_id"], }
def send_kahio_score(channel_id, new_message): """ Sends the end message for the KAHIO game Parameters: channel_id(int): the channel the game is in new_message(message dict): The message dictionary to be sent created int the timer function Returns: """ #Gets the stored answer from the data answer = data.return_kahio_answer(channel_id) #The firstline of the end print out new_message["message"] = "Kahio game has ended.\nThe correct answer was " + answer +"\n" #The number of users that correctly guessed the answer num_correct_answers = data.return_kahio_num_correct_answers(channel_id) if num_correct_answers == 0: #If no message was added to the kahio it will not print kahio new_message["message"] += "No correct answers" else: new_message["message"] += str(num_correct_answers) + " correct answers" #Adds all stored correct guesses new_message["message"] += data.return_kahio_score(channel_id) #Puts the message in the channel data.add_message(new_message, channel_id)
def start(u_id, channel_id, message, message_id): """ Checks if no standups are in progess and initiates hangman """ # Check can start validation.check_can_start_hangman(channel_id) msg = message['message'] buffer = len('/hangman start ') word = msg[buffer:].lower() #word = ''.join(msg.split(' ')[2:]).lower() # Check valid word validation.check_valid_word(word) # Updates hangman info with that word info = data.get_hangman_info(channel_id) update_info(info, u_id, message_id, word) # Prints hangman starting message message['message'] = print_start(info) data.add_message(message, channel_id) message_info = data.get_message(channel_id, message_id) message_info['is_pinned'] = True return {'message_id': message_id}
def kahio_timer(*args): """ Sends the standup message from the given u_id Parameters: args[0] (u_id(int)) : The u_id that standup will be sent from args[1] (channel_id(int)) : The channel that the message will be sent to args[2] (time_remaining(int)) : The time that kahio has remaining Returns: """ #Make a message using the u_id from the user that started the KAHIO game new_message_id = data.make_message_id() new_message = {} new_message["message"] = "The kahoi game has " + str(args[2]) + " seconds remaining" new_message["u_id"] = args[0] new_message["time_created"] = datetime.datetime.now().replace().timestamp() new_message["message_id"] = new_message_id new_message["reacts"] = [ { "react_id": 1, "u_ids": [] } ] if args[2] <= 0: #If the timer has finished it will print the end message data.end_kahio_game(args[1]) send_kahio_score(args[1], new_message) return #Inputs the timing message into the channel messages data.add_message(new_message, args[1]) #Stores how long the multithreading wait will be and the time left #after the multithreading ends time_remaing = args[2] time_interval = 0 if time_remaing <= 5: time_interval = 1 elif math.remainder(time_remaing, 5) == 0: time_interval = 5 else: time_interval = int(math.remainder(time_remaing, 5)) #Removes how long the timer will wait from the time remaining time_remaing -= time_interval #Calls this function again after time_interval amount of time timer_class = threading.Timer(time_interval, kahio_timer, [args[0], args[1], time_remaing]) timer_class.start() #Stores new timer_class so the timer can still be stopped data.kahio_update_timer_class(args[1], timer_class)
def send_standup(*args): """ Sends the standup message from the given u_id Parameters: args[0] (u_id(int)) : The u_id that standup will be sent from args[1] (channel_id(int)) : The channel that the message will be sent to """ new_message_id = data.make_message_id() new_message = {} new_message["message"] = data.return_standup_message(args[1]) if new_message["message"] == "": #If no message was added to the standup it will not print standup return new_message["u_id"] = args[0] new_message["time_created"] = datetime.datetime.now().replace().timestamp() new_message["message_id"] = new_message_id new_message["is_pinned"] = False new_message["reacts"] = [{"react_id": 1, "u_ids": []}] data.add_message(new_message, args[1])
def test_add_message(): """ Add_message always returns None, but we can test it by checking if the number of messages changes after add_message is run. """ # Need user token so can't use user1 pytest fixture user = auth.auth_register("*****@*****.**", "testing", "anew", "user") chan1 = channels.channels_create(user["token"], "green", True) assert data.get_message_num() == 0 fake_message = { "message" : "Pretend this is legit", "u_id" : user["u_id"], "time_created" : datetime.datetime.now(), "message_id" : data.make_message_id() } assert data.add_message(fake_message, chan1["channel_id"]) == None assert data.get_message_num() == 1 other.clear()
def message_send(token, channel_id, message): """ Adds a new message to the messages in a channel Parameters: token(string): An authorisation hash channel_id(int): The channel_id of the channel the message is being added too message(string): The message of the message being added Returns: message_id(int): An identifier for the new message """ # Check that the token is valid user_input_id = validation.check_valid_token(token) # Check that the message is valid. validation.valid_message(message) # Check that the channel_id is valid validation.check_valid_channel_id(channel_id) # Check that user is in channel validation.check_user_in_channel(user_input_id, channel_id) new_message_id = data.make_message_id() new_message = {} new_message["message"] = message new_message["u_id"] = user_input_id new_message["time_created"] = datetime.datetime.now().replace().timestamp() new_message["message_id"] = new_message_id new_message["is_pinned"] = False new_message["reacts"] = [{ "react_id": 1, "u_ids": [] }, { "react_id": 2, "u_ids": [] }, { "react_id": 3, "u_ids": [] }, { "react_id": 4, "u_ids": [] }] # Check if message is calling weather API if validation.check_weather_call(message): new_message['message'] = weather.get_weather(message) # Check if message will start a hangman session if validation.check_start_hangman(channel_id, message): return hangman.start(user_input_id, channel_id, new_message, new_message_id) # Check if hangman is active and message is a guess if validation.check_if_hangman(channel_id, message): hangman.guess(user_input_id, channel_id, message) # Check if hangman should stop (/hangman stop) if validation.check_if_stop_message(message): return hangman.stop(user_input_id, channel_id) if message.startswith("/KAHIO/END"): new_message["message"] = kahio.kahio_end(user_input_id, channel_id) elif message.startswith("/KAHIO"): new_message["message"] = kahio.start_kahio(user_input_id, channel_id, message) elif data.check_kahio_running(channel_id): return kahio.kahio_guess(user_input_id, channel_id, new_message) data.add_message(new_message, channel_id) return { "message_id": new_message_id, }