View unanswered posts | View active topics It is currently 18 Jul 2019 16:23

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.



Reply to topic  [ 2 posts ] 
Tutorial: WebClient scripting with JavaScript 
Author Message
Wizard
User avatar

Joined: 28 Sep 2015 16:42
Posts: 45
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:
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:
var room = gwc.gmcp.data.room;
var index = Math.floor(Math.random() * (room.exits.length));
var nextRoom = room.exits[index];
if (nextRoom && nextRoom != 'out')
{
    gwc.connection.send(nextRoom);
}


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:
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:
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.
HTML5 Web DB
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:
var Counter = function()
{
  var self = this;
  self.count = 0;
  self.IncrementCount = function()
  {
    self.count++;
    if (self.count <= 10)
    {
      gwc.connection.send('\'' + self.count + ' mississippi');
      setTimeout(self.IncrementCount, 2000);
    }
    else
    {
      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>


28 Sep 2015 18:14
Profile
Beginner

Joined: 06 Feb 2019 18:28
Posts: 21
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.

_________________
Signed
Kolvar

"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 >:)


01 Mar 2019 05:54
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.