Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Lottery NPC (https://forums.graalonline.com/forums/showthread.php?t=134264255)

Gunderak 08-19-2011 10:35 AM

Lottery NPC
 
Basically i got the idea from xXAndrewXx.
But i noticed somthing with his script.
The information isn't stored properly, meaning if the npc server were restarted or someone updated the level all the data would disappear.
so i set off to script it from scratch.
here is my problem, i cant figure out how to make serverr.winner be the winner.
here is what i would like it to do, if any player has clientr.Ticket=1 there a potential candidate for the winner flag.
here is my script so far.

PHP Code:

function onCreated(){
  
onUpdateText();
}
function 
onPlayertouchsme(){
  
say2("To Buy A Lottery Ticket Say #b''/Buy Ticket'' It Cost 20 Gralats!#bThe Lottery Is Drawn At Random#bIf You Are Chosen At Random#bYoul Win The Jackpot!");
}
function 
onUpdateText(){
  if(
serverr.jackpot == 0){
    
this.chat "The Current Jackpot Is 0 Gralats.";
    return;
  }
  
this.chat "The Current Jackpot Is "@serverr.jackpot@" Gralats.";
}
function 
onPlayerchats(){
  if(
player.chat == "/Buy Ticket"){
    if(
clientr.Ticket == 1){
      
player.chat "I Have Already Purchased A Ticket!";
      return;
    }
    if(
player.rupees 20){
      
player.chat "I Dont Have Enough Money!";
    }
    if(
player.rupees 19){
      
clientr.Ticket=1;
      
player.chat "I Brought A Lottery Ticket!";
      
player.rupees -= 20;
      
serverr.jackpot += 20;
      
onUpdateText();
    }
  }
  if(
player.chat == "/Draw Lottery"){
    if(
clientr.isStaff){
      
onAwardWinner();
    }
  }
}
function 
onAwardWinner(){
  for (
temp.pl allplayers){
    
findplayer(temp.pl).clientr.Ticket=0;
  }
  echo(
"Lottery: "@serverr.winner@" Has Won The Lottery! They Won: "@serverr.jackpot@" Gralats!");
  
findplayer(serverr.winner).rupees += serverr.jackpot;
  
serverr.jackpot 0;
  
onUpdateText();



callimuc 08-19-2011 01:13 PM

1. Would be best if you post the original link. Lottery System

2.
Quote:

Originally Posted by Gunderak (Post 1664296)
The information isn't stored properly, meaning if the npc server were restarted or someone updated the level all the data would disappear.

Did you make it a class/level NPC or as a DB-NPC? I think the one from Andrew needs to be a DB-NPC

Gunderak 08-19-2011 01:54 PM

Mine i made as a level npc.
how would i accomplish the same thing without using a db npc do you know?
thanks btw.

callimuc 08-19-2011 09:14 PM

I donīt think you can store stuff without using a DB. Only way would be to store the players in server flags OR to save them in a text file where you read it from.

PowerProNL 08-19-2011 09:27 PM

I'd prefer making it databased, but nice work

fowlplay4 08-19-2011 09:32 PM

You can use a DB to store the people who entered the lottery like this:

DB-NPC: Lottery

PHP Code:

// Adds account to the list of lottery entries
public function addAccount(acct) {
  if (
inLottery(acct)) {
    
// Already in lottery
    
return false;
  } else {
    
// Add to lottery
    
this.lottery.add(acct);
    
this.trigger("update"); // Force-saves DB
    
return true;
  }
}

// Checks if acct is entered in the lottery
public function inLottery(acct) {
  return (
acct in this.lottery);
}

// Clears accounts entered in the lottery
public function clearAccounts() {
  
this.lottery "";
}

// Picks a random person entered in the lottery.
public function pickLotteryWinner() {
  return 
this.lottery[int(random(0this.lottery.size()))];


To add a player to the lottery use:
Lottery.addAccount(player.account);

To check if a player is in the lottery use:
if (Lottery.inLottery(player.account))

To pick a winner use:
temp.winner = Lottery.pickLotteryWinner();

To clear the lottery entries:
Lottery.clearAccounts();

You don't have to store any information in the player's or server flags, you can also expand upon this and add/update/store the "Jackpot" amount in it as well but I'm not going to do everything for you.

cbk1994 08-19-2011 10:31 PM

Quote:

Originally Posted by Gunderak (Post 1664296)
PHP Code:

function onAwardWinner(){
  for (
temp.pl allplayers){
    
findplayer(temp.pl).clientr.Ticket=0;
  } 


In addition to what Jer said, the above code is incorrect, although it technically works. allPlayers is an array of player objects, not accounts. That means you could just do this:

PHP Code:

for (temp.pl allplayers) {
  
temp.pl.clientr.Ticket=0;


For comparison, using findPlayer like you did would be equivalent to doing

PHP Code:

findPlayer(player).client.Ticket=0

The reason it works even though temp.pl is not an account is that the object is coerced into a string, which just happens to be the player's account (since temp.pl.name would equal temp.pl.account).

Thanks for taking the time to either style your code or run it through Jer's beautifier before posting it.

fowlplay4 08-19-2011 10:41 PM

Another thing to note is your usage of serverr variables. Judging by your script you really only need to use server (only one r) variables because you aren't accessing them on the client-side and are instead using player chat to display it.

serverr variables are stored on the server-side and synced with the client-side.
server variables are just stored on the server-side.

Gunderak 08-20-2011 03:40 AM

Thanks fowlplay for explaining it clearly how to use database npcs :D
without your help id probably be stuck for the next week :redface:
and cbk1994 i copied it into the offline level editor inside a npc and clicked style.
@Everyone thanks for your help :)

Gunderak 08-20-2011 03:44 AM

I forgot to ask, when rewarding the players the jackpot would this work?
PHP Code:

findplayer(temp.winner).rupees += server.jackpot;
server.jackpot 0

would that work even if they are offline?
thats another problem im likely to face.
and thanks fowlplay server variables work for this.
so serverr variables are accesible clientside?
and server variables are acessible serverside?
can they both be written server and clientside?
thanks

fowlplay4 08-20-2011 03:52 AM

DB-NPC's script, I labeled it as such.

and no, it returns the account of the player not the object. That's when you would use findplayer. I.e:

findplayer(temp.winner).rupees += jackpot;

Rather than having your script award them directly, you could just have the NPC pick a winner and hand out the amount yourself.

There is a way to add to people who are offline though but you should fix your scripts existing problems before you introduce more.

Use my beautifier instead of the level editor, it does a lot more than indenting. Link: http://fp4.ca/gs2beautifier

Gunderak 08-20-2011 04:34 AM

Ah ok, i think il do the awarding my self and then later if i can be bothered make it automatic, il make it send the winner to rc maybe.
also nice work with the styler, did the makers of the original one give you the source code? or is it publicly avaliable?

fowlplay4 08-20-2011 04:41 AM

Quote:

Originally Posted by Gunderak (Post 1664425)
also nice work with the styler, did the makers of the original one give you the source code? or is it publicly avaliable?

The source code for the original project is publicly available here:
http://github.com/einars/js-beautify

Gunderak 08-20-2011 09:35 AM

PHP Code:

public function pickLotteryWinner() {
  return 
this.lottery[int(random(0this.lottery.size()))];


@Fowlplay this picks the first person who entered the lottery. X_X

cbk1994 08-20-2011 10:14 AM

Quote:

Originally Posted by Gunderak (Post 1664444)
@Fowlplay this picks the first person who entered the lottery. X_X

Have you tried debugging it? What steps did you take to debug it? Did you make sure there were multiple people in the array when you ran it?

At the very least, post your whole script. It's very hard to help you when you don't provide any information besides "it's not working X_X!".

Gunderak 08-20-2011 10:56 AM

no, i havnt tried debugging it. i dont know how to do tasks that advanced.
here is the db npc script

PHP Code:

// Adds account to the list of lottery entries
public function addAccount(acct) {
  if (
inLottery(acct)) {
    
// Already in lottery
    
return false;
  } else {
    
// Add to lottery
    
this.lottery.add(acct);
    
this.trigger("update"); // Force-saves DB
    
return true;
  }
}

// Checks if acct is entered in the lottery
public function inLottery(acct) {
  return (
acct in this.lottery);
}

// Clears accounts entered in the lottery
public function clearAccounts() {
  
this.lottery "";
}

// Picks a random person entered in the lottery.
public function pickLotteryWinner() {
  return 
this.lottery[int(random(0this.lottery.size()))];


here is whats in the npc that does it

PHP Code:

function onCreated(){
  
onUpdateText();
}
function 
onUpdateText(){
if(
server.jackpot == 0){
this.chat "The Current Jackpot Is 0 Gralats.";
return;
}
this.chat "The Current Jackpot Is "@server.jackpot@" Gralats.";
}
function 
onPlayerchats(){
if(
player.chat == "/Buy Ticket"){
if (
Lottery.inLottery(player.account)){
player.chat "I Have Already Purchased A Ticket!";
return;
}
if(
player.rupees 20){
player.chat "I Dont Have Enough Money!";
}
if(
player.rupees 19){
player.chat "I Brought A Lottery Ticket!";
Lottery.addAccount(player.account);
player.rupees -= 20;
server.jackpot += 20;
onUpdateText();
}
}
if(
player.chat == "/Draw Lottery"){
if(
clientr.isStaff){
onAwardWinner();
}
}
}
function 
onAwardWinner(){
server.winner Lottery.pickLotteryWinner();
echo(
"Lottery: "@server.winner@" Has Won The Lottery! They Won: "@server.jackpot@" Gralats!");
findplayer(server.winner).chat "I Just Won The Lottery! I Won "@server.jackpot@" Gralats!";
savelog2("Lottery.txt"server.winner@" Won The Lottery! They Won "@server.jackpot@" Gralats!");
server.jackpot 0;
Lottery.clearAccounts();
onUpdateText();



0PiX0 08-21-2011 05:14 AM

Quote:

PHP Code:

if(player.rupees 19){
player.chat "I Brought A Lottery Ticket!";
Lottery.addAccount(player.account);
player.rupees -= 20;
server.jackpot += 20;
onUpdateText();



You probably should check if the player has already gotten a ticket before removing their rupees, or increase his/her chances by removing:
Quote:

PHP Code:

if (inLottery(acct)) {
    
// Already in lottery
    
return false;
  } 


Also, I believe there is a rediculously slight chance that
Quote:

PHP Code:

public function pickLotteryWinner() {
  return 
this.lottery[int(random(0this.lottery.size()))];



will return null in the case that the random function returned the maximum possibility. However, I might be wrong about that. I'm not sure.
Perhaps try something like this to ensure that does not happen, although this does very slightly affect the odds...
PHP Code:

return this.lottery[int(random(0this.lottery.size()-0.01))]; 


cbk1994 08-21-2011 05:26 AM

Quote:

Originally Posted by 0PiX0 (Post 1664584)
Also, I believe there is a rediculously slight chance that

will return null in the case that the random function returned the maximum possibility. However, I might be wrong about that.

You are. random(min, max) returns a number between the specified minimum and maximum, not equaling the maximum (the minimum can be equalled in rare circumstances).

fowlplay4 08-21-2011 06:46 AM

I've looked at his script and I don't really see anything wrong with it, and tested mine.

PHP Code:

function onCreated() {
  for (
temp.0temp.5temp.t++) {
    
test();
  }
}

function 
test() {
  
temp.accts = {
    
"a""b""c""d""e"
  
};
  
clearAccounts();
  for (
temp.accttemp.accts) {
    for (
temp.0temp.3temp.i++) {
      
addAccount(temp.acct);
    }
  }
  echo(
"People entered " this.lottery.size() SPC "Winner " pickLotteryWinner());
}

// Adds account to the list of lottery entries
public function addAccount(acct) {
  if (
inLottery(acct)) {
    
// Already in lottery
    
return false;
  } else {
    
// Add to lottery
    
this.lottery.add(acct);
    
this.trigger("update"); // Force-saves DB
    
return true;
  }
}

// Checks if acct is entered in the lottery
public function inLottery(acct) {
  return (
acct in this.lottery);
}

// Clears accounts entered in the lottery
public function clearAccounts() {
  
this.lottery "";
}

// Picks a random person entered in the lottery.
public function pickLotteryWinner() {
  return 
this.lottery[int(random(0this.lottery.size()))];


Echoed...

PHP Code:

The script of NPC Lottery has been updated by fowlplay4
People entered 5 Winner b
People entered 5 Winner c
People entered 5 Winner a
People entered 5 Winner d
People entered 5 Winner a 

The first person winning was probably just a coincidence.

Gunderak 08-21-2011 10:06 AM

*facepalm*
i couldn't be more wrong.
it turns out it was a coincidence.
Its just when he won four times in a row and we both entered in the same order each time i got suspicious.

Gunderak 08-21-2011 10:10 AM

May i ask how the computer is physically able to pick a random one?
does it use huge algorithms or something?

cbk1994 08-21-2011 10:28 AM

Quote:

Originally Posted by Gunderak (Post 1664626)
May i ask how the computer is physically able to pick a random one?
does it use huge algorithms or something?

http://en.wikipedia.org/wiki/Pseudor...mber_generator

jamitsu89 08-21-2011 02:00 PM

Quote:

Originally Posted by Gunderak (Post 1664458)
no, i havnt tried debugging it. i dont know how to do tasks that advanced.

Gunderak,
In terms of debugging, it's not as complicated as it seems. You simply make things that aren't visible, visible.

ie, in your code you have

PHP Code:

// Add to lottery
    
this.lottery.add(acct); 

For debugging, you could simply add
PHP Code:

// Add to lottery
    
this.lottery.add(acct);
    echo(
this.lottery); 

Then you'll see it in RC. If you echo'd on the clientside, you would need to press F2 to read the output.

That's just one method of debugging, but just remember all you need to do to debug it is produce extra output that you can see, so you can watch how the code is working.

Gunderak 08-21-2011 04:05 PM

In terms of debugging i didnt think for a second to think outside the square.
i understand what id have to do to debug things.
so basically try and produce a visual output so i can physically see whats not working?

Gunderak 08-21-2011 04:07 PM

@cbk1994 so it uses algorithms =D yay me is smrt! LIKE A BOSS!

jamitsu89 08-21-2011 05:16 PM

Quote:

Originally Posted by Gunderak (Post 1664647)
so basically try and produce a visual output so i can physically see whats not working?

Indeed, that is exactly what debugging aims to achieve.

Gunderak 08-22-2011 07:17 AM

@jamitsu89 ah ok i get how debugging works now :D!
@everyone thanks for your help.

Gunderak 08-22-2011 07:21 AM

@Whoever neg repped me for joking around at cbk1994 seriously skrew you.
your comment is "don't act like a child" where you are the one who is truely acting like a child.
wait sorry didnt realise we couldnt have any fun on these forums.
This proves furthermore you are the child.

ff7chocoboknight 08-23-2011 05:22 PM

Quote:

Originally Posted by Gunderak (Post 1664767)
@Whoever neg repped me for joking around at cbk1994 seriously skrew you.
your comment is "don't act like a child" where you are the one who is truely acting like a child.
wait sorry didnt realise we couldnt have any fun on these forums.
This proves furthermore you are the child.

You making a big stink out of someone giving you neg rep shows that you are a child. Negative reputation is nothing to get mad over.

callimuc 08-23-2011 09:06 PM

Quote:

Originally Posted by ff7chocoboknight (Post 1664969)
You making a big stink out of someone giving you neg rep shows that you are a child. Negative reputation is nothing to get mad over.

this

Gunderak 08-29-2011 12:52 PM

Well neg repping someone because there having a little bit of fun isn't necessary.

Gunderak 09-02-2011 04:19 PM

and again someone neg reps me for posting...
i cant believe some people.

ff7chocoboknight 09-02-2011 04:47 PM

Maybe because you keep double posting for no reason and keep starting crap with people.

Emera 09-02-2011 05:49 PM

Quote:

Originally Posted by ff7chocoboknight (Post 1666739)
Maybe because you keep double posting for no reason and keep starting crap with people.

BINGO
You win a yacht!


All times are GMT +2. The time now is 05:10 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.