-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAllianceCore.cpp
More file actions
444 lines (404 loc) · 9.72 KB
/
AllianceCore.cpp
File metadata and controls
444 lines (404 loc) · 9.72 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//
// AllianceCore.cpp
// Project Spitfire
//
// Copyright (c) 2013 Daizee (rensiadz at gmail dot com)
//
// This file is part of Spitfire.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "stdafx.h"
#include "AllianceCore.h"
#include "Alliance.h"
#include "Server.h"
#include "Client.h"
AllianceCore::AllianceCore(Server * server)
{
m_main = server;
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
m_alliances[i] = 0;
}
}
AllianceCore::~AllianceCore()
{
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i])
delete m_alliances[i];
}
}
void AllianceCore::DeleteAlliance(Alliance * alliance)
{
int32_t found = 0;
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i])
{
if (m_alliances[i]->m_allianceid != alliance->m_allianceid)
{
if (m_alliances[i]->IsAlly(alliance->m_allianceid))
m_alliances[i]->UnAlly(alliance->m_allianceid);
if (m_alliances[i]->IsNeutral(alliance->m_allianceid))
m_alliances[i]->UnNeutral(alliance->m_allianceid);
if (m_alliances[i]->IsEnemy(alliance->m_allianceid))
m_alliances[i]->UnEnemy(alliance->m_allianceid);
list<Client*>::iterator playeriter;
for (playeriter = m_main->players.begin(); playeriter != m_main->players.end(); ++playeriter)
{
Client * client = *playeriter;
if (client)
{
if ((client->m_allianceapply == alliance->m_name))
client->m_allianceapply = "";
}
}
}
else
{
found = i;
}
}
}
alliance->m_invites.clear();
delete m_alliances[found];
m_alliances[found] = 0;
}
Alliance * AllianceCore::CreateAlliance(string name, int64_t ownerid, int64_t allianceid)
{
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i] == 0)
{
m_alliances[i] = new Alliance(m_main, name, ownerid);
if (allianceid == 0)
m_alliances[i]->m_allianceid = m_main->m_allianceid++;
else
m_alliances[i]->m_allianceid = allianceid;
return m_alliances[i];
}
}
return 0;
}
bool AllianceCore::JoinAlliance(uint32_t allianceid, Client * client)
{
if (client->m_allianceid > 0)
{
return false; //already in an alliance
}
Alliance * alliance = AllianceById(allianceid);
if (alliance == (Alliance*)-1)
{
return false; //alliance doesn't exist
}
if (alliance->AddMember(client->m_accountid, DEF_ALLIANCEMEMBER))
{
client->m_allianceid = alliance->m_allianceid;
client->m_alliancename = alliance->m_name;
client->m_alliancerank = DEF_ALLIANCEMEMBER;
client->PlayerUpdate();
return true;
}
return false;
}
bool AllianceCore::RemoveFromAlliance(uint32_t allianceid, Client * client)
{
if (client->m_allianceid <= 0)
{
return false; //not in an alliance
}
Alliance * alliance = AllianceById(allianceid);
if (alliance == (Alliance*)-1)
{
return false; //alliance doesn't exist
}
alliance->RemoveMember(client->m_accountid);
client->m_allianceid = 0;
client->m_alliancename = "";
client->m_alliancerank = 0;
client->PlayerUpdate();
return true;
}
bool AllianceCore::SetRank(uint32_t allianceid, Client * client, int8_t rank)
{
if (client->m_allianceid <= 0)
{
return false; //no alliance id given
}
Alliance * alliance = AllianceById(allianceid);
if (alliance == (Alliance*)-1)
{
return false; //alliance doesn't exist
}
for (int i = 0; i < alliance->m_members.size(); ++i)
{
if (alliance->m_members[i].clientid == client->m_accountid)
{
client->m_alliancerank = rank;
alliance->m_members[i].rank = rank;
return true;
}
}
return false;
}
bool AllianceCore::CheckName(char * name)
{
char * tempname;
tempname = new char[strlen(name)+1];
memset(tempname, 0, strlen(name)+1);
for (int i = 0; i < strlen(name); ++i)
tempname[i] = tolower(name[i]);
if (strstr(tempname, "penis"))
return false;
if (strstr(tempname, "cock"))
return false;
if (strstr(tempname, "dick"))
return false;
if (strstr(tempname, "asshole"))
return false;
if (strstr(tempname, "fuck"))
return false;
if (strstr(tempname, "shit"))
return false;
if (strstr(tempname, "bitch"))
return false;
if (strstr(tempname, "tits"))
return false;
if (strstr(tempname, "cunt"))
return false;
if (strstr(tempname, "testicle"))
return false;
if (strstr(tempname, "queer"))
return false;
if (strstr(tempname, "fag"))
return false;
if (strstr(tempname, "gay"))
return false;
if (strstr(tempname, "homo"))
return false;
if (strstr(tempname, "whore"))
return false;
if (strstr(tempname, "nigger"))
return false;
return true;
}
int16_t AllianceCore::GetRelation(int64_t client1, int64_t client2)
{
if (client1 == client2)
return DEF_SELFRELATION;
Client * c1 = m_main->GetClient(client1);
Client * c2 = m_main->GetClient(client2);
if (!c1 || !c2 || c1->m_allianceid < 0 || c2->m_allianceid < 0)
return DEF_NORELATION;
if (c1->m_allianceid == c2->m_allianceid)
return DEF_ALLIANCE;
if (c1->m_allianceid <= 0 || c2->m_allianceid <= 0)
return DEF_NORELATION;
Alliance * a1 = m_alliances[c1->m_allianceid];
Alliance * a2 = m_alliances[c2->m_allianceid];
if (a1->IsEnemy(c2->m_allianceid))
return DEF_ENEMY;
else if (a1->IsAlly(c2->m_allianceid))
return DEF_ALLY;
else if (a1->IsNeutral(c2->m_allianceid))
return DEF_NEUTRAL;
else
return DEF_NORELATION;
}
bool comparemembers(stAlliance first, stAlliance second)
{
if (first.members > second.members)
return true;
else
return false;
}
bool compareprestige(stAlliance first, stAlliance second)
{
if (first.prestige > second.prestige)
return true;
else
return false;
}
bool comparehonor(stAlliance first, stAlliance second)
{
if (first.honor > second.honor)
return true;
else
return false;
}
void AllianceCore::SortAlliances()
{
m_membersrank.clear();
m_prestigerank.clear();
m_honorrank.clear();
stAlliance stmem;
stAlliance stpre;
stAlliance sthon;
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i])
{
sthon.honor = stpre.prestige = 0;
stmem.id = stpre.id = sthon.id = m_alliances[i]->m_allianceid;
stmem.members = m_alliances[i]->m_members.size();
m_alliances[i]->m_citycount = 0;
for (int k = 0; k < m_alliances[i]->m_members.size(); ++k)
{
Client * client = m_main->GetClient(m_alliances[i]->m_members[k].clientid);
if (client)
{
sthon.honor += client->m_honor;
stpre.prestige += client->m_prestige;
m_alliances[i]->m_citycount += client->m_citycount;
}
}
stmem.ref = m_alliances[i];
stpre.ref = m_alliances[i];
sthon.ref = m_alliances[i];
m_membersrank.push_back(stmem);
m_prestigerank.push_back(stpre);
m_honorrank.push_back(sthon);
}
}
m_membersrank.sort(comparemembers);
m_prestigerank.sort(compareprestige);
m_honorrank.sort(comparehonor);
list<stAlliance>::iterator iter;
int num = 1;
for ( iter = m_membersrank.begin() ; iter != m_membersrank.end(); ++iter )
{
iter->rank = num;
iter->ref->m_membersrank = num++;
}
num = 1;
for ( iter = m_prestigerank.begin() ; iter != m_prestigerank.end(); ++iter )
{
iter->rank = num;
iter->ref->m_prestigerank = num++;
iter->ref->m_prestige = iter->prestige;
}
num = 1;
for ( iter = m_honorrank.begin() ; iter != m_honorrank.end(); ++iter )
{
iter->rank = num;
iter->ref->m_honorrank = num++;
iter->ref->m_honor = iter->honor;
}
}
Alliance * AllianceCore::AllianceById(uint32_t id)
{
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i] && m_alliances[i]->m_allianceid == id)
{
return m_alliances[i];
}
}
return (Alliance*)-1;
}
Alliance * AllianceCore::AllianceByName(string name)
{
for (int i = 0; i < DEF_MAXALLIANCES; ++i)
{
if (m_alliances[i] && m_alliances[i]->m_name == name)
{
return m_alliances[i];
}
}
return (Alliance*)-1;
}
/*
struct stHero
{
int32_t m_id;
int16_t m_level;
int8_t m_loyalty;
int16_t m_management;
int16_t m_managementbuffadded;
int16_t m_power;
int16_t m_powerbuffadded;
int16_t m_stratagem;
int16_t m_stratagembuffadded;
int8_t m_status;
};
struct stResources
{
double food;
double stone;
double iron;
double wood;
double gold;
};
struct stForts
{
int32_t traps;
int32_t abatis;
int32_t towers;
int32_t logs;
int32_t trebs;
};
struct stTroops
{
int32_t worker;
int32_t warrior;
int32_t scout;
int32_t pike;
int32_t sword;
int32_t archer;
int32_t cavalry;
int32_t cataphract;
int32_t transporter;
int32_t ballista;
int32_t ram;
int32_t catapult;
};
struct stResult
{
stResources resources;
bool attackerwon;
};
struct stBuff
{
string id;
double endtime;
} m_buffs;
struct stResearch
{
int16_t level;
double endtime;
} m_research;
struct stAttacker
{
stTroops troops;
stBuff ** buffs;
stResearch ** techs;
PlayerCity * city;
stHero hero;
};
struct stDefender
{
stTroops troops;
stForts forts;
stBuff ** buffs;
stResearch ** techs;
PlayerCity * city;
stHero hero;
};
stResult * CalculateBattle(stAttacker & attacker, stDefender & defender)
{
}*/