Optimised suboptimal code.
This commit is contained in:
parent
810ca0d9f5
commit
b5db91addb
5 changed files with 102 additions and 147 deletions
|
@ -20,19 +20,19 @@ Item {
|
|||
border.width: 0
|
||||
|
||||
color: {
|
||||
if (PlayerAreaModel.focusedPlayer == 0 && rouletteTable.numberOfBets == 1 && border.width != 3) {
|
||||
"#00000000"
|
||||
} else if (PlayerAreaModel.focusedPlayer == 0 && betType != 9) {
|
||||
"#00000000"
|
||||
} else if (rouletteTable.numberOfBets < 2 || border.width == 3) {
|
||||
if ((PlayerAreaModel.focusedPlayer == 0 && rouletteTable.numberOfBets < 1) || PlayerAreaModel.focusedPlayer != 0 || border.width == 3) {
|
||||
if (tapHandler.pressed) {
|
||||
"#88999999"
|
||||
} else if (hoverHandler.hovered) {
|
||||
"#88EEEEEE"
|
||||
} else {
|
||||
"#00000000"
|
||||
}
|
||||
/*
|
||||
* A bit of a mess
|
||||
* The left side of the OR is checking if the player is croupier, and if it is, if it hasn't made a bet that has to be a number.
|
||||
* Even if the above isn't true, it still highlights if it's hovering a bet so that it can remove the bet.
|
||||
*
|
||||
* The right side of the OR is similar logic but for normal players that can make two bets on any square of the board
|
||||
*/
|
||||
if (((PlayerAreaModel.focusedPlayer == 0 && betType == 9) && (rouletteTable.numberOfBets < 1 || border.width == 3)) ||
|
||||
(PlayerAreaModel.focusedPlayer != 0 && (rouletteTable.numberOfBets < 2 || border.width == 3))) {
|
||||
if (tapHandler.pressed) {
|
||||
"#88999999"
|
||||
} else if (hoverHandler.hovered) {
|
||||
"#88EEEEEE"
|
||||
} else {
|
||||
"#00000000"
|
||||
}
|
||||
|
@ -48,11 +48,12 @@ Item {
|
|||
id: tapHandler
|
||||
|
||||
onTapped: {
|
||||
if (PlayerAreaModel.focusedPlayer == 0 && rouletteTable.numberOfBets == 1 && parent.border.width != 3) {
|
||||
|
||||
} else if (PlayerAreaModel.focusedPlayer == 0 && betType != 9) {
|
||||
|
||||
}else if (rouletteTable.numberOfBets < 2 && parent.border.width == 0) {
|
||||
/*
|
||||
* Similar logic to what is used for setting the color attribute, but this time only go inside the "if" if a bet hasn't
|
||||
* been made on the square that was clicked.
|
||||
*/
|
||||
if (((PlayerAreaModel.focusedPlayer == 0 && betType == 9) && (rouletteTable.numberOfBets < 1 && parent.border.width != 3)) ||
|
||||
(PlayerAreaModel.focusedPlayer != 0 && (rouletteTable.numberOfBets < 2 && parent.border.width != 3))) {
|
||||
parent.border.width = 3
|
||||
|
||||
/* Don't show this dialog for the croupier */
|
||||
|
|
|
@ -4,10 +4,15 @@
|
|||
PlayerAreaModel::PlayerAreaModel(QObject *parent)
|
||||
{
|
||||
Player defaultValues;
|
||||
Bet defaultBets;
|
||||
|
||||
for(int i = 0; i < 8; i++){
|
||||
players.append(defaultValues);
|
||||
for (int ii = 0; ii < 2; ii++) {
|
||||
players[i].bets.append(defaultBets);
|
||||
}
|
||||
}
|
||||
/* This is set to avoid segfaults if the starting value is out of bounds */
|
||||
focusedPlayer_ = 1;
|
||||
}
|
||||
|
||||
|
@ -20,10 +25,10 @@ void PlayerAreaModel::setFocusedPlayer(int n)
|
|||
{
|
||||
focusedPlayer_ = n;
|
||||
Player player = players.at(n);
|
||||
BetType b1 = player.bet1.betType;
|
||||
BetType b2 = player.bet2.betType;
|
||||
uint n1 = player.bet1.betSpot;
|
||||
uint n2 = player.bet2.betSpot;
|
||||
BetType b1 = player.bets[0].betType;
|
||||
BetType b2 = player.bets[1].betType;
|
||||
uint n1 = player.bets[0].betSpot;
|
||||
uint n2 = player.bets[1].betSpot;
|
||||
emit focusedPlayerChanged(b1, n1, b2, n2);
|
||||
}
|
||||
|
||||
|
@ -41,44 +46,38 @@ void PlayerAreaModel::bet(BetType b, int n, int bet)
|
|||
{
|
||||
Player *player = &players[focusedPlayer_];
|
||||
|
||||
if (player->bet1.betType == NONE) {
|
||||
player->bet1.betType = b;
|
||||
player->bet1.betValue = bet;
|
||||
player->bet1.betSpot = n;
|
||||
/*
|
||||
* This is needed because the visual roulette board is inverted from the actual QML grid we use to represent it.
|
||||
* Here we use 35 because the grid is 0-indexed, but later we use 36 for convinience
|
||||
*/
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (player->bets[i].betType == NONE) {
|
||||
player->bets[i].betType = b;
|
||||
player->bets[i].betValue = bet;
|
||||
player->bets[i].betSpot = n;
|
||||
/*
|
||||
* This is needed because the visual roulette board is inverted from the
|
||||
* actual QML grid we use to represent it.
|
||||
* Here we use 35 because the grid is 0-indexed, but later we use 36 when it is 1-indexed.
|
||||
*/
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
}
|
||||
emit betChanged(b, n, bet);
|
||||
break;
|
||||
}
|
||||
emit betChanged(b, n, bet);
|
||||
} else if (player->bet2.betType == NONE) {
|
||||
player->bet2.betType = b;
|
||||
player->bet2.betValue = bet;
|
||||
player->bet2.betSpot = n;
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
}
|
||||
emit betChanged(b, n, bet);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerAreaModel::removeBet(BetType b, int n)
|
||||
{
|
||||
Player *player = &players[focusedPlayer_];
|
||||
if (player->bet1.betType == b) {
|
||||
player->bet1.betType = NONE;
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (player->bets[i].betType == b && player->bets[i].betSpot == n) {
|
||||
player->bets[i].betType = NONE;
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
}
|
||||
emit betRemoved(b, n);
|
||||
}
|
||||
emit betRemoved(b, n);
|
||||
} else if (player->bet2.betType == b) {
|
||||
player->bet2.betType = NONE;
|
||||
if (b == SINGLE && n <= 35) {
|
||||
n = 35 - n;
|
||||
}
|
||||
emit betRemoved(b, n);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,99 +88,61 @@ void PlayerAreaModel::cancelBet(BetType b, int n)
|
|||
|
||||
void PlayerAreaModel::payout()
|
||||
{
|
||||
if (players.at(0).bet1.betType == NONE) {
|
||||
if (players.at(0).bets[0].betType == NONE) {
|
||||
fprintf(stderr, "Select where the ball landed\n");
|
||||
} else {
|
||||
int rollSpot = 36 - players.at(0).bet1.betSpot;
|
||||
int rollSpot = 36 - players.at(0).bets[0].betSpot;
|
||||
|
||||
for (int i = 1; i < 8; i++) {
|
||||
int betType = players.at(i).bet1.betType;
|
||||
int betSpot;
|
||||
players[i].payout = 0;
|
||||
for (int ii = 0; ii < 2; ii++) {
|
||||
int betType = players.at(i).bets[ii].betType;
|
||||
int betSpot;
|
||||
|
||||
if (betType != NONE) {
|
||||
betSpot = players.at(i).bet1.betSpot;
|
||||
if (betType == SINGLE && betSpot <= 35) {
|
||||
betSpot = 36 - betSpot;
|
||||
} else {
|
||||
betSpot += 1;
|
||||
}
|
||||
if (betType != NONE) {
|
||||
betSpot = players.at(i).bets[ii].betSpot;
|
||||
|
||||
if (betType == SINGLE && betSpot == rollSpot) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 21;
|
||||
} else if (betType == RED && (((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == BLACK && !(((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == ODD && (rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == EVEN && !(rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == LOW && rollSpot <= 18 ) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == HIGH && rollSpot >= 19 && rollSpot < 37) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 2;
|
||||
} else if (betType == DOZEN) {
|
||||
if (rollSpot <= 12 * betSpot && rollSpot >= (12 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 3;
|
||||
if (betType == SINGLE && betSpot <= 35) {
|
||||
betSpot = 36 - betSpot;
|
||||
} else {
|
||||
betSpot += 1;
|
||||
}
|
||||
} else if (betType == STREET) {
|
||||
if (rollSpot <= 3 * betSpot && rollSpot >= (3 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 6;
|
||||
}
|
||||
} else if (betType == COLUMN) {
|
||||
if (betSpot == 1 && (((int64_t)1 << (rollSpot - 1)) & column1)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 3;
|
||||
} else if (betSpot == 2 && (((int64_t)1 << (rollSpot - 1)) & column2)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 3;
|
||||
} else if (betSpot == 3 && (((int64_t)1 << (rollSpot - 1)) & column3)) {
|
||||
players[i].payout += players.at(i).bet1.betValue * 3;
|
||||
|
||||
if (betType == SINGLE && betSpot == rollSpot) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 21;
|
||||
} else if (betType == RED && (((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == BLACK && !(((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == ODD && (rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == EVEN && !(rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == LOW && rollSpot <= 18 ) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == HIGH && rollSpot >= 19 && rollSpot < 37) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 2;
|
||||
} else if (betType == DOZEN) {
|
||||
printf("CHECK %d %d\n", betSpot, rollSpot);
|
||||
if (rollSpot <= 12 * betSpot && rollSpot >= (12 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 3;
|
||||
}
|
||||
} else if (betType == STREET) {
|
||||
if (rollSpot <= 3 * betSpot && rollSpot >= (3 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 6;
|
||||
}
|
||||
} else if (betType == COLUMN) {
|
||||
if (betSpot == 1 && (((int64_t)1 << (rollSpot - 1)) & column1)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 3;
|
||||
} else if (betSpot == 2 && (((int64_t)1 << (rollSpot - 1)) & column2)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 3;
|
||||
} else if (betSpot == 3 && (((int64_t)1 << (rollSpot - 1)) & column3)) {
|
||||
players[i].payout += players.at(i).bets[ii].betValue * 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
betType = players.at(i).bet2.betType;
|
||||
|
||||
if (betType != NONE) {
|
||||
betSpot = players.at(i).bet2.betSpot;
|
||||
if (betType == SINGLE && betSpot <= 35) {
|
||||
betSpot = 36 - betSpot;
|
||||
} else {
|
||||
betSpot += 1;
|
||||
}
|
||||
|
||||
if (betType == SINGLE && betSpot == rollSpot) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 21;
|
||||
} else if (betType == RED && (((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == BLACK && !(((int64_t)1 << (rollSpot - 1)) & redNumbers)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == ODD && (rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == EVEN && !(rollSpot % 2)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == LOW && rollSpot <= 18 ) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == HIGH && rollSpot >= 19 && rollSpot < 37) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 2;
|
||||
} else if (betType == DOZEN) {
|
||||
if (rollSpot <= 12 * betSpot && rollSpot >= (12 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 3;
|
||||
}
|
||||
} else if (betType == STREET) {
|
||||
if (rollSpot <= 3 * betSpot && rollSpot >= (3 * (betSpot - 1)) + 1) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 6;
|
||||
}
|
||||
} else if (betType == COLUMN) {
|
||||
if (betSpot == 1 && (((int64_t)1 << (rollSpot - 1)) & column1)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 3;
|
||||
} else if (betSpot == 2 && (((int64_t)1 << (rollSpot - 1)) & column2)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 3;
|
||||
} else if (betSpot == 3 && (((int64_t)1 << (rollSpot - 1)) & column3)) {
|
||||
players[i].payout += players.at(i).bet2.betValue * 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
emit results(players.at(i).playerName, i, players.at(i).payout);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
class PlayerAreaModel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
/* The name under which this "global variable" will be known in QML files */
|
||||
QML_NAMED_ELEMENT(PlayerAreaModel)
|
||||
QML_SINGLETON
|
||||
|
||||
|
@ -55,7 +56,6 @@ public slots:
|
|||
|
||||
|
||||
private:
|
||||
|
||||
struct Bet {
|
||||
uint betValue = 0;
|
||||
BetType betType = NONE;
|
||||
|
@ -64,8 +64,7 @@ private:
|
|||
|
||||
struct Player {
|
||||
QString playerName = "";
|
||||
Bet bet1;
|
||||
Bet bet2;
|
||||
QList<Bet> bets;
|
||||
int payout;
|
||||
};
|
||||
|
||||
|
|
15
Root.qml
15
Root.qml
|
@ -14,9 +14,9 @@ Window {
|
|||
Pane {
|
||||
anchors.fill: parent
|
||||
|
||||
/* Exists exclusively to remove focus from TextFields by clicking anywhere else on the screen */
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
|
||||
onClicked: focus = true
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,6 @@ Window {
|
|||
|
||||
Button {
|
||||
id: payoutButton
|
||||
highlighted: false
|
||||
onClicked: PlayerAreaModel.payout()
|
||||
enabled: false
|
||||
|
||||
|
@ -66,14 +65,10 @@ Window {
|
|||
implicitWidth: 150
|
||||
implicitHeight: 60
|
||||
opacity: {
|
||||
if (enabled == true) {
|
||||
if (parent.pressed) {
|
||||
0.5
|
||||
} else if (parent.hovered) {
|
||||
0.2
|
||||
} else {
|
||||
0.3
|
||||
}
|
||||
if (enabled == true && parent.pressed) {
|
||||
0.5
|
||||
} else if (enabled == true && parent.hovered) {
|
||||
0.2
|
||||
} else {
|
||||
0.3
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ Item {
|
|||
LayoutMirroring.enabled: true
|
||||
flow: GridLayout.TopToBottom
|
||||
rows: 3
|
||||
//rotation: 180
|
||||
|
||||
anchors {
|
||||
right: parent.right
|
||||
|
|
Loading…
Reference in a new issue