Now Loading ...
-
Some Thoughts on Building Bet Buddies Part 1
I’m definitely not doing anything ground breaking, but I wanted to talk a little about some of what I have learned from working on making a Texas Hold’em Poker application in Elixir.
GenServers, Registries, and DynamicSupervisors Are Lovely
Elixir is a very cool language because it’s grandpappy Erlang is cool. It’s cool because you have processes as a primitive within the language and these processes are decoupled from one another. So if one process encounters a bug and dies, it doesn’t take down the entire application with it. One common use of this in Elixir land is setting up a GenServer, registering it to a Registry, and having a DynamicSupervisor supervise the GenServer.
Starting with this kind of knowledge base and having seen other examples of how this can be used with a game, I figured I would start building out a Texas Hold’em poker application. As it would turn out too, there aren’t a lot of great multiplayer poker applications and I’m out here building probably the worst one. However, it’s mine and I love it.
So what does this GenServer do? Well, you can put the state of the poker game as the state of the GenServer. So this GenServer process is holding onto some data structure representing what’s going on in the game and I can show what this looks like.
defmodule Poker.GameState do
use Ecto.Schema
alias Poker.Card
alias Poker.Player
embedded_schema do
field :game_id, :string
field :game_started_at, :utc_datetime
field :password, :string
field :game_stage, :string
embeds_many :dealer_hand, Poker.Card
embeds_many :dealer_deck, Poker.Card
field :pot, :integer
field :side_pot, :integer
embeds_many :players, Poker.Player
field :turn_number, :integer
field :minimum_bet, :integer, default: 0
field :most_recent_max_bet, :integer, default: 0
field :big_blind, :integer, default: 800
field :small_blind, :integer, default: 400
end
...
end
Now this structure will likely change from what I’m showing in this article, but essentially this is what is being tracked for a poker game. What is particularly nice is that we can give unique poker games unique game ids. These get registered to the Registry and supervised by the DynamicSupervisor. In this way with Elixir, we can have a hypothetical millions of poker games going on each decoupled from each other so if there were some issue in one poker game, it wouldn’t affect anyone else’s poker games. It’s really the multiple poker games going on that’s nice though.
None of that would be possible without the fact that GenServers, Registries, and DynamicSupervisors exist and I find myself while building out the program and debugging just going and creating a new poker game and running through it. Make some changes, make a new game, validate changes–wash, rinse, repeat.
Now continuing with the GenServer, because it’s a server, we can send it messages like, “Hey man, in the ‘Eevee’ game, player 1 had just bet 400.”
Then the GenServer can handle the processing for making 400 leave player 1’s wallet, go into the pot, and move onto the next player. Really cool stuff!
Some of the Challenges
Now probably the biggest challenge that I found myself encountering while building out this thing was figuring out how to handle sidepots, and managing whose turn it is.
What has been great about living in 2024 is now we have YouTube and AI chatbots and I found myself watching YouTube videos on how to handle side-pots and asking chatbots how to handle side pots and between the two, one can begin to close knowledge gaps.
So just to share about these knowledge gaps, let’s talk about side-pots.
Side-pots are created when there are 3 or more players betting but one player has decided to all-in but they had less money than everyone else. Suppose that we have three players: Alice, Bob, and Charlie. Suppose that Alice bets 1000, Bob calls for 1000, and Charlie only has 50. Charlie, desperate to win or lose, bets it all. This means that for Alice and Bob, their 1000 bet will have 50s added to the main-pot and the overflow 950 will go into a side-pot. Although Alice, Bob, and Charlie are all competing for the main-pot, only Alice and Bob can win the side-pot. It took me a while to learn this.
Next, we have managing whose turn it is. Naively, I had thought, “Let me just assign a turn number to everyone. Alice is first, Bob is second, Charlie is third. Then when they make a move, we’ll increment between 0, 1, and 2 respectively.”
Although this manages to get a game where you can loop through players, poker is tricky. When someone folds, they aren’t part of the turns happening the rest of the hand. If someone bets, then everyone else has to call up to the same amount. If everyone called, then the first person to have moved has a chance to check. With all of this complexity to moving around players, I ended up learning to set up a “Player Queue” where this queue will remove players who had just made some kind of move, however if someone bets, then everyone gets added back into the queue except for the better unless someone bets even more.
So with the next updates of my poker app at this time, I’m working on implementing this stuff. Recently I had just started to write up getting the dealer to put cards on the table and needing to edit some of that front-end UI displaying that.
Thanks for reading!
-
Elixir and Astronomy Could Be Friends
Astropy exists for being able to utilize FITS files however I think it would be really cool to have it in Elixir.
FITS files and where to get them
I happen to be a bit of an astronomy nerd.
Now in my astronomical pursuits, I have been looking to be able to use Elixir for reading FITS files–a specific file standard endorsed by NASA and the International Astronomical Union.
These files are cool because they can contain information such as spectra about star systems, planets, galaxies, etc. So if one was curious as to the presence of hydrogen in the atmosphere of some planet, they could pull down the FITS data for that planet at a repository like the Mikulski Archive for Space Telescopes (MAST).
With your FITS file in hand, you’ll start having a fit trying to utilize it for anything. Now what one could do is get ahold of the Astropy library, some Jupyter Notebook, and some of your boy DESI ASTRO’s videos on YouTube.
But I’m built different. I really like Elixir. Unfortunately, there are no Elixir libraries for reading FITS files–so I decided to make one that doesn’t work yet. However, FITS files have a primer provided by NASA on how to at least get started doing something with them.
The big thing that I learned is that FITS files can be broken apart, byte by byte, into 80 byte chunks. When you do this, you’ll start to get information that almost looks like something useable.
Although this is great, I’ve recently heard from some of the good folks on the Elixir slack that you can simply invoke Python from Elixir and doing some more digging into that, it seems that one simply could do that.
So perhaps pursuing building some “AstroPy, but in Elixir so obviously it’ll be AstroEx” is probably something that doesn’t need to be done.
However, where I’m at with my little library–perhaps one day it could get there. Admittedly with how little progress I’ve made on though, perhaps someone would beat me to that.
-
Job Hunting Sucks
2024 has seen some horrible layoffs especially in the technology market and boy howdy it sucks. So here is a rant on my 2024 job hunt.
Rant on 2024’s Job Hunt
Well, it’s quite unfortunate but in December of 2023 I got laid off and received the lovely privilege of being promoted to possible future customer with my former employer.
Now why did this happen?
Well I’d definitely accept that there is probably some of my own personal responsibility involved however the company did admit it is a layoff, so they are admitting their own fault. Solar companies are incredibly sensitive to changing economics and in 2024, interest rates got hiked up more. For those who haven’t bought a solar panel system before, you’re likely taking out a loan to purchase it. These loans of course are going to have interest rates and of course, they’ve been raised an amount substantial enough that during 2023 the company had 40% less sales than they had expected.
Pretty unfortunate for everyone involved if we’re being honest. Now the main point of my writing is to rant about how the job market just sucks.
I have heard some word from friends that this is probably the worst job market since 2008–which although I wasn’t in the workforce at the time, I understand that it was a horrible time for everyone who was, especially those who were homeowners.
So why exactly has this job market sucked?
It’s been bad because there isn’t an abundance of hiring jobs. The technology industry in The United States had let go of a lot of technology workers which had significantly increased the amount of competition in the industry.
Now that just means that you have to get good and that I haven’t gotten a job yet clearly signals to me, “Skill Issue” however I’ve also been told that this is just a normal amount of time to be looking for a job.
Is there anything that’ll make things better? At this point, I really don’t think so. One will see that during the pandemic, a lot of companies had begun growing a ton. This all looked good on paper but it was all synthetic and fake. These companies were growing because they were accepting a ton of COVID relief money and using that money to grow. Once the money tap was shut off, these companies had to scale back operations significantly and that included letting go a lot of good people.
If you’re out there working on the job hunt, I’m sorry. I understand your struggle and I think we’re going to make it. However, we might have to take up jobs that we don’t want to be able to survive for an amount of time. Consider yourself lucky if you’re able to collect unemployment insurance. Luckier still if you don’t have a lot of bills to pay such as a mortgage or child care.
Everyone out there looking for jobs right now should continue grinding and bettering their skills in whatever category they’re in. Although I’ve been told that certifications are a waste of time, they’re probably worth considering to acquire in this time because it certainly helps one to stand out in the sea of competition–and the competition is fierce.
Don’t forget to also take care of your physical health. There is a real mind-body connection where if you’re taking care of your body by getting physical activity, taking omega3s and ashwagandha, then you might just make it. This has been my strategy for the last while and it seems to be quite helpful.
Good luck out there everyone. I wish you all the best. We’re going to make it.
Touch background to close