create account

The Challenges of Developing a Simple Text Game on Discord by reazuliqbal

View this thread on: hive.blogpeakd.comecency.com
· @reazuliqbal ·
$12.07
The Challenges of Developing a Simple Text Game on Discord
![lowcard.png](https://cdn.steemitimages.com/DQmR64QW6qC11cfRN4DRwMeCJSZrpRowkrjuuqiuaYnRMNg/lowcard.png)

I got appointed as a team member of [Neoxian.City Tribe](https://www.neoxian.city/). 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](https://steemit.com/neoxian/@zaku/lowcard-game-first-steem-engine-token-based-discord-game-bot-by-neoxian-city-tribe).

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](https://discord.js.org/#/docs/main/master/class/Collection). Discord.js Collection introduces additional utility methods over JS [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete) 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.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 51 others
properties (23)
authorreazuliqbal
permlinkthe-challenges-of-developing-a-simple-text-game-on-discord
categoryneoxian
json_metadata{"app":"steempeak/1.15.3","format":"markdown","tags":["neoxian","palnet","programming","technology","discord.js","nodejs","games"],"users":["zaku"],"links":["https://www.neoxian.city/","/@zaku","/neoxian/@zaku/lowcard-game-first-steem-engine-token-based-discord-game-bot-by-neoxian-city-tribe","https://discord.js.org/#/docs/main/master/class/Collection","https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map","https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete"],"image":["https://cdn.steemitimages.com/DQmR64QW6qC11cfRN4DRwMeCJSZrpRowkrjuuqiuaYnRMNg/lowcard.png"]}
created2019-09-01 07:49:27
last_update2019-09-01 07:49:27
depth0
children2
last_payout2019-09-08 07:49:27
cashout_time1969-12-31 23:59:59
total_payout_value6.941 HBD
curator_payout_value5.125 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,265
author_reputation61,984,354,446,410
root_title"The Challenges of Developing a Simple Text Game on Discord"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,163,434
net_rshares36,918,024,136,982
author_curate_reward""
vote details (115)
@smartsteem ·
👍
*~Smartsteem Curation Team*
properties (22)
authorsmartsteem
permlinkpx9dcc
categoryneoxian
json_metadata{"tags":["neoxian"],"app":"steemit/0.1"}
created2019-09-03 13:55:24
last_update2019-09-03 13:55:24
depth1
children0
last_payout2019-09-10 13:55:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length29
author_reputation157,838,860,587,218
root_title"The Challenges of Developing a Simple Text Game on Discord"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,192,982
net_rshares0
@zaku-ag ·
Thanks for giving us this amazingly amazing game bot .

!neoxag 50
properties (22)
authorzaku-ag
permlinkpx9by2
categoryneoxian
json_metadata{"tags":["neoxian"],"app":"neoxiancity/0.1"}
created2019-09-03 13:25:18
last_update2019-09-03 13:25:18
depth1
children0
last_payout2019-09-10 13:25:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length66
author_reputation16,109,952,676,363
root_title"The Challenges of Developing a Simple Text Game on Discord"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id90,191,694
net_rshares0