PACNEM – Developing Games or Applications with the NEM Blockchain and
PACNEM – Developing Games or Applications with the NEM Blockchain and Knot.js
The NEM Team would like to thank Grégory Saive (Telegram user: @evias) for writing blog and tutorial.
Today I am going to share a lump of my development practice on the PacNEM game developed using Knot.js and the nem blockchain (Feb. Two thousand seventeen to Jul. 2017). Details of the implementation will be posted along with this article in a more in-depth article.
This beginner’s guide aims to be ordinary and should only introduce you to a few basic features of blockchain related application and game development.
Brainstorming and Documentation
Linking a blockchain to a game or application development project is a little more complicated than what you might think as it requires a good understanding of the Peer to Peer networks brought back to life by blockchains.
Yes, blockchains use Peer-to-Peer and so — each cryptocurrency’s blockchain is distributed across a network of knots. These knots also validate transactions for a protocol defined by the given cryptocurrency — this validation process is usually referred to as Consensus Rules.
This combination of Peer-to-Peer networks and Consensus Rules is a game changer in the software development industry. It gives us a totally fresh — trustless — software environment. We can now build apps whose outputs will be validated not by one server but by thousands of blockchain knots — replicating our data — and making sure it is censorship resistant through distribution.
I have looked around for fairly some time before I found an effortless to use blockchain with genuine features and growth — Then I discovered nem.
In this article, I will describe my very first idea of saving PacNEM high scores to the NEM blockchain. I won’t cover all the details about Multi Signature Accounts, Namespaces & Mosaics, etc. in this article – I will only give you an idea of how effortless it is to interact with the NEM blockchain and integrate NEM into your business layer implementations.
The source code for the finish game is the result of months of documentation about blockchains, NEM, Trustware, Multi Signature Accounts, Mosaics, etc. and a good chunk of hobby programming time and unspoiled developing envy. The utter source code for PacNEM can be found at Github.
Now, let’s do a quick Brainstorming session with following keywords on the Board :
Those are the main areas we will be trespassing for this article. Feel free to document and come back later if anything is too complicated and I’ll attempt to link this article with good sources for your reading too. The blockchain features, implemented with nem, are more complicated to explain than they are to implement!
Work the [ne]magic
So, the needs for our Game are pretty elementary: We defined that we want to save High Scores on the nem blockchain.
The very first key aspect of blockchain application development is that saving data to a blockchain will always happen in a process of sending a transaction which gets included in a block of the given blockchain. nem provides with different transactions types but we will need only one: Mosaic Transfer Transactions.
A transfer transaction on the nem blockchain can contain an amount, a message, a fee and affixed mosaics. This already gives us numerous implementation choices for the high scores implementation:
- We could save high scores in transactions with messages (the message is the score).
- We could save high scores in transactions with mosaics (amount of mosaic is the score).
- We could save high scores in transactions with both messages (for authenticity) and mosaics (for the score).
In this article, I will illustrate an example of the 2nd implementation choice. This keeps it fairly plain.
We now know that our high scores will be represented as an amount of a given mosaic published on the nem blockchain. We don’t want people to be able to forward or transfer these high scores to other accounts so we will create a non-transferable mosaic. If you don’t know about NEM Namespaces & Mosaics yet, I recommend reading this article.
Another constraint of blockchain application development is that we will need a XEM account that we will create for our game or application. Using the NanoWallet is fairly feasible for everyone, right? Once you have created the account, copy your account address and private key somewhere safe. In this article, we will refer to the created Application Wallet as pacnem-business. This wallet should contain your business funds (mosaics).
You are now all set to embark developing games or applications with the NEM blockchain. As a little test experiment, following snippet will retrieve the last block of the NEM Testnet blockchain:
Effortless, wasn’t it? OK, this was only a test experiment that will let you test your NEM-sdk package installation.
As you can see, the SDK lets you query the blockchain for data out-of-the-box. Now let us get back to our horses and create an effortless function to query a given account’s High Scores entries.
Blockchain is distributed, but NEM is an account-based Ledger. This means that you can query blockchain knots through HTTP APIs to get current Account State Data. With this skill, it is now possible to determine which account has received our Mosaic pacnem:cheese.
This process will be fairly resource expensive as you will need to query each account who received the Mosaic to retrieve their current Balances. What would be more optimized is to determine a specific paying account for this Mosaic pacnem:cheese and query only this account’s outgoing transactions to retrieve Player High Scores. Again, we will call it the pacnem-business account — which, in fact, is just your NEM wallet address.
Also worth noting, the NEM blockchain knots will only permit you to retrieve data about twenty five transactions per request. Some knots accept a hash parameter and all knots accept the id parameter. We will need to query the blockchain numerous times in case there is more than twenty five transactions, this is why the following example uses recursion to wrap around the SDK Promises calls and make sure we retrieve all available transactions.
Ok, now let’s build the code snippet to read High Scores from the Blockchain using our wallet pacnem-business and the SDK. For the sake of plainness, I will store the data in a global object that we will re-use in later examples. To the source code:
Executing this snippet will give you a list of outgoing transactions. This was pretty straight forward, as I explained earlier the only little hick-up is about the twenty five transactions limit for which we end up querying a HTTP API several times. Nevertheless, I think this example keeps it ordinary enough.
This code snippet will be used later in other lists. The outgoing transactions list is what we will be using to retrieve our Hall of Fame data in the form of NEM blockchain Mosaics.
NEM Mosaics are Tokens published on the NEM blockchain. Using our outgoing transactions list, we will be able to determine the amounts of Mosaics. This was a very first step-in to reading blockchain transactions with the NEM-sdk.
Use NEM blockchain transactions
Now we have all outgoing transactions for the given pacnem-business account and all we need to do is check in these transactions to find all transactions containing pacnem:cheese mosaics.
NEM makes it indeed effortless to read Mosaic data in blockchain transactions. Mosaic Transfers Transaction can be differentiated from others through their mosaics field containing an array of affixed Mosaics. This is very plain too, so let’s tackle the snippet to retrieve exact latest scores on the NEM Blockchain:
With this executed, you have successfully read your very first High Score list ever from a blockchain.
Those code snippets are relatively long but they are also kept very plain and I think with this you should be able to interpret and use your very first NEM blockchain transactions without problems.
The NEM-sdk for Knot.js offers a lot more features. I recommend you have a read at: https://github.com/QuantumMechanics/NEM-sdk. This SDK implements pretty much anything needed to interact with the NEM blockchain and even permits connecting to knots with Websockets!
Lots of projects have been commenced using the NEM blockchain and we can only be more satisfied to see even more coming!
The NEM blockchain offers advanced blockchain features out-of-the-box and provides a superb ground layer for any blockchain related Game or Application development project.
Recall, if you are fresh to the blockchain industry — this requires some thinking ahead — plan those extra hours of studying blockchain data if you don’t know about it yet!
I hope everyone could go after along with this article. I will be posting more articles about NEM and blockchain integrations in the Software Industry in the coming weeks and months. I hope you liked it!
I would love some inputs/feedback on my latest projects:
This article and the PacNEM project are published Open Source. Feel free to send donations to one of the following addresses:
- NEM/XEM: NCK34K5LIXL4OMPDLVGPTWPZMGFTDRZQEBRS5Q2S
- Bitcoin: 1Js4Tz3KSC6PAbvzy4xHga5caQLtKLysye
Credits to Telegram user @spizzerb (Patrick) for the PacNEM logos. Thank you a lot Patrick!
NEM Projects / Links
- PacNEM: https://www.pacnem.com
- NEM Knot.js Bot: https://github.com/evias/nem-nodejs-bot
- DIM: https://dimcoin.io
- Xarcade: https://xarcade.io
- NEMPay : https://medium.com/@dgarcia360/introducing-nempay-bd721e383027
- Blockchain Lotto: http://bclotto.io/
- NEM PayContentJS: https://github.com/aenima86/NEM-PayContentJS
- nem-tools Suite: https://marc0olo.github.io/nem-tools
- NEM Resources: https://github.com/Xpedite/NEM-Resources
- NEM Authenticator: https://medium.com/@aleixmorgadas/nem-authenticator-2fc-ab607513c62d
- NEM MicroWallet: https://medium.com/@anrodon/introducing-nem-microwallet-17697bb0d723
- NEM Peak Bot: https://github.com/daifukunem/nem-tip-bot
- Landstead: http://landstead.atraurablockchain.com/
- LuxTag: http://luxtag.io/
Other Resources / References
- Play PacNEM : https://www.pacnem.com
- PacNEM Source Code: https://github.com/evias/pacNEM
- NEM-sdk for Knot.js: https://github.com/QuantumMechanics/NEM-sdk
- NEM Foundation Website: https://nem.io
- NEM Wiki: http://wiki.nem.io
- NEM Forum: https://forum.nem.io
- NEM NIS API Documentation: http://bob.nem.ninja/docs
- NEM Developer Guide: https://rb2nem.github.io/nem-dev-guide/
- NEM Tutorial List: https://blog.nem.io/nem-tutorial-list/
- NEM NanoWallet: https://github.com/NemProject/NanoWallet/releases
- NEM Resources: https://github.com/Xpedite/NEM-Resources
This blog was originally posted on here.