Guidance for Blacksmiths

Before reading this page, first read:
 * Contributing to Habitica (from the "Coders (Web & Mobile)" section onwards)
 * Setting up Habitica Locally

Technology Stack
The technologies that Habitica uses for its website are listed below. You don't need to be familiar with all of them, or even most of them, to be able to contribute! Some links to high-quality learning material are included.


 * Server:
 * ExpressJS
 * Node.js
 * How do I get started with Node.js
 * nodeschool - included in the SO article linked above, but a particularly awesome entry level interactive node curriculum
 * MongoDB
 * MongooseJS
 * Bower
 * Gulp
 * Client:
 * AngularJS
 * Shaping up with Angular.js - A video tutorial with exercises. You'll probably find that the videos alone are enough to help you understand Habitica's AngularJS code, although of course completing the exercises too would be more beneficial.
 * Learn Angular - Free interactive lessons.
 * Boostrap
 * Jade
 * Stylus
 * Testing:
 * Karma
 * Mocha
 * Jasmine (used only for an end-to-end test for the static home page working)
 * Selenium (used only for an end-to-end test for the static home page working)
 * Services:
 * New Relic
 * Heroku
 * AWS
 * Amplitude
 * Other:
 * iOS
 * Android
 * git and GitHub
 * Pro Git - An excellent resource. You will be much more comfortable with git if you take the time to read this.
 * git-it - scroll down to see more info. This is part of the nodeschool curriculum and is a fun and interactive way to learn git and github.

Tips for Working with a Local Installation
After you've followed the instructions on the Setting up Habitica Locally page, these are a couple useful tips to using your installation.

