Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-15-2014, 07:30 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Scoreboard

So, I started working on a scoreboard script.

Here's what I have so far-
PHP Code:
const MAX_STATS 26;

function 
onCreated() {
  
this.registerStat("Rocks_Staff3""xAndrewx"4);
}

public function 
registerStat(temp.stattemp.pltemp.value) {
  if (
temp.stat != "Rocks_Staff3") return;
  
    
//Create place holders if no stats exist
  
if (this.("Stat_" temp.stat "_" 1) == null) {
    for (
temp.1temp.<= MAX_STATStemp.i++) {
      
this.("Stat_" temp.stat "_" temp.i) = {27 temp.i"(npc-server)""Place Holder""head19.png"};      
      
this.("Stat_PlayerCache_" temp.stat).add("(npc-server" temp.@")");
    }         
  }  
  
    
//Loop from lowest score to highest
  
for (temp.i=MAX_STATStemp.i>=1temp.i--) {
    
temp.data this.("Stat_" temp.stat "_" temp.i);

    
temp.score temp.data[0];
    
temp.owner temp.data[1];
    
      
//Not hit the lowest score- end the loop
    
if (temp.== MAX_STATS) {
      if (
temp.value <= temp.score) {
        break;
      }
    }
        
    if (
temp.value temp.score) {
        
//If it's the last entry, finally add it in!
      
if (temp.== MAX_STATS) continue;      
      
        
//Replace old stats with new score
      
this.("Stat_" temp.stat "_" @ (temp.1)) = temp.data;      
      
this.("Stat_PlayerCache_" temp.stat)[(temp.1)] = temp.owner;
            
        
//If they've beaten the high score
      
if (temp.== 1) {
        
this.("Stat_" temp.stat "_" temp.i) = {temp.valuetemp.plfindplayer(temp.pl).nickfindplayer(temp.pl).head};      
        
this.("Stat_PlayerCache_" temp.stat)[(temp.i)] = temp.pl;        
//        echo("-Beat High Score- Adding stat in at" SPC temp.i SPC "old-" @ temp.score @ ". new-" @ temp.value @ ".");      
      
}      
    } else {
        
//Replace the score where they should sit
      
this.("Stat_" temp.stat "_" @ (temp.1)) = {temp.valuetemp.plfindplayer(temp.pl).nickfindplayer(temp.pl).head};      
      
this.("Stat_PlayerCache_" temp.stat)[(temp.1)] = temp.pl;       
      break;
    }
  }

  
this.trigger("update""");

So, I'm wanting a system which only displays the player in the list once.

The only way to do this is another loop- but what do you guys think?? Can you think of an alternative & more robust method
__________________
Reply With Quote
  #2  
Old 11-15-2014, 01:47 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
Basically what you want to do is create a sorted array?

Look into the sortByValue function.

Quote:
TGraalVar.sortbyvalue(str, str, bool) - sorts an array, specify the variable of the array members which is compared, also the variable type and if it should be sorted ascending; variable type can be "string", otherwise it is sorted by floating point value
Example:
PHP Code:
function onCreated() {

  
temp.plScores = {
    {
"BlueMelon",9001},
    {
"snk",69},
    {
"BigMonster",1337},
    {
"supaman771",7331}
  };

  
temp.board null;
  for(
temp.plScore temp.plScores) {
    
temp.board.add(temp.plScore[0]); // acc
    
temp.board[temp.board.size() - 1].score temp.plScore[1]; // score
  
}

  
temp.board.sortByValue("score""float"false);

  echo(
"Scoreboard: ");
  for(
temp.i=0temp.i<temp.board.size(); temp.i++) {
    
temp.acc temp.board[temp.i];
    
temp.score temp.board[temp.i].score;
    echo(
temp.i+@". "@temp.acc SPC "with" SPC temp.score);
  }

which gives
Quote:
Scoreboard:
1. BlueMelon with 9001
2. supaman771 with 7331
3. BigMonster with 1337
4. snk with 69
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #3  
Old 11-15-2014, 01:49 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
no

With that system you're storing millions of variables - I only want it to store the top 25 scores

p.s. love the 69
__________________
Reply With Quote
  #4  
Old 11-15-2014, 02:15 PM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
That was just an example, change it to fit your needs

What you want to do seems simple enough,
PHP Code:
  temp.board= {25 values};
  
temp.board.sortdescending();

  
// add a new entry
  
temp.newEntry 123;
  
temp.lowest temp.board[temp.board.size()-1];
  if(
temp.newEntry temp.lowest) {
    
// delete lowest
    // add to board
    // re-sort
  
}
// display or whatever 
I can whip up another example later tonight if needed.
__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #5  
Old 11-15-2014, 08:31 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
sure

don't store more than 25 scores in the DB though
__________________
Reply With Quote
  #6  
Old 11-16-2014, 03:23 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
I don't like DBNPC's being used like that anymore. I have had way to many problems with the DBNPC flags being reset randomly =/
Reply With Quote
  #7  
Old 11-16-2014, 03:52 AM
BlueMelon BlueMelon is offline
asdfg
BlueMelon's Avatar
Join Date: Sep 2008
Posts: 1,481
BlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to beholdBlueMelon is a splendid one to behold
Quote:
Originally Posted by xAndrewx View Post
sure

don't store more than 25 scores in the DB though


Try this.
PHP Code:
/* Scoreboard system */

function onCreated() {
  
this.numEntrys 25;
  
clearBoard();

  
addScore("BlueMelon",9001);
  
addScore("snk",69);
  
addScore("BigMonster",1337);
  
addScore("supaman771",7331);

  
displayBoard();
}

function 
displayBoard() {
  for(
temp.i=0temp.i<this.board.size(); temp.i++) {
    echo(
temp.i+1 SPC this.board[temp.iSPC this.board[temp.i].score);
  }
}

function 
clearBoard() {
  
this.board = {};
  for(
temp.i=0temp.i<this.numEntrystemp.i++){
    
this.addScore("(npc server)"0);
  }
}

public function 
addScore(temp.acctemp.score) {
  
this.board.sortByValue("score""float"false);

  
temp.lowest this.board[this.board.size() -1];

  if(
this.board.size() < this.numEntrys){
    
this.board.add(temp.acc);
    
this.board[this.board.size() -1].score temp.score;
  }else if(
temp.score temp.lowest.score && temp.score 0) {
    
this.board.delete(this.board.size() -1);
    
this.board.add(temp.acc);
    
this.board[this.board.size() -1].score temp.score;
  }

  
this.board.sortByValue("score""float"false);

__________________
http://i.imgur.com/OOJbW.jpg
Reply With Quote
  #8  
Old 11-16-2014, 07:12 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Brilliant, thank you very much! (I repped you- you now have 3 bars! **** yeah)

Here's my final piece
PHP Code:
const MAX_STATS 25;

function 
onCreated() {  
  
temp.statsize 5;
  
temp.stats getStat("PlantsPicked");
  for (
temp.0temp.5temp.i++) {
    global.
out(temp.stats[temp.i], "Debug");
  }
}

  
//Return Account, Score, {Nickname, Head, Hat}
public function getStat(stat) {
  for (
temp.0temp.MAX_STATStemp.++) {
    
temp.stats.add({this.("board_" temp.stat)[temp.i], this.("board_" temp.stat)[temp.i].scorethis.("board_" temp.stat)[temp.i].outfit}); 
  } 
  
  return 
temp.stats;
}

public function 
registerStat(statplscore) {  
    
//Create placeholders
  
if (this.("board_" temp.stat) == null) {
    echo(
"Clearing Board-" SPC temp.stat);
    
this.onClearBoard(temp.stat); 
  }
    
//Sort in descending order
  
this.("board_" temp.stat).sortByValue("score""float"false); 
  
    
//If they already have a score, just update it. No duplicates!
  
temp.found this.("board_" temp.stat).index(temp.pl);  
  if (
temp.found >= 0) {
    
this.("board_" temp.stat)[temp.found].score temp.score;

      
//Update outfit
    
temp.pla findplayer(temp.pl);
    if (
temp.pla != null
      
this.("board_" temp.stat)[this.("board_" temp.stat).size() -1].outfit = {temp.pla.nicktemp.pla.headtemp.pla.attr[1]};               
  } else {
      
//Board size
    
temp.board_size this.("board_" temp.stat).size();
  
      
//Get lowest score  
    
temp.lowest this.("board_" temp.stat)[temp.board_size -1];   
      
    if(
temp.board_size MAX_STATS){ 
      
this.("board_" temp.stat).add(temp.pl); 
      
this.("board_" temp.stat)[temp.board_size 1].score temp.score
    }else if(
temp.score temp.lowest.score && temp.score 0) { 
        
//Delete lowest score
      
this.("board_" temp.stat).delete(temp.board_size 1); 
        
        
//Store score
      
this.("board_" temp.stat).add(temp.pl);    
      
this.("board_" temp.stat)[this.("board_" temp.stat).size() -1].score temp.score
    
        
//Store outfit
      
temp.pla findplayer(temp.pl);
      if (
temp.pla != null
        
this.("board_" temp.stat)[this.("board_" temp.stat).size() -1].outfit = {temp.pla.nicktemp.pla.headtemp.pla.attr[1]};     
      
    } 
  }
  
this.("board_" temp.stat).sortByValue("score""float"false);   
  
  
this.trigger("update"""); 
}

  
//Clear stats (only called when creating a new stat)
function onClearBoard(stat) { 
  
this.("board_" temp.stat) = {}; 
  for(
temp.0temp.MAX_STATStemp.i++) { 
    
this.registerStat(temp.stat"(npc server)"1); 
  } 

__________________
Reply With Quote
  #9  
Old 11-16-2014, 07:15 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Quote:
Originally Posted by scriptless View Post
I don't like DBNPC's being used like that anymore. I have had way to many problems with the DBNPC flags being reset randomly =/
p.s. add this once you add / remove any variables

PHP Code:
this.trigger("update"); 
It saves the NPC
__________________
Reply With Quote
  #10  
Old 11-16-2014, 10:54 AM
Inari Inari is offline
Registered User
Join Date: Sep 2014
Posts: 47
Inari is on a distinguished road
Couldn't you just like, use SQL, have a database with each player + their scores, update the player entry and build the scoreboard using something like
SELECT * FROM myTable ORDER BY Stat_Blah ASC LIMIT 25
?
Reply With Quote
  #11  
Old 11-16-2014, 04:43 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
no, i don't want to store EVERY players score to only pull back the highest 25.
__________________
Reply With Quote
  #12  
Old 11-16-2014, 06:06 PM
Inari Inari is offline
Registered User
Join Date: Sep 2014
Posts: 47
Inari is on a distinguished road
Yeah, but why? :3 seems nice to have the saves anyway and its a few bytes per player
Reply With Quote
  #13  
Old 11-17-2014, 01:42 AM
scriptless scriptless is offline
Banned
Join Date: Dec 2008
Location: N-Pulse
Posts: 1,412
scriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to beholdscriptless is a splendid one to behold
Quote:
Originally Posted by xAndrewx View Post
p.s. add this once you add / remove any variables

PHP Code:
this.trigger("update"); 
It saves the NPC
Not always. We were doing this on the scoreboard on GK and it still had issues.
Reply With Quote
  #14  
Old 11-18-2014, 04:11 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Why would you store unnecessary information that will never be used if you don't have to.
__________________
Reply With Quote
  #15  
Old 11-18-2014, 01:00 PM
Inari Inari is offline
Registered User
Join Date: Sep 2014
Posts: 47
Inari is on a distinguished road
Well it has to be stored somewhere either way, SQL is nice for backups like that imo. Also you get a more central place to manage such info than the client vars provide.
Reply With Quote
Reply


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

Forum Jump


All times are GMT +2. The time now is 12:20 PM.


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