mirror of
https://github.com/FAUSheppy/tmnf-replay-server.git
synced 2025-12-06 07:01:37 +01:00
wip:
This commit is contained in:
21
notifications.py
Normal file
21
notifications.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import sys
|
||||
import requests
|
||||
|
||||
def send_notification(app, target_user, mapname, old_replay, new_replay):
|
||||
'''Build notification and handoff to dispatcher'''
|
||||
|
||||
url = app.config["DISPATCH_SERVER"]
|
||||
|
||||
# send to event dispatcher #
|
||||
message = "Trackmania: Record broken on {}".format(mapname)
|
||||
message += "Old time: {}".format(old_replay.get_human_readable_time())
|
||||
message += "New time: {}".format(new_replay.get_human_readable_time())
|
||||
message += "by {}".format(new_replay.clean_login())
|
||||
|
||||
payload = { "users": [user], "msg" : message }
|
||||
|
||||
r = requests.post(app.config["DISPATCH_SERVER"] + "/smart-send",
|
||||
json=payload, auth=app.config["DISPATCH_AUTH"])
|
||||
|
||||
if not r.ok:
|
||||
print("Error handing off notification to dispatch ({})".format(r.status_code), file=sys.stderr)
|
||||
24
server.py
24
server.py
@@ -81,6 +81,8 @@ class UserSettings(db.Model):
|
||||
|
||||
__tablename__ = "user_settings"
|
||||
|
||||
user = Column(String, primary_key=True)
|
||||
|
||||
show_tm_2020 = Column(Boolean)
|
||||
show_tmnf = Column(Boolean)
|
||||
show_tm_2020_current = Column(Boolean)
|
||||
@@ -114,7 +116,7 @@ class ParsedReplay(db.Model):
|
||||
else:
|
||||
return self.login
|
||||
|
||||
def get_human_readable_time(self):
|
||||
def get_human_readable_time(self, thousands=False):
|
||||
t = datetime.timedelta(microseconds=self.race_time*1000)
|
||||
t_string = str(t)
|
||||
if t.seconds < 60*60:
|
||||
@@ -419,11 +421,21 @@ def upload():
|
||||
|
||||
def check_replay_trigger(replay):
|
||||
|
||||
# get replay rank
|
||||
# get second best
|
||||
# uploader = second best owner
|
||||
# check notifications on
|
||||
# request to dispatch
|
||||
map_obj = db.session.get(Map).filter(Map.map_uid == replay.map_uid).first()
|
||||
assert(map_uid)
|
||||
|
||||
best = map_obj.get_best_replay()
|
||||
second = map_obj.get_second_best_replay()
|
||||
|
||||
if replay.filehash != best.filehash:
|
||||
return
|
||||
|
||||
if second.uploader == replay.uploader:
|
||||
return
|
||||
|
||||
settings = db.session.query(UserSettings).filter(UserSettings.user == second.uploader).first()
|
||||
if settings and settings.notifcations_self:
|
||||
notifications.send_notification(app, settings.user, map_obj.map_uid, second, replay)
|
||||
|
||||
def create_app():
|
||||
db.create_all()
|
||||
|
||||
Reference in New Issue
Block a user