The Challenges of Developing a Simple Text Game on Discord

avatar

lowcard.png

I got appointed as a team member of Neoxian.City Tribe. We wanted to make a fun token sink for the NEOXAG token. A Discord text game idea was put forward by @zaku, I was tasked with coding it.

It is a random card pull game where users can join by depositing NEOXAG and start pulling cards. Top card puller wins the round, and low card puller gets out. If you want to know more how its play check this post.

At first look it was very easy, I mean how difficult can a random card puller game could be. I was correct it was easy but there were many challenges. Let me explain.

The challenges:

  • Card pulled by a player in a round should be out for that round (can't be pulled by others).
  • If a player is out s/he can't join the next rounds in the game.
  • Each round should have a time limitation.
  • If nobody pulls manually the bot should pull automatically.
  • Players leader-board should be maintained.
  • A server can have multiple games running at the same time.

They were challenges because I didn't want to use an API or make many database calls at once. Also, I cannot get any help from the user agents, as the players will be playing on Discord.

How I overcome the challenges:

I have decided to create an in-memory database for all these and push data to persistent database (MongoDB) when a game ends.

I introduced 3 global variables (allWinners, possibleCards, and gameTimer), each a instance of Discord.js Collection. Discord.js Collection introduces additional utility methods over JS Map which hold key-value pairs also remembers the order of entry.

A Discord.js collection looks like this:

Collection [Map] {
  'unique_game_id' => Collection [Map] {
    'winners' => [ 'id_1', 'id_1' ],
    'players' => [ 'id_1', 'id_2', 'id_3' ],
    _array: null,
    _keyArray: null } }

A collection can have multiple collections. For key I have used discord's message ID for games, user ID for players.

allWinners contains all the players for all the games (each game is a Collection) until they are completed.
gameTimer holds game time for each round of a game (each game is a Collection).
possibleCards holds available cards for each game round (each game is a Collection).

When a game ends, I pushed the results from allWinners to the DB, flushed the ID from the collection. The same process goes for gameTimer and possibleCards, but they were flushed after each round.

There is one caveat using this process. If the bot restarts when I game is running, the bot will lose all game progresses and have to re-played from the beginning. The chance of happening this is very rare but we should be careful when coding the bot - all errors should be caught, and also all collection should be cleared when a game ends.



0
0
0.000
2 comments
avatar

Thanks for giving us this amazingly amazing game bot .

!neoxag 50

0
0
0.000