~ubuntu-branches/ubuntu/precise/zaz/precise

« back to all changes in this revision

Viewing changes to src/hiscores.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miriam Ruiz
  • Date: 2009-08-31 20:08:58 UTC
  • Revision ID: james.westby@ubuntu.com-20090831200858-54lcmcrna6dwk3wr
Tags: upstream-0.2.9+dfsg1
ImportĀ upstreamĀ versionĀ 0.2.9+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Zaz
 
3
 * Copyright (C) Remigiusz Dybka 2009 <remigiusz.dybka@gmail.com>
 
4
 *
 
5
 Zaz is free software: you can redistribute it and/or modify it
 
6
 under the terms of the GNU General Public License as published by the
 
7
 Free Software Foundation, either version 3 of the License, or
 
8
 (at your option) any later version.
 
9
 
 
10
 Zaz is distributed in the hope that it will be useful, but
 
11
 WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
13
 See the GNU General Public License for more details.
 
14
 
 
15
 You should have received a copy of the GNU General Public License along
 
16
 with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include <sstream>
 
20
#include <fstream>
 
21
#include <iostream>
 
22
#include "hiscores.h"
 
23
#include "error.h"
 
24
#include "common.h"
 
25
 
 
26
using namespace std;
 
27
 
 
28
namespace Scenes
 
29
{
 
30
 
 
31
    HiScores::HiScores(string filename)
 
32
            : filename(filename)
 
33
    {
 
34
        ifstream inph(filename.c_str());
 
35
        if (!inph) // may be empty or not exist
 
36
            return;
 
37
 
 
38
        string l;
 
39
        while (!getline(inph, l).fail())
 
40
        {
 
41
            int score;
 
42
            string name;
 
43
            string level;
 
44
            istringstream sl;
 
45
 
 
46
            Strip(l);
 
47
            sl.clear();
 
48
            sl.str(l);
 
49
            sl >> score;
 
50
 
 
51
            bool ok = (!getline(inph, l).fail());
 
52
            Strip(l);
 
53
            name = l;
 
54
 
 
55
            ok = ok && (!getline(inph, l).fail());
 
56
            Strip(l);
 
57
            level = l;
 
58
 
 
59
            if (ok)
 
60
                scores.push_back(HiScoreEntry(score, name, level));
 
61
        }
 
62
        inph.close();
 
63
    }
 
64
 
 
65
    HiScores::~HiScores()
 
66
    {
 
67
        ofstream oph(filename.c_str());
 
68
        if (!oph) // very bad
 
69
        {
 
70
            ERR(filename + ": could not save hi scores");
 
71
        }
 
72
 
 
73
        vector<HiScoreEntry>::iterator i;
 
74
 
 
75
        int f = 0;
 
76
        for (i = scores.begin(); i != scores.end(); ++i)
 
77
        {
 
78
            if ((f < 10) && (i->score > 0))
 
79
            {
 
80
                oph << i->score << std::endl;
 
81
                oph << i->name << std::endl;
 
82
                oph << i->level << std::endl;
 
83
            }
 
84
            ++f;
 
85
        }
 
86
 
 
87
        oph.close();
 
88
    }
 
89
 
 
90
    void HiScores::SubmitHiScore(HiScoreEntry ent)
 
91
    {
 
92
        vector<HiScoreEntry>::iterator i;
 
93
        vector<HiScoreEntry>::iterator ii;
 
94
 
 
95
        ii = scores.begin();
 
96
        for (i = scores.begin(); i != scores.end(); ++i)
 
97
        {
 
98
            if (i->score > ent.score)
 
99
            {
 
100
                ii = i + 1;
 
101
            }
 
102
        }
 
103
 
 
104
        scores.insert(ii, ent);
 
105
    }
 
106
 
 
107
    bool HiScores::GoodEnough(int s)
 
108
    {
 
109
        vector<HiScoreEntry>::iterator i;
 
110
 
 
111
        if (s == 0)
 
112
            return false;
 
113
 
 
114
        if (scores.size() < 10)
 
115
            return true;
 
116
 
 
117
        int f = 0;
 
118
        for (i = scores.begin(); i != scores.end(); ++i)
 
119
        {
 
120
            if (f < 10)
 
121
            {
 
122
                if (i->score < s)
 
123
                    return true;
 
124
            }
 
125
            ++f;
 
126
        }
 
127
 
 
128
        return false;
 
129
    }
 
130
}