-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRearrangePlayers.cpp
More file actions
175 lines (147 loc) · 5.42 KB
/
RearrangePlayers.cpp
File metadata and controls
175 lines (147 loc) · 5.42 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
#include "RearrangePlayers.h"
#include "ui_RearrangePlayers.h"
#include <QDebug>
#include <QMessageBox>
#include <QRandomGenerator>
RearrangePlayers::RearrangePlayers(QWidget *parent) :
QDialog(parent),
ui(new Ui::RearrangePlayers)
{
ui->setupUi(this);
numberOfPlayersInEachTeam = MAX_PLAYERS/3;
populateListWithPlayers();
if(gameInfo.getNumberOfTeams() == 2)
{
ui->list_Team3->setVisible(false);
ui->label_Team3->setVisible(false);
}
ui->label_TheBench->setVisible(false);
ui->list_Bench->setVisible(false);
}
RearrangePlayers::~RearrangePlayers()
{
delete ui;
}
void RearrangePlayers::resizeEvent(QResizeEvent*)
{
reSizeListWidgetButtonWidth();
}
void RearrangePlayers::on_btn_Close_clicked()
{
//Check for too many players in any team
bool tooManyPlayers = false;
if(ui->list_Team1->count() > numberOfPlayersInEachTeam) tooManyPlayers = true;
if(ui->list_Team2->count() > numberOfPlayersInEachTeam) tooManyPlayers = true;
if(ui->list_Team3->count() > numberOfPlayersInEachTeam) tooManyPlayers = true;
if(tooManyPlayers)
{
QMessageBox::critical(0,"Error","There are too many players in one of the teams.\nPlease fix and try again.");
return;
}
else
{
reAssignPlayerData();
close();
}
}
void RearrangePlayers::clearPlayerLists()
{
ui->list_Team1->clear();
ui->list_Team2->clear();
ui->list_Team3->clear();
}
void RearrangePlayers::populateListWithPlayers()
{
for(int index = 1;index < (numberOfPlayersInEachTeam+1); index++)
{
ui->list_Team1->addItem(playerInfo[index].getPlayerName());
ui->list_Team1->item(index-1)->setTextAlignment(Qt::AlignCenter);
ui->list_Team1->item(index-1)->setData(Qt::UserRole, index);
ui->list_Team2->addItem(playerInfo[index+8].getPlayerName());
ui->list_Team2->item(index-1)->setTextAlignment(Qt::AlignCenter);
ui->list_Team2->item(index-1)->setData(Qt::UserRole, index+8);
ui->list_Team3->addItem(playerInfo[index+16].getPlayerName());
ui->list_Team3->item(index-1)->setTextAlignment(Qt::AlignCenter);
ui->list_Team3->item(index-1)->setData(Qt::UserRole, index+16);
// if(index < 5)
// {
// ui->list_Bench->addItem("Sub " + QString::number(index));
// ui->list_Bench->item(index-1)->setTextAlignment(Qt::AlignCenter);
// ui->list_Bench->item(index-1)->setData(Qt::UserRole, index+24);
// }
}
reSizeListWidgetButtonWidth();
}
void RearrangePlayers::reSizeListWidgetButtonWidth()
{
int minSize = (ui->list_Team1->width() / numberOfPlayersInEachTeam ) - 11;
//reduce the size slightly so that you can see a bit of any excess players in a team.
minSize--;
for(int index = 0; index < ui->list_Team1->count(); index++)
{
ui->list_Team1->item(index)->setSizeHint(QSize(minSize, ui->list_Team1->sizeHint().height() ));
}
for(int index = 0; index < ui->list_Team2->count(); index++)
{
ui->list_Team2->item(index)->setSizeHint(QSize(minSize, ui->list_Team2->sizeHint().height() ));
}
for(int index = 0; index < ui->list_Team3->count(); index++)
{
ui->list_Team3->item(index)->setSizeHint(QSize(minSize, ui->list_Team3->sizeHint().height() ));
}
for(int index = 0; index < ui->list_Bench->count(); index++)
{
ui->list_Bench->item(index)->setSizeHint(QSize(minSize, ui->list_Bench->sizeHint().height() ));
}
}
void RearrangePlayers::assignPlayersToTeams(bool isRandom)
{
//TODO: Need to modify to only randomise selected players
// Also need to respect Team balances (eg. 5 in Team1 and 3 in Team2).
//Pseudo code
// Check how many players on Team1/2/3
// Randomise only selected players
// Check Team1/2/3 numbers are correct, if not force some to move.
int numberOfPlayersToShuffle = MAX_PLAYERS;
if(gameInfo.getNumberOfTeams() == 2) numberOfPlayersToShuffle = 16;
// Create an array and initialise all as Zer0.
std::vector<int> newPlayerNumber(numberOfPlayersToShuffle + 1);
for(int index = 1; index <= numberOfPlayersToShuffle; index++)
{
newPlayerNumber[index] = index;
}
//Shuffle the array
if(isRandom) std::random_shuffle(std::begin(newPlayerNumber)+1, std::end(newPlayerNumber));
// Update the player numbers and refresh the grid.
for(int index = 1; index <= numberOfPlayersToShuffle; index++)
{
if (isRandom) playerInfo[0].copyPlayerSettings(index, newPlayerNumber[index] );
else playerInfo[0].copyPlayerSettings(index, playerInfo[index].getPlayerIndex() );
}
playerInfo[0].moveAllPlayersFromTempToMain();
clearPlayerLists();
populateListWithPlayers();
}
void RearrangePlayers::reAssignPlayerData()
{
int playerToCopy = 0;
for(int index = 1; index <= MAX_PLAYERS; index++)
{
if (index <= numberOfPlayersInEachTeam ) playerToCopy = ui->list_Team1->item(index -1)->data(Qt::UserRole).toInt();
else if (index <= numberOfPlayersInEachTeam * 2) playerToCopy = ui->list_Team2->item(index -9)->data(Qt::UserRole).toInt();
else if (index <= numberOfPlayersInEachTeam * 3) playerToCopy = ui->list_Team3->item(index-17)->data(Qt::UserRole).toInt();
playerInfo[0].copyPlayerSettings(playerToCopy, index);
}
playerInfo[0].moveAllPlayersFromTempToMain();
emit dataUpdated(); //This updates the buttons in PlayerWindow.cpp
// TODO: Clean out the PlayerInfoTemp database
// TODO: Also reset/disable missing players with default data and set as not in the game !
}
void RearrangePlayers::on_btn_Randomise_clicked()
{
assignPlayersToTeams(true);
}
void RearrangePlayers::on_btn_SetDefaultTeams_clicked()
{
assignPlayersToTeams(false);
}