""" SavedGames.py (C)2008 Don A. Hanlen I make no claims, and take no reponsibility, for use or abuse of this code! I require those who DO use it to cite its source! If this code is used for commercial purposes I expect a (negotiable) cut! This file is an adjunct for rePYbot written for Python 1.5.2 (OWT's v.) It handles saved games information from http://www.fibs.com/savedgames/list.html """ import urllib import string import rbos webSite = "http://www.fibs.com/savedgames/list.html" _hashHinit = 97 True = 1==1 False = not True class SavedGames: """ handle information from the list of FIBS.com saved games savedDict = {} ## player saved games dictionary ## entry = ['name': ['nAME', 'saved1', 'saved2' ....]] ## key is lower case, ## data is preserved case ## (including first entry which preserves key) """ def __init__(self): """ reading from FIBS.com may take a while """ self.savedDict = {} self.hashH = 0 ## a hash value for savedDict self.getsavedDict() def dLen(self, dKey): """ return number of saved games for dKey | none """ if self.savedDict.has_key(string.lower(dKey)): return str(len(self.savedDict[string.lower(dKey)])-1) return 'none' def dList(self, dKey): """ return list of saved games for dKey including dKey's case-correct first name | [dKey, "was not found in the saved games list."] """ if self.savedDict.has_key(string.lower(dKey)): return self.savedDict[string.lower(dKey)] return [dKey, "was not found in the saved games list."] def getsavedDict(self): """ Load saved games from fibs.com, return 'updated' 'AND' with most recent (if any) version of same same (Patti says update is like, 2:45 AM, Pacific.) """ h = urllib.urlopen(webSite) t = h.read() h.close() t = string.split(t, '\n') self.savedDict.clear() dictList = [] self.hashH = _hashHinit for x in t[2:len(t)-1]: entry = [] for y in string.split(x): current = self.deHTMLize(y) if len(current) > 0: entry.append(current) if len(entry) > 1: dictList.append(entry) lastList = rbos.getsavedList(dictList, str(self.hashH)) remV = 0 ## remK = 0 ## lkeys = [] for x in lastList: lkeys.append(x[0]) for x in dictList: if x[0] in lkeys: lastL = lastList[lkeys.index(x[0])] for y in x[1:]: if not(y in lastL[1:]): remV = remV + 1 ## x.remove(y) if len(x)==1: remK = remK + 1 ## else: self.savedDict[string.lower(x[0])] = x else: remK = remK + 1 ## return "updated(%d,%d)" %(remV, remK) def deHTMLize(self, word): """ returns deHTMLized word (The name says it all...) """ result = '' lBrace = False for c in word: self.hashH = (self.hashH+ord(c))*997 %99929 ## sleezy hash function if lBrace: if c == '>': lBrace = not(lBrace) elif c == '<': lBrace = not(lBrace) else: result = result + c if result == '<kdc>': ## 's very own two line of code! result = '' return result