Explorar el Código

store nodes+clients highscores (new command !highscore)

Helge Jung hace 9 años
padre
commit
5dd0b2fc00
Se han modificado 1 ficheros con 79 adiciones y 2 borrados
  1. 79 2
      modules/ffpb.py

+ 79 - 2
modules/ffpb.py

@@ -10,7 +10,9 @@ import json
 import urllib2
 import re
 import os
+import shelve
 import subprocess
+import time
 
 import dns.resolver,dns.reversename
 import socket
@@ -20,6 +22,7 @@ import threading
 msgserver = None
 peers_repo = None
 stats = None
+highscores = None
 
 alfred_method = None
 alfred_data = None
@@ -64,7 +67,15 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
 	pass
 
 def setup(bot):
-	global msgserver, peers_repo, alfred_method
+	global msgserver, peers_repo, alfred_method, highscores
+
+	highscores = shelve.open('highscoredata', writeback=True)
+	if not 'nodes' in highscores:
+		highscores['nodes'] = 0
+		highscores['nodes_ts'] = time.time()
+	if not 'clients' in highscores:
+		highscores['clients'] = 0
+		highscores['clients_ts'] = time.time()
 
 	if not bot.config.has_section('ffpb'):
 		return
@@ -92,7 +103,12 @@ def setup(bot):
 	ffpb_updatealfred(bot)
 
 def shutdown(bot):
-	global msgserver
+	global msgserver, highscores
+
+	if not highscores is None:
+		highscores.sync()
+		highscores.close()
+		highscores = None
 
 	if not msgserver is None:
 		msgserver.shutdown()
@@ -394,6 +410,20 @@ def ffpb_get_stats(bot):
 	stats["nodes_total"] = nodes_total
 	stats["clients"] = clients_count
 
+	highscore_changed = False
+	if nodes_active > highscores['nodes']:
+		highscores['nodes'] = nodes_active
+		highscores['nodes_ts'] = time.time()
+		highscore_changed = True
+
+	if clients_count > highscores['clients']:
+		highscores['clients'] = clients_count
+		highscores['clients_ts'] = time.time()
+		highscore_changed = True
+
+	if highscore_changed:
+		print('HIGHSCORE changed: {0} nodes ({1}), {2} clients ({3})'.format(highscores['nodes'], highscores['nodes_ts'], highscores['clients'], highscores['clients_ts']))
+
 @willie.module.commands('status')
 def ffpb_status(bot, trigger):
 	"""Status des FFPB-Netzes: Anzahl (aktiver) Knoten + Clients"""
@@ -403,6 +433,53 @@ def ffpb_status(bot, trigger):
 
 	bot.say('Es sind {0} Knoten und ca. {1} Clients online.'.format(stats["nodes_active"], stats["clients"]))
 
+def pretty_date(time=False):
+    """
+    Get a datetime object or a int() Epoch timestamp and return a
+    pretty string like 'an hour ago', 'Yesterday', '3 months ago',
+    'just now', etc
+    """
+    from datetime import datetime
+    now = datetime.now()
+    compare = None
+    if type(time) is int:
+        compare = datetime.fromtimestamp(time)
+    elif isinstance(time,datetime):
+        compare = time
+    elif not time:
+        compare = now
+    diff = now - compare
+    second_diff = diff.seconds
+    day_diff = diff.days
+
+    if day_diff < 0:
+        return ''
+
+    if day_diff == 0:
+        if second_diff < 10:
+            return "gerade eben"
+        if second_diff < 60:
+            return "vor " + str(second_diff) + " Sekunden"
+        if second_diff < 120:
+            return "vor einer Minute"
+        if second_diff < 3600:
+            return "vor " + str(second_diff / 60) + " Minuten"
+        if second_diff < 7200:
+            return "vor einer Stunde"
+        if second_diff < 86400:
+            return "vor " + str(second_diff / 3600) + " Stunden"
+    if day_diff == 1:
+        return "gestern"
+    if day_diff < 7:
+        return "vor " + str(day_diff) + " Tagen"
+    return "am " + compare.strftime('%d.%m.%Y um %H:%M Uhr') 
+
+@willie.module.commands('highscore')
+def ffpb_highscore(bot, trigger):
+	bot.say('Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(
+		highscores['nodes'], pretty_date(int(highscores['nodes_ts'])),
+		highscores['clients'], pretty_date(int(highscores['clients_ts']))))
+
 @willie.module.commands('rollout-status')
 def ffpb_rolloutstatus(bot, trigger):
 	result = { }