import sys, pickle, time from operator import itemgetter class Container: def __init__(self): self.total_connections = 0 self.country_count = {} self.last_10 = [] self.messages = [] self.nr_active = 0 self.nr_waiting = 0 def set_nr_active(self, nr_active): self.nr_active = nr_active def set_nr_waiting(self, nr_waiting): self.nr_waiting = nr_waiting def copy_from_other(self, other): try: self.nr_active = 0 # Always reset self.nr_waiting = 0 self.total_connections = other.total_connections self.country_count = other.country_count self.last_10 = other.last_10 self.messages = other.messages except: pass def add_country(self, country): try: cur = self.country_count[country] except KeyError, e: cur = 0 self.country_count[country] = cur + 1 def set_messages(self, messages): self.messages = messages def add_connection(self, who, country): time_now = time.strftime("%Y-%m-%d %H:%M", time.gmtime()) s = "%s - %s (%s)" % (time_now, who, country) self.total_connections = self.total_connections + 1 self.last_10 = [s] + self.last_10[:9] self.add_country(country) class HtmlGenerator: def __init__(self, container): self.container = container def generate(self, outf): sorted_countries = sorted(self.container.country_count.items(), reverse=True, key=itemgetter(1)) outf.write("\n") outf.write("

Frodo-Wii network statistics

\n") outf.write("The total number of connections is %d

\n" % (self.container.total_connections)) outf.write("There are currently %d players waiting for connections and %d players playing

\n" % (self.container.nr_waiting, self.container.nr_active)) outf.write("\n") outf.write("\n" % (len(self.container.last_10)) ) cnt = 0 for item in self.container.last_10: images = "" if cnt % 4 == 0: cur = cnt images = "" % (cur, cur + 1, cur + 2, cur + 3) outf.write("%s\n" % (item, images) ) cnt = cnt + 1 outf.write("") outf.write("

Last %d connections

 

Random connection screenshots

%s 
 
\n") outf.write("

\n") outf.write("\n") count = 1 outf.write("

Last server messages

\n") outf.write("
") n_countries = len(sorted_countries) for i in range(0, n_countries / 3): c1, n1 = sorted_countries[i] c2, n2 = sorted_countries[i + n_countries / 3] c3, n3 = sorted_countries[i + (n_countries / 3) * 2] outf.write("\n" % (count, c1, n1, count + n_countries / 3, c2, n2, count + (n_countries / 3) * 2, c3, n3) ) count = count + 1 # Output the last if it's odd for i in range(0, n_countries % 3): c1, n1 = sorted_countries[n_countries - i - 1] outf.write("\n" % (n_countries - n_countries % 3 + i + 1, c1, n1) ) outf.write("

List of countries

%3d %s (%d)   %3d %s (%d)   %3d %s (%d)
%3d %s (%d)
\n") outf.write("\n") g_stat = None def save(filename): global g_stat of = open(filename, "wb") pickle.dump(g_stat, of) of.close() def generate_html(filename): of = open(filename, "wb") hg = HtmlGenerator(g_stat) hg.generate(of) of.close() def load(filename): global g_stat g_stat = Container() try: of = open(filename, "r") other = pickle.load(of) g_stat.copy_from_other(other) of.close() except: pass def update_peer_nr(waiting, active): g_stat.set_nr_waiting(waiting) g_stat.set_nr_active(active) def add_connection(who, country): g_stat.add_connection(who, country) def set_messages(messages): g_stat.set_messages(messages) if __name__ == "__main__": load("/tmp/vobb") for i in range(0, 10): add_connection("MABOO", "Unknown country") add_connection("SIMONK", "Sweden") add_connection("SIMONK", "Sweden") add_connection("SIMONK", "Sweden") add_connection("Linda", "Germany") add_connection("Linda", "Germany") set_messages(["Hej", "Who framed Roger Rabbit?", "IK+ at 19.00 CET?"]) save("/tmp/vobb") hg = HtmlGenerator(g_stat) hg.generate(sys.stdout)