Quote:
Originally Posted by maximus_asinus
So I was scripting in an online editor and was annoyed by the people popping into the level to see what I was working on. So I scripted a system to remove players froma level in the offline editor in GS1. As part of my practice learning GS2 I want input on mistakes I made, how to make the script more efficient, design security for the NPC, and finally convert the script into GS2. Any advice is appreciated.
PHP Code:
// Script to add/remove players from ban list
if (playerchats) {
if (strequals(#a,maximus_asinus)) {
if (startswith(/add,#c)) {
tokenize #c;
this.duplicate = false;
if (strequals(#t(1),#a)) {
setplayerprop #c,You cannot ban yourself.;
}
else {
for (i=0;i<sarraylen(server.warp);i++;) {
if (strequals(#I(server.warp,i),#t(1))) {
this.duplicate = true;
}
}
if (this.duplicate == true) {
setplayerprop #c,"#t(1)" is already on the ban list.;
}
else {
addstring server.warp,#t(1);
setplayerprop #c,Added "#t(1)" to the ban list.;
}
}
}
if (startswith(/remove,#c)) {
tokenize #c;
for (i=0;i<sarraylen(server.warp);i++;) {
if (strequals(#I(server.warp,i),#t(1))) {
deletestring server.warp,i;
setplayerprop #c,Removed "#t(1)" from the ban list.;
}
}
}
}
}
PHP Code:
// Script that removes the player
if (created) timeout = 0.05;
if (timeout) {
for (i=0;i<sarraylen(server.warp);i++;) {
if (strequals(#I(server.warp,i),#a)) {
setplayerprop #c,You're banned from this level.;
setlevel2 onlinestartlocal.nw,30,30;
}
}
timeout = 0.05;
}
I'm assuming placing a for loop inside a never ending timeout is bad practice and possibly process intensive. Can you manipulate another player's level and coordinates through a script like this? That way I could remove the player when I add them to the list and only have a playerenters check to see if they're on the list which would eliminate the timeout completely. This would also help me understand how you can manipulate other players via script.
Also I think I read that variables can now be assigned text values in GS2 like how strings work in GS1. Can you manipulate those variables in the same way as I am doing it in my script? If so, how?
|
Yea timeout is definitely not needed you can just invoke playerenters and kick them.
I would create a database and when you create it place it in your level.
PHP Code:
function onCreated() {
this.commandList = {
"account1",
"account2"
};
}
function onPlayerChats() {
if(!(player.account in this.commandList)) return;
if(player.chat == "/add") {
temp.user = player.chat.substring(5);
player.chat = "";
onAddPlayer(temp.user);
}
if(player.chat == "/remove") {
temp.user = player.chat.substring(8);
player.chat = "";
onRemovePlayer(temp.user);
}
}
function onAddPlayer(temp.user) {
if(!(temp.user in this.allowedUsers)) {
this.allowedUsers.add(temp.user);
this.chat = format("User %s has been added!", temp.user);
} else {
this.chat = format("User %s already added!", temp.user);
}
scheduleevent(3, "onResetChat");
}
function onRemovePlayer(temp.user) {
if(temp.user in this.allowedUsers) {
this.allowedUsers.remove(temp.user);
this.chat = format("User %s has been removed!", temp.user);
} else {
this.chat = format("User %s is not in allowlist!", temp.user);
}
scheduleevent(3, "onResetChat");
}
function onResetChat() {
this.chat = "";
}
function onPlayerEnters() {
if(!(player.account in this.allowedUsers)) {
player.chat = "I'm not allowed in this level!";
player.setlevel2("levelname.nw", x, y);
}
}
I didn't test this as I did it on the forums, but you should get a good idea on how to do it if it doesn't work. I added a couple methods that I normally wouldn't in such a small situation like format. But it does make your code cleaner and its good to learn it.