mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
servermaps: implement network maps over relay!
Local-only map is available too using the 'localmap' command.
This commit is contained in:
parent
68c247f764
commit
595bceda2e
@ -9,7 +9,7 @@ import collections
|
||||
def _percent(num, total):
|
||||
return '%.1f' % (num/total*100)
|
||||
|
||||
def _map(irc, source, args):
|
||||
def _map(irc, source, args, show_relay=True):
|
||||
"""[<network>]
|
||||
|
||||
Shows the network map for the given network, or the current network if not specified."""
|
||||
@ -31,23 +31,28 @@ def _map(irc, source, args):
|
||||
hostsid = ircobj.sid
|
||||
usercount = len(ircobj.users)
|
||||
|
||||
# Iterate over every connected server.
|
||||
serverlist = ircobj.servers.copy()
|
||||
for sid, serverobj in serverlist.items():
|
||||
# Save the server as UNDER its uplink.
|
||||
if sid == hostsid:
|
||||
continue
|
||||
servers[(netname, serverobj.uplink or ircobj.sid)].add(sid)
|
||||
# Iterate over every connected server on every network.
|
||||
for remotenet, remoteirc in world.networkobjects.items():
|
||||
for sid, serverobj in remoteirc.servers.copy().items():
|
||||
if sid == remoteirc.sid: # Don't re-add our own SID to the index
|
||||
continue
|
||||
|
||||
# Save the server as UNDER its uplink.
|
||||
servers[(remotenet, serverobj.uplink or remoteirc.sid)].add(sid)
|
||||
|
||||
log.debug('(%s) servermaps.map servers fetched for %s: %s', irc.name, netname, servers)
|
||||
|
||||
reply = lambda text: irc.reply(text, private=True)
|
||||
|
||||
def showall(sid, hops=0):
|
||||
def showall(ircobj, sid, hops=0, is_relay_server=False):
|
||||
log.debug('servermaps: got showall() for SID %s on network %s', sid, ircobj.name)
|
||||
serverlist = ircobj.servers.copy()
|
||||
netname = ircobj.name
|
||||
|
||||
if hops == 0:
|
||||
# Show our root server once.
|
||||
rootusers = len(serverlist[sid].users)
|
||||
reply('%s[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid,
|
||||
reply('\x02%s\x02[%s]: %s user(s) (%s%%)' % (serverlist[sid].name, sid,
|
||||
rootusers, _percent(rootusers, usercount)))
|
||||
|
||||
log.debug('(%s) servermaps: servers under sid %s: %s', irc.name, sid, servers)
|
||||
@ -56,22 +61,39 @@ def _map(irc, source, args):
|
||||
hops += 1
|
||||
leaves = servers[(netname, sid)]
|
||||
for leafcount, leaf in enumerate(leaves):
|
||||
if is_relay_server and hasattr(serverlist[leaf], 'remote'):
|
||||
# Don't show relay subservers more than once.
|
||||
continue
|
||||
|
||||
# If we reach the end of a server's map, display `- instead of |- for prettier output.
|
||||
linechar = '`-' if leafcount == len(leaves)-1 else '|-'
|
||||
|
||||
serverusers = len(serverlist[leaf].users)
|
||||
reply("%s%s%s[%s]: %s user(s) (%s%%)" % (' '*hops, linechar, serverlist[leaf].name, leaf,
|
||||
serverusers, _percent(serverusers, usercount)))
|
||||
showall(leaf, hops)
|
||||
if is_relay_server:
|
||||
# Skip showing user data for relay servers.
|
||||
reply("%s%s\x02%s\x02[%s] (via PyLink Relay)" % (' '*hops, linechar, serverlist[leaf].name, leaf))
|
||||
else:
|
||||
reply("%s%s\x02%s\x02[%s]: %s user(s) (%s%%)" % (' '*hops, linechar, serverlist[leaf].name, leaf,
|
||||
serverusers, _percent(serverusers, usercount)))
|
||||
showall(ircobj, leaf, hops, is_relay_server=is_relay_server)
|
||||
|
||||
if (not is_relay_server) and hasattr(serverlist[leaf], 'remote') and show_relay:
|
||||
# This is a relay server - display the remote map of the network it represents
|
||||
relay_server = serverlist[leaf].remote
|
||||
remoteirc = world.networkobjects[relay_server]
|
||||
# Only ever show relay subservers once - this prevents infinite loops.
|
||||
showall(remoteirc, remoteirc.sid, hops=hops, is_relay_server=True)
|
||||
|
||||
else:
|
||||
# Afterwards, decrement the hopcount.
|
||||
hops -= 1
|
||||
|
||||
# Start the map at our PyLink server
|
||||
firstserver = hostsid
|
||||
showall(firstserver)
|
||||
reply('Total %s users on %s servers - average of %1.f per server' % (usercount, len(serverlist),
|
||||
showall(ircobj, firstserver)
|
||||
serverlist = irc.servers
|
||||
reply('Total %s users on %s local servers - average of %1.f per server' % (usercount, len(serverlist),
|
||||
usercount/len(serverlist)))
|
||||
|
||||
utils.add_cmd(_map, 'map')
|
||||
utils.add_cmd(lambda irc, source, args: _map(irc, source, args, show_relay=False), 'localmap')
|
||||
|
Loading…
Reference in New Issue
Block a user