From b2da2ffe0b74dda78a825c910fca5f3620b9b6e7 Mon Sep 17 00:00:00 2001 From: Simon Schampijer Date: Sat, 14 Jul 2007 11:28:35 +0000 Subject: - added new model - the config files for games have changed to use xml added reading and writing of the config files - adopted the ui to use the model - adopted the dbus methods to send the new grid when a game changes - fixed some buddy left issues - cleaned the misc folder --- (limited to 'model.py') diff --git a/model.py b/model.py index b795cb3..7f0b1d0 100644 --- a/model.py +++ b/model.py @@ -23,27 +23,25 @@ import logging import random import gobject -IMAGES_PATH = 'games/drumgit/images' -SOUNDS_PATH = 'games/drumgit/sounds' -GAME_PATH = '' - _logger = logging.getLogger('model') - class Pair(gobject.GObject): __gproperties__ = { 'aimg' : (str, None, None, None, gobject.PARAM_READWRITE), 'asnd' : (str, None, None, None, gobject.PARAM_READWRITE), 'achar' : (str, None, None, None, gobject.PARAM_READWRITE), + 'acharalign' : (str, None, None, None, gobject.PARAM_READWRITE), 'bimg' : (str, None, None, None, gobject.PARAM_READWRITE), 'bsnd' : (str, None, None, None, gobject.PARAM_READWRITE), 'bchar' : (str, None, None, None, gobject.PARAM_READWRITE), + 'bcharalign': (str, None, None, None, gobject.PARAM_READWRITE), 'color': (gobject.TYPE_INT, 'Base', 'Base', 0, 10, 0, gobject.PARAM_READWRITE) } def __init__(self): gobject.GObject.__init__(self) - self._properties = {'aimg':None, 'asnd':None, 'bimg':None, 'bsnd':None, 'color':100} + self._properties = {'aimg':None, 'asnd':None, 'achar':None, 'acharalign':'1', 'bimg':None, + 'bsnd':None, 'bchar':None, 'bcharalign':1, 'color':100} def do_get_property(self, pspec): """Retrieve a particular property from our property dictionary @@ -54,12 +52,16 @@ class Pair(gobject.GObject): return self._properties["asnd"] elif pspec.name == "achar": return self._properties["achar"] + elif pspec.name == "acharalign": + return self._properties["acharalign"] elif pspec.name == "bimg": return self._properties["bimg"] elif pspec.name == "bsnd": return self._properties["bsnd"] elif pspec.name == "bchar": return self._properties["bchar"] + elif pspec.name == "bcharalign": + return self._properties["bcharalign"] elif pspec.name == "color": return self._properties["color"] @@ -70,30 +72,32 @@ class Pair(gobject.GObject): self._properties["asnd"] = value elif name == "achar": self._properties["achar"] = value + elif name == "acharalign": + self._properties["acharalign"] = int(value) elif name == "bimg": self._properties["bimg"] = value elif name == "bsnd": self._properties["bsnd"] = value elif name == "bchar": self._properties["bchar"] = value + elif name == "bcharalign": + self._properties["bcharalign"] = value elif name == "color": - self._properties["color"] = int(value) - ''' - def do_set_property(self, props, value): - if props.name == 'a_img': - self._properties['a_img'] = value - ''' + self._properties["color"] = value + class Model(object): ''' The model of the activity. Contains methods to read and save the configuration for a game from xml. Stores the pairs and grid information. - ''' - def __init__(self, gamepath, dtdpath, name='noname'): + ''' + _GAMES_PATH = os.path.join(os.path.dirname(__file__), 'games') + + def __init__(self, dtdpath): self.data = {} - self.gamepath = gamepath self.dtdpath = dtdpath - + self.data['face'] = '' + try: self.dtd = libxml2.parseDTD(None, os.path.join(self.dtdpath, 'memorize.dtd')) except libxml2.parserError, e: @@ -112,10 +116,15 @@ class Model(object): self.started = 0 self.count = 0 - def read(self, filename): + def read(self, gamename): ''' reades the configuration from an xml file ''' + self.data['path'] = os.path.join( self._GAMES_PATH, gamename) + self.data['pathimg'] = os.path.join(self.data['path'], 'images') + self.data['pathsnd'] = os.path.join(self.data['path'], 'sounds') + self.pairs = {} + try: - doc = libxml2.parseFile(os.path.join(self.gamepath, filename)) + doc = libxml2.parseFile(os.path.join(self.data['path'], gamename+'.mem')) if doc.validateDtd(self.ctxt, self.dtd): # get the requested nodes @@ -133,41 +142,41 @@ class Model(object): pass else: pair.set_property(attribute.name, attribute.content) - self.pairs[self.idpair] = pair + self.pairs[str(self.idpair)] = pair self.idpair+=1 elif( elem.name == 'memorize' ): for attribute in attributes: if(attribute.name == 'text'): pass elif(attribute.name == 'name'): - self.data['game_name'] = attribute.content + self.data['name'] = attribute.content elif(attribute.name == 'scoresnd'): self.data['scoresnd'] = attribute.content elif(attribute.name == 'winsnd'): self.data['winsnd'] = attribute.content elif(attribute.name == 'divided'): self.data['divided'] = attribute.content - elif(attribute.name == 'divided'): - self.data['divided'] = attribute.content elif(attribute.name == 'face'): self.data['face'] = attribute.content elif(attribute.name == 'face1'): self.data['face1'] = attribute.content elif(attribute.name == 'face2'): - self.data['face2'] = attribute.content + self.data['face2'] = attribute.content xpa.xpathFreeContext() else: _logger.error('Error in validation of the file') + return 1 doc.freeDoc() + return 0 except libxml2.parserError, e: _logger.error('Error parsing file ' +str(e)) - + return 2 def save(self, filename): ''' saves the configuration to an xml file ''' doc = libxml2.newDoc("1.0") root = doc.newChild(None, "memorize", None) - root.setProp("name", self.data['game_name']) + root.setProp("name", self.data['name']) ### Fixme: add other attributes here for key in self.pairs: @@ -194,97 +203,143 @@ class Model(object): def def_grid(self, size): ''' create the grid for the play from the pairs information and shuffles the grid so they always appear in a different - place - grid [pair_key, a_or_b, flipstatus] + place ''' - _logger.debug(' pairs: %s', self.pairs) + psize=(size*size/2) + _logger.debug('Size requested: %d' %psize) + self.grid = [] + temp1 = [] + temp2 = [] i=0 for key in self.pairs.iterkeys(): - if i < size: - self.grid.append([key, 0, 0]) - self.grid.append([key, 1, 0]) + if i < psize: + elem = {} + elem['pairkey'] = key + elem['state'] = '0' + elem['ab'] = 'a' + if self.pairs[key].props.aimg != None: + elem['img'] = os.path.join(self.data['pathimg'], self.pairs[key].props.aimg) + if self.pairs[key].props.asnd != None: + elem['snd'] = os.path.join(self.data['pathsnd'], self.pairs[key].props.asnd) + if self.pairs[key].props.achar != None: + elem['char'] = self.pairs[key].props.achar + elem['charalign'] = self.pairs[key].props.acharalign + temp1.append(elem) + + elem = {} + elem['pairkey'] = key + elem['state'] = '0' + elem['ab'] = 'b' + if self.pairs[key].props.bimg != None: + elem['img'] = os.path.join(self.data['pathimg'], self.pairs[key].props.bimg) + if self.pairs[key].props.bsnd != None: + elem['snd'] = os.path.join(self.data['pathsnd'], self.pairs[key].props.bsnd) + if self.pairs[key].props.bchar != None: + elem['char'] = self.pairs[key].props.bchar + elem['charalign'] = self.pairs[key].props.bcharalign + temp2.append(elem) i+=1 else: break - + numpairs = len(self.pairs) - if numpairs < size: - _logger.debug('We did not have enough pairs. requested=%s had=%s' %(numpairs, size)) + if numpairs < psize: + _logger.debug('We did not have enough pairs. requested=%s had=%s' %(psize, numpairs)) + self.data['size'] = str(size) - self.data['size'] = numpairs - - random.shuffle(self.grid) - _logger.debug(' grid: %s', self.grid) + if self.data['divided'] == '1': + random.shuffle(temp1) + random.shuffle(temp2) + temp1.extend(temp2) + else: + temp1.extend(temp2) + random.shuffle(temp1) + self.grid = temp1 + _logger.debug(' grid( size=%s ): %s' %(self.data['size'], self.grid)) + + + +if __name__ == '__main__': + model = Model(os.path.dirname(__file__)) + model.read('drumgit') + + print 'name=%s scoresnd=%s winsnd=%s div=%s' %(model.data['name'], model.data['scoresnd'], + model.data['winsnd'], model.data['divided']) + model.def_grid(4) + print 'grid %s'%model.grid #['size'] - def gettile(self, tilenum): - ''' gets the information of an object associated with a tile number ''' - img = None - snd = None - char = None - pairkey, moch, state = self.grid[tilenum] - if moch == 0: - if self.pairs[pairkey].props.aimg != None: - img = os.path.join(IMAGES_PATH, self.pairs[pairkey].props.aimg) - if self.pairs[pairkey].props.asnd != None: - snd = os.path.join(SOUNDS_PATH, self.pairs[pairkey].props.asnd) - char = self.pairs[pairkey].props.achar - if moch == 1: - if self.pairs[pairkey].props.bimg != None: - img = os.path.join(IMAGES_PATH, self.pairs[pairkey].props.bimg) - if self.pairs[pairkey].props.bsnd != None: - snd = os.path.join(SOUNDS_PATH, self.pairs[pairkey].props.bsnd) - char = self.pairs[pairkey].props.bchar - color = self.pairs[pairkey].props.color - return (img, snd, char, color) + print 'Test set state of tile 7:' + tilenum = 7 + model.grid[tilenum]['state'] = '1' + print ' %s' %model.grid[tilenum] + print 'Test sound:' + snd = model.grid[tilenum].get('snd', None) + if snd == None: + print ' no sound' + else: + print ' play sound=%s'%snd - def same(self, a, b): - ''' checks wether two tiles are matching ''' - pairkeya, moch, state = self.grid[a] - pairkeyb, moch, state = self.grid[b] - return (pairkeya == pairkeyb) + print 'Test the same function: 0 1' + if model.grid[0]['pairkey'] == model.grid[1]['pairkey']: + print ' they are the same' + else: + print ' they are NOT the same' + for tile in model.grid: + id = model.grid.index(tile) + if tile.get('img', None): + print 'we got an image=%s '%tile['img'] + elif tile.get('char', None): + print 'we got an char=%s'%tile.get('char') + else: + print 'we got no pic so prepare for sound game' + + print '\n_______________________________\n' + ''' + if model.read('addition') == 0: + print '%s' %model.pairs[0]._properties + print 'name=%s' %model.data['name'] + print 'scoresnd=%s' %model.data['scoresnd'] + print 'winsnd=%s' %model.data['winsnd'] + print 'div=%d' %model.data['divided'] + model.def_grid(12) + for tile in model.grid: + id = model.grid.index(tile) + if tile.get('img', None): + print 'we got an image=%s '%tile.get('img') + elif tile.get('char', None): + print 'we got an char=%s'%tile.get('char') + else: + print 'we got no img so prepare for sound game' -if __name__ == '__main__': - model = Model(GAME_PATH, os.path.dirname(__file__)) - model.read('drumgit.mem') - print '%s' %model.pairs[0]._properties - print 'name=%s' %model.data['game_name'] - print 'scoresnd=%s' %model.data['scoresnd'] - print 'winsnd=%s' %model.data['winsnd'] - print 'div=%s' %model.data['divided'] - model.def_grid(8) - print 'grid size=%d'%model.data['size'] - print model.grid + else: + print 'error during reading of the game' - i=0 - while i < model.data['size']: - pairkey, moch, state = model.grid[i] - if moch == 0: - if model.pairs[pairkey].props.aimg != None: - print model.pairs[pairkey].props.aimg - if moch == 1: - if model.pairs[pairkey].props.bimg != None: - print model.pairs[pairkey].props.bimg - i+=1 - - ''' - print '\n_______________________________\n' - - model.read('addition.mem') - print '%s' %model.pairs[0]._properties - print 'name=%s' %model.data['game_name'] - print 'scoresnd=%s' %model.data['scoresnd'] - print 'winsnd=%s' %model.data['winsnd'] - print 'div=%s' %model.data['divided'] + print '\n_______________________________\n' + if model.read('numbers') == 0: + print '%s' %model.pairs[0]._properties + print 'name=%s' %model.data['name'] + print 'scoresnd=%s' %model.data['scoresnd'] + print 'winsnd=%s' %model.data['winsnd'] + print 'div=%d' %model.data['divided'] + print 'face1=%s' %model.data['face1'] + print 'face2=%s' %model.data['face2'] - model.def_grid(12) - print model.grid - print model.gettile(0) - print model.gettile(1) - model.save('/tmp/mod.txt') + model.def_grid(12) + for tile in model.grid: + id = model.grid.index(tile) + if tile.get('img', None): + print 'we got an image=%s '%tile.get('img') + elif tile.get('char', None): + print 'we got an char=%s'%tile.get('char') + else: + print 'we got no img so prepare for sound game' + + else: + print 'error during reading of the game' ''' -- cgit v0.9.1