-
Notifications
You must be signed in to change notification settings - Fork 18
Views v7
- status: complete
- version: 7.x
- follows from: PushManager
nodeGame serves all the static files for the browser from the
public/
folder of a game directory.
However, it is possible to dynamically create HTML pages by adding
files inside the views/
folder.
The views/
folder is further divided in two sub-folders:
templates/
and /contexts/
.
The templates/
folder contains Jade files
to create and format HTML pages. Templates support dynamic code, and
allow to reuse and extends components across pages.
The /contexts/
folder contains JavaScript files exporting a local
context function for a template file. The context must include all the
variables that will be used to fill-in the content of a template.
For instructions how to format Jade templates refer to the Jade language reference.
Context files are contained inside the views/contexts/
folder. They
must be named after one of the template files inside
views/templates/
. For example, if a template is named
'instructions.jade', the context must be named 'instructions.js'.
Each context file must export a function that returns the context, that is an object containing all the variables that the template will need to fill-in its content.
Each context-function receives two input parameters: the current game-settings, and the headers of the connections of the requester.
The input parameters can be used inside the context-function to perform operations such as: randomization, shuffling, customization, as in the example below.
var J = require('JSUS').JSUS;
module.exports = function(settings, headers) {
var coins = settings.standard.COINS;
var values = [
Math.floor(coins/2, 10),
coins,
0
];
return {
"x": 0,
"title": "Quiz",
"beforeStarting": "Before starting the game answer the following questions:",
"Q": "Q",
"howManyCoins": "How many coins will you divide with your partner?",
"vals": J.shuffle(values),
"correctAnswers": "Correct Answers:"
};
};
When a client requests the url:
http://myserver/mygame/mypage.html
the following operations are performed:
-
First, the page is looked up inside the
public/
folder, and if found it is served immediately. -
Then, the page is looked up inside the
views/templates/
folder. -
Together with the template, the related context files will be located in
views/contexts/
. If found, the context will be used to build the template. -
The template is rendered and sent to the client.
-
If no static file in
public/
and no template inviews/templates/
was found, thena 404 Page Not Found code is sent to the client.
Using templates and contexts permits to achieve the internationalization of a game easily.
To do so, create a new sub-directory for each language you want to
support inside the views/contexts/
folder. For example
views/contexts/en/
, and views/contexts/de/
. In each of those
sub-directory you can store the context translated in the language of
choice.
When the address http://myserver/mygame/LANG/mypage.html
is
requested, the template views/templates/mypage.jade
will be rendered
using the context views/templates/LANG/mypage.js
.
Optionally, you can add file named languageInfo.json
inside each of
the language-directories containing information about the language. At
the very least, the languageInfo file must contain the following data:
{
"name": "English",
"nativeName": "English",
"flag": ""
}
Such information will be collected and can be queried by connected clients. For example, the Language Selector widget automatically displays all the languages available for a game and allows to select one from a list.
Next: Logging
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.