mIRC Home    About    Download    Register    News    Help

Print Thread
trivia script #117984 21/04/05 01:26 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
Hey, I'm making a trivia script and I was wondering, what would be the best way to store peoples scores and figure out what rank users are so i can display like the top 10 users and tell people if they moved up a rank etc?


Check out http://kalsiddon.com for games, videos & more! Good for when your bored in work or school!
Re: trivia script #117985 21/04/05 01:36 PM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
You can use many methods... ours just stores weekly, monthly, and all-time scores and how fast they answer in text files which works just fine for us. You could use a hash table as well, though I'm not sure that would be better.

As far as top 10, the easiest and fastest way to handle that is to use 10 variables and use if statements to see if someone's score goes higher than one of the top 10 variable scores and adjust accordingly. You only ever have to track the top 10 that way. If you want to track everyone's position as some scripts do, then you'd need to store the position with the nick and have it change as needed. It's more work, but can be done without that much difficulty.


Invision Support
#Invision on irc.irchighway.net
Re: trivia script #117986 21/04/05 01:37 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
You could store the users in a hash table and do an infinite loop to see which item has the highest data perhaps?

Just my 2 cents worth. grin

Re: trivia script #117987 21/04/05 01:39 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
yeah right now the scores are in an ini... That was pretty helpful, thanks.
I never really thought of it that way before, lol.

Re: trivia script #117988 21/04/05 01:41 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
meh, I don't know how to use hash tables yet.. I really need to learn.. but meh.


Check out http://kalsiddon.com for games, videos & more! Good for when your bored in work or school!
Re: trivia script #117989 21/04/05 02:01 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Little example for you, it's not much..

Code:
alias scores {
  ;the table doesnt exist so we make one.
  if (!$hget(scores)) hmake scores 100
  linesep
  ;adding the nicknames with their points in our table.
  hadd scores Andy 10
  hadd scores Bob 25
  hadd scores Riamus 35
  hadd scores Mike 50
  ;this tells us how many users have points in this case we'd get (4)
  echo -a $+($chr(40),$hget(scores,0).item,$chr(41)) items in table.
  echo -a Listing...
  ;We loop through all the users and echo the item and data.
  ;Item would be their nick.  Data would be their points.
  var %x = $hget(scores,0).item
  while (%x) {
    echo -a $hget(scores,%x).item with $hget(scores,%x).data points
    dec %x
  }
  hsave -i scores scores.dat
  linesep
}


We'd get the following data back.

-
(4) items in table.
Listing...
Mike with 50 points
Riamus with 35 points
Andy with 10 points
Bob with 25 points
-

To free the table you type /hfree scores.

As it stands you're writing the scores to a file (scores.dat).

With (/hsave -i scores scores.dat)

The -i switch means we're saving in INI format as you already are.

For more help type /help Hash Tables.

Hope this helps,

-Andy.

Re: trivia script #117990 21/04/05 02:11 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
yeah it did, thanks.


Check out http://kalsiddon.com for games, videos & more! Good for when your bored in work or school!
Re: trivia script #117991 21/04/05 02:17 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
You're welcome. smile

Re: trivia script #117992 21/04/05 02:48 PM
Joined: Apr 2005
Posts: 18
B
BarGarth Offline
Pikka bird
Offline
Pikka bird
B
Joined: Apr 2005
Posts: 18
What I've done with my triviabot is to use 2 hashtables: one for the scores of the players like SladeKrave said. The second to store the rankings of the players.

To sort the rankings, you could use the bubble-sort algorithm. Although it's complexity is O(nē) , it works great with almost sorted arrays. Just update the Ranking-table after each answer.

With these tables you can easily print out the top-10 by giving the first 10 players of the rankings-table and the scores of these players

Re: trivia script #117993 21/04/05 03:08 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
A quick and easy way to sort a hashtable is to /FILTER it to a hidden window using the sorting ability of /FILTER

Re: trivia script #117994 22/04/05 09:06 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
I ended up having a friend of mine make a dll with functions to get ranks in stuff in c++. Works well smile


Check out http://kalsiddon.com for games, videos & more! Good for when your bored in work or school!
Re: trivia script #117995 22/04/05 11:18 PM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
bit over the top a dll.

Code:
var %filename = $+(tempfile.delete.me.if.im.still.here.,$ticks,$mid($rand(10000,19999),2),.txt)
hsave -io hashtablename %filename
window -hn @results
filter -fwxcteu 2 61 %filename @results [*]
remove %filename


Above creates a @results window with the item=value one per line largest numericly sorted items first

Code:
var %filename = yourfilenamehere.txt
hsave -io hashtablename %filename
filter -ffxcteu 2 61 %filename %filename [*]


This creates a the file yourfilenamehere.txt with them in instead

Re: trivia script #117996 24/04/05 03:18 PM
Joined: Feb 2005
Posts: 43
M
Mike2005 Offline OP
Ameglian cow
OP Offline
Ameglian cow
M
Joined: Feb 2005
Posts: 43
meh..


Check out http://kalsiddon.com for games, videos & more! Good for when your bored in work or school!
Re: trivia script #117997 25/04/05 09:19 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
Two small notes:
  1. Hash table items with an [ or ] or both in it will be corrupted when saving the hash table in .ini format. The brackets will be transformed to ~, so that they don't cause ambiguity problems with topics in an ini file.
  2. If the hash table gets reasonably big (already at 300 items or so), hsave -oi gets slow. I know because for my old rpgtrivia bot, I used to use the same approach. Doing a simple hsave -oi took 350 ms, mainly because of how overwriting with INI files works, meaning it has to go through each item and see if it already exists, if so overwrite, if not append to end.

    Scripting an alias that loops through the hash table and either writes to a file using the file handling commands, or to a hidden window was considerably faster. If I recall correctly, the results when writing to a file varied from 16 to 30 milliseconds on my computer vs the 310-350 ms with hsave -oi. I think there were around 400 players in the database.
Not really aimed at you, but thought I should mention this.

Greets


Gone.