#!/usr/bin/env python # bastetscore - Modify the high score list for bastet (Bastard Tetris) # Copyright (C) 2007 Lenny Domnitser # # 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import os import struct FORMAT = '12sI' * 10 SCORES_FILE = '/var/games/bastet.scores' def decode(data): unpacked_data = struct.unpack(FORMAT, data) decoded_data = [] for c in xrange(0, 20, 2): name = unpacked_data[c].rstrip('\0') score = unpacked_data[c+1] decoded_data.append((name, score)) return decoded_data def encode(decoded_data): unpacked_data = reduce(lambda a, b: a + b, decoded_data) data = struct.pack(FORMAT, *unpacked_data) return data def test(): # sanity test data = open(SCORES_FILE).read() assert data == encode(decode(data)) def main(): data = open(SCORES_FILE).read() decoded_data = decode(data) new_decoded_data = [] c = 1 for name, score in decoded_data: print '%2d. %s: %s' % (c, name, score) print ' New name:', name = raw_input() while True: try: print ' New score:', score = int(raw_input()) break except ValueError: print ' invalid score' print new_decoded_data.append((name, score)) c += 1 data = encode(new_decoded_data) filename = os.path.join(os.environ.get('TMP', '/tmp'), 'bastet.scores') open(filename, 'wb').write(data) print 'Wrote scores to %s. To use this file, move it to %s.' % (filename, SCORES_FILE) if __name__ == '__main__': test() main()