Bootstrap Buttons with Loading Progress Bars

20 Nov 2015

Here’s how to add loading bars to bootstrap buttons.

To start, you’ll need the following files:

progress_bar.css

progress_bar.js

In your header, you’ll need to include the above Javascript file (adapted from here)

You’ll also need jQuery and Bootstrap, of course.

<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<!-- Bootstrap CSS and JS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!-- Progress Bar CSS and JS -->
<link href="css/progress_bar.css" rel="stylesheet" />
<script src="js/progress_bar.js"></script>

Create a Bootstrap button in your HTML. We’ll attach a loading class to this so we can manipulate it in Javascript later.

<button class="btn btn-default btn-md progress-button" type="button" id="progressBtn">
Show Progress
</button>

Let’s go ahead and initialize the button.

$('#progressBtn')
            .progressInitialize() //initialize the progress-meter code
            .blur(); //remove focus from the button.

Now, here’s a real-life example of how to use this. When we call the function .progressIncrement() on $(‘#progressBtn’), we’ll specify an integer (which represents a percentage) to increment the progress by.

For example, .progressIncrement(50) would halfway-fill the progress bar.

Let’s try a simple loop.

var numberOfTries = 15;

/* Run a loop, incrementing the progress towards 100.
 * This loop will proportionally increment the request
 * (you can change the numberOfTries to anything less than 100,
 * and it'll do the math for you to ensure a smooth loading bar)
 */
for(var i = 0; i < numberOfTries; i++){

    $('#progressBtn').progressIncrement(100 / (numberOfTries - 1)); //update progress bar by this much.. 100/15 = 6.66..

}

$('#progressBtn').progressFinish(); after the loop is done, resets the progress function for its next use

Scrabble, and Counting Cards

22 Jun 2015

Nigel Richards is a legend among the Scrabble community. He employs the same approach to language that a card counter applies to a deck of cards. There are constants - numbers and symbols on the cards, text shapes on the scrabble pieces. The board is always the same in Scrabble - rules vary in Blackjack.

What I found interesting was the discussion on HackerNews about his style of play. Nigel doesn’t know French, and he can’t speak a lick of it beyond “Bonjour.” He’s just memorized random combinations of characters, essentially. Some people were quite annoyed with this, arguing that Nigel should have to define the French words (i.e. have a degree of familiarity with them in any context other than their literal spelling). But that’s an obtuse way to trying to place a limit on human creativity. Scrabble programmers have known this for years - scrabble is a game of dictionary brute forcing, and mathematical theory to maximize point values from the board.

But that doesn’t make it very fun to play against your cousin who’s memorized all of the two and three letter words in the Official Scrabble Dictionary and runs circles around you.

It’s not fun to get dropkicked by someone who plays the game. And so there are people who will always bemoan someone who’s perceived to be playing outside the “spirit” of the game.

Which brings me to card counting.

There is a very weird stigma about counting cards that has been created by the media, casinos, and general misinformation. Counting cards does not entail any illegal or immoral behavior.

To count cards, pick up this book and read it 20-30 times. Learn all of the tables and be able to map the tables out on a piece of paper at any time.

Then read this book to figure out how to handle money. Read it 3-4 times and internalize the messages about winning and losing money.

​Practice counting decks until you can count one down in 20 seconds or less. Start playing simulation games. Use Casino Verite to play thousands of hands - the computer will give you feedback, and adapt to your counting strategy. I use Wonging.

You need to be able to divorce your emotion from winning and losing money. The amount of money you win or lose is up to you. The eternal struggle is against the reptilian part of your brain that wants to slither into your rational thoughts and twist them into irrational ones. The snake from the Garden of Eden. It’s easy to make the dumb decision in professional blackjack. It’s very hard to keep making the right ones.

I always considered myself fairly in control of the rational part of my brain. Maybe not other parts, but I could always buckle down and solve a problem quickly.

I started out playing $25 minimums in Charleston, WV. This was a poor idea - I did not have enough of a bankroll to sustain any sort of time there (I would need $12,500 to sustain that play and assure myself a good exit). I made a couple grand one night, then lost most of it the next. I bounced around my starting bankroll of $400, up to a high of $4200, and back down to $60 at one lonely moment. After a few weeks, I stopped driving to Charleston. I ended up with $700, and much richer in experience.

I learned the finer points of ignoring distractions. I learned that casinos are very sad places. I saw the same people night after night who were convinced they had a system. The first few nights I laughed internally at their mystical routines. Then I began to doubt my own.

The Asian crowd liked to bang on the table with their fist. The white people said to the dealer “Okay, now deal me an ace! (forced guffaw)”

I’d get bored and start playing two hands in front of me. When the count is positive, it’s easier to make money that way.

Most people didn’t like it when I did that. For them, I was interrupting a preconceived “flow” of the game. Lots of people shot glares at me because I took “their” good card. They would have gotten that ace if I hadn’t doubled up! Why did you split on 10-10?

You start to tune people out because it’s very clear that the people in this casino are here because they make fundamentally bad decisions.

I was offered unsolicited advice by everyone. Lots of people telling me not to double. Lots of people wondering why I didn’t split. Lots of people watched me make money. Lots of people watched me lose money.

What I learned is that lots of people aren’t very good at blackjack.

But all of them were sure they played well.

When you go to most casinos, you are actually allowed to bring this little card with you.

A perfectly legal cheatsheet - you'll still lose to the house in the long run

They won’t kick you out, even though all of the answers are right there! The reason they won’t kick you out is that blackjack, like all other casino games, is rigged for your failure. You may experience short-term gains and windfalls, but you will lose in the end. Perfect basic strategy, which is making the right decision on every hand (but not counting), will give you a little over 49% chance of winning. What that means is that the house always wins. Slowly, and inevitably, it will eat every gambler alive. And remember, this is PERFECT basic strategy. You still lose!

Card counting evens the gap. By keeping count of the following variables, you can do some math/memorization and increase your odds of winning to 50.5-51%!

  • Number of cards dealt
  • Type of visible cards dealt
  • Number of decks (most casinos use 8, which means you’ll have to track 5-6 decks per round, as 2-3 will stay in the shoe)
  • Number of cards in the shoe
  • Dealer’s hand
  • Your hand

Keep track of those variables, memorize a few hundred card combinations, and voila, you’re counting cards. Nothing illegal or weird, just a lot of effort. The idea is that you can leverage the casino’s game back at them. Play a game long enough with a 51% win ratio, and you’ll eventually come out on top.

But, just as a lucky customer can win $13,000 in one night, so too can the casino take your money despite playing every hand perfectly.

Now, nobody plays truly perfect blackjack. There are constant distractions, noise, lights, shuffling. You’re often counting for hours at a time, and the hands can run together.You have to hear and acknowledge conversation at the table, and tune it out in order to pay attention to every hand, every time. It is a mentally exhausting enterprise.

So you will drop some hands. A waitress would come to me and ask me what I wanted to drink. I once blurted “Sixteen” as that was the current count. I usually just ordered Coke while mentally repeating the count inside, trying to keep it alive.

Overly talkative neighbors required attention and banter, another chance to increase errors. I’d occasionally miss a card at the far end of the table due to my poor sight. Sometimes the dealer is damn quick - it’s best to move to another table in this case. Counting cards is hard enough as-is, you should take care to remove other barriers to success.

The worst were the weekend warriors. These guys had enough money to blow that they could come back week after week and lose all of it again. They mainly got drunk and leered at the waitresses. In between pulls of their bourbon, they’d sagely warn me against doubling or splitting or hitting or staying. They had a system, you see.

It takes a lot of effort in this case to avoid telling someone to shut the fuck up.

Imagine you’re taking an important exam, and the kid next to you is just telling you the wrong answer and calling you an idiot for not listening to him. Now, you can’t tell him he’s wrong because if you tell him the answer, you’ll get in trouble. Also, if the teacher asks you and you know the right answer, you’ll be thrown out of the exam.

People thump the table with a balled-up first when they get an Ace. They place chips in certain configurations, they tip the dealer for “good hands,” and they absolutely loathe unemotional playstyles.

But they were all blustering and confident. They knew how to win.

And so I watched lots of people walk to the everyday, routine slaughter of their wallet. Tip the dealer ten bucks for taking your money and walk to the parking lot.

All of this made me doubt myself as well. I acted so confident and studied every detail, but at the end of the day, I was following some goofy system out of a book! Could I be the sucker? Am I just a goddamned idiot for sitting here, night after night, passively watching hundreds of dollars come and go?

I lost $1,900 one night. I played every hand perfectly (the lie every card counter tells himself). I actually had pretty good counts throughout the evening. I played according to the book. And I lost every hand that mattered. I lost on huge splits with $800 on the line. I lost on the small stuff. I just lost. Towards the end, down $1600, I started making horrifically irrational decisions. I was hitting on hands that I knew were bad. I stopped counting on my last five hands. I was dejected. I succumbed to a gambler’s mentality, hoping I could magically win some of it back.

Of course, I didn’t.

I had won $2,100 from that same place a few nights before. Now I could barely hold on to my last ounce of sanity as I drove home. I replayed every hand in my head, cringing. The worst part was the glance of dealers who knew I was having a rough night. To them, I was just another shmuck losing his money in a casino. They didn’t know how hard I’d studied and practiced before I came here. They didn’t know how much time and effort I put into understanding the game.

But they didn’t need to know, because we all saw the same thing. I lost a lot of hands at blackjack, and I lost my money.

I’d like to say it took a while for me to work up the nerve to play again, but I was back the next night. I played for three hours and walked away $120 richer. I wasn’t going to let fear ruin me.

I definitely stalled out after that night though. Even though I’d tried hard to internalize all of the lessons about losing streaks and the unfairness of random cards, I still found it difficult to process. The cards don’t care, of course, that I’m losing money. They exist just to be flipped over. I could only manage my emotions, not the cards.

To this day I’ve shied away from playing “for real.” I enjoy the game and still count random decks of cards just to keep my mind sharp, but my desire to hang out in depressing casinos has been reduced.

For a place that exists to swallow money, there’s not much of value within casino walls.


Splitting .csv files that exceed Excel's row limit

29 May 2015

Best tool for the job I’ve found: Open Source CSV File Splitter (OSV)

Very quick and easy to use. Specify lines per chunk file and you’re good to go.

Keep in mind many office suites (OpenOffice/LibreOffice) have row limits around 65k (pre 3.3.3: limit is 65,536 rows3.3.3 and later: 1,048,576 rows)

In Excel 2010, the maximum worksheet size is 1,048,576 rows.

Honorable mention: HeidiSQL, which I really wanted to use, but errors threw constantly on my import attempts. It could be my novice mistakes, or the tool just didn’t work. It’s certainly promising.

Nearly forgot to mention: to easily set up column headers in MySQL, I used http://www.convertcsv.com/csv-to-sql.htm, which is a lovely tool. Pre-writes your SQL statements for you. Grabbed the headers from the (too-large) .csv, made a file with only those headers, imported into this tool, and then set up a new test schema/table.


Removing auditctl rules/watches

28 May 2015

I used auditctl to help me track down what was modifying files on my Ubuntu server.

To remove an auditctl rule, you have to match each field in a rule.

For example:

root@web01-gpv1:~# auditctl -l
LIST_RULES: exit,always watch=/var/www/vhosts/testsite.com/wp-includes/nav-menu.php perm=rwxa key=RULE1
LIST_RULES: exit,always watch=/var/www/vhosts/testsite.com/wp-includes/nav-menu.php perm=wa key=RULE2
root@web01-gpv1:~# auditctl -W /var/www/vhosts/testsite.com/wp-includes/nav-menu.php -p wa -k RULE1
root@web01-gpv1:~# auditctl -W /var/www/vhosts/testsite.com/wp-includes/nav-menu.php -p rwxa -k RULE2

Also remember to enable logging of ausearch/aureport.

Random commands:

auditctl -w /var/www/vhosts/testsite.com/wp-includes/nav-menu.php -p wa -k KEY

I find that write + append give me readable logs, especially when dealing with Wordpress files. Otherwise, you’ll be sifting through thousands of repetitions of apache2 accessing the .php file.

ausearch -i -k KEY // -i is a human friendly interpreter, KEY is established with auditctl -k KEY
aureport -k //not very useful to me personally

Typical output:

myUbuntuServer:~# ausearch -i -k RULE1 //USE THE -i SWITCH! You won't like the results without it!
----
type=PATH msg=audit(05/26/2015 10:08:34.823:534859) : item=0 name=/var/www/vhosts/testsite.com/wp-includes/nav-menu.php inode=1410970 dev=ca:01 mode=file,664 ouid=www-data ogid=www-data rdev=00:00
type=CWD msg=audit(05/26/2015 10:08:34.823:534859) :  cwd=/var/www/vhosts/testsite.com/wp-admin
type=SYSCALL msg=audit(05/26/2015 10:08:34.823:534859) : arch=x86_64 syscall=open success=yes exit=56 a0=7f141a4a82d8 a1=41 a2=1b6 a3=62e0 items=1 ppid=4699 pid=25840 auid=unset uid=www-data gid=www-data euid=www-data suid=www-data fsuid=www-data egid=www-data sgid=www-data fsgid=www-data tty=(none) ses=4294967295 comm=apache2 exe=/usr/lib/apache2/mpm-prefork/apache2 key=RULE1
----
type=PATH msg=audit(05/26/2015 13:58:35.295:538884) : item=0 name=/var/www/vhosts/testsite.com/wp-includes/nav-menu.php inode=1410970 dev=ca:01 mode=file,664 ouid=www-data ogid=www-data rdev=00:00
type=CWD msg=audit(05/26/2015 13:58:35.295:538884) :  cwd=/var/www/vhosts/testsite.com/wp-admin
type=SYSCALL msg=audit(05/26/2015 13:58:35.295:538884) : arch=x86_64 syscall=open success=yes exit=56 a0=7f14198b1620 a1=241 a2=1b6 a3=7ffef00107f0 items=1 ppid=4699 pid=26789 auid=unset uid=www-data gid=www-data euid=www-data suid=www-data fsuid=www-data egid=www-data sgid=www-data fsgid=www-data tty=(none) ses=4294967295 comm=apache2 exe=/usr/lib/apache2/mpm-prefork/apache2 key=RULE1
For help interpreting ausearch results: Try the RedHat docs.

In my case, auditctl/ausearch helped me track down a php file that was modifying files every couple of days.