-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.cpp
164 lines (131 loc) · 4 KB
/
group.cpp
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
155
156
157
158
159
160
161
162
163
164
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// 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 3 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, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include <iostream>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include "group.h"
#include "tools.h"
Group Groups::defGroup = Group();
void Groups::clear()
{
for(GroupsMap::iterator it = groupsMap.begin(); it != groupsMap.end(); ++it)
delete it->second;
groupsMap.clear();
}
bool Groups::reload()
{
clear();
return loadFromXml();
}
bool Groups::loadFromXml()
{
xmlDocPtr doc = xmlParseFile(getFilePath(FILE_TYPE_XML, "groups.xml").c_str());
if(!doc)
{
std::cout << "[Warning - Groups::loadFromXml] Cannot load groups file." << std::endl;
std::cout << getLastXMLError() << std::endl;
return false;
}
xmlNodePtr p, root = xmlDocGetRootElement(doc);
if(xmlStrcmp(root->name,(const xmlChar*)"groups"))
{
std::cout << "[Error - Groups::loadFromXml] Malformed groups file." << std::endl;
xmlFreeDoc(doc);
return false;
}
p = root->children;
while(p)
{
parseGroupNode(p);
p = p->next;
}
xmlFreeDoc(doc);
return true;
}
bool Groups::parseGroupNode(xmlNodePtr p)
{
if(xmlStrcmp(p->name, (const xmlChar*)"group"))
return false;
int32_t intValue;
if(!readXMLInteger(p, "id", intValue))
{
std::cout << "[Warning - Groups::parseGroupNode] Missing group id." << std::endl;
return false;
}
std::string strValue;
int64_t int64Value;
Group* group = new Group(intValue);
if(readXMLString(p, "name", strValue))
{
group->setFullName(strValue);
group->setName(asLowerCaseString(strValue));
}
if(readXMLInteger64(p, "flags", int64Value))
group->setFlags(int64Value);
if(readXMLInteger64(p, "customFlags", int64Value))
group->setCustomFlags(int64Value);
if(readXMLInteger(p, "access", intValue))
group->setAccess(intValue);
if(readXMLInteger(p, "ghostAccess", intValue))
group->setGhostAccess(intValue);
else
group->setGhostAccess(group->getAccess());
if(readXMLInteger(p, "violationReasons", intValue))
group->setViolationReasons(intValue);
if(readXMLInteger(p, "nameViolationFlags", intValue))
group->setNameViolationFlags(intValue);
if(readXMLInteger(p, "statementViolationFlags", intValue))
group->setStatementViolationFlags(intValue);
if(readXMLInteger(p, "depotLimit", intValue))
group->setDepotLimit(intValue);
if(readXMLInteger(p, "maxVips", intValue))
group->setMaxVips(intValue);
if(readXMLInteger(p, "outfit", intValue))
group->setOutfit(intValue);
groupsMap[group->getId()] = group;
return true;
}
Group* Groups::getGroup(uint32_t groupId)
{
GroupsMap::iterator it = groupsMap.find(groupId);
if(it != groupsMap.end())
return it->second;
std::cout << "[Warning - Groups::getGroup] Group " << groupId << " not found." << std::endl;
return &defGroup;
}
int32_t Groups::getGroupId(const std::string& name)
{
for(GroupsMap::iterator it = groupsMap.begin(); it != groupsMap.end(); ++it)
{
if(!strcasecmp(it->second->getName().c_str(), name.c_str()))
return it->first;
}
return -1;
}
uint32_t Group::getDepotLimit(bool premium) const
{
if(m_depotLimit > 0)
return m_depotLimit;
return (premium ? 2000 : 1000);
}
uint32_t Group::getMaxVips(bool premium) const
{
if(m_maxVips > 0)
return m_maxVips;
return (premium ? 100 : 20);
}