#!/usr/bin/env python
#
###
# Application: Pycadia
# File:        hiscores.py
# Description: pycadia Hi-Scores class
# Copyright (c) 2002-2003   Andy Balcombe <http://www.anti-particle.com>
###
#
# 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import pickle

"""Hi-Scores classes"""

class ScoreRecord:
    """one record in a HiScore table"""
    def __init__(self, name='', score=0):
        self.name = name
        self.score = score

        
class HiScore:
    """a hi-score table"""
    def __init__(self, num_entries=10, filename='hiscores.dat', load=0):
        """initialise the hi-score table"""
        self.max_entries = num_entries
        self.scores=[]
        self.filename = filename
        #load from file?
        if load:
            self.load()
        #generate initial entries (if not loaded)
        if len(self.scores) == 0:
            for i in range(num_entries):
                self.scores.append(ScoreRecord())
            
    def add_hiscore(self, name, score):
        """add a score to the table"""
        #find place
        pos = self.place(score)
        if pos >= 0:
            #insert score
            self.scores.insert(pos, ScoreRecord(name, score))
            #and cut new scores down to size
            self.scores = self.scores[:self.max_entries]
            self.save()
            
    def save(self):
        """save the scores"""
        pickle.dump(self.scores, open(self.filename, 'w'))
        
    def load(self):
        """load the scores"""
        try:
            s = pickle.load(open(self.filename, 'r'))
            self.scores = s
        except IOError:
            pass
                
    def place(self, score):
        """return the place in the table where the new score should go"""
        if score >= self.scores[0].score:
            #new top score
            pos = 0
        elif score < self.scores[-1].score:
            #not in hi-scores
            pos = -1
        else:
            #search through scores
            for pos in range(len(self.scores)):
                if score >= self.scores[pos].score:
                    break
        #done
        return pos
        
        
if __name__ == '__main__':
    #example use...
    
    #start a 10 position table
    hs = HiScore(10)
    #add some example scores (note: not in any order)
    hs.add_hiscore('ab1', 500)
    hs.add_hiscore('ab2', 200)
    hs.add_hiscore('ab3', 5000)
    hs.add_hiscore('ab3', -50)
    hs.add_hiscore('ab5', 275)
    hs.add_hiscore('ab5', 5000)
    #save it
    hs.save()
    
    #print out the list
    print "initial list"
    print "pos    name    score"
    for i in range(len(hs.scores)):
        print "%2s    %5s    %5s" % (i+1, hs.scores[i].name, hs.scores[i].score) 

    #create a new list
    hs2 = HiScore(load=1)
    #and display it
    print "\nloaded list"
    print "pos    name    score"
    for i in range(len(hs.scores)):
        print "%2s    %5s    %5s" % (i+1, hs.scores[i].name, hs.scores[i].score) 
    
