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!".


All times are GMT +2. The time now is 02:05 PM.

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