Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Two Qs. Reg Expression Help and Notepad Trivia Script
Two Qs. Reg Expression Help and Notepad Trivia Script
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Bummer
(14 posts) Bio
|
Date
| Thu 28 Jan 2010 09:19 PM (UTC) |
Message
| Hello everyone!
I have two questions that I need some tips on and can't wait to see what you all throw at me. Thank you in advance, to those who respond.
1.)I need help formulating a simple trigger. I have trouble fully using the power of regular expressions, and need help with this line.
A Ghast's claw decimates Name!
This text will always appear in the color Cyan, and I have made other triggers to catch lines like the following:
match="^(.*?)\*\*\* (.*?) \*\*\* (\w+)\!$"
match_text_colour="y"
regexp="y"
text_colour="14"
....res %3
(I cut this short just to show the match line)
That line would look like this from the mud:
A Ghast's claw *** DECIMATES *** Name!
I am just looking for a clean way to catch this in one trigger instead of three + triggers, and I know reg exps can do this.
2.)I intend to make a small game that will be played through the channels on my mud. TRIVIA! I need help understand ways that I can take a notepad file that (for example) contains the questions on every Odd number (1,3,5,.etc) and the matching answer will be on the Even line following it (1=2, 3=4, 5=6). Now, this is only how i imagine it to work, so i could easily edit the text files to add/amend the trivia files. I do not have much to work with, as I am trying to understand how to build this part of the script/triggers/etc....
Basically, What are the major hurtles to making a trivia script that would pull questions and check answers from a file of some sort.
Thank you all for you efforts (Nick Gammon and others!)
Bummer
| Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #1 on Thu 28 Jan 2010 09:45 PM (UTC) |
Message
|
Bummer said:
I am just looking for a clean way to catch this in one trigger instead of three + triggers, and I know reg exps can do this.
For this part you can probably use the "or" feature of regular expressions, like this:
match: ^(.*?) (slices|smites|decimates|\*{3} DECIMATES \*{3}|kicks) (\w+)\!$
You basically put each attack type between the | symbols. Note that \*{3} means exactly 3 asterisks. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,133 posts) Bio
Forum Administrator |
Date
| Reply #2 on Thu 28 Jan 2010 10:32 PM (UTC) Amended on Thu 28 Jan 2010 10:34 PM (UTC) by Nick Gammon
|
Message
| As for the trivia quiz, this was an interesting challenge. The alias below should be able to be used as a basis for getting a quiz to work. You probably need to tweak it a bit, depending on exactly how responses from the MUD arrive (eg. will you "say" the quiz, or chat it, or whatever).
<aliases>
<alias
match="trivia"
enabled="y"
send_to="12"
sequence="100"
>
<send>
require "wait"
wait.make ( function ()
math.randomseed (os.time ()) -- randomize
local question, answer
-- read quiz into memory
f = assert (io.open (GetInfo (66) .. "trivia.txt", "r"))
quiz = {} -- table of questions / answers
while true do
repeat
question = f:read ("*l") -- get question
until question ~= "" -- skip blank lines
if not question then -- exit loop at end of file
break
end -- if
repeat
answer = f:read ("*l") -- get answer
until answer ~= "" -- skip blank lines
if not answer then -- exit loop at end of file
break
end -- if
table.insert (quiz, { question = question, answer = answer } )
end -- while loop
f:close () -- close quiz file
assert (#quiz > 0, "No questions found in trivia.txt file")
exit_trivia = false
Note ("Starting trivia quiz with ", #quiz, " questions.")
while #quiz > 0 and not exit_trivia do
local which = math.random (#quiz)
Send ("say Trivia question!")
-- ask question, wait 60 seconds for reply
Send ("say " .. quiz [which].question)
local line, wildcards = wait.match ("* says '" .. quiz [which].answer .. "'", 60)
if not line then -- timeout
if exit_trivia then break end -- quit on end of trivia
Send ("say No-one got that!")
Send ("say The question was: " .. quiz [which].question)
Send ('say The answer was: "' .. quiz [which].answer .. '"')
else
Send ("say Congratulations, " .. wildcards [1] .. "! You are correct!")
end -- if
table.remove (quiz, which) -- don't ask same question again
wait.time (15) -- wait 15 seconds before next question
end -- while loop
Note ("trivia quiz exited with ", #quiz, " questions unasked.")
end) -- wait.make
</send>
</alias>
<alias
match="trivia end"
enabled="y"
send_to="12"
sequence="100"
>
<send>
exit_trivia = true
Note "trivia quiz will exit on next timeout"
</send>
</alias>
</aliases>
The main alias (you type "trivia" to kick it off) reads a file trivia.txt in the same directory as MUSHclient.exe.
This file should be question/answer pairs. I used this for testing:
What is your favourite colour?
blue
What is the answer to the meaning of life?
42
How do you exit the game?
quit
Blank lines are ignored. The alias does not do case-conversions so the answer should be exactly how you expect to see it typed.
A sample session is:
<20hp 100m 100mv>
Admin says 'Trivia question!'
<20hp 100m 100mv>
Admin says 'What is your favourite colour?'
<20hp 100m 100mv>
say blue
You say 'blue'
<20hp 100m 100mv> Admin says 'Congratulations, Nick! You are correct!'
...
<20hp 100m 100mv>
Admin says 'Trivia question!'
<20hp 100m 100mv>
Admin says 'What is the answer to the meaning of life?'
<20hp 100m 100mv>
A light drizzling rain falls to the ground.
<20hp 100m 100mv>
Admin says 'No-one got that!'
<20hp 100m 100mv>
Admin says 'The question was: What is the answer to the meaning of life?'
<20hp 100m 100mv>
Admin says 'The answer was: "42"'
The alias reads the entire quiz into memory, and then selects a question at random. It asks the question, and then uses wait.match to wait for the corresponding answer. If it doesn't get the answer in 60 seconds (you can tweak that) it gives up and gives the correct answer. If you DO get the answer right it congratulates the person.
It then removes that question from the list (so you don't see it twice) and then waits 15 seconds for a break. It then loops and asks another until it runs out of questions.
If you decide to stop early you can type "trivia end" which sets a flag which exits the trivia question loop at the next timeout.
Modifications you could make would be to keep track of who gets the right answers, and how many they got right, etc.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Bummer
(14 posts) Bio
|
Date
| Reply #3 on Sun 07 Feb 2010 01:42 AM (UTC) |
Message
| Thank you so much! I have been working on this over the past weeks, and it works!
I do need help with a tracking system, IE keeping the score of the game. Can someone help me? I can set variables when a player gets a question right, yet I know i am doing it very sloppy and also having problems with the numeric vs string problem of variables.
Another wish would be to have a way to change the .txt file selected for the trivia at the beginning of the game. Something like
trivia wildwest.txt
and it would start the game using the wildwest.txt notepad file.
Thanks!!!
Bummer | Top |
|
Posted by
| Bummer
(14 posts) Bio
|
Date
| Reply #4 on Sun 07 Feb 2010 01:47 AM (UTC) |
Message
| I figured out how to get 'trivia wildwest.txt' to work. it works great!
Just really need a way to keep score and I will be super set.
Thanks! | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
14,512 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top