Tutorial: WebClient scripting with JavaScript

General discussion about the Genesis Web Client located at https://www.genesismud.org/play/

Moderator: Eowul

Forum rules
- Use common sense and be respectful towards each other at all times, even when disagreeing.
- Do not reveal sensitive game information. Guild secrets, player seconds are examples of things not allowed.
Post Reply
User avatar
Posts: 45
Joined: 28 Sep 2015 16:42

Tutorial: WebClient scripting with JavaScript

Post by britanica » 28 Sep 2015 18:14

I was excited when I finally decided to extend the web client with triggers and aliases rather than create a new UI for a c# rules engine. I saw the power of what could be done, I just had one reservation as I am not as fluent in js as I am with c#. As a result, I like examples with working outcomes, so I though I might add some.

A - What can I display?

Goal: Dump the contents of the gmcp to the screen.

Create an Alias that will output the information to screen
  • Click on Settings button in lower right corner
    Click on the Aliases tab
    Click on the Add new alias button in the lower left corner
    In the Command text box type: showgmcp
    Under the Script Type, select Javascript
    Add the below code to the script body
    Click save

Code: Select all

gwc.output.append('gmcp: ' + JSON.stringify(gwc.gmcp.data));
Now when you close the settings windows and type <showgmcp> in the main web client, you will receive a json string with all of the data.

What can this data do for us?
We could create another alias that moves us a random direction but not take the 'out' exit

Code: Select all

var room = gwc.gmcp.data.room;
var index = Math.floor(Math.random() * (room.exits.length));
var nextRoom = room.exits[index];
if (nextRoom && nextRoom != 'out')
B - How do I store state?

Goal: Reference data from another Alias or Trigger

Create an alias that modifies the behavior of a trigger passing information with the gwc.userdata
  • Click on Settings
    Click on Alias Tab
    Click on Add new alias
    Enter the Command Name: enable_scavaging
    Enter the Code

Code: Select all

if (args['*'] == 'on') {
    gwc.userdata.scavaging = true;
} else {
    gwc.userdata.scavaging = false;
  • Click Save
    Click on Triggers Tab
    Add new trigger
    Enter Patter: You killed

Code: Select all

if (gwc.userdata.scavaging)
    gwc.connection.send('get all from corpse');
When you close the settings window and type "enable_scavaging on" you will automatically collect anything you killed.

Now while the above only uses a Boolean you can hold objects in the userdata dictionary. What name can you select? Any. As a javascript object, just make up a name and stick it on the end of userdata. One thing you must keep in mind, is that it is send back to the server between requests. It is best to keep this light.

Alternative storage methods to be investigated.
Web Service on http://localhost/

C - How can I count to ten with a delay when no messages arrive to be triggered upon?

Goal: Perform a series of steps with a time delay

Create an alias that gets your character to count to ten slowly
  • Click on Settings
    Click on Alias Tab
    Click on Add new alias
    Enter the Command Name: count_to_ten
    Enter the below code
    Click Save

Code: Select all

var Counter = function()
  var self = this;
  self.count = 0;
  self.IncrementCount = function()
    if (self.count <= 10)
      gwc.connection.send('\'' + self.count + ' mississippi');
      setTimeout(self.IncrementCount, 2000);
      gwc.connection.send('\'Over 10');

var c = new Counter();
setTimeout(c.IncrementCount, 2000);
Now close the settings and back in the main web client window type <count_to_ten>

Posts: 23
Joined: 06 Feb 2019 18:28

Re: Tutorial: WebClient scripting with JavaScript

Post by Arsonist » 01 Mar 2019 05:54

So you have the command to go to a random room, how would one adapt that to make a command that would not... double back if you know what I mean.
To explain further, I have a series of triggers helping me grind and the random exits is one of them; however, it ends up draining my energy really fast going from room to room- back and forth.

"Build a man a fire, he'll be warm for a day.
Set a man on fire, he'll be warm for the rest of his life." Terry Pratchett >:)

Post Reply