def build_arena_list(chapters_list, outfile): root = mml.Node('arenas', '_') for chapter in chapters_list: name = filename(chapter[0])[:2] render_arenas(chapter, 'atlas_' + name + '.png') chapter_node = mml.Node('chapter', name) for arena in chapter: contents = open(arena, 'r') tree = mml.deserialize(contents.read()) contents.close() max_players = '0' for child in tree.children: if child.name == 'max_players': max_players = child.value arena_node = mml.Node('greed_assets/' + filename(arena), max_players) name_node = mml.Node('name', tree.value) arena_node.children.append(name_node) chapter_node.children.append(arena_node) root.children.append(chapter_node) out = open(outfile, 'w') out.write(mml.serialize(root)) out.close()
def scan(): arenas_list = [] arena_expr = re.compile('\S+\.(mml)\Z') for root, dirs, files in os.walk('.'): if root == '': path = '.' + separator else: path = root + separator for file in files: if arena_expr.match(file) != None: contents = open(path + file, 'r') tree = mml.deserialize(contents.read()) contents.close() if tree.name == 'arena': arenas_list.append(path + file) # Group by chapter arenas_list.sort() chapters_list = [] last_chapter = '' for arena in arenas_list: chapter = filename(arena)[:2] if chapter != last_chapter: last_chapter = chapter chapters_list.append([arena]) else: chapters_list[len(chapters_list)-1].append(arena) return chapters_list
def put(request): response = HttpResponse() #print 'Put: request: ', request , ' -- end\n' try: tree = mml.deserialize(request.META['raw_post_data']) except Exception: response.status_code = 400 # Bad Request response.write('Bad entry format') return response if len(tree.children) != 4: response.status_code = 400 # Bad Request response.write('Bad entry format. 4 fields required, got ') response.write(len(tree.children)) return response fields = dict( [(tree.children[i].name, tree.children[i].value) for i in range(4)] ) #TODO: validate fields r = record() r.gid = fields['gid'] r.uid = fields['uid'] r.time = datetime.datetime.now() r.weight = fields['weight'] r.data = fields['data'] r.save() response.status_code = 201 # Created return response
def query(request): #print '\nQuery: request: ', request , ' -- end\n' response = HttpResponse(mimetype="text/plain") try: tree = mml.deserialize(request.META['QUERY_STRING']) except Exception: response.status_code = 400 # Bad Request response.write('Bad query format') return response if len(tree.children) != 5: response.status_code = 400 # Bad Request response.write('Bad query format. 5 fields required, got ') response.write(len(tree.children)) return response #for i in record.objects.values_list(): # print i #print '\n' fields = dict( [(tree.children[i].name, tree.children[i].value) for i in range(5)] ) startTime = datetime.datetime(*time.strptime(fields['startTime'], '%Y%m%d%H%M%S')[0:6] ) endTime = datetime.datetime(*time.strptime(fields['endTime'], '%Y%m%d%H%M%S')[0:6] ) results = record.objects.filter(gid=fields['gid']) if startTime < endTime: results = results.filter(time__gt=startTime).filter(time__lt=endTime) elif startTime > endTime: print 'startTime > endTime' results = results.order_by('weight')[fields['startEntry'] : fields['startEntry'] + fields['entryCount']] resp = mml.Node( 'entries', str(len(results)) ) for r in results: entry = (mml.Node( 'entry', '_')) entry.children.append(mml.Node('gid', r.gid)) entry.children.append(mml.Node('uid', r.uid)) entry.children.append(mml.Node('time', r.time.strftime('%Y%m%d%H%M%S'))) entry.children.append(mml.Node('weight', str(r.weight)) ) entry.children.append(mml.Node('data', r.data)) resp.children.append(entry) response.write(mml.serialize(resp)) response.status_code = 201 # Created return response
def render_arenas(chapter, outfile): img = Image.new('RGBA', (1024, 512), (0, 0, 0, 0)) cursor_x, cursor_y = 1, 1 for arena in chapter: contents = open(arena, 'r') tree = mml.deserialize(contents.read()) contents.close() arena_img = render_arena(tree) if cursor_x + 338 >= 1024: cursor_x = 1 cursor_y += 226 img.paste(arena_img, (cursor_x, cursor_y)) cursor_x += 339 img.save(outfile)
img = img.resize((img.size[0]/2, img.size[1]/2), Image.ANTIALIAS) img_hd = Image.open(prop.value) if prop.name == 'def': grid = parse_puzzle_def(prop.value) if img == None or grid == None: raise RuntimeError('Bad puzzle ' + puzzle.value + ' description') slices = slice(grid, img, img_hd) add_to_atlas(slices, puzzle.value) if __name__ == '__main__': usage = '%prog puzzles.mml OUT' parser = OptionParser(usage=usage) (opt, args) = parser.parse_args(sys.argv) if len(args) != 3: parser.error('You must provide puzzles desc and output destination') desc_file = open(args[1], 'r') desc_mml = mml.deserialize(desc_file.read()) tiles_mml = mml.Node('puzzle_tiles', '_') parse_puzzles(desc_mml) save_current_atlas() out = open(args[2], 'w') out.write(mml.serialize(tiles_mml))
if child.name == 'walls' or child.name == 'tex': (img_directory, img_filename) = os.path.split(child.value) new_img_filename = img_filename.replace('tga', 'png') if not os.path.exists(folder + '/' + new_img_filename): img = Image.open(folder + '/' + img_filename) img.save(folder + '/' + new_img_filename, 'png') child.value = img_directory + '/' + new_img_filename os.remove(folder + '/' + img_filename) arena_file = open(out_path, 'w') arena_file.write(mml.serialize(arena_mml)) arena_file.close() if __name__ == '__main__': usage = '%prog SOURCE DEST' parser = OptionParser(usage=usage) (opt, args) = parser.parse_args(sys.argv) if len(args) != 3: parser.error('You must provide processed arena description file and output file') arena_file = open(args[1], 'r') arena_mml = mml.deserialize(arena_file.read()) arena_file.close() compress(arena_mml, args[1], args[2])