Quick Server Restart
After you've started the server (i.e., run ), the end of the console's output should look something like Running "nodemon:dev" (nodemon) task [nodemon] v1.2.1 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node ./website/src/server.js` info: Express server listening on port 3000 info: Connected with Mongoose The lines about  mean that after you make any change to the code that you'd like to test, you can simply type rs and then hit enter to restart the server. This is much faster than exiting and running  again.

The Debug Menu
The following menu is in the footer of every page of your local installation, and can be used to quickly cause certain actions to happen, making testing your code easier.



Git
Here is a small tutorial to explain how to fork habitrpg, create new feature branches, and perform pull requests:

Fork & Clone Habitica
The easiest way is to go on the habitrpg github page and click the Fork button. Then do a git clone of your own repository.

Another way is to

git

directly, then (and note you need to change "YourUsername" with your actual github.com username)

cd habitrpg git

Finally,

git push

to update your fork.

Setup Upstream Remote
Next step is to keep track of the upstream habitrpg repository, so we can easily update our fork with the latest changes. Because origin is the url to our fork, we will create another remote upstream for habitrpg:

Then, every time you want to get habitrpg's latest code, you can do:

git fetch upstream

Rebase Branch
When you want to update your local branch, you need to rebase it from the upstream branch. Let's update develop:

git checkout develop git fetch upstream git rebase upstream/develop git push # to update your fork

If you get the error "fatal: 'upstream' does not appear to be a git repository", make sure the main repository is added as upstream git remote add upstream https://github.com/HabitRPG/habitrpg.git

After updating, there may be new node packages in the project so remember to run these again: sudo npm install Note: If you want to work on a feature that was started by someone else, and not yet merged in upstream, you can use the same technique as shown in 2. & 3. to pull their commits:

Let's say the user 'Fandekasp' wrote a feature branch 'add_theme' which is awaiting a pull request into upstream/audio. You also have your fork of upstream/audio, and would like to get Fandekasp's changes right now.

git remote add fandekasp https://github.com/Fandekasp/habitrpg.git git fetch fandekasp git rebase fandekasp/add_themes

Create a New Feature
Now, you want to start a new task, and pull request it. First, you need to find which branch to start the feature from. Do a  to see the list of remote branches. Choose the appropriate branch depending on the task you want to work on.

Let's say you decide to start your task from the develop branch. To create your feature, do:

git checkout -b relevant_branch_name upstream/develop

Then you can  it in your fork.

Write Commits
When developing code, It is recommend that you do regular small commits, which are easier to review than a big one. It's also easier for developers to refuse or edit one specific commit that needs changes.

For example, you could have a commit dedicated to tests, followed by one with ui changes, one to write the logic, and one for the translations.

If your changes are done in several days, make sure you rebase your branch to properly update your code. If some conflicts appear, you'll often want to simply use the upstream version:

git rebase -Xours upstream/develop

Pull Request
When you're done with your code, or half-done but wish to do the pull request anyway (to get some feedback, for example), push your branch code in your fork.

You can then go to https://github.com/YourUsername/habitrpg.git, and you will see a Pull request button. When doing the pull request, verify that the merge will be done in the correct branch. It often defaults to upstream/develop even if you created your branch from another one.

Before submitting the pull request, you can run the tests locally to see if things aren't working properly.

Another way is to use the tool hub (install it with your package manager), then run  from your branch.

Issues
You tried to do a rebase and now you have many new and irrelevant commits in your branch.

No worries, there are several solutions to get out of that mess:

Cherry-pick
One solution is to keep a  window open, with all your commit hash IDs (the long string of letters and numbers) visible in a list.

You can now perform  (if there is only one commit to remove, otherwise HEAD~nb_of_commits). If you delete too many commits, just  again from your fork. Then, copy the commit hash of the commits you want to re-add on top of your branch (from the git log window you left open before doing a reset). For example,

git cherry-pick ee1768b7e2c0bcee9eff1a45be1e3543fac0687b

This will re-add commit ee1768b7e2c0bcee9eff1a45be1e3543fac0687b on top of HEAD.

Stash
if your attempt to push failed because remote has a different HEAD than your local repo (this happens to people working together on the same branch and pushing on the same remote), don't pull! Instead, do

git reset --soft HEAD~n_local_commits_ahead git stash git pull git stash pop git commit -am "message"

Work with habitrpg-shared
NOTE!  habitrpg-shared has been deprecated. The habitrpg-shared files have been moved to the /common subdirectory under habitrpg, and all actions you used to do in habitrpg-shared can be done there. If you see any instructions telling you to take actions in habitrpg-shared, please report them in the Aspiring Coders guild and ask for clarification if you need it.

MongoDB
Quick tips for new developers to get the hang of using the database.

In the commands below, the $ sign indicates a Unix or Windows or Git Shell prompt and the > sign indicates a mongo shell prompt. Type only the text that appears after $ or >

Access a shell
Start the mongo shell then select the database:

$ mongo > show dbs > use habitrpg

Alternatively, directly start the shell with the correct database:

$ mongo habitrpg

Use the shell
View the "collections" in the database ("users", "groups", etc):

> show collections

Find your test user and examine its data. From localhost settings, copy the User ID, then run this command:

> db.users.find({_id: '85b007a2-b5b9-4bb4-8b82-e4567edb4919'})[0]

Want to see the preferences only?

> db.users.find({_id: '85b007a2...'})[0].preferences

To update something for your user, use the update method with $set. For example, edit your profile blurb:

> db.users.update({_id: '85b007a2...'}, {$set: {'profile.blurb':'test'}})

Give yourself 10 gems (can also be done from the Debug menu in the lower right of the footer): > db.users.update({_id: '85b007a2...'}, {$set: {'balance':10}})

Get admin rights (you will then see extra options in the Hall):

> db.users.update({_id: '85b007a2...'}, {$set: {'contributor': {'admin':1}}})

Assign yourself unpurchasable quest scrolls (e.g., for limited edition quests):

> db.users.update({_id: '85b007a2...'}, {$set: {'items.quests': {"egg":2,"dilatory":1,"stressbeast":1,"basilist":1,"evilsanta":1,"evilsanta2":1}}})

This gives the user two Egg Hunt scrolls ('egg') and one scroll each for another five quests. Please note that this will clear all other quest scrolls that the user possesses, but as others can be added in the same way (respective keys can be found in the content.coffee) or can be purchased later from the market.

Creating a Local User Account Identical to your Production Site Account
Export your user data using the GET /user API route, and import it locally using MongoDB commands such as. This can be a fast way to create a local account pre-populated with tasks, equipment, etc.

Your account will need to be imported into the  collection, but this collection won't exist in mongo until an account is created on your local copy. This can be done simply by starting your local copy and creating a test account, and then inserting your production account into the  collection.

Migrations
Some Habitica website changes require existing user accounts to be modified (e.g., to assign a default value for a new setting to all users). Such database changes are done with a migration script, which contains JavaScript that connects to the database and makes modifications. All migration scripts are in migrations/. Read them and use them as examples to write your own script. The more recently edited scripts are likely to contain better, more up-to-date code than the older scripts.

To test a migration script:

$ mongo habitrpg migrations/name_of_script.js

Data Binding, and Template Conditionals:
When working in the Jade templates, you may see element attributes such as ng-class, ng-show, or ng-if. These are bindings that are used to map model data to elements for display that are part of AngularJS. These bindings can be used to define styles or display based on conditionals.

You may also notice some other attributes such as bo-class or bo-if.... so what's the difference?

Bindings that begin with "ng" are part of Angular and are completely dynamic. This means that every time something changes with the app, Angular will test all of these bindings with conditionals again and take action again. Obviously this is a concern for performance.

Sometimes the data used to calculate these conditionals will not change often, if ever. When that is the case, we have an option for static binding, using a library called Bindonce. These are similar bindings, but they only are checked at the initial load of the application. If possible, this should be the preferred method of binding as long as the data will not change.

Bindonce directives should only be used if the following rule is true: the model data being used for the conditional will not change during a session, or will change infrequently enough that it's not unreasonable to expect the user to refresh the page. If not, stick with traditional binding. If you can't decide or have no idea what you just read, just use the ng-&lt;whatever&gt; attributes.

Adding Translatable Strings
If you need to add a new translatable string in some template, it must be written in the jade file as follow:

env.t("stringLabel")

Then, in the  directory, edit the json files, adding the new string as follow:

'lastLabel': 'Add a comma at the end of this line', 'stringLabel': 'String Title' }

Do not update files in other directories under ; translations are managed in Transifex.

To test the string:


 * stop npm if it is running (Ctrl-C)
 * run  

Modifying Translatable Strings
Translatable strings appear in files in the  directory. Each string consists of a key and a piece of text, for example:

'clearAll': 'clear all items',

If you need to change the text of a translatable string, do not also change the key unless there is a very good reason to do so. For example, if you needed to change 'clear all items' to 'delete all items', you would not also change 'clearAll' to 'deleteAll'.

This is because the key ('clearAll') can be used in many places throughout Habitica's code, and so changing the key would require you to also make otherwise unnecessary changes to the code.

In addition, the keys that are used in the English translatable strings are also used throughout all the other languages directories under. When a piece of English text is changed without the key being changed, the other languages will keep using the existing translations for the original English text until the translators have had time to update the translations. Usually this is what you want because the existing translations are usually still good enough to be used. However if you were to change the key as well as the text, all the existing translations would no longer be used because the language files do not contain the new key. This would mean that people using languages other than English would see the new English text until the translators had time to provide strings for the new key.

Tests
Habitica has a suite of tests that can be run manually and automatically to help us avoid bugs.

Whenever a pull request is made in GitHub, Travis CI is used to run all the tests. Any failing tests should be examined to see if the new code is the cause of the failure.

Most new code should have tests written for it before it will be merged into the code base, however Habitica admins will be very happy to help you write such tests, or to write them for you if you aren't able to. Do not be afraid to submit a pull request without tests, especially if you are not sure what is involved in the testing process, but please be aware that your submission might not be accepted until you or the admins have created the tests.

To run the tests on your local install, change directory into the top-level directory and run. This is an alias for, which prepares the test environment and runs all the test suites.

To run a single test on your local install, run

Other
Various useful commands.

Search code
In the console, type:

grep -R "STRING" *

To search for STRING in all files in the current directory and all the directories within it.

If you want to make a search case insensitive (STRING or String or string etc), add -i

grep -iR "STRING" *

You'll often want to search all files containing some keyword, in order to determine what files need to be edited when adding/editing some feature.

The grep command also takes a regex as its search parameter: grep -R "REGEX" * Read more about grep and regex here.

Search and replace
Here is a perl command to run in the terminal:

perl -e "s/FROM/TO/g" -pi $(find . -name "*.js")

Replace FROM by the string you want to replace, and TO by the string to replace the first one with. Note that this example will replace that string only in javascript files (extension .js), but you can specify other filetypes if you want.

If you want to replace all strings, but not their plural or other words containing that string (e.g. replace weapon by TEST, but do not replace weapons, do:

perl -e 's/weapon\b/TEST/g' -pi *

If you want to remove all lines in the javascript files that contain some keyword, do:

perl -ni -e 'print unless /keyword/' -pi $(find . -name "*.js")

These commands are particularly useful with translation-related work.

Testing the Swagger API Interface Locally
Habitica has an API interface at https://habitrpg.com/static/api, which uses the production database and code. When you have made your own local install of Habitica, you can test the local version of that interface at http://localhost:3000/static/api

If your version of config.json was created a long time ago and you don't have the most up to date data from config.json.example, you might need to edit config.json to change the BASE_URL line to  and then cancel and rerun the npm start command.

Preference Settings
Habitica has preference settings for the users to customise the website's behaviour. You can add new settings if necessary. However please do not add settings that only a small proportion of users are likely to use, or settings for trivial customisations; instead choose a behaviour that the majority of users are likely to be happy with. Too many preferences will make the settings screen look bloated and cluttered and will increase the appearance of complexity. If you're uncertain about whether a preference setting is desirable, you can discuss it in the Aspiring Coders guild or in a relevant GitHub issue or pull request.

Cool tip for storing preferences
From a git issue discussing a possible new user preference setting comes this tip: New preference options can be added to  and the database will pick them up automatically (but if it's ever necessary to adjust the database, we can do that).

"Information for Developers" Sections on General Wiki Pages
At the bottom of some wiki pages, you will see sections called Information for Developers. These contain useful tips for Blacksmiths related to the content on that page.

The information is hidden behind a spoiler-style show/hide toggle button so that it doesn't clutter the page for non-technical users.

The sections use the InfoForDevs Start and InfoForDevs End templates to ensure correct formatting and wording for the buttons and other text. To see how that is done, open any page that has the section in the source editor.

To see a list of all pages that have this section, use the "What links here" tool for the 'Start' template.

If you'd like to see all Information for Developers sections unhidden by default: /* Force all "Information for Developers" sections to always be visible */ .habitrpg-InfoForDevs { display:block !important; }   .habitrpg-InfoForDevs-hideIfDev { display:none; } You can see an example at http://habitrpg.wikia.com/wiki/User:LadyAlys/wikia.css
 * create a Wikia account if you don't already have one
 * edit your personal Wikia css file, which you will find at http://habitrpg.wikia.com/wiki/User:YOUR_USERNAME_HERE/wikia.css
 * insert these lines into that css file: