working milestones prototype in dashboard

This commit is contained in:
Lili (Tlapka) 2023-03-14 16:26:04 +01:00
parent 68b8588c76
commit cb574618a6
3 changed files with 73 additions and 31 deletions

View File

@ -184,27 +184,50 @@ def add_to_redeem_queue(db, user_id, redeem_name, note=None):
return False return False
def add_to_milestone(db, redeem_name, points): def start_milestone(db, redeem_name):
try: try:
cursor = db.execute( cursor = db.execute(
"SELECT count, goal FROM milestones WHERE name = ?", "SELECT count, goal FROM milestones WHERE name = ?",
(redeem_name,) (redeem_name,)
) )
row = cursor.fetchone() milestone = cursor.fetchone()
if row is not None: current_app.logger.error(f"Milestone: {milestone}")
count = row[0] if milestone is None:
goal = row[1] current_app.logger.error(f"Adding milestone to db.")
result = count + points
if result > goal:
result = goal
cursor = db.execute( cursor = db.execute(
"UPDATE milestones SET count = ? WHERE name = ?", "INSERT INTO milestones(name, count, goal) VALUES(?, ?, ?)",
(result, redeem_name) (redeem_name, 0, current_app.config['REDEEMS'][redeem_name]['goal'])
) )
db.commit() db.commit()
return True except Error as e:
except (Error) as e: current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}")
def add_to_milestone(db, redeem_name, points):
try:
start_milestone(db, redeem_name)
cursor = db.execute(
"SELECT count, goal FROM milestones WHERE name = ?",
(redeem_name,)
)
row = cursor.fetchone()
count = row[0]
goal = row[1]
result = count + points
if result > goal:
result = goal
current_app.logger.error(f"Doing db operation to add to milestone.")
cursor = db.execute(
"UPDATE milestones SET count = ? WHERE name = ?",
(result, redeem_name)
)
db.commit()
return True
except Error as e:
current_app.logger.error(f"Error occured updating milestone: {e.args[0]}") current_app.logger.error(f"Error occured updating milestone: {e.args[0]}")
current_app.logger.error(f"Milestone add failed.")
return False return False

View File

@ -51,8 +51,8 @@ def handle_redeem(message, user_id):
send_chat(f"Cannot redeem {redeem}, no amount of points specified.") send_chat(f"Cannot redeem {redeem}, no amount of points specified.")
elif not note.isdigit(): elif not note.isdigit():
send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.") send_chat(f"Cannot redeem {redeem}, amount of points is not an integer.")
elif int(note) < points: elif int(note) > points:
send_chat(f"Can't redeem {redeem}, you don't have enough points.") send_chat(f"Can't redeem {redeem}, you don't have enough points.2")
else: else:
add_to_milestone(db, redeem, int(note)) add_to_milestone(db, redeem, int(note))
else: else:

View File

@ -1,15 +1,16 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<head> <head>
<title>Redeems Dashboard</title> <title>Redeems Dashboard</title>
</head> </head>
<div id="script"> <div id="script">
<script src="/static/dashboard.js"></script> <script src="/static/dashboard.js"></script>
<div class="tab"> <div class="tab">
<button class="tablinks" onclick="openTab(event, 'dashboard')", id="defaultOpen">Tlapbot dashboard</button> <button class="tablinks" onclick="openTab(event, 'dashboard')" , id="defaultOpen">Tlapbot dashboard</button>
{% if not passive %} {% if not passive %}
{% if queue %} {% if queue %}
<button class="tablinks" onclick="openTab(event, 'redeem-queue')">Redeem queue</button> <button class="tablinks" onclick="openTab(event, 'redeem-queue')">Redeem queue</button>
@ -19,6 +20,7 @@
</div> </div>
<div id='dashboard' class="tabcontent"> <div id='dashboard' class="tabcontent">
<body> <body>
<h3>Redeems Dashboard</h3> <h3>Redeems Dashboard</h3>
{% if (username and users ) %} {% if (username and users ) %}
@ -59,24 +61,36 @@
</table> </table>
{% endif %} {% endif %}
{% if milestones %} {% if milestones %}
{% for milestone in milestones %} <table>
<label for="file"> {{ milestone[0] }}</label> <thead>
<tr>
<progress id="file" max={{ milestone[2] }} value={{ milestone[1] }}></progress> <th>Active milestones</th>
{% endfor %} </tr>
{% endif %} </thead>
{% endif %} {% for milestone in milestones %}
<tbody>
<td> <label for="file"> {{ milestone[0] }}</label> </td>
<td> <progress id="file" max={{ milestone[2] }} value={{ milestone[1] }}></progress> </td>
</tbody>
{% endfor %}
{% endif %}
{% endif %}
</body> </body>
</div> </div>
<div id='redeem-queue' class="tabcontent"> <div id='redeem-queue' class="tabcontent">
<p>Tlap tlap.</p>
<body> <body>
<h3>Redeems Queue</h3> <h3>Redeems Queue</h3>
<p>Tlap tlap 2.</p>
{% if queue %} {% if queue %}
<p>Tlap tlap. 3</p>
<table> <table>
<thead> <thead>
<tr> <tr>
<th colspan="2">Recent redeems</th> <th colspan="2">Recent redeems</th>
<p>Tlap tlap. 4</p>
</tr> </tr>
<tr> <tr>
<th>Time</th> <th>Time</th>
@ -86,7 +100,7 @@
</tr> </tr>
</thead> </thead>
{% for row in queue %} {% for row in queue %}
<tbody> <tbody>
<td>{{ row[0].replace(tzinfo=utc_timezone).astimezone().strftime("%H:%M") }}</td> <td>{{ row[0].replace(tzinfo=utc_timezone).astimezone().strftime("%H:%M") }}</td>
<td>{{ row[1] }}</td> <td>{{ row[1] }}</td>
<td>{{ row[3] }}</td> <td>{{ row[3] }}</td>
@ -94,10 +108,14 @@
<td>{{ row[2] }}</td> <td>{{ row[2] }}</td>
{% endif %} {% endif %}
</tbody> </tbody>
<p>Tlap tlap. 5</p>
{% endfor %} {% endfor %}
</table> </table>
<p>Tlap tlap. 6</p>
{% endif %} {% endif %}
<p>Tlap tlap. 7</p>
</body> </body>
<p>Tlap tlap. 8</p>
</div> </div>
<div id='redeems-list' class="tabcontent"> <div id='redeems-list' class="tabcontent">
@ -107,9 +125,10 @@
<li><strong>Counter</strong> redeems add +1 to their counter.</li> <li><strong>Counter</strong> redeems add +1 to their counter.</li>
<li><strong>List</strong> redeems get added to the list of recent redeems (without a note).</li> <li><strong>List</strong> redeems get added to the list of recent redeems (without a note).</li>
<li><strong>Note</strong> redeems require you to send a message together with the redeem.</li> <li><strong>Note</strong> redeems require you to send a message together with the redeem.</li>
<li><strong>Milestone</strong> redeems are long-term goals to which you can donate any amount of points you want.</li> <li><strong>Milestone</strong> redeems are long-term goals to which you can donate any amount of points you
<li>They will be completed once the amount of points donated reaches the goal.</li> want. They will be completed once the amount of points donated reaches the goal.</li>
</ul> </ul>
<body> <body>
{% if redeems %} {% if redeems %}
<table> <table>