Vice City: Multiplayer

Server Development => Community Plugins => Topic started by: NewK on August 16th, 2018, 04:42 AM

Title: Javascript plugin
Post by: NewK on August 16th, 2018, 04:42 AM
VCMP bindings for javascript based on the already existing java plugin.

Download(https://newk5.github.io/jsdocs/)

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
If you want to use javascript classes you need to enable javascript's "strict mode" on your classes with "use strict". For example:
Code: [Select]
"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.
Title: Re: Javascript plugin
Post by: NewK on August 17th, 2018, 04:45 AM
Documentation here(https://github.com/newk5/vcmp-javascript-plugin/wiki). 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(https://github.com/newk5/vcmp-java-plugin/releases/tag/v78) and put it on your plugins folder
Title: Re: Javascript plugin
Post by: NewK on December 3rd, 2018, 12:39 AM
Documentation is now hosted at https://newk5.github.io/jsdocs/
Title: Re: Javascript plugin
Post by: NewK on December 20th, 2018, 05:26 AM
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(https://newk5.github.io/jsdocs/#/). 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(https://newk5.github.io/jsdocs/#/websockets)

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(https://i.imgur.com/0XoNSB3.gifv) 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
Code: [Select]
<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:
Code: [Select]
"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;
}
Title: Re: Javascript plugin
Post by: NewK on December 26th, 2018, 04:35 AM
Module release: Discord
Documentation(https://newk5.github.io/jsdocs/#/discord)

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:
You can check the documentation for more information on how to implement those events. More events will be added in the future.
Title: Re: Javascript plugin
Post by: MEGAMIND on December 26th, 2018, 04:48 AM
awsome.. Thats something i was waiting for in vcmp
Title: Re: Javascript plugin
Post by: NewK on January 2nd, 2019, 09:43 PM
New update released: Download here(https://github.com/newk5/vcmp-javascript-plugin/releases) 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(https://newk5.github.io/jsdocs/#/)