File: achievements.cpp

package info (click to toggle)
frogatto 1.3.1%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 3,980 kB
  • sloc: cpp: 74,685; objc: 526; ansic: 375; sh: 79; makefile: 77
file content (78 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download | duplicates (6)
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
#include <algorithm>
#include <map>

#include "achievements.hpp"
#include "filesystem.hpp"
#include "foreach.hpp"
#include "formula_callable.hpp"
#include "i18n.hpp"
#include "json_parser.hpp"
#include "preferences.hpp"
#include "string_utils.hpp"
#include "of_bridge.h"
#include "variant.hpp"

namespace {
std::map<std::string, achievement_ptr> cache;
}

achievement_ptr achievement::get(const std::string& id)
{
	if(cache.empty()) {
		variant node;
		try {
			node = (json::parse_from_file("data/achievements.cfg"));
		} catch(json::parse_error&) {
			cache[""]; //mark as loaded
			return achievement_ptr();
		}

		
		foreach(variant achievement_node, node["achievement"].as_list()) {
			achievement_ptr a(new achievement(achievement_node));
			cache[a->id()] = a;
		}
	}

	return cache[id];
}

achievement::achievement(variant node)
  : id_(node["id"].as_string()), name_(i18n::tr(node["name"].as_string())),
    description_(i18n::tr(node["description"].as_string())),
	points_(node["points"].as_int()),
	of_id_(node["of_id"].as_int())
{
}

namespace {
std::vector<std::string>* achievements = NULL;
}

bool attain_achievement(const std::string& id)
{
	if(achievements == NULL) {
		achievements = new std::vector<std::string>;
		variant val = preferences::registry()->query_value("achievements");
		if(val.is_string()) {
			*achievements = util::split(val.as_string());
			std::sort(achievements->begin(), achievements->end());
		}
	}

	if(std::binary_search(achievements->begin(), achievements->end(), id)) {
		return false;
	}
	
	#ifdef ENABLE_OPENFEINT
	achievement_ptr a = achievement::get(id);
	of_earn_achievement(a->of_id());
	#endif

	achievements->push_back(id);
	std::sort(achievements->begin(), achievements->end());

	preferences::registry()->mutate_value("achievements", variant(util::join(*achievements)));

	return true;
}