1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
#! /usr/bin/env python
#
# Copyright (C) 2006, 2007, One Laptop Per Child
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
import libxml2
import os
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),
'bimg' : (str, None, None, None, gobject.PARAM_READWRITE),
'bsnd' : (str, None, None, None, gobject.PARAM_READWRITE),
'bchar' : (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}
def do_get_property(self, pspec):
"""Retrieve a particular property from our property dictionary
"""
if pspec.name == "aimg":
return self._properties["aimg"]
elif pspec.name == "asnd":
return self._properties["asnd"]
elif pspec.name == "achar":
return self._properties["achar"]
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 == "color":
return self._properties["color"]
def set_property(self, name, value):
if name == 'aimg':
self._properties['aimg'] = value
elif name == "asnd":
self._properties["asnd"] = value
elif name == "achar":
self._properties["achar"] = value
elif name == "bimg":
self._properties["bimg"] = value
elif name == "bsnd":
self._properties["bsnd"] = value
elif name == "bchar":
self._properties["bchar"] = 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
'''
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'):
self.data = {}
self.gamepath = gamepath
self.dtdpath = dtdpath
try:
self.dtd = libxml2.parseDTD(None, os.path.join(self.dtdpath, 'memorize.dtd'))
except libxml2.parserError, e:
_logger.error('No memorize.dtd found ' +str(e))
self.dtd = None
self.ctxt = libxml2.newValidCtxt()
self.pairs = {}
self.grid = []
# used by the leader of the game to keep track of the game state
self.players = {}
self.player_active = 0
self.selected = 0
self.turn = 0
self.started = 0
self.count = 0
def read(self, filename):
''' reades the configuration from an xml file '''
try:
doc = libxml2.parseFile(os.path.join(self.gamepath, filename))
if doc.validateDtd(self.ctxt, self.dtd):
# get the requested nodes
xpa = doc.xpathNewContext()
res = xpa.xpathEval("//*")
# write their content to the data structure
self.idpair = 0
for elem in res:
attributes = elem.get_properties()
pair = Pair()
if( elem.name == 'pair' ):
for attribute in attributes:
if(attribute.name == 'text'):
pass
else:
pair.set_property(attribute.name, attribute.content)
self.pairs[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
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
xpa.xpathFreeContext()
else:
_logger.error('Error in validation of the file')
doc.freeDoc()
except libxml2.parserError, e:
_logger.error('Error parsing file ' +str(e))
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'])
### Fixme: add other attributes here
for key in self.pairs:
elem = root.newChild(None, "pair", None)
if self.pairs[key].props.aimg != None:
elem.setProp("aimg", self.pairs[key].props.aimg)
if self.pairs[key].props.asnd != None:
elem.setProp("asnd", self.pairs[key].props.asnd)
if self.pairs[key].props.achar != None:
elem.setProp("achar", self.pairs[key].props.achar)
if self.pairs[key].props.bimg != None:
elem.setProp("bimg", self.pairs[key].props.bimg)
if self.pairs[key].props.bsnd != None:
elem.setProp("bsnd", self.pairs[key].props.bsnd)
if self.pairs[key].props.bchar != None:
elem.setProp("bchar", self.pairs[key].props.bchar)
elem.setProp("color", str(self.pairs[key].props.color))
if doc.validateDtd(self.ctxt, self.dtd):
doc.saveFormatFile(filename, 1)
doc.freeDoc()
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]
'''
_logger.debug(' pairs: %s', self.pairs)
i=0
for key in self.pairs.iterkeys():
if i < size:
self.grid.append([key, 0, 0])
self.grid.append([key, 1, 0])
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))
self.data['size'] = numpairs
random.shuffle(self.grid)
_logger.debug(' grid: %s', self.grid)
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)
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)
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
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']
model.def_grid(12)
print model.grid
print model.gettile(0)
print model.gettile(1)
model.save('/tmp/mod.txt')
'''
|