11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. |
12 * GNU General Public License for more details. |
13 * |
13 * |
14 * You should have received a copy of the GNU General Public License |
14 * You should have received a copy of the GNU General Public License |
15 * along with this program; if not, write to the Free Software |
15 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 */ |
17 */ |
18 |
18 |
19 #include <QFile> |
19 #include <QFile> |
20 #include <QTextStream> |
20 #include <QTextStream> |
21 #include <QStringList> |
21 #include <QStringList> |
22 #include <QLineEdit> |
22 #include <QLineEdit> |
23 #include <QCryptographicHash> |
23 #include <QCryptographicHash> |
24 #include <QSettings> |
24 #include <QSettings> |
25 #include <QStandardItemModel> |
25 #include <QStandardItemModel> |
|
26 #include <QDebug> |
26 |
27 |
27 #include "team.h" |
28 #include "team.h" |
28 #include "hwform.h" |
29 #include "hwform.h" |
29 #include "DataManager.h" |
30 #include "DataManager.h" |
|
31 #include "gameuiconfig.h" |
30 |
32 |
31 HWTeam::HWTeam(const QString & teamname) : |
33 HWTeam::HWTeam(const QString & teamname) : |
32 QObject(0) |
34 QObject(0) |
33 , m_difficulty(0) |
35 , m_difficulty(0) |
34 , m_numHedgehogs(4) |
36 , m_numHedgehogs(4) |
35 , m_isNetTeam(false) |
37 , m_isNetTeam(false) |
|
38 , m_isMissionTeam(false) |
36 { |
39 { |
37 m_name = teamname; |
40 m_name = teamname; |
38 OldTeamName = m_name; |
41 OldTeamName = m_name; |
39 for (int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
42 for (int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
40 { |
43 { |
41 m_hedgehogs.append(HWHog()); |
44 m_hedgehogs.append(HWHog()); |
42 m_hedgehogs[i].Name = (QLineEdit::tr("hedgehog %1").arg(i+1)); |
45 m_hedgehogs[i].Name = (QLineEdit::tr("Hedgehog %1").arg(i+1)); |
43 m_hedgehogs[i].Hat = "NoHat"; |
46 m_hedgehogs[i].Hat = "NoHat"; |
44 } |
47 } |
45 m_grave = "Statue"; |
48 m_grave = "Statue"; |
46 m_fort = "Plane"; |
49 m_fort = "Plane"; |
47 m_voicepack = "Default"; |
50 m_voicepack = "Default"; |
48 m_flag = "hedgewars"; |
51 m_flag = "hedgewars"; |
49 for(int i = 0; i < BINDS_NUMBER; i++) |
52 for(int i = 0; i < BINDS_NUMBER; i++) |
50 { |
53 { |
51 m_binds.append(BindAction()); |
54 m_binds.append(BindAction()); |
52 m_binds[i].action = cbinds[i].action; |
55 m_binds[i].action = cbinds[i].action; |
53 m_binds[i].strbind = cbinds[i].strbind; |
56 m_binds[i].strbind = QString(); |
54 } |
57 } |
55 m_rounds = 0; |
|
56 m_wins = 0; |
|
57 m_campaignProgress = 0; |
|
58 m_color = 0; |
58 m_color = 0; |
59 } |
59 } |
60 |
60 |
61 HWTeam::HWTeam(const QStringList& strLst) : |
61 HWTeam::HWTeam(const QStringList& strLst) : |
62 QObject(0) |
62 QObject(0) |
63 , m_numHedgehogs(4) |
63 , m_numHedgehogs(4) |
64 , m_isNetTeam(true) |
64 , m_isNetTeam(true) |
|
65 , m_isMissionTeam(false) |
65 { |
66 { |
66 // net teams are configured from QStringList |
67 // net teams are configured from QStringList |
67 if(strLst.size() != 23) throw HWTeamConstructException(); |
68 if(strLst.size() != 23) throw HWTeamConstructException(); |
68 m_name = strLst[0]; |
69 m_name = strLst[0]; |
69 m_grave = strLst[1]; |
70 m_grave = strLst[1]; |
156 m_binds = other.m_binds; |
150 m_binds = other.m_binds; |
157 m_numHedgehogs = other.m_numHedgehogs; |
151 m_numHedgehogs = other.m_numHedgehogs; |
158 m_color = other.m_color; |
152 m_color = other.m_color; |
159 m_isNetTeam = other.m_isNetTeam; |
153 m_isNetTeam = other.m_isNetTeam; |
160 m_owner = other.m_owner; |
154 m_owner = other.m_owner; |
161 m_campaignProgress = other.m_campaignProgress; |
|
162 m_rounds = other.m_rounds; |
|
163 m_wins = other.m_wins; |
|
164 m_color = other.m_color; |
155 m_color = other.m_color; |
|
156 m_isMissionTeam = other.m_isMissionTeam; |
165 } |
157 } |
166 |
158 |
167 return *this; |
159 return *this; |
168 } |
160 } |
169 |
161 |
170 bool HWTeam::loadFromFile() |
162 bool HWTeam::loadFromFile() |
171 { |
163 { |
172 QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + m_name + ".hwt", QSettings::IniFormat, 0); |
164 QSettings teamfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(DataManager::safeFileName(m_name)), QSettings::IniFormat, 0); |
173 teamfile.setIniCodec("UTF-8"); |
165 teamfile.setIniCodec("UTF-8"); |
174 m_name = teamfile.value("Team/Name", m_name).toString(); |
166 m_name = teamfile.value("Team/Name", m_name).toString(); |
175 m_grave = teamfile.value("Team/Grave", "Statue").toString(); |
167 m_grave = teamfile.value("Team/Grave", "Statue").toString(); |
176 m_fort = teamfile.value("Team/Fort", "Plane").toString(); |
168 m_fort = teamfile.value("Team/Fort", "Plane").toString(); |
177 m_voicepack = teamfile.value("Team/Voicepack", "Default").toString(); |
169 m_voicepack = teamfile.value("Team/Voicepack", "Default").toString(); |
178 m_flag = teamfile.value("Team/Flag", "hedgewars").toString(); |
170 m_flag = teamfile.value("Team/Flag", "hedgewars").toString(); |
179 m_difficulty = teamfile.value("Team/Difficulty", 0).toInt(); |
171 m_difficulty = teamfile.value("Team/Difficulty", 0).toInt(); |
180 m_rounds = teamfile.value("Team/Rounds", 0).toInt(); |
|
181 m_wins = teamfile.value("Team/Wins", 0).toInt(); |
|
182 m_campaignProgress = teamfile.value("Team/CampaignProgress", 0).toInt(); |
|
183 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
172 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
184 { |
173 { |
185 QString hh = QString("Hedgehog%1/").arg(i); |
174 QString hh = QString("Hedgehog%1/").arg(i); |
186 m_hedgehogs[i].Name = teamfile.value(hh + "Name", QString("hedgehog %1").arg(i+1)).toString(); |
175 m_hedgehogs[i].Name = teamfile.value(hh + "Name", QString("Hedgehog %1").arg(i+1)).toString(); |
187 m_hedgehogs[i].Hat = teamfile.value(hh + "Hat", "NoHat").toString(); |
176 m_hedgehogs[i].Hat = teamfile.value(hh + "Hat", "NoHat").toString(); |
188 m_hedgehogs[i].Rounds = teamfile.value(hh + "Rounds", 0).toInt(); |
|
189 m_hedgehogs[i].Kills = teamfile.value(hh + "Kills", 0).toInt(); |
|
190 m_hedgehogs[i].Deaths = teamfile.value(hh + "Deaths", 0).toInt(); |
|
191 m_hedgehogs[i].Suicides = teamfile.value(hh + "Suicides", 0).toInt(); |
|
192 } |
177 } |
193 for(int i = 0; i < BINDS_NUMBER; i++) |
178 for(int i = 0; i < BINDS_NUMBER; i++) |
194 m_binds[i].strbind = teamfile.value(QString("Binds/%1").arg(m_binds[i].action), cbinds[i].strbind).toString(); |
179 m_binds[i].strbind = teamfile.value(QString("Binds/%1").arg(m_binds[i].action), QString()).toString(); |
195 for(int i = 0; i < MAX_ACHIEVEMENTS; i++) |
180 for(int i = 0; i < MAX_ACHIEVEMENTS; i++) |
196 if(achievements[i][0][0]) |
181 if(achievements[i][0][0]) |
197 AchievementProgress[i] = teamfile.value(QString("Achievements/%1").arg(achievements[i][0]), 0).toUInt(); |
182 AchievementProgress[i] = teamfile.value(QString("Achievements/%1").arg(achievements[i][0]), 0).toUInt(); |
198 else |
183 else |
199 break; |
184 break; |
200 return true; |
185 return true; |
201 } |
186 } |
202 |
187 |
203 bool HWTeam::fileExists() |
188 bool HWTeam::fileExists() |
204 { |
189 { |
205 QFile f(cfgdir->absolutePath() + "/Teams/" + m_name + ".hwt"); |
190 QFile f(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(DataManager::safeFileName(m_name))); |
206 return f.exists(); |
191 return f.exists(); |
|
192 } |
|
193 |
|
194 // Returns true if the team name has been changed but a file with the same team name already exists. |
|
195 // So if this team would be saved, another team file would be overwritten, which is generally not |
|
196 // desired. |
|
197 bool HWTeam::wouldOverwriteOtherFile() |
|
198 { |
|
199 return (m_name != OldTeamName) && fileExists(); |
207 } |
200 } |
208 |
201 |
209 bool HWTeam::deleteFile() |
202 bool HWTeam::deleteFile() |
210 { |
203 { |
211 if(m_isNetTeam) |
204 if(m_isNetTeam) |
212 return false; |
205 return false; |
213 QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + m_name + ".hwt"); |
206 QFile cfgfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(DataManager::safeFileName(m_name))); |
214 cfgfile.remove(); |
207 cfgfile.remove(); |
215 return true; |
208 return true; |
216 } |
209 } |
217 |
210 |
218 bool HWTeam::saveToFile() |
211 bool HWTeam::saveToFile() |
219 { |
212 { |
220 if (OldTeamName != m_name) |
213 if (OldTeamName != m_name) |
221 { |
214 { |
222 QFile cfgfile(cfgdir->absolutePath() + "/Teams/" + OldTeamName + ".hwt"); |
215 QFile cfgfile(QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(DataManager::safeFileName(OldTeamName))); |
223 cfgfile.remove(); |
216 cfgfile.remove(); |
224 OldTeamName = m_name; |
217 OldTeamName = m_name; |
225 } |
218 } |
226 QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + m_name + ".hwt", QSettings::IniFormat, 0); |
219 |
|
220 QString fileName = QString(cfgdir->absolutePath() + "/Teams/%1.hwt").arg(DataManager::safeFileName(m_name)); |
|
221 DataManager::ensureFileExists(fileName); |
|
222 QSettings teamfile(fileName, QSettings::IniFormat, 0); |
227 teamfile.setIniCodec("UTF-8"); |
223 teamfile.setIniCodec("UTF-8"); |
228 teamfile.setValue("Team/Name", m_name); |
224 teamfile.setValue("Team/Name", m_name); |
229 teamfile.setValue("Team/Grave", m_grave); |
225 teamfile.setValue("Team/Grave", m_grave); |
230 teamfile.setValue("Team/Fort", m_fort); |
226 teamfile.setValue("Team/Fort", m_fort); |
231 teamfile.setValue("Team/Voicepack", m_voicepack); |
227 teamfile.setValue("Team/Voicepack", m_voicepack); |
232 teamfile.setValue("Team/Flag", m_flag); |
228 teamfile.setValue("Team/Flag", m_flag); |
233 teamfile.setValue("Team/Difficulty", m_difficulty); |
229 teamfile.setValue("Team/Difficulty", m_difficulty); |
234 teamfile.setValue("Team/Rounds", m_rounds); |
230 |
235 teamfile.setValue("Team/Wins", m_wins); |
|
236 teamfile.setValue("Team/CampaignProgress", m_campaignProgress); |
|
237 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
231 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
238 { |
232 { |
239 QString hh = QString("Hedgehog%1/").arg(i); |
233 QString hh = QString("Hedgehog%1/").arg(i); |
240 teamfile.setValue(hh + "Name", m_hedgehogs[i].Name); |
234 teamfile.setValue(hh + "Name", m_hedgehogs[i].Name); |
241 teamfile.setValue(hh + "Hat", m_hedgehogs[i].Hat); |
235 teamfile.setValue(hh + "Hat", m_hedgehogs[i].Hat); |
242 teamfile.setValue(hh + "Rounds", m_hedgehogs[i].Rounds); |
|
243 teamfile.setValue(hh + "Kills", m_hedgehogs[i].Kills); |
|
244 teamfile.setValue(hh + "Deaths", m_hedgehogs[i].Deaths); |
|
245 teamfile.setValue(hh + "Suicides", m_hedgehogs[i].Suicides); |
|
246 } |
236 } |
247 for(int i = 0; i < BINDS_NUMBER; i++) |
237 for(int i = 0; i < BINDS_NUMBER; i++) |
248 teamfile.setValue(QString("Binds/%1").arg(m_binds[i].action), m_binds[i].strbind); |
238 { |
|
239 if(QString(m_binds[i].action) != QString("!MULTI")) |
|
240 teamfile.setValue(QString("Binds/%1").arg(m_binds[i].action), m_binds[i].strbind); |
|
241 } |
249 for(int i = 0; i < MAX_ACHIEVEMENTS; i++) |
242 for(int i = 0; i < MAX_ACHIEVEMENTS; i++) |
250 if(achievements[i][0][0]) |
243 if(achievements[i][0][0]) |
251 teamfile.setValue(QString("Achievements/%1").arg(achievements[i][0]), AchievementProgress[i]); |
244 teamfile.setValue(QString("Achievements/%1").arg(achievements[i][0]), AchievementProgress[i]); |
252 else |
245 else |
253 break; |
246 break; |
|
247 |
254 return true; |
248 return true; |
255 } |
249 } |
256 |
250 |
257 QStringList HWTeam::teamGameConfig(quint32 InitHealth) const |
251 QStringList HWTeam::teamGameConfig(quint32 InitHealth) const |
258 { |
252 { |
259 QStringList sl; |
253 QStringList sl; |
|
254 QString cmdAddHog = "eaddhh"; |
|
255 |
260 if (m_isNetTeam) |
256 if (m_isNetTeam) |
261 { |
257 { |
262 sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(QString(QCryptographicHash::hash(m_owner.toUtf8(), QCryptographicHash::Md5).toHex()))); |
258 sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(QString(QCryptographicHash::hash(m_owner.toUtf8(), QCryptographicHash::Md5).toHex()))); |
263 sl.push_back("erdriven"); |
259 sl.push_back("erdriven"); |
264 } |
260 } |
265 else sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash)); |
261 else |
|
262 { |
|
263 if (m_isMissionTeam) |
|
264 { |
|
265 sl.push_back(QString("esetmissteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash)); |
|
266 cmdAddHog = "eaddmisshh"; |
|
267 } |
|
268 else |
|
269 { |
|
270 sl.push_back(QString("eaddteam %3 %1 %2").arg(qcolor().rgb() & 0xffffff).arg(m_name).arg(playerHash)); |
|
271 } |
|
272 } |
266 |
273 |
267 sl.push_back(QString("egrave " + m_grave)); |
274 sl.push_back(QString("egrave " + m_grave)); |
268 sl.push_back(QString("efort " + m_fort)); |
275 sl.push_back(QString("efort " + m_fort)); |
269 sl.push_back(QString("evoicepack " + m_voicepack)); |
276 sl.push_back(QString("evoicepack " + m_voicepack)); |
270 sl.push_back(QString("eflag " + m_flag)); |
277 sl.push_back(QString("eflag " + m_flag)); |
271 |
278 |
272 if (!m_isNetTeam) |
279 if(!m_owner.isEmpty()) |
273 for(int i = 0; i < BINDS_NUMBER; i++) |
280 sl.push_back(QString("eowner ") + m_owner); |
274 if(!m_binds[i].strbind.isEmpty()) |
|
275 sl.push_back(QString("ebind " + m_binds[i].strbind + " " + m_binds[i].action)); |
|
276 |
281 |
277 for (int t = 0; t < m_numHedgehogs; t++) |
282 for (int t = 0; t < m_numHedgehogs; t++) |
278 { |
283 { |
279 sl.push_back(QString("eaddhh %1 %2 %3") |
284 sl.push_back(QString(cmdAddHog + " %1 %2 %3") |
280 .arg(QString::number(m_difficulty), |
285 .arg(QString::number(m_difficulty), |
281 QString::number(InitHealth), |
286 QString::number(InitHealth), |
282 m_hedgehogs[t].Name)); |
287 m_hedgehogs[t].Name)); |
283 sl.push_back(QString("ehat %1") |
288 sl.push_back(QString("ehat %1") |
284 .arg(m_hedgehogs[t].Hat)); |
289 .arg(m_hedgehogs[t].Hat)); |
285 } |
290 } |
286 return sl; |
291 return sl; |
287 } |
292 } |
288 |
293 |
|
294 |
|
295 void HWTeam::setNetTeam(bool isNetTeam) |
|
296 { |
|
297 m_isNetTeam = isNetTeam; |
|
298 } |
|
299 |
289 bool HWTeam::isNetTeam() const |
300 bool HWTeam::isNetTeam() const |
290 { |
301 { |
291 return m_isNetTeam; |
302 return m_isNetTeam; |
292 } |
303 } |
293 |
304 |
|
305 void HWTeam::setMissionTeam(bool isMissionTeam) |
|
306 { |
|
307 m_isMissionTeam = isMissionTeam; |
|
308 } |
|
309 |
|
310 bool HWTeam::isMissionTeam() const |
|
311 { |
|
312 return m_isMissionTeam; |
|
313 } |
294 |
314 |
295 bool HWTeam::operator==(const HWTeam& t1) const |
315 bool HWTeam::operator==(const HWTeam& t1) const |
296 { |
316 { |
297 return m_name==t1.m_name; |
317 return m_name==t1.m_name; |
298 } |
318 } |