def msg_read(mid): """ This is the message page. The only available messages are the ones where sender or recipient is the current username logged in. Other MIDs (Message IDs) give 404 error (if the user forces another mid) If the current logged in users reads a message from the inbox, it automatically marks it as MSG READ. :param mid: current message ID :return: """ # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) try: # get the current message from inbox or outbox and if not deleted m_read = Messages.query. \ filter(Messages.mid == mid). \ filter((Messages.recipient == current_user.username) & (Messages.del_in == 0) | ( Messages.sender == current_user.username) & (Messages.del_out == 0)). \ first() # mark the inbox message as read when the message is opened if m_read.recipient == current_user.username: m_read.seen = True db.session.add(m_read) db.session.commit() # delete the specific message and return to inbox or outbox form = DeleteSingleMessageButton() if form.validate_on_submit(): msg_del = Messages.query.get_or_404(mid) if msg_del.recipient == current_user.username: msg_del.del_in = True db.session.add(msg_del) db.session.commit() return redirect(url_for("msgs.msg_inbox")) else: msg_del.del_out = True db.session.add(msg_del) db.session.commit() return redirect(url_for("msgs.msg_outbox")) return render_template("msg_read.html", msg_read=m_read, form=form, get_unread_msgs=get_unread_msgs) except AttributeError: # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) return render_template("errors/404.html", get_unread_msgs=get_unread_msgs)
def account_update(): form = UpdateAccount() # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) if form.validate_on_submit(): current_user.username = form.username.data current_user.email = form.email.data current_user.hidemail = form.hidemail_checkbox.data current_user.msg_per_page = form.msg_per_page.data try: db.session.commit() flash("Your account has been updated.", "success") redirect(url_for("users.account_home")) except IntegrityError: db.session.rollback() flash("Username or E-mail already exists.", "danger") redirect(url_for("users.account_home")) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email form.hidemail_checkbox.data = current_user.hidemail form.msg_per_page.data = current_user.msg_per_page return render_template("account_update.html", form=form, get_unread_msgs=get_unread_msgs)
def hero_training(): """ Training zone for hero_main """ # Check if the account has a hero_main created # If FALSE redirect user to create a hero_main if acc_has_hero(current_user.id): return redirect(url_for("hero_main.hero_create")) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) # Regenerate HP of hero_main if it is lower then hp_max hp_regeneration(current_user.id) # Get the Hero from DB. Needed for stats page to list Hero hero_main vital info hero = db.session.query(Hero, HeroPoints).\ join(HeroPoints, Hero.hid == HeroPoints.hpid).\ filter(Hero.hid == current_user.id).\ first() # Get the Max HP of the hero_main based on HP Points max_hp = hp_max(hero[1].hp_point) # Calculates the time when HP will be full hp_full_time = hero[0].hp_check_regen + timedelta(seconds=max_hp - hero[0].hp) # Check if the hero_main is returning from a battle if hero[0].action == 1: battle_return_time(current_user.id) # Check for level Up if hero[0].current_exp >= hero[0].next_lvl_exp: level_up(current_user.id) # Get all animals for the training section animals = AnimalsTraining.query.all() # Reviving Hero if it is in state of reviving if hero[0].alive == 1: revive_hero(current_user.id) # Time when the hero_main is alive again reviving_time = hero[0].death_check + timedelta( seconds=hero[0].revive_time) # Time when hero_main will arrive from battle returning_time = hero[0].return_from_action + timedelta( seconds=hero[0].return_seconds) return render_template("hero_training.html", hero=hero, animals=animals, hp_full_time=hp_full_time, get_unread_msgs=get_unread_msgs, max_hp=max_hp, reviving_time=reviving_time, returning_time=returning_time)
def profile(id): user = User.query.get_or_404(id) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) return render_template("profile.html", user=user, get_unread_msgs=get_unread_msgs)
def statistics(): stats = db.session.query(User, Hero).\ outerjoin(Hero, User.id == Hero.hid).\ order_by(Hero.current_exp.desc(), Hero.level_date.asc(), Hero.name.desc()) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) return render_template("statistics.html", stats=stats, get_unread_msgs=get_unread_msgs)
def msg_inbox(): """ Get messages for INBOX page. Show only messages where recipient is the current logged in user """ # Get Inbox Messages and Paginate them page = request.args.get("page", 1, type=int) msg_per_page = User.query.get_or_404(current_user.id) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) if request.args and request.args.get("unread") == "true": unread_msg_count = Messages.query. \ filter_by(recipient=current_user.username, del_in=False, seen=False). \ count() msgs_received = Messages.query. \ order_by(Messages.date.desc()). \ filter_by(recipient=current_user.username, del_in=False, seen=False). \ paginate(page=page, per_page=unread_msg_count) else: msgs_received = Messages.query. \ order_by(Messages.date.desc()). \ filter_by(recipient=current_user.username, del_in=False). \ paginate(page=page, per_page=msg_per_page.msg_per_page) next_page_button = url_for("msgs.msg_inbox", page=msgs_received.next_num) if msgs_received.next_num else None previous_page_button = url_for("msgs.msg_inbox", page=msgs_received.prev_num) if msgs_received.prev_num else None # Multiple delete form form = MessageMultipleDelete() if form.validate_on_submit(): del_lst = request.form.getlist("multi_del") if len(del_lst) == 0: flash("No messages selected", "warning") return redirect(url_for("msgs.msg_inbox")) for i in del_lst: msg_id = Messages.query.get_or_404(i) msg_id.del_in = 1 db.session.add(msg_id) db.session.commit() flash("All selected messages were deleted.", "success") return redirect(url_for("msgs.msg_inbox")) return render_template("msg_inbox.html", msgs_received=msgs_received, next_page_button=next_page_button, previous_page_button=previous_page_button, form=form, get_unread_msgs=get_unread_msgs)
def hero_create(): """ If you do not have a hero_main, create one here. """ # Check if the account has a hero_main created # If TRUE redirect user to Overview Page if not acc_has_hero(current_user.id): return redirect(url_for("hero_main.hero_home")) form = CreateHeroForm() # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) if form.validate_on_submit(): try: hero_main = Hero(hid=current_user.id, name=form.h_name.data, gender=form.h_gender.data, date_created=datetime.now(), level=1, current_exp=0, next_lvl_exp=100, hp=100, hp_regen_rate=1, hp_check_regen=datetime.now(), alive=2, death_check=datetime.now(), revive_time=10, action=0, return_from_action=datetime.now(), return_seconds=0) hero_points = HeroPoints(hpid=current_user.id, unused_points=0, hp_point=5, attack_point=5) db.session.add(hero_main) db.session.add(hero_points) db.session.commit() flash("Hero created successfully.", "success") return redirect(url_for("hero_main.hero_home")) except IntegrityError: flash( "`" + form.h_name.data + "` already exists. Please choose another name for your hero_main.", "warning") return redirect(url_for("hero_main.hero_create")) return render_template("hero_create.html", form=form, get_unread_msgs=get_unread_msgs)
def msg_send(): """ Send message based on the Username entered. Checks if the introduced username exists in the database. IF the user does not exist, it gives a warning and the message is kept in the current form for more editing. IF the message was successfully sent then the form is reset. """ form = SendMessageForm() # Get the current Send Message Form # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) if form.validate_on_submit(): try: # Check if username exists get_username_record = User.query.filter_by(username=form.send_to.data).first() # If everything is OK, add message to DB message = Messages(recipient=get_username_record.username, sender=current_user.username, subject_msg=form.subject.data, body_msg=form.msg_body.data, date=datetime.now(), seen=False, del_in=False, del_out=False) db.session.add(message) db.session.commit() flash("Message sent.", "success") return redirect(url_for('msgs.msg_send')) except AttributeError: # Get error if username is invalid or does not exist flash("Username " + form.send_to.data + " not found.", "danger") return render_template("msg_send.html", form=form, get_unread_msgs=get_unread_msgs)
def error_404(error): # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) return render_template("errors/404.html", get_unread_msgs=get_unread_msgs), 404
def hero_home(): """ Homepage for hero_main """ # Check if the account has a hero_main created # If FALSE redirect user to create a hero_main if acc_has_hero(current_user.id): return redirect(url_for("hero_main.hero_create")) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) # Get the Hero from DB. Needed for overview page to list Hero details hero = db.session.query(Hero, HeroPoints).\ join(HeroPoints, Hero.hid == HeroPoints.hpid).\ filter(Hero.hid == current_user.id).\ first() # Check if the hero_main is returning from a battle if hero[0].action == 1: battle_return_time(current_user.id) # Get the Max HP of the hero_main based on HP Points max_hp = hp_max(hero[1].hp_point) # Regenerate HP of hero_main if it is lower then hp_max hp_regeneration(current_user.id) # Reviving Hero if it is in state of reviving if hero[0].alive == 1: revive_hero(current_user.id) # Revive and Kill Hero Form # Kill hero_main is just for testing purposes form = ReviveHeroForm() if form.h_kill.data and form.validate_on_submit(): hero[0].hp = 0 hero[0].alive = 0 db.session.commit() if form.h_revive.data and form.validate_on_submit(): hero[0].death_check = datetime.now() hero[0].alive = 1 db.session.commit() if form.h_upd_points and form.validate_on_submit(): pass # Time when the hero_main is alive again reviving_time = hero[0].death_check + timedelta( seconds=hero[0].revive_time) # Time when hero_main will arrive from battle returning_time = hero[0].return_from_action + timedelta( seconds=hero[0].return_seconds) # Calculates the time when HP will be full hp_full_time = hero[0].hp_check_regen + timedelta(seconds=max_hp - hero[0].hp) return render_template("hero_home.html", hero=hero, form=form, max_hp=max_hp, hp_full_time=hp_full_time, get_unread_msgs=get_unread_msgs, reviving_time=reviving_time, returning_time=returning_time)
def account_home(): # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) return render_template("account_home.html", get_unread_msgs=get_unread_msgs)
def train(id): """ Training on different levels for different animals """ # Check if the account has a hero_main created # If FALSE redirect user to create a hero_main if acc_has_hero(current_user.id): return redirect(url_for("hero_main.hero_create")) # Get the number of unread messages get_unread_msgs = unread_msgs(current_user.username) # Get the Hero from DB. Needed for stats page to list Hero hero_main vital info hero = db.session.query(Hero, HeroPoints).\ join(HeroPoints, Hero.hid == HeroPoints.hpid).\ filter(Hero.hid == current_user.id).\ first() # Get the Max HP of the hero_main based on HP Points max_hp = hp_max(hero[1].hp_point) # if the user enters the link manual, check if the hp is >0 or redirect user home if hero[0].hp == 0: return redirect(url_for("hero_main.hero_home")) # Check for level Up if hero[0].current_exp >= hero[0].next_lvl_exp: level_up(current_user.id) # Check if the hero_main is returning from a battle if hero[0].action == 1: battle_return_time(current_user.id) # Return message if the hero_main is on it's way to somewhere if hero[0].action == 1: msg = hero[ 0].name + " is currently returning from a battle. Wait until your hero_main is home and try again!" flash(msg, "warning") return redirect(url_for("hero_main.hero_home")) # Get the animal for the current training level animal = AnimalsTraining.query.get_or_404(id) # ----- The Process of fighting - 1st try ----- # output = [] # output messages # Creating variables for the fight hero_hp = hero[0].hp animal_hp = animal.hp while True: hero_dmg = randint(round(hero[1].attack_point * 0.5), round(hero[1].attack_point * 1.26)) animal_hp -= hero_dmg if animal_hp <= 0: # IF the hero_main wins the battle out = hero[0].name + " hit " + animal.name + " with " + str(hero_dmg) + " dmg. " + animal.name + \ " is DEAD. " + hero[0].name + " WON this battle." output.append(out) # random experience gained from battle based on the value from db exp = randint(round(animal.exp_given * 0.76), round(animal.exp_given * 1.26)) break else: out = hero[0].name + " hit " + animal.name + " with " + str(hero_dmg) + ". " + animal.name + \ " HP is lowered from " + str(animal_hp + hero_dmg) + " to " + str(animal_hp) output.append(out) animal_dmg = randint(round(animal.attack_point * 0.5), round(animal.attack_point * 1.26)) hero_hp -= animal_dmg if hero_hp <= 0: # IF the animal wins the battle out = animal.name + " hit " + hero[0].name + " with " + str(animal_dmg) + " dmg. " + hero[0].name + \ " is DEAD. " + animal.name + " won this battle." output.append(out) # if hero_main is dead 0 exp will gain from battle exp = 0 msg_dead = Markup( "<i class='far fa-sad-tear'></i> You have died. Revive your hero and try again!" ) flash(msg_dead, "danger") break else: out = animal.name + " hit " + hero[0].name + " with " + str(animal_dmg) + \ ". " + hero[0].name + " HP is lowered from " + str(hero_hp + animal_dmg) + " to " + str(hero_hp) output.append(out) if hero_hp <= 0: hero[0].hp = 0 hero[0].alive = 0 else: hero[0].hp_check_regen = datetime.now() hero[0].action = 1 hero[0].return_from_action = datetime.now() hero[0].return_seconds = animal.duration hero[0].hp = hero_hp hero[0].current_exp += exp # Check for level Up if hero[0].current_exp >= hero[0].next_lvl_exp: level_up(current_user.id) msg_lvl_up = "Congratulation, " + hero[ 0].name + "! You have leveled up!" flash(msg_lvl_up, "success") db.session.commit() # ----- The Battle is Over ----- # # Time when hero_main will arrive from battle returning_time = hero[0].return_from_action + timedelta( seconds=hero[0].return_seconds) # Calculates the time when HP will be full hp_full_time = hero[0].hp_check_regen + timedelta(seconds=max_hp - hero[0].hp) return render_template("hero_train.html", animal=animal, hero=hero, max_hp=max_hp, hp_full_time=hp_full_time, get_unread_msgs=get_unread_msgs, output=output, exp=exp, returning_time=returning_time)