Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pgu/high.py
blob: e05d22af7cec2b252246a43fae755bc7a53fe9c2 (plain)
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
"""Classes for handling high score tables.
"""

import os

def High(fname,limit=10):
    """Create a Highs object and returns the default high score table.
    
    <pre>High(fname,limit=10)</pre>
    
    <dl>
    <dt>fname <dd>filename to store high scores in
    <dt>limit <dd>limit of scores to be recorded, defaults to 10
    </dl>
    """
    return Highs(fname,limit)['default']
    
class _Score:
    def __init__(self,score,name,data=None):
        self.score,self.name,self.data=score,name,data
    
class _High:
    """A high score table.  These objects are passed to the user, but should not be created directly.
    
    <p>You can iterate them:</p>
    <code>
    for e in myhigh:
        print e.score,e.name,e.data
    </code>
        
    <p>You can modify them:</p>
    <code>
    myhigh[0].name = 'Cuzco'
    </code>
    
    <p>You can find out their length:</p>
    <code>
    print len(myhigh)
    </code>
    """
    
    def __init__(self,highs,limit=10):
        self.highs = highs
        self._list = []
        self.limit = limit
        
    def save(self):
        """Save the high scores.
        
        <pre>_High.save()</pre>
        """
        self.highs.save()
        
    def submit(self,score,name,data=None):
        """Submit a high score to this table.
        
        <pre>_High.submit(score,name,data=None)</pre>
        
        <p>return -- the position in the table that the score attained.  None if the score did not attain a position in the table.</p>
        """
        n = 0
        for e in self._list:
            if score > e.score:
                self._list.insert(n,_Score(score,name,data))
                self._list = self._list[0:self.limit]
                return n
            n += 1
        if len(self._list) < self.limit:
            self._list.append(_Score(score,name,data))
            return len(self._list)-1
    
    def check(self,score):
        """Check if a score will attain a position in the table.
        
        <pre>_High.check(score)</pre>
        
        <p>return -- the position the score will attain, else None</p>
        """
        n = 0
        for e in self._list:
            if score > e.score:
                return n
            n += 1
        if len(self._list) < self.limit:
            return len(self._list)
        
        
    def __iter__(self):
        return self._list.__iter__()
        
    def __getitem__(self,key):
        return self._list[key]
        
    def __len__(self):
        return self._list.__len__()
        

class Highs:
    """The high score object.
    
    <pre>Highs(fname,limit=10)</pre>
    <ul>
    <dt>fname <dd>filename to store high scores in
    <dt>limit <dd>limit of scores to be recorded, defaults to 10
    </ul>
    
    <p>You may access _High objects through this object:</p>
   
    <code> 
    my_easy_hs = highs['easy']
    my_hard_hs = highs['hard']
    </code>
    
    """
    def __init__(self,fname,limit=10):
        self.fname = fname
        self.limit = limit
        self.load()
        
    def load(self):
        """Re-load the high scores.
        
        <pre>Highs.load()</pre>
        """
        
        self._dict = {}
        try:
            f = open(self.fname)
            for line in f.readlines():
                key,score,name,data = line.strip().split("\t")
                if key not in self._dict:
                    self._dict[key] = _High(self,self.limit)
                high = self._dict[key]
                high.submit(int(score),name,data)
            f.close()
        except:
            pass
    
    def save(self):
        """Save the high scores.
        
        <pre>Highs.save()</pre>
        """
        
        f = open(self.fname,"w")
        for key,high in self._dict.items():
            for e in high:
                f.write("%s\t%d\t%s\t%s\n"%(key,e.score,e.name,str(e.data)))
        f.close()
        
    def __getitem__(self,key):
        if key not in self._dict:
            self._dict[key] = _High(self,self.limit)
        return self._dict[key]