# # Statistics.py # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # Copyright # Author: Salvatore S. Gionfriddo # Contributors: Nicholas F. Hoover # Created: 2007.07.19 # Last Modified: 2007.11.04 # import Settings class Statistics: NUM_FLAGS = 0 def __init__(self): self.statistics = {} def reset(self): for s in self.statistics: self.statistics[s] = 0 def increment(self, flags, amount = 1): flags.sort() flags = tuple(flags) #if 0 in flags: print flags if flags in self.statistics: self.statistics[flags] += amount else: self.statistics.update([(flags, amount)]) def view(self, flags): result = 0 #print flags for key in self.statistics: match = True for flag in flags: if flag not in key: match = False break if match: result += self.statistics[key] return result def viewFunction(self, flags): return lambda: self.view(flags) def addFlags(self, names): for name in names: self.addFlag(name) def addFlag(self, name): Statistics.__dict__.update([(name, Statistics.NUM_FLAGS)]) Statistics.NUM_FLAGS += 1 def getFlag(flag): return Statistics.__dict__[flag.upper()] class EG_Stats(Statistics): def __init__(self): Statistics.__init__(self) levels = ["LEVEL_0"] flags = Settings.ACTIONS.keys() + Settings.SUBACTIONS + Settings.ENEMIES.keys() self.powerUpOffset = len(flags) flags += Settings.POWERUPS + Settings.OTHER + levels flags = map(str.upper, flags) self.addFlags(flags) self.current_level = 0 def addLevel(self): self.current_level += 1 self.addFlag("LEVEL_" + str(self.current_level)) #special flags def levelFlag(self, number): return Statistics.__dict__["LEVEL_"+str(number)] def powerUpFlag(self, pu_type): return pu_type + self.powerUpOffset def enemyFlag(self, enemy): return Statistics.__dict__[enemy.upper()] def actionFlag(self, action): return Statistics.__dict__[action.upper()] def viewByLevelFunction(self, flags, get_level): def internal(fl): fl = fl[:] fl.append(self.levelFlag(get_level())) return self.view(fl) return lambda: internal(flags)