There are three things you have to do to add an assignment:
- Create a new HTML file ASSIGNMENT_ID.HTML in the assignments directory
- Create a new JS file ASSIGNMENT_ID.JS in the assignments directory
- Parse new assignment list trough terminal initialization
How to introduce the new assignment to HG:
$(terminalSelector).hackerGame({ assignments: [ ... new element ... ] });
If you want the assignment to be one of the defaults, skip the third step and change the configuration file (place the assignment somewhere in the context)
The JavaScript file MUST call the hg.load.assignment() method.
Example:
hg.load.assignment([
taskObj1,
taskObj2
], {
startTime: 0
});
The HTML file must contain these DIV element:
- ass-title: Assignment title
- ass-instructions: the assignment instructions
- ass-tasks: task descriptions (each task in a separate DIV element with the same class as the assignment id
- ass-learn-more: the learn-more text
- ass-try-it-out: the try-it-out text
The HTML file can also contain:
- ass-greeting: The greeting mail message (optional)
- ass-misc: other stuff
Example:
<div id="ass-title">Assignment title</div>
<div id="ass-instructions">...</div>
<div id="ass-greeting">...</div>
<div id="ass-tasks">
<div class="task1"></div>
<div class="task2"></div>
</div>
<div id="ass-learn-more"></div>
<div id="ass-try-it-out"></div>
There are two types of translations used in HackerGameJS (yes, sorry about that). The majority are in form:
englishSentence => newTranslationSetence
and a fiew are:
someKey => translation
In the begining of the development there were only sentence translations. When the pages nedeed translations, it would be to brutal to maintain the page sentance by sentence.
Example:
<p class='i18n' data-lang='English setence'></p>
or
<p class='i18n text' data-lang'pageKey'>
This paragraph has a lot of words and it wouldnt be optimal to
maintain it sentence by sentence.
</p>
HTML elements with class i18n will get translated.
Example:
string = hg.t('English sentence')
Example:
hg.load.language("myLanguage", {
'English sentence': 'Sentence in new translation',
'pageKey': 'This is the translation for the paragraph that has too many words.'
});
Special module with functions that get triggered by location hashes. Spec: #/actionMethod/actionArgument -> hg.action.actionMethod (actionArgument) Example: #/page/help -> hg.action.page (help)
- assId string - selected assignment
Select assignment.
- inputId string - input tab to change to
Change input tab.
Open email.
- pageId string - page id to switch to
Switch page
- tabId string - info tab to change to
Change info tab.
- term object - terminal object
- string string - current input string
- fn function - terminal callback
Searches trough available commands and returns the candidates for command completion.
- assId assignment - id
Constructor for Assignment object. Note: this gets called before assignment is initialized. Constructor also calls loadAssignment ()
Object fields:
- id string - assignment id
- currentTask integer - pointer to current task
- numOfTasks integer - number of tasks
- tasks array - container for Task objects
- isRunning boolean - true when assignment is initialized and started (running)
- startTime integer - starting counter for timer
- evaluate function - evaluate function for every assignment
- queue array - assignment actions queue
- maxTaskPoints integer - maximum number of points tasks can bring
- bestScore integer - best assignment score
Object methods:
- startTimer () - start hg.timer
- nextTask () - switch to next task
- fail () - called when assignment fails
- complete () - called when all tasks are completed
- name string - computer name
- isDefault boolean - is default computer?
Constructor for Computer object. Fields:
- name string - computer name
- isDefault boolean - is default computer
- location string - local ip address
- pwd string - current working directory
- hasChanged boolean - has filesystem changed (important for dumping)
- fs object - computer file system
- dfs object - computer dynamic file system
- properties object - all computer properties in a object
- hostname string - computer hostname
- localIP string - computer local IP adress 192.168.1.2
- user:* string* - default computer user
- externalIP (NOT USED)
- visibleFrom (NOT USED)
- domain (NOT USED)
- commandBlackList object - blacklisted commands
- computer object - computer object
- config object - configuration for the state
- innerState object - NOT USED YET
Constructor for State object.
Object fields:
- computer object - computer object
- place object - directory object for current working directory
Object methods:
- hasCompletedAssignments ()
- getDefaultComputer ()
- changeDir (directory)
- makeDir (directory)
- removeFile (filePath)
- openFile (filePath)
- saveFile (filePath, content)
- emptyTmp ()
- copy (src, dst)
- move (src, dst)
- taskObj object - task configurations
- taskHtml string - task html from #stash
Object taskObj:
- evaluate function - callback when user uses a command
- set function - callback when task is initialized
- unset function - callback when task is destroyed
- points number - points user can achieve with this task
- bonus function - add a callback to check if bonus should be added
Constructor for Task object.
Object fields:
- id string
- evaluate function
- set function
- unset function
- pointes function
Object methods:
- switchTask (previousTask) - switch between tasks
Dump computer information if changes. Also, mark computer.hasChanges = false If chanes in filesystem (computer.hasChanges):
- dump = [ computerObj, callback ] Else:
- dump = [ null, callback ] Computer object has next keys:
- user string
- hostname string
- fs object - file system (DFS cannot be loaded)
If callback is called, the computer.hasChanges is reset to True
Return: array
Dump state to JSON. Return format: [stateJson, callback] Object stateJson can be null if no state changes exist. Callback resets the changes back to previous state. Everything That has happend between one dump and one callback call is also appended to the state.
Blur Editor (switch to Terminal tab)
Disable Editor
Enable Editor
Focus Editor (switch to Editor tab)
Get Editor content
Save contents to watch file (this overwrites special fies !!!)
- content string - content to be set
Set editor content.
Unwatch current file.
- path string - path to file to watch
- specialInput string - optional special input for special files
Watch text or special file in editor. If not special, the file get editable.
- input string - user input
- term object - terminal object
Evaluates user input. This is the most important function called from the terminal. It serches for available commands or parses the input to javascript or displays an error. It also calls the assignment callback for evaluating tasks.
THIS FUNCTION IS PASSED TO THE TERMINAL.
Indicator object. Cosits of global states and constants.
- modal boolean - is there a modal dialog displayed
- NUM_OF_ASSIGNMENTS int - number of assignments
Initializes basic computer comands when the computer is initialized.
- tasks array - contains tasks objects
- other object - other important information
Object in 'tasks' array should contain:
- evaluate function - evaluation function
- id string - task id
- set function - callback before task is initialized
- unset function - callback after the task is completed
- points number - number of points this task can bring
Object 'other' must contain:
- startTime number - number of seconds available for assignment, if 0 then unlimited
Object 'other' can contain:
- startMail boolean - show email before assignment starts
- successCallback function - callback if assignment successfully completed
- failCallback function - callback if assignment fails
- startCallback function - callback after assignment is started (Start button)
- initCallback function - callback after assignment is initialized
Method loads the assignment into the game, prepares the tasks Resets the stats, etc.
THIS SHOULD BE CALLED FROM ASSIGNMENT SCRIPTS!
- internalFilePath string - internal file path to new file
- externalFilePath string - external file path to file that will be loaded
Load external file as text with AJAX into the filesystem as a text file.
- languageId string - language id
- languageObject object - contains translations in form english: translated
Load language object to the game. Doesn't run hg.refreshTranslations()
- path string - internal path to file
- content function (input) - function which takes one argument
Method for loading special files. Special file is represented in filesystem as a binary object, but its logic ('conent') gets loaded into the dynamic fielsystem (DFS).
The content function must return an array in the form: [status, string]
- status boolean - if the input was correct
- string string - the return string
- obj object - state object
Load saved state.
State object can contain two objects:
- state object - same format as in config
- computer object
- user string
- hostname string
- fs object - file system (DFS cannot be loaded)
Close message dialog.
Message dialog box initialization after mail icon is clicked.
- message object
- clickOpen function - callback when mail is opened
Object 'message' must contain:
- body string - message body Object 'message' can contain:
- sender string - sender name
- isSensei boolean - if true, message will be treated more importantly
- button object - object for mail button
Object 'button' must contain:
- name string - button title
- action function - callback when button is clicked
Call to set the mailbox icon as empty.
Call to set the mail icon red.
- message : string
- title : string - title string
Display alert dialog with 'message' as message body and 'title' as dialog title. If no title is specified, 'Alert' will be used
- location string - location to ping
Pings 'location' and returns TRUE on success.
- selector string - jQuery selector string
Refresh body or selector translations.
- hold boolean - if true, don't automatically refresh stats
Aggregates overall score.
- stat string - stat to be incremented
- val integer - increment stat by value
- hold boolean - if true, don't refresh stats after changes
Increment stats.
Function refreshes the stats values in DOM.
- string string - translation key
The 'string' gets translated if possible.
- text string
Echo the text in the terminal. It also translates the text if possible.
- text string
Echo the text in the terminal as an error. It also translates the text if possible.
Terminal object (contains all $.terminal api methods).
Integer field containing remaining number of seconds.
Stores the last counter value if timer was stopped before counter was zero.
- setCounter integer - counter value
- ztCakkbacj function - callback called when counter gets to 0
Initialize and set the timer.
Start counting.
Stores the timeout value.
Stop counting. Stop callback doesn't get called.
- path string - relative path to file
Return: string
Create absolute path to file (this is a straightforward and stupid method, it just prepends PWD. Note: no checking if path is already absolute is done.
- absPath string - absolute path to file
- totalTest boolean - if true, path will be cleand and if relative, changed to absolute Return: boolean
Check if wile is writtable.
- path string - absolute or relative path
Return: string
Return cleaned path. Example: /one/two/../two2 -> /one/two2
- default object - default object that gets overwritten
- over object - overwritting object
Return: object
This utility behaves a lot like $.extend but if it detects that both default[key] and over[key] are objects, it will recursively call itself upon them.
- loc string - location of file (filepath)
Return: boolean
Check if file exists. Note: directory is also a file!
- filemixed - file content
- longName boolean - if true, long filetype name will be returned
Return: string
Return filetype with respect to file content. Types:
- b: binary ( can also be special! )
- d: directory
- t: text
- pathToFile string - path to file
Return: array|null
Gets the specified file in a informative way. The output array is: [dirPath, fileName, fileContent, fileType]
- dirPath: path to containing directory
- fileName: filename of file in DirPath
- fileContent: contents of file
- fileType: file type of file If file doesn't exits or path is incorrect, null is returned.
- pathToFile string - path to some file
Return: array
Function cleans the path and returns the path to file and filename in array. Example 1: /home/user/file -> ['/home/user/', 'file'] Example 2: / -> ['/', '']
- path string - ABSOLUTE path to special file
Return: function
Get special file. If no special file exists, a function that when called returns null is generated.
- dir string - directory filepath
Return: boolean
Check if file is directory. If file doesn't exists, false is returned
- input string - user input from command line
Return: array
Parse user input and return: [command, argsStr, args, rawArgsString]
- command string - user command
- argsStr string - parsed arguments as a string (removing ' and " )
- args array - parsed argumetns as array
- rawArgsString string - same as argsString but unprocessed
- rawPathString string - raw path (relative, absolute ... )
Return: array - path array
Return path array. If rawPathString isn't specified, PWD will be used. Absolute path is prepended automatically. Example: /one/two -> ['one', 'two']
- dir string : directory to iterate to
- fn function - callback
Return: boolean
Iterate trough path and call the callback function on the last directory that was iterated. Return true if everything OK, else false.
Return: string
Generate random IP address.
- from integer
- to integer
Return: function
Return generator for random response time.
- array array
Return a random value in array.
- length int - length of random string
- lower boolean - if true, only lower letters
Generate a random string.
- pathToFile string - pat to file to set (can be a new file)
- content mixed - content of the new file
Return: boolean
Sets new file content if path to file exists and return true. If not, false is returned.
- settings object - settings objects to overwrite config
This is the base initialization. It initializes the terminal and assignments. It has to be called right after the page loads and before any server scripts or similar are loaded.
Content initialization (translations evaluated and loading image removed). Note: has to be called after baseInit and after any server scripts are loaded.
- evt object - event object
Event function which listens to hash changes.
Initialize loaded assignment. Switch to Game page.
- selector string - jQuery selector string
Go trough selector or just body and replace dynamic fields with associated values.
- $task jQuery object - task HTML
Initialize task HTML. Convert hits and help to buttons etc.
Start assignment (time courting, command line evaluation ...).
- settings object - object to overwrite configuration
Make DOM object into the game terminal
Make DOM object into game Editor. Settings are currently not used.
Initialize hackergame timer.
- numOfBlinks integer - number of blinks
- time integer - time between blinks
jQuery plugin for DOM element blinking.
Close current task.
Evaluate assignment queue. Calculate bonuses etc.
- assId string - assignment id
- callback function - callback called when script file is loaded
Load assignment HTML and JavaScript. First HTML is loaded into #stash div element. Then the JS is loaded and evaluated (it can use its HTML elements directly from #stash. Callback is called if script is successfully loaded.
Return: integer
Convert remaining time to score.