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): ...@@ -112,6 +112,29 @@ def db_add_minigame(member_id, minigame_name, state):
112 WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name)) 112 WHERE member_id = ? AND minigame_name = ?;""", (state, member_id, minigame_name))
113 conn.commit() 113 conn.commit()
114 114
115 def db_get_pings():
116 conn = sqlite3.connect('db.sqlite3')
117 c = conn.cursor()
118 result = c.execute("SELECT * FROM pings;").fetchall()
119 conn.close()
120 results = []
121 for row in result:
122 results.append(dict_factory(c, row))
123
124 return results
125
126 def db_update_ping(ping_id, last_ping):
127 conn = sqlite3.connect('db.sqlite3')
128 c = conn.cursor()
129 if not last_ping:
130 outage = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
131 c.execute("UPDATE pings SET last_ping = ?, last_outage = ? WHERE ping_id = ?;", (last_ping, outage, ping_id))
132 conn.commit()
133 else:
134 c.execute("UPDATE pings SET last_ping = ?, average_ping = (average_ping + ?) / 2 WHERE ping_id = ?;", (last_ping, last_ping, ping_id))
135 conn.commit()
136 conn.close()
137
115 def db_get_minigame_state(member_id, minigame_name): 138 def db_get_minigame_state(member_id, minigame_name):
116 conn = sqlite3.connect('db.sqlite3') 139 conn = sqlite3.connect('db.sqlite3')
117 c = conn.cursor() 140 c = conn.cursor()
......
No preview for this file type
...@@ -27,8 +27,12 @@ import wolframalpha ...@@ -27,8 +27,12 @@ import wolframalpha
27 import sqlite3 27 import sqlite3
28 from blackjack import Blackjack 28 from blackjack import Blackjack
29 import data 29 import data
30 import pyping
30 from pankration import Pankration, HuntResponse, Jobs, Action, MagicResistAction, MissAction, AttackAction, DefeatAction, TemperamentPosture, TemperamentAttitude 31 from pankration import Pankration, HuntResponse, Jobs, Action, MagicResistAction, MissAction, AttackAction, DefeatAction, TemperamentPosture, TemperamentAttitude
31 32
33 import subprocess
34
35
32 VERSION = 2.3 36 VERSION = 2.3
33 37
34 quitting = False 38 quitting = False
...@@ -178,6 +182,35 @@ def search_google_images(query, animated=False): ...@@ -178,6 +182,35 @@ def search_google_images(query, animated=False):
178 return "boo you fail.." 182 return "boo you fail.."
179 183
180 184
185 def ping(hostname, timeout):
186 ping_response = subprocess.Popen(["/bin/ping", "-c1", "-w100", hostname], stdout=subprocess.PIPE).stdout.read()
187 log(ping_response)
188 matches = re.match('.*time=([0-9]+) ms.*', ping_response, re.DOTALL)
189 if matches:
190 return matches.group(1)
191 else:
192 return False
193
194
195 def check_pings():
196 ping_list = data.db_get_pings()
197 new_ping = ping(ping_list[0].get('ip_address'), 1000)
198 if new_ping:
199 log("New Ping: {}".format(new_ping))
200 data.db_update_ping(ping_list[0].get('ping_id'), new_ping)
201 for channel in client.get_all_channels():
202 if channel.id == '193028170184785920': # Reflex channel
203 send_message(client, channel, "{} - {}ms Average: {}ms".format(ping_list[0].get('ip_address'), new_ping, ping_list[0].get('average_ping')))
204 break
205 else:
206 for member in client.get_all_members():
207 if member.id == '122079633796497409':
208 send_message(client, member, "Outage! {} - {}ms Average: {}ms".format(ping_list[0].get('ip_address'), new_ping, ping_list[0].get('average_ping')))
209 break
210
211 data.db_update_ping(ping_list[0].get('ping_id'), False)
212
213
181 def check_msg_queue(client): 214 def check_msg_queue(client):
182 messages = data.db_get_messages() 215 messages = data.db_get_messages()
183 if messages: 216 if messages:
...@@ -1391,7 +1424,11 @@ def check_arena(): ...@@ -1391,7 +1424,11 @@ def check_arena():
1391 ### END PANKRATION 1424 ### END PANKRATION
1392 def start_timer(client): 1425 def start_timer(client):
1393 needs_loot = True 1426 needs_loot = True
1427 time_to_ping = True
1394 while not quitting: 1428 while not quitting:
1429 if time_to_ping:
1430 check_pings()
1431 time_to_ping = not time_to_ping
1395 if not battle_in_progress: 1432 if not battle_in_progress:
1396 thread.start_new_thread(check_arena, ()) 1433 thread.start_new_thread(check_arena, ())
1397 1434
......