1 /*******************************************************************
2 Jan Cermak | johniez | aspt.johniez.com
3 Creative Commons 2.5 Attribution-NonCommercial-NoDerivs 2.5 license
4 http://creativecommons.org/licenses/by-nc-nd/2.5/legalcode
5 *******************************************************************/
7 #include "performance.h"
9 void PerformanceTab::init() {
10 QSettings settings("config/formation.ini", QSettings::IniFormat);
12 format = new QComboBox();
13 format->addItem("---", QVariant(0));
14 format->addItem("5-4-1", QVariant(1));
15 format->addItem("5-3-2", QVariant(2));
16 format->addItem("4-3-3", QVariant(3));
17 format->addItem("4-4-2", QVariant(4));
18 format->addItem("4-5-1", QVariant(5));
19 format->addItem("3-4-3", QVariant(6));
20 format->addItem("3-5-2", QVariant(7));
21 format->setToolTip(tr("Team formation."));
23 reorderPl = new QPushButton(tr("reorder players"));
24 reorderPl->setToolTip(tr("This button set on positions with wrong player type this player, who plays there (LM on LM post..)."));
26 aggression = new QComboBox();
27 for(int i=0; i<=100; i+=5) {
28 aggression->addItem(tr("aggression ") + QString().setNum(i) + "%", i);
30 aggression->setCurrentIndex(settings.value("aggr", 0).toInt()/5);
31 aggression->setToolTip(tr("Team aggression, same as you set in match."));
32 aggress = settings.value("aggr", 0).toInt();
34 home = new QComboBox();
35 home->addItem(tr("play home"), 0.0);
36 home->addItem(tr("play away"), 0.1);
37 home->setToolTip(tr("Where you will play the match."));
39 for(int j=0; j<11; ++j) {
40 plCont[j] = new QHBoxLayout();
41 player[j] = new QComboBox();
42 orders[j] = new QComboBox();
43 orders[j]->setToolTip(tr("'Ind. order' mean one of: Normal, Play along, Play along wing, Play it out, Passing, Own chance, Targetman and Free role."));
44 orders[j]->addItem(tr("ind. order"), 0);
45 for(int i=0; i<playerCnt; ++i) {
47 int age = pls[i].age.toInt();
48 double perf = ((pls[i].morale.toDouble()+pls[i].fitness.toDouble())/200)*pls[i].bestRate.toDouble();
50 oldPenale = (age-30)*3; // 3 points penale per each year over 30
53 player[j]->addItem("[" + pls[i].position + "] " + pls[i].fname + " " + pls[i].lname, QVariant(perf));
55 player[j]->setCurrentIndex(settings.value("Player/"+QString().setNum(j), 0).toInt());
56 postLabel[j] = new QLabel("pos");
57 plPerfLabel[j] = new QLabel(QString().setNum(player[j]->itemData(player[j]->currentIndex()).toDouble(), 'f', 2));
58 plCont[j]->addWidget(postLabel[j]);
59 plCont[j]->addWidget(player[j]);
60 plCont[j]->addWidget(orders[j]);
61 plCont[j]->addWidget(plPerfLabel[j]);
64 container = new QHBoxLayout();
66 container->addWidget(format);
67 container->addWidget(reorderPl);
68 container->addWidget(aggression);
69 container->addWidget(home);
70 mainLayout->addLayout(container);
71 for(int j=0; j<11; ++j) mainLayout->addLayout(plCont[j]);
73 cButt = new QPushButton(tr("count team performance"));
74 mainLayout->addWidget(cButt);
76 result = new QLabel(tr("Set formation, aggression and players.<br>") + "<span style=\"color:red\">" + tr("Results could be inaccurate!") + "</span><br>" + tr("This version do not penalize players at wrong position!<br>See http://flyspray.johniez.com/task/14"));
77 mainLayout->addWidget(result);
79 format->setCurrentIndex(settings.value("form", 0).toInt());
80 formatChange(settings.value("form", 0).toInt());
82 connect(format, SIGNAL(currentIndexChanged(int)), this, SLOT(formatChange(int)));
83 connect(aggression, SIGNAL(currentIndexChanged(int)), this, SLOT(aggressChange(int)));
84 connect(cButt, SIGNAL(clicked()), this, SLOT(recount()));
85 for(int i=0; i<11; i++) {
86 connect(player[i], SIGNAL(currentIndexChanged(int)), this, SLOT(playerChange(int)));
88 connect(reorderPl, SIGNAL(clicked()), this, SLOT(reorderPlayers()));
92 PerformanceTab::PerformanceTab(QWidget *parent): QWidget(parent), defs(0), mids(0), fors(0) {
93 mainLayout = new QVBoxLayout;
94 setLayout(mainLayout);
98 PerformanceTab::~PerformanceTab() {
99 for(int i=0; i<11; ++i) {
102 delete plPerfLabel[i];
116 void PerformanceTab::dataChanged() {
117 container->removeWidget(format);
119 container->removeWidget(aggression);
121 container->removeWidget(home);
123 container->removeWidget(reorderPl);
125 mainLayout->removeWidget(reinterpret_cast<QWidget*>(container));
128 for(int i=0; i<11; ++i) {
129 mainLayout->removeWidget(reinterpret_cast<QWidget*>(plCont[i]));
132 delete plPerfLabel[i];
136 mainLayout->removeWidget(cButt);
138 mainLayout->removeWidget(result);
140 mainLayout->update();
144 void PerformanceTab::postLabels() {
147 for(int i=0; i<11; ++i) {
148 postLabel[i]->setText("??");
151 postLabel[0]->setText("GK");
152 postLabel[1]->setText("LB");
154 orders[1]->addItem(tr("ind. order"), 0);
155 orders[1]->addItem(tr("offensive"), 2);
156 postLabel[2]->setText("CB");
159 postLabel[3]->setText("SW");
161 orders[3]->addItem(tr("ind. order"), 0);
162 postLabel[4]->setText("CB");
164 orders[4]->addItem(tr("ind. order"), 0);
165 postLabel[5]->setText("RB");
167 orders[5]->addItem(tr("ind. order"), 0);
168 orders[5]->addItem(tr("offensive"), 2);
172 postLabel[3]->setText("CB");
174 orders[3]->addItem(tr("ind. order"), 0);
175 postLabel[4]->setText("RB");
177 orders[4]->addItem(tr("ind. order"), 0);
178 orders[4]->addItem(tr("offensive"), 2);
182 postLabel[3]->setText("RB");
184 orders[3]->addItem(tr("ind. order"), 0);
185 orders[3]->addItem(tr("offensive"), 2);
191 postLabel[LMpos]->setText("LM");
192 postLabel[LMpos+1]->setText("CM");
193 postLabel[LMpos+2]->setText("CM");
194 postLabel[LMpos+3]->setText("CM");
195 postLabel[LMpos+4]->setText("RM");
198 postLabel[LMpos]->setText("LM");
199 postLabel[LMpos+1]->setText("CM");
200 postLabel[LMpos+2]->setText("CM");
201 postLabel[LMpos+3]->setText("RM");
204 postLabel[LMpos]->setText("LM");
205 postLabel[LMpos+1]->setText("CM");
206 postLabel[LMpos+2]->setText("RM");
208 for(int i=LMpos; i<LMpos+midsC; ++i) {
210 orders[i]->addItem(tr("ind. order"), 0);
211 orders[i]->addItem(tr("defensive"), 1);
212 orders[i]->addItem(tr("offensive"), 2);
216 postLabel[8]->setText("LF");
218 orders[8]->addItem(tr("ind. order"), 0);
219 orders[8]->addItem(tr("defensive"), 1);
220 postLabel[9]->setText("CF");
222 orders[9]->addItem(tr("ind. order"), 0);
223 postLabel[10]->setText("RF");
225 orders[10]->addItem(tr("ind. order"), 0);
226 orders[10]->addItem(tr("defensive"), 1);
229 postLabel[9]->setText("CF");
231 orders[9]->addItem(tr("ind. order"), 0);
232 postLabel[10]->setText("CF");
234 orders[10]->addItem(tr("ind. order"), 0);
237 postLabel[10]->setText("CF");
239 orders[10]->addItem(tr("ind. order"), 0);
244 void PerformanceTab::countPerf() {
249 for(int i=0; i<=defsC; ++i) {
250 perf = player[i]->itemData(player[i]->currentIndex()).toDouble();
251 if(orders[i]->itemData(orders[i]->currentIndex()).toInt()==0) {
254 defs += (0.78 * perf);
255 mids += (0.22 * perf);
258 for(int i=defsC+1; i<=defsC+midsC; ++i) {
259 perf = player[i]->itemData(player[i]->currentIndex()).toDouble();
260 if(orders[i]->itemData(orders[i]->currentIndex()).toInt()==0) {
262 } else if(orders[i]->itemData(orders[i]->currentIndex()).toInt()==1) {
263 defs += (0.22 * perf);
264 mids += (0.78 * perf);
266 fors += (0.22 * perf);
267 mids += (0.78 * perf);
270 for(int i=defsC+midsC+1; i<11; ++i) {
271 perf = player[i]->itemData(player[i]->currentIndex()).toDouble();
272 if(orders[i]->itemData(orders[i]->currentIndex()).toInt()==0) {
275 fors += (0.78 * perf);
276 mids += (0.22 * perf);
281 void PerformanceTab::recount() {
282 double homeMod = home->itemData(home->currentIndex()).toDouble();
283 double pre = (1.0+((double)aggress/1000))/(7.0 - (club.spirit.toDouble()/1000) + homeMod);
287 if(!defsC) text = tr("Choose a formation");
292 text = "<table cellpadding=\"1\" width=\"100%\"><tr><td>----</td><td style=\"color:blue;\" align=\"right\">"+tr("Long Ball")+"</td><td align=\"right\">"+tr("Without tactics")+"</td><td style=\"color:blue;\" align=\"right\">"+tr("Kick and rush")+"</td><td align=\"right\">"+tr("Passing")+"</td><td style=\"color:blue;\" align=\"right\">"+tr("Defensive wall")+"</td></tr><tr><td>D</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(O*1.05,'f',2)+"</td><td align=\"right\">"+QString().setNum(O,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(O*0.9,'f',2)+"</td><td align=\"right\">"+QString().setNum(O*0.9,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(O*1.08,'f',2)+"</td></tr><tr><td>M</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(M*0.9,'f',2)+"</td><td align=\"right\">"+QString().setNum(M,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(M*0.9,'f',2)+"</td><td align=\"right\">"+QString().setNum(M*1.1,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(M*0.9,'f',2)+"</td></tr><tr><td>F</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(F*1.05,'f',2)+"</td><td align=\"right\">"+QString().setNum(F,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(F*1.27,'f',2)+"</td><td align=\"right\">"+QString().setNum(F*0.9,'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum(F*0.9,'f',2)+"</td></tr><tr><td>Sum</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum((O*1.05+M*0.9+F*1.05),'f',2)+"</td><td align=\"right\">"+QString().setNum((O+M+F),'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum((O*0.9+M*0.9+F*1.27),'f',2)+"</td><td align=\"right\">"+QString().setNum((O*0.9+M*1.1+F*0.9),'f',2)+"</td><td style=\"color:blue;\" align=\"right\">"+QString().setNum((O*1.08+M*0.9+F*0.9),'f',2)+"</td></tr></table>";
294 result->setText(text);
297 void PerformanceTab::aggressChange(int a) {
299 QSettings settings("config/formation.ini", QSettings::IniFormat);
300 settings.setValue("aggr", aggress);
304 void PerformanceTab::formatChange(int t) {
305 QSettings settings("config/formation.ini", QSettings::IniFormat);
306 if(t<8 && t>=0) { settings.setValue("form", t); settings.sync(); }
309 defsC = 0; midsC = 0; forsC = 0;
312 defsC = 5; midsC = 4; forsC = 1;
315 defsC = 5; midsC = 3; forsC = 2;
318 defsC = 4; midsC = 3; forsC = 3;
321 defsC = 4; midsC = 4; forsC = 2;
324 defsC = 4; midsC = 5; forsC = 1;
327 defsC = 3; midsC = 4; forsC = 3;
330 defsC = 3; midsC = 5; forsC = 2;
337 void PerformanceTab::playerChange(int id) {
338 id = 1; //avoid warning :-D
339 QSettings settings("config/formation.ini", QSettings::IniFormat);
340 for(int i=0; i<11; ++i) {
341 settings.setValue("Player/"+QString().setNum(i), player[i]->currentIndex());
342 plPerfLabel[i]->setText(QString().setNum(player[i]->itemData(player[i]->currentIndex()).toDouble(), 'f', 2)); //player[i]->itemData(player[i]->currentIndex()).toInt()
347 void PerformanceTab::reorderPlayers() {
350 for(i=1; i<=defsC; ++i) { // defenders
352 if(pls[player[i]->currentIndex()].position != "LB") {
353 player[i]->setCurrentIndex(findFirstOnPost("LB"));
355 } else if(i==defsC) { // RB
356 if(pls[player[i]->currentIndex()].position != "RB") {
357 player[i]->setCurrentIndex(findFirstOnPost("RB"));
359 // RB is last managed from defenders, should set finding from him
360 from = player[i]->currentIndex();
361 } else if(defsC == 5 && i==3) { // SW
362 if(pls[player[i]->currentIndex()].position != "SW") {
363 player[i]->setCurrentIndex(findFirstOnPost("SW"));
366 if(pls[player[i]->currentIndex()].position != "CB") {
367 player[i]->setCurrentIndex(findFirstOnPost("CB"));
371 for(i=defsC+1; i<=defsC+midsC; ++i) {
372 if(i==defsC+1) { // LM
373 if(pls[player[i]->currentIndex()].position != "LM") {
374 player[i]->setCurrentIndex(findFirstOnPost("LM"));
376 } else if(i==defsC+midsC) { // RM
377 if(pls[player[i]->currentIndex()].position != "RM") {
378 player[i]->setCurrentIndex(findFirstOnPost("RM"));
380 // RM is last managed from midfields, should set finding from him
381 from = player[i]->currentIndex();
383 if(pls[player[i]->currentIndex()].position != "CM") {
384 player[i]->setCurrentIndex(findFirstOnPost("CM"));
388 for(i=defsC+midsC+1; i<=defsC+midsC+forsC; ++i) {
390 if(i==defsC+midsC+1) { // LF
391 if(pls[player[i]->currentIndex()].position != "LF") {
392 player[i]->setCurrentIndex(findFirstOnPost("LF"));
394 } else if(i==10) { // RF
395 if(pls[player[i]->currentIndex()].position != "RF") {
396 player[i]->setCurrentIndex(findFirstOnPost("RF"));
399 } else { // always CF
400 if(pls[player[i]->currentIndex()].position != "CF") {
401 player[i]->setCurrentIndex(findFirstOnPost("CF"));
407 int PerformanceTab::findFirstOnPost(const QString &post) {
408 for(int i=0; i<playerCnt; ++i) {
409 if(post == pls[i].position) return i;