Guest User

Warbands 710 tracking v3

a guest
Jul 24th, 2015
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.59 KB | None | 0 0
  1. import time
  2. import re,string
  3. import httplib, urllib
  4.  
  5. print_debug = False
  6.  
  7. log_folder = r"J:\Steam\SteamApps\common\Path of Exile\logs"
  8. log_name   = r"Client.txt"
  9.  
  10. maps = {
  11.     68 : {"Crypt":None,"Desert":None,"Dunes":None,"Dungeon":None,"Grotto":None,"Pit":None,"Tropical Island":["Trop","Tropical","Island"]},
  12.     69 : {"Arcade":None,"Cemetery":None,"Channel":None,"Mountain Ledge":["MLedge","Ledge","Mountain"],"Sewer":None,"Thicket":None,"Wharf":None},
  13.     70 : {"Ghetto":None,"Mud Geyser":["Geyser"],"Museum":None,"Quarry":None,"Reef":None,"Spider Lair":["Spider","Lair"],"Vaal Pyramid":"Pyramid"},
  14.     71 : {"Arena":None,"Overgrown Shrine":["Overgrown"],"Promenade":None,"Shore":None,"Spider Forest":["Spider","Forest"],"Tunnel":None},
  15.     72 : {"Bog":None,"Coves":None,"Graveyard":None,"Pier":None,"Underground Sea":["Und","Underground","Udg","Sea"],"Villa":None},
  16.     73 : {"Arachnid Nest":["Arachnid","Arach","Nest"],"Catacomb":None,"Colonnade":None,"Dry Woods":["Dry","Woods"],"Temple":None,"Strand":None,"Poorjoy's Asylum":["Poorjoy","Poorjoys","Asylum"]},
  17.     74 : {"Jungle Valley":["Jungle","Valley"],"Labyrinth":None,"Mine":None,"Torture Chamber":["Torture","Chamber","Chambre"],"Waste Pool":["Waste","Pool"],"Oba's Cursed Trove":["Oba","Obas","Cursed","Curse","Trove"]},
  18.     75 : {"Canyon":None,"Cells":None,"Dark Forest":["Dark","Forest"],"Dry Peninsula":["Peninsula"],"Orchard":None},
  19.     76 : {"Arid Lake":["Arid","Lake"],"Gorge":None,"Residence":None,"Underground River":["Und","Underground","Udg","River"]},
  20.     77 : {"Bazaar":None,"Necropolis":None,"Plateau":None,"Volcano":None},
  21.     78 : {"Academy":None,"Crematorium":None,"Precinct":None,"Springs":None},
  22.     79 : {"Arsenal":None,"Overgrown Ruin":["Overgrown","Ruin"],"Shipyard":None,"Village Ruin":["Village","Ruin"]},
  23.     80 : {"Courtyard":None,"Excavation":None,"Wasteland":None,"Waterways":None},
  24.     81 : {"Maze":None,"Palace":None,"Shrine":None},
  25.     82 : {"Abyss":None,"Colosseum":None,"Core":None}
  26. }
  27.  
  28. status = {}
  29.  
  30. def prepare_status():
  31.     for level,maps_data in maps.iteritems():
  32.         for map_name in maps_data.iterkeys():
  33.             status[map_name] = {
  34.                 "level" : level,
  35.                 "reports" : [],
  36.                 "dots" : None,
  37.                 "band": None
  38.             }
  39.  
  40. needs_two_words = [
  41.     "Tropical Island","Spider Lair","Spider Forest","Underground Sea",
  42.     "Dry Woods","Waste Pool","Oba's Cursed Trove","Dark Forest",
  43.     "Arid Lake","Underground River","Overgrown Ruin","Village Ruin"
  44. ]
  45.  
  46. bands = {
  47.     "Brinerot" : ["Lookouts", "Pillagers", "Buccaneers", "Captured"],
  48.     "Redblade" : ["Scouts", "Raiders", "Fanatics", "Conquered"],
  49.     "Mutewind" : ["Trackers", "Hunters", "Purifiers", "Cleansed"]
  50. }
  51.  
  52. threshold_length_for_levenshtein1 = 5
  53. threshold_length_for_levenshtein2 = 8
  54.  
  55. def levenshtein(s1, s2):
  56.     if len(s1) < len(s2):
  57.         return levenshtein(s2, s1)
  58.     if len(s2) == 0:
  59.         return len(s1)
  60.     previous_row = range(len(s2) + 1)
  61.     for i, c1 in enumerate(s1):
  62.         current_row = [i + 1]
  63.         for j, c2 in enumerate(s2):
  64.             insertions = previous_row[j + 1] + 1
  65.             deletions = current_row[j] + 1
  66.             substitutions = previous_row[j] + (c1 != c2)
  67.             current_row.append(min(insertions, deletions, substitutions))
  68.         previous_row = current_row
  69.     return previous_row[-1]
  70.  
  71. def test_lv(s1, s2):
  72.     max_dist = 0
  73.     if min(len(s1),len(s2)) >= threshold_length_for_levenshtein1:
  74.         max_dist = 1
  75.     if min(len(s1),len(s2)) >= threshold_length_for_levenshtein2:
  76.         max_dist = 2
  77.     return (levenshtein(s1.lower(),s2.lower()) <= max_dist)
  78.  
  79. def debug(str):
  80.     if print_debug:
  81.         print(str)
  82.  
  83. def update_html():
  84.     with open("warbands.html","w") as f:
  85.         f.write("<html>\n<head>\n  <title>Warbands Info Script</title>\n</head>\n")
  86.         f.write("<body>\n")
  87.         f.write("  <table>\n")
  88.         f.write("    <tr><th>Map</th><th>Level</th><th>Band</th><th>Dots</th></tr>\n")
  89.         for level in range(68,83):
  90.             for map_name in maps[level].iterkeys():
  91.                 map_status = status[map_name]
  92.                 f.write("    <tr><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>\n" % (map_name,map_status["level"],map_status["band"] or "?",str(map_status["dots"]) or "?"))
  93.         f.write("  </table>\n")
  94.         f.write("</body>\n")
  95.         f.write("</html>\n")
  96.  
  97. def reset():
  98.     prepare_status()
  99.  
  100. def found(map_name,band_name,dots,username,timestamp,log_line):
  101.     if dots == 0:
  102.         print(
  103.             "=======> 0 dots in %s according to %s's message at t=%d" %
  104.             (map_name,username,timestamp)
  105.         )
  106.     else:
  107.         print(
  108.             "=======> %s has %d dots in %s according to %s's message at t=%d" %
  109.             (band_name or "A warband",dots,map_name,username,timestamp)
  110.         )
  111.     map_status = status[map_name]
  112.     map_status["dots"] = dots
  113.     map_status["band"] = band_name
  114.     map_status["last_update"] = timestamp
  115.     map_status["reports"].append([username,log_line])
  116.     update_html()
  117.  
  118.     params = urllib.urlencode({'mapname': map_name.replace(' ','_'), 'band': band_name, 'dots': dots})
  119.     headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
  120.     conn = httplib.HTTPConnection("nembibi.com")
  121.     conn.request("POST", "/warbands_test/report.php", params, headers)
  122.     response = conn.getresponse()
  123.     print("Response:", response.status, response.reason)
  124.     print(response.read())
  125.     conn.close()
  126.  
  127. # Handles one log line, returns true if it was considered to be a warbands report
  128. def handle_line(line):
  129.     data = line.split()
  130.     if len(data) < 9:
  131.         return
  132.     timestamp = int(data[2])
  133.     username  = data[7]
  134.     if username[0] != "#":
  135.         return
  136.     username = username[1:-1]
  137.     words = [re.sub(r"[^a-zA-Z0-9]", "", word).lower() for word in data[8:] if re.search(r"\w",word)]
  138.  
  139.     populations_found = []
  140.     dots_found = []
  141.     bands_found = []
  142.     maps_found = []
  143.  
  144.     # Detects "3 dots", "4-d", "2dot" etc
  145.     m = re.search(r"(?:\A|\W)(\d)[- ]?d(?:ots?)?(?:\Z|\W)",string.join(words))
  146.     if m:
  147.         dots = int(m.group(1))
  148.         #debug("%d dots detected by %s at t=%d!" % (dots, username, timestamp))
  149.         dots_found.append(int(dots))
  150.  
  151.     debug("===============================================================")
  152.     debug(string.join(words))
  153.     for word in words:
  154.         # Detects occurences of a map/population/band name
  155.         for band_name, populations in bands.iteritems():
  156.             if test_lv(word,band_name):
  157.                 #debug("%s detected by %s at t=%d!" % (band_name, username, timestamp))
  158.                 bands_found.append(band_name)
  159.             for population in populations:
  160.                 max_dist = 1
  161.                 if test_lv(word,population):
  162.                     #debug("(%s) %s detected by %s at t=%d!" % (band_name, population, username, timestamp))
  163.                     populations_found.append(population)
  164.         for level,maps_data in maps.iteritems():
  165.             for map_name,map_altnames in maps_data.iteritems():
  166.                 altnames = map_altnames
  167.                 if not altnames:
  168.                     altnames = [map_name]
  169.                 for altname in altnames:
  170.                     if test_lv(word,altname):
  171.                         #debug("%s detected by %s at t=%d!" % (map_name, username, timestamp))
  172.                         maps_found.append(map_name)
  173.                         break
  174.  
  175.         # Detects things like "B3" or "m4"
  176.         m = re.search(r"^([brm])([1234])$",word,flags=re.I)
  177.         if m:
  178.             just_to_be_sure = False
  179.             for band_name in bands.iterkeys():
  180.                 if m.group(1).lower() == band_name[0].lower():
  181.                     bands_found.append(band_name)
  182.                     just_to_be_sure = True
  183.             if just_to_be_sure == False:
  184.                 print("==========================\nSomething weird happened\n==========================")
  185.             dots_found.append(int(m.group(2)))
  186.  
  187.         # Detects the word "empty" / "clear" / etc
  188.         if word in ["empty","clear","nothing"]:
  189.             dots_found.append(0)
  190.        
  191.     # If nothing was detected, return
  192.     if max(len(maps_found), len(populations_found), len(bands_found), len(dots_found)) == 0:
  193.         return
  194.        
  195.     for map_name in maps_found:
  196.         if map_name in needs_two_words:
  197.             if maps_found.count(map_name) < 2:
  198.                 maps_found = [m for m in maps_found if m != map_name]
  199.    
  200.     # Remove duplicates in the lists of occurrences of everything
  201.     populations_found = list(set(populations_found))
  202.     dots_found        = list(set(dots_found))
  203.     bands_found       = list(set(bands_found))
  204.     maps_found        = list(set(maps_found))
  205.  
  206.     # Exception for map names that are a substring of another map's name
  207.     if ("Shrine" in maps_found) and ("Overgrown Shrine" in maps_found):
  208.         maps_found.remove("Shrine")
  209.    
  210.     # If there is a contradiction, return
  211.     if max(len(maps_found), len(populations_found), len(bands_found), len(dots_found)) >= 2:
  212.         debug("Ignored report: Detected too many of at least one of: maps, pops, bands or dots")
  213.         return
  214.  
  215.     # Check if we have sufficient information and, if so, process the report
  216.     if len(maps_found) == 1:
  217.         m = maps_found[0]
  218.         if len(populations_found) == 1:
  219.             p = populations_found[0]
  220.             if len(bands_found) == 1:
  221.                 b = bands_found[0]
  222.                 if p not in bands[b]:
  223.                     debug("Ignored report: Population didn't match band")
  224.                     return
  225.                 d = bands[b].index(p)+1
  226.                 found(m,b,d,username,timestamp,line)
  227.                 return True
  228.             for b,populations in bands.iteritems():
  229.                 if p in populations:
  230.                     d = bands[b].index(p)+1
  231.                     found(m,b,d,username,timestamp,line)
  232.                     return True
  233.         if len(dots_found) == 1:
  234.             d = dots_found[0]
  235.             if d == 0:
  236.                 found(m,None,d,username,timestamp,line)
  237.                 return True
  238.             if len(bands_found) == 1:
  239.                 b = bands_found[0]
  240.                 found(m,b,d,username,timestamp,line)
  241.                 return True
  242.             found(m,None,d,username,timestamp,line)
  243.             return True
  244.     debug("Ignored report: Not denough data")
  245.  
  246. minutes_reset = 3
  247. def main():
  248.     prepare_status()
  249.     update_html()
  250.     prev_minutes = int(time.time()/60) % 30
  251.     with open(r"%s\%s" % (log_folder, log_name)) as f:
  252.         f.seek(0,2)
  253.         while True:
  254.             minutes = int(time.time()/60) % 30
  255.             if minutes == minutes_reset and prev_minutes != minutes:
  256.                 reset()
  257.             prev_minutes = minutes
  258.             where = f.tell()
  259.             line = f.readline()
  260.             if not line:
  261.                 time.sleep(1)
  262.                 f.seek(where)
  263.             else:
  264.                 if handle_line(line):
  265.                     update_html()
  266.  
  267.  
  268. main()
Advertisement
Add Comment
Please, Sign In to add comment