SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 18/09/2015, 08:07 AM   #101
SkittlesAreFalling
Big Clucker
 
Join Date: Oct 2013
Posts: 155
Reputation: 14
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by HardWar View Post
and how to escape the string for use in SQLite functions? in pawn I use format and %q
You'll be interested in:
Code:
var mysql_escape_string = function(str) {
    return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function (char) {
        switch (char) {
            case "\0":
                return "\\0";
            case "\x08":
                return "\\b";
            case "\x09":
                return "\\t";
            case "\x1a":
                return "\\z";
            case "\n":
                return "\\n";
            case "\r":
                return "\\r";
            case "\"":
            case "'":
            case "\\":
            case "%":
                return "\\"+char; // prepends a backslash to backslash, percent,
                                  // and double/single quotes
        }
    });
}

String.format = function(str, arr) {
    var i = -1;
	var callback;
	
    callback = function(exp, p0, p1, p2, p3, p4) {
        if(exp=='%%') {
			return '%';
		}
		
        if(arr[++i] === undefined) {
			return undefined;
		}
		
        var exp  = p2 ? parseInt(p2.substr(1)) : undefined;
        var base = p3 ? parseInt(p3.substr(1)) : undefined;
        var val;
		
        switch(p4) {
            case 's': val = arr[i]; break;
            case 'c': val = arr[i][0]; break;
	    case 'i': val = arr[i].toString(); break;
	    case 'd': val = arr[i].toString(); break;
            case 'f': val = parseFloat(arr[i]).toFixed(exp); break;
            case 'p': val = parseFloat(arr[i]).toPrecision(exp); break;
            case 'e': val = parseFloat(arr[i]).toExponential(exp); break;
            case 'x': val = parseInt(arr[i]).toString(base?base:16); break;
            case 'd': val = parseFloat(parseInt(arr[i], base?base:10).toPrecision(exp)).toFixed(0); break;
	    case 'q': val = mysql_escape_string(arr[i]); break;
        }
		
        val = (typeof(val) == 'object') ? JSON.stringify(val) : val.toString(base);
		
        var sz = parseInt(p1); /* padding size */
        var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */
		
        while(val.length<sz) {
			val = p0 !== undefined ? val+ch : ch+val; /* isminus? */
		}
		return val;
    }
	
    var regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd])/g;
    return str.replace(regex, callback);
}

String.prototype.format = function() {
    return String.format(this, Array.prototype.slice.call(arguments));
}
works similar to SAMP's format function. [https://wiki.sa-mp.com/wiki/Format]

Example:
Code:
const COLOR = {
	WHITE 	: 0xFFFFFFFF,
	TEAL	: 0x008080FF
};

SendClientMessage(player.id, COLOR.WHITE, ("Welcome to {%06x}%s{%06x}, {%06x}%s{%06x}!").format((COLOR.TEAL >>> 8), ServerData.name, (COLOR.WHITE >>> 8), (COLOR.TEAL >>> 8), player.name, (COLOR.WHITE >>> 8)));
^ copy&paste from my code.

Last edited by SkittlesAreFalling; 18/09/2015 at 10:37 AM.
SkittlesAreFalling is offline   Reply With Quote
Old 18/09/2015, 08:36 AM   #102
Mowgli
Huge Clucker
 
Join Date: Apr 2009
Location: England
Posts: 268
Reputation: 1
Default Re: samp.js - JavaScript for SA-MP

Interesting.
Mowgli is offline   Reply With Quote
Old 18/09/2015, 10:28 AM   #103
SkittlesAreFalling
Big Clucker
 
Join Date: Oct 2013
Posts: 155
Reputation: 14
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by Kamper View Post
Is it possible create a global variable to be accessible from another JS files? A kind of global variable accessible from any file

#edit
I figured out that I can simply use include function to load some JS file with a global variable xD

#another question
Can I assign a value to a new attribute of player's object, eg player.test = 1, that when the player disconnect this object will be cleared?

