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