Javascript plugin

Started by NewK, Aug 16, 2018, 03:42 AM

Previous topic - Next topic

NewK

VCMP bindings for javascript based on the already existing java plugin.

Download

There are no 32bit linux versions available because there's no V8 java bindings available for 32bit linux.

All javascript code runs on V8 v5.9.1. I'll look into possibly upgrading to a newer version in the future but right now this is not a priority. I've included a sample script that's set up with an IRC bot that connects to a LUnet channel and that echoes what people type ingame and what people type on IRC. This script uses SQLite for player registration and has the /login command the /register command and the /stats command. If you'd like to use MySQL instead of SQLite on this sample script, all you need to change is how you create the DB connection. Everything else is the exact same. The SQL module shares the same API for both SQLite and MySQL, function names are all the same. So if you ever need to migrate your server from SQLite to MySQL or MySQL to SQLite, all you'll need to change is how you create the connection. You can see an example on line 35 of the main.js file. If you use that connection instead of the one on line 32, you won't need to change anything else on the script, you just need to create your MySQL schema and create the same players table like the one on the SQLite db.

If you'd like to start fresh with an empty script, you can remove this script by deleting the "commands" folder inside the "src" folder, deleting the "sampleServer.db" SQLite database and deleting all the code inside "main.js". Just don't delete the node_modules folder because you'll need this to use the available modules.

Installation instructions

A few notes
  • All javascript code must be inside the "src" folder
  • The entry script file must be named "main.js" and must be placed on the root of "src"
  • The load() function is equivalent to squirrel's dofile() function and can be used to load other .js files
  • require() is used to import modules you want to use, the available modules are inside the node_modules folder (the only reason I decided to keep this folder with the same name of the nodejs's node_modules folder was to help text editors (sublime, visual studio code, atom, etc...) give you better autocomplete suggestions when importing the modules
If you want to use javascript classes you need to enable javascript's "strict mode" on your classes with "use strict". For example:
"use strict"
class Rectangle {
    constructor(height, width) {
      this.height = height;
      this.width = width;
    }
  }
This is a limitation of version 5.9.1 of V8.

In the next following days I'll be creating documentation for the plugin. If you have any questions feel free to ask here. If you have problems create a new topic on the support board seeing as support is not allowed on this board.

NewK

#1
Documentation here. It's still not finished but it's a starting point.

EDIT: A server crashing bug was fixed on the java plugin that this plugin relies on. I've updated the 3 links above to include it. Make sure to either re-download it or just download the plugin from here and put it on your plugins folder

NewK


NewK

#3
A new version of the plugin has been released. The plugin has gone through a major refactor and now uses a module loader to load each module individually. What this means is that all modules have been removed from the plugin, as in, the plugin no longer contains module specific code in it. Each module is now an individual .jar file that you just have to download and drop into the /modules/ folder. As a result, the plugin is now "thinner" and lighter which means faster startups and no unnecessary resource usage.

Make sure you download the new version from the website. After downloading the plugin, check the sidebar for "modules" and download the modules that you need and extract the content of the each module zip file to the "modules" folder (some modules come with a lib folder, this should go inside the "modules" folder too).


Module release: Websockets
Documentation

With the release of this new plugin version, a new websockets module has also been released. This module allows you to create websocket clients and servers from your script. Giving you the ability to interact with any other websocket server or client running elsewhere. All of this is done in an asynchronous lagg-free manner using the event loop model. A few use cases for this module would be having a 2-way communication with the browser, or for example connecting multiple VCMP servers to eachother, as in, you could type in chat while you are inside server #1 and make your messages appear on your 2nd and 3rd server as if you were inside those servers too.

Here's a demo I created that showcases a 2-way browser-->server, server-->browser communication where I send messages both ways in real time.

The code for that example is as follows:
index.html
<html>
<body>

    <input id="userInput" type="text">
    <button onclick="ws.send(document.getElementById('userInput').value)">Send</button>

    <div id="message">Connecting..</div>

    <script>
        function showMessage(text) {
            document.querySelector('#message').innerHTML = text;
        }

        var ws = new WebSocket('ws://localhost:3030/general');

        ws.onopen = function () { showMessage('Connected!'); };
        ws.onclose = function () { showMessage('Lost connection'); };
       
        ws.onmessage = function (msg) {
            document.querySelector('body').innerHTML += "<p> " + msg.data + " </p>"
        };
    </script>
</body>
</html>

VCMP Server main.js:
"use strict"

var ws = require("websockets");
var socketServer = null;

function onServerInitialise() {

// starts up the websocket server
    var serverEvents = {
        onMessage: function (conn, msg, uri, address) {
            server.sendClientMsg(null, new Colour(93, 193, 89), "(WEB): " +msg)
        }
    };
    socketServer = ws.startServer("/general", 3030, { events: serverEvents });
}

function onPlayerMessage(player, message) { //when a player types in chat
    socketServer.broadcast("/general", player.name + ": " + message);
    return true;
}

NewK

Module release: Discord
Documentation

A discord module to interact with a discord bot from your vcmp server. Can be used to send messages to a specific discord channel or private messages to a specific discord user inside the servers that the bot is in. At the moment the module implements 3 events:
  • onReady ( when the connection to the bot is opened)
  • onMessage (when someone sends a message to a channel )
  • onPrivateMsg (when someone sends the bot a private message )
You can check the documentation for more information on how to implement those events. More events will be added in the future.

MEGAMIND

awsome.. Thats something i was waiting for in vcmp

NewK

#6
New update released: Download here and put javascript-plugin.jar on the server root directory ( replace the old one ) and integration-javascript.jar on the lib folder (replace the old one) or just download everything from the home page
  • CoordBlipInfo properties are now writable
  • Added a new .pos property to CoordBlipInfo (both .pos and .position can be used)
  • Fixed server.getCoordBlipInfo(index) creating a new CoorBlipInfo when passed an index of a non existent CoordBlip

MEGAMIND

#7
sorry for bumping but i guess i posted my msg wrong earlier in other java plugin https://forum.vc-mp.org/index.php?topic=2574.60 , my quesrion is how do u connect this to ur server any video will be helpfull

also these both topics are interlinked or totaly different cuz its more confusing now

NewK

This plugin is built on top of the java plugin so what I wrote in the other topic applies to this aswell. But I dont recommend using this plugin anymore as I'm no longer working on it, if you still want to use it, the documentation website has all the steps needed to install it

MEGAMIND

Quote from: NewK on Aug 20, 2024, 01:44 PMThis plugin is built on top of the java plugin so what I wrote in the other topic applies to this aswell. But I dont recommend using this plugin anymore as I'm no longer working on it, if you still want to use it, the documentation website has all the steps needed to install it
k thanks