I noticed that when you use the function GameModeExit to reload the server, the changes made in source don't have any effect in the server until you close/open it again. It's quite bother...
#1
Code:
include("js/SomeFunction.js"); // Includes all global variables from the path into the script.

SomeFunction();
Code:
var script = require("js/SomeFunction.js"); // Includes all exported variables into the script.

script.SomeFunction();
SomeFunction.js
Code:
var SomeFunction = function() {
	print("This is some function");
}

exports = {
	SomeFunction : SomeFunction
}
#2
Use a global variable instead:
var PlayerData = [];

//PlayerConnect
PlayerData[player.id] = {};
PlayerData[player.id].test = 1;

//PlayerDisconnect
PlayerData[player.id] = null; // Or if you want it to free up memory do: delete PlayerData[player.id];

Last edited by SkittlesAreFalling; 26/09/2015 at 07:45 PM.
SkittlesAreFalling is offline   Reply With Quote
Old 18/09/2015, 04:40 PM   #104
HardWar
Huge Clucker
 
Join Date: Oct 2012
Posts: 388
Reputation: 44
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by SkittlesAreFalling View Post
You'll be interested in:
~code~

works similar to SAMP's format function. [https://wiki.sa-mp.com/wiki/Format]

Example:
~code~
^ copy&paste from my code.
Thank you!
HardWar is offline   Reply With Quote
Old 19/09/2015, 07:59 AM   #105
!damo!spiderman
Huge Clucker
 
!damo!spiderman's Avatar
 
Join Date: Oct 2006
Posts: 244
Reputation: 160
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by Kamper View Post
#another question
Can I assign a value to a new attribute of player's object, eg player.test = 1, that when the player disconnect this object will be cleared?

I noticed that when you use the function GameModeExit to reload the server, the changes made in source don't have any effect in the server until you close/open it again. It's quite bother...
Yes you can assign custom variables to the player object and it should be cleared when they disconnect

You can put reload('scriptname.js'); in the GameModeExit and it will reload the javascript file
__________________
!damo!spiderman is offline   Reply With Quote
Old 19/09/2015, 11:38 AM   #106
Kamper
Guest
 
Posts: n/a
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by !damo!spiderman View Post
Yes you can assign custom variables to the player object and it should be cleared when they disconnect

You can put reload('scriptname.js'); in the GameModeExit and it will reload the javascript file
Great! For me that is the best plugin ever in SA-MP! I hope you keep working on it! I'm creating a great project using SAMP.js and build some includes to contributing to it
  Reply With Quote
Old 22/09/2015, 08:45 PM   #107
Omer.
Little Clucker
 
Omer.'s Avatar
 
Join Date: Dec 2010
Posts: 39
Reputation: 14
Default Re: samp.js - JavaScript for SA-MP

WOW! this is great! I've just started to new script with your plugin, had same problem with non-english characters

saved .js file with encoding utf-8

