aeb9e40a by Barry

Added new ping functionality to track a reflex server.

1 parent 8d971f89
......@@ -112,6 +112,29 @@ def db_add_minigame(member_id, minigame_name, state):
WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name))
conn.commit()
def db_get_pings():
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
result = c.execute("SELECT * FROM pings;").fetchall()
conn.close()
results = []
for row in result:
results.append(dict_factory(c, row))
return results
def db_update_ping(ping_id, last_ping):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
if not last_ping:
outage = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
c.execute("UPDATE pings SET last_ping = ?, last_outage = ? WHERE ping_id = ?;", (last_ping, outage, ping_id))
conn.commit()
else:
c.execute("UPDATE pings SET last_ping = ?, average_ping = (average_ping + ?) / 2 WHERE ping_id = ?;", (last_ping, last_ping, ping_id))
conn.commit()
conn.close()
def db_get_minigame_state(member_id, minigame_name):
conn = sqlite3.connect('db.sqlite3')
c = conn.cursor()
......
No preview for this file type
......@@ -27,8 +27,12 @@ import wolframalpha
import sqlite3
from blackjack import Blackjack
import data
import pyping
from pankration import Pankration, HuntResponse, Jobs, Action, MagicResistAction, MissAction, AttackAction, DefeatAction, TemperamentPosture, TemperamentAttitude
import subprocess
VERSION = 2.3
quitting = False
......@@ -178,6 +182,35 @@ def search_google_images(query, animated=False):
return "boo you fail.."
def ping(hostname, timeout):
ping_response = subprocess.Popen(["/bin/ping", "-c1", "-w100", hostname], stdout=subprocess.PIPE).stdout.read()
log(ping_response)
matches = re.match('.*time=([0-9]+) ms.*', ping_response, re.DOTALL)
if matches:
return matches.group(1)
else:
return False
def check_pings():
ping_list = data.db_get_pings()
new_ping = ping(ping_list[0].get('ip_address'), 1000)
if new_ping:
log("New Ping: {}".format(new_ping))
data.db_update_ping(ping_list[0].get('ping_id'), new_ping)
for channel in client.get_all_channels():
if channel.id == '193028170184785920': # Reflex channel
send_message(client, channel, "{} - {}ms Average: {}ms".format(ping_list[0].get('ip_address'), new_ping, ping_list[0].get('average_ping')))
break
else:
for member in client.get_all_members():
if member.id == '122079633796497409':
send_message(client, member, "Outage! {} - {}ms Average: {}ms".format(ping_list[0].get('ip_address'), new_ping, ping_list[0].get('average_ping')))
break
data.db_update_ping(ping_list[0].get('ping_id'), False)
def check_msg_queue(client):
messages = data.db_get_messages()
if messages:
......@@ -1391,7 +1424,11 @@ def check_arena():
### END PANKRATION
def start_timer(client):
needs_loot = True
time_to_ping = True
while not quitting:
if time_to_ping:
check_pings()
time_to_ping = not time_to_ping
if not battle_in_progress:
thread.start_new_thread(check_arena, ())
......