Code:
$server.on("ScriptInit", function() {
	setlocale("Turkish"); // this only works on console
}
Code:
result:
Attached Images
File Type: png sa-mp-014.png (29.7 KB, 99 views)
File Type: png code.png (1.2 KB, 95 views)

Last edited by Omer.; 22/10/2015 at 06:44 PM.
Omer. is offline   Reply With Quote
Old 23/09/2015, 06:11 PM   #108
DRIFT_HUNTER
High-roller
 
Join Date: Oct 2009
Posts: 2,151
Reputation: 215
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by Omer. View Post
WOW! this is great! I've just started to new script with your plugin, had same problem with non-english characters

saved .js file with encoding utf-8

Code:
$server.on("ScriptInit", function() {
	setlocale("Turkish");
Code:
player.message("ç Ç ğ Ğ ı İ ö Ö ş Ş ü Ü");
result:
As far as i know samp client uses unicode... (But i may be completely wrong)
DRIFT_HUNTER is offline   Reply With Quote
Old 23/09/2015, 06:56 PM   #109
Omer.
Little Clucker
 
Omer.'s Avatar
 
Join Date: Dec 2010
Posts: 39
Reputation: 14
Default Re: samp.js - JavaScript for SA-MP

Quote:
Originally Posted by DRIFT_HUNTER View Post
As far as i know samp client uses unicode... (But i may be completely wrong)
kye says it's ANSI, I've also tried ANSI, but it gives worse result than this

Last edited by Omer.; 23/09/2015 at 10:34 PM.
Omer. is offline   Reply With Quote
Old 29/09/2015, 01:44 AM   #110
SkittlesAreFalling
Big Clucker
 
Join Date: Oct 2013
Posts: 155
Reputation: 14
Default Re: samp.js - JavaScript for SA-MP

md5.js
Code:
!function(a){"use strict";function b(a,b){var c=(65535&a)+(65535&b),d=(a>>16)+(b>>16)+(c>>16);return d<<16|65535&c}function c(a,b){return a<<b|a>>>32-b}function d(a,d,e,f,g,h){return b(c(b(b(d,a),b(f,h)),g),e)}function e(a,b,c,e,f,g,h){return d(b&c|~b&e,a,b,f,g,h)}function f(a,b,c,e,f,g,h){return d(b&e|c&~e,a,b,f,g,h)}function g(a,b,c,e,f,g,h){return d(b^c^e,a,b,f,g,h)}function h(a,b,c,e,f,g,h){return d(c^(b|~e),a,b,f,g,h)}function i(a,c){a[c>>5]|=128<<c%32,a[(c+64>>>9<<4)+14]=c;var d,i,j,k,l,m=1732584193,n=-271733879,o=-1732584194,p=271733878;for(d=0;d<a.length;d+=16)i=m,j=n,k=o,l=p,m=e(m,n,o,p,a[d],7,-680876936),p=e(p,m,n,o,a[d+1],12,-389564586),o=e(o,p,m,n,a[d+2],17,606105819),n=e(n,o,p,m,a[d+3],22,-1044525330),m=e(m,n,o,p,a[d+4],7,-176418897),p=e(p,m,n,o,a[d+5],12,1200080426),o=e(o,p,m,n,a[d+6],17,-1473231341),n=e(n,o,p,m,a[d+7],22,-45705983),m=e(m,n,o,p,a[d+8],7,1770035416),p=e(p,m,n,o,a[d+9],12,-1958414417),o=e(o,p,m,n,a[d+10],17,-42063),n=e(n,o,p,m,a[d+11],22,-1990404162),m=e(m,n,o,p,a[d+12],7,1804603682),p=e(p,m,n,o,a[d+13],12,-40341101),o=e(o,p,m,n,a[d+14],17,-1502002290),n=e(n,o,p,m,a[d+15],22,1236535329),m=f(m,n,o,p,a[d+1],5,-165796510),p=f(p,m,n,o,a[d+6],9,-1069501632),o=f(o,p,m,n,a[d+11],14,643717713),n=f(n,o,p,m,a[d],20,-373897302),m=f(m,n,o,p,a[d+5],5,-701558691),p=f(p,m,n,o,a[d+10],9,38016083),o=f(o,p,m,n,a[d+15],14,-660478335),n=f(n,o,p,m,a[d+4],20,-405537848),m=f(m,n,o,p,a[d+9],5,568446438),p=f(p,m,n,o,a[d+14],9,-1019803690),o=f(o,p,m,n,a[d+3],14,-187363961),n=f(n,o,p,m,a[d+8],20,1163531501),m=f(m,n,o,p,a[d+13],5,-1444681467),p=f(p,m,n,o,a[d+2],9,-51403784),o=f(o,p,m,n,a[d+7],14,1735328473),n=f(n,o,p,m,a[d+12],20,-1926607734),m=g(m,n,o,p,a[d+5],4,-378558),p=g(p,m,n,o,a[d+8],11,-2022574463),o=g(o,p,m,n,a[d+11],16,1839030562),n=g(n,o,p,m,a[d+14],23,-35309556),m=g(m,n,o,p,a[d+1],4,-1530992060),p=g(p,m,n,o,a[d+4],11,1272893353),o=g(o,p,m,n,a[d+7],16,-155497632),n=g(n,o,p,m,a[d+10],23,-1094730640),m=g(m,n,o,p,a[d+13],4,681279174),p=g(p,m,n,o,a[d],11,-358537222),o=g(o,p,m,n,a[d+3],16,-722521979),n=g(n,o,p,m,a[d+6],23,76029189),m=g(m,n,o,p,a[d+9],4,-640364487),p=g(p,m,n,o,a[d+12],11,-421815835),o=g(o,p,m,n,a[d+15],16,530742520),n=g(n,o,p,m,a[d+2],23,-995338651),m=h(m,n,o,p,a[d],6,-198630844),p=h(p,m,n,o,a[d+7],10,1126891415),o=h(o,p,m,n,a[d+14],15,-1416354905),n=h(n,o,p,m,a[d+5],21,-57434055),m=h(m,n,o,p,a[d+12],6,1700485571),p=h(p,m,n,o,a[d+3],10,-1894986606),o=h(o,p,m,n,a[d+10],15,-1051523),n=h(n,o,p,m,a[d+1],21,-2054922799),m=h(m,n,o,p,a[d+8],6,1873313359),p=h(p,m,n,o,a[d+15],10,-30611744),o=h(o,p,m,n,a[d+6],15,-1560198380),n=h(n,o,p,m,a[d+13],21,1309151649),m=h(m,n,o,p,a[d+4],6,-145523070),p=h(p,m,n,o,a[d+11],10,-1120210379),o=h(o,p,m,n,a[d+2],15,718787259),n=h(n,o,p,m,a[d+9],21,-343485551),m=b(m,i),n=b(n,j),o=b(o,k),p=b(p,l);return[m,n,o,p]}function j(a){var b,c="";for(b=0;b<32*a.length;b+=8)c+=String.fromCharCode(a[b>>5]>>>b%32&255);return c}function k(a){var b,c=[];for(c[(a.length>>2)-1]=void 0,b=0;b<c.length;b+=1)c[b]=0;for(b=0;b<8*a.length;b+=8)c[b>>5]|=(255&a.charCodeAt(b/8))<<b%32;return c}function l(a){return j(i(k(a),8*a.length))}function m(a,b){var c,d,e=k(a),f=[],g=[];for(f[15]=g[15]=void 0,e.length>16&&(e=i(e,8*a.length)),c=0;16>c;c+=1)f[c]=909522486^e[c],g[c]=1549556828^e[c];return d=i(f.concat(k(b)),512+8*b.length),j(i(g.concat(d),640))}function n(a){var b,c,d="0123456789abcdef",e="";for(c=0;c<a.length;c+=1)b=a.charCodeAt(c),e+=d.charAt(b>>>4&15)+d.charAt(15&b);return e}function o(a){return unescape(encodeURIComponent(a))}function p(a){return l(o(a))}function q(a){return n(p(a))}function r(a,b){return m(o(a),o(b))}function s(a,b){return n(r(a,b))}function t(a,b,c){return b?c?r(b,a):s(b,a):c?p(a):q(a)}"function"==typeof define&&define.amd?define(function(){return t}):a.md5=t}(this);
Usage:
Code:
md5("Hello world!"); // 86fb269d190d2c85f6e0468ceca42a20
http://md5.gromweb.com/?md5=86fb269d...e0468ceca42a20

PLEASE:
Do not use this to store user passwords.
SkittlesAreFalling is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript. Banditukas Everything and Nothing 18 04/04/2015 05:00 PM
[Pedido] botão: script/ HTML/ JAVASCRIPT | SAMP FOLDER/FILE MestreSmart Português/Portuguese 6 28/02/2015 05:16 PM
Use PHP in javascript Banditukas Everything and Nothing 5 08/11/2014 05:44 AM
javascript samp:// [Ger]Little_Grandpa Help Archive 6 21/08/2010 06:24 PM


All times are GMT. The time now is 04:08 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.