-- Ragaman.lua, created by Antony Warbrooke (antw@ihug.co.nz) 14 Aug 2003
-- Traduit par ?_SHaKa_?
-- is an anagram bot for use with PtokaX hub software. Tested on 0.326 td4
-- This has a similar look to the bot created by FLiXD for the nmdc hub software.
-- Change Log
-- V1.2 few bugs fixed by Guibs, 12 Sept 2003
-- bothandle = "a." -- Removed by Guibs
-- Added pass command
-- Points gap displayed when anagram answered
-- Version number displayed when bot starts and stops, and in help
-- Added resetscores command for ops only
-- Global variables set here
-- These control the bot
botname = "[?LIMPO].ANaGRaM" -- Name of bot in user list
tickmax = 36 -- Number of five second ticks per question
bonusmax = 5 -- Maximum bonus points for speed
wordfile = "abtext/engwords.txt" -- Words file
scorefile = "abtext/scores.txt" -- Scores file
vernum = "1.02" -- Bot version number
-- These are general global variables
wordlist = {} -- The word list table
ticknum = 0 -- Number of timer ticks used for current anagram
bonusnum = 0 -- Bonus points for answering faster
currentword = "" -- Current scrambled answer
currentclue = "" -- Current half unscrambled answer
currentanswer = "" -- Current unscrambled answer
PlayerScore = {} -- Table of players and scores
RankPlayerScore = {} -- Table of ranks, player names and scores
AnaBotState = 0 -- Current state of bot. 0 = stopped, 1 = started
otherspelling = {} -- Table of possible answers to anagram
-- This is run when the script is started
function Main()
AnaBotLoadScores()
AnaBotLoadWords()
frmHub:RegBot(botname)
end
-- This is run when ever someone types something
function DataArrival(user, data)
-- If message from user
if (strsub(data,1,1)=="<") then
data=strsub(data,1,strlen(data)-1)
-- get the msg only using regular expression
s,e,msg = strfind(data,"%b<>%s+(%S+)")
msg = strlower(msg) -- Convert to lower case
if(msg == "!anagram") then
AnaBotStart ()
elseif(msg == "!stopanagram") then
AnaBotStop ()
elseif(msg == "?anagram") then
AnaBotHelp ()
return 1
elseif(msg == "!top10") then
AnaBotTopTen()
elseif(msg == "!scores") then
AnaBotScores(user)
elseif(msg == "!suivant") then
AnaBotPass()
elseif(msg == "!resetscores") then
if (user.bOperator) then
AnaBotResetScores()
else
SendToAll("***Questo comando non pu? essere utilizzato da chi non ? OP! :p")
end
else
if (AnaBotState == 1) then
-- Check answer
otherspelling[msg]= msg
AnaBotCheck (user.sName)
end
end
end
end
-- This is when the timer has reached it's set time
function OnTimer()
ticknum = ticknum + 1
if (bonusnum > 0) then
bonusnum = bonusnum - 1
end
if (ticknum == (tickmax / 2)) then
AnaBotClue () -- Display help if half way through time
elseif (ticknum == tickmax - 2) then
SendToAll(botname,"***10 secondi restano per trovare la soluzione...")
elseif (ticknum == tickmax) then
AnaBotSolve () -- Show answer if full time
AnaBotNew () -- Pick a new word
end
end
function AnaBotStart()
if (getn(wordlist) == 0) then
SendToAll(botname,"??? Non ci sono parole caricate")
return
end
SetTimer(1000*5) -- Set timer to five seconds
SendToAll(botname,"???"..botname.." Ver. "..vernum.." avviato.")
AnaBotNew() -- Get a new word
StartTimer()
AnaBotState = 1
end
function AnaBotStop()
SendToAll(botname,"???"..botname.." Ver. "..vernum.." stoppato.")
AnaBotSolve()
StopTimer()
AnaBotState = 0
end
function AnaBotHelp()
TextToSend = "???"..botname.." Ver. "..vernum.." hai a disposizione alcuni di questi comandi:\r\n"
TextToSend = TextToSend.." ??anagram per ottenere questo aiuto\r\n"
TextToSend = TextToSend.." ?!scores per il tuo punteggio\r\n"
TextToSend = TextToSend.." ?!top10 la top 10 del gioco\r\n"
TextToSend = TextToSend.." ?!suivant per passare all'anagramma in corso\r\n"
TextToSend = TextToSend.." ?!anagram per cominciare il gioco\r\n"
TextToSend = TextToSend.." ?!stopanagram per stoppare il gioco"
SendToAll (botname,TextToSend)
end
function AnaBotNew()
SendToAll(botname,"***Anagramma corrente")
ticknum = 0 -- Reset tick counter
bonusnum = bonusmax -- Reset bonus amount
currentword = ""
otherspelling = {} -- Reset list of words with same letters
tempword1 = {}
local i = random(getn(wordlist)) -- Index into wordlist
currentanswer = wordlist
-- Put word into a table so we can use each letter (faster than gsub)
for i=1,strlen(currentanswer) do
tempword1 = strsub(currentanswer,i,i)
end
-- Get other spellings of the chosen letters
for i=1,getn(wordlist) do
if (strlen(currentanswer) == strlen(wordlist)) then
--Split word to use each letter
tempword2 = {}
for j=1, strlen(wordlist) do
tempword2[j] = strsub(wordlist,j,j)
end
-- Check each letter
for j=1, getn(tempword1) do
for k=1, getn(tempword2) do
if (tempword1[j] == tempword2[k]) then
tremove(tempword2,k)
break -- skip to next letter in j
end
end
end
if (getn(tempword2) == 0) then
-- Create entry in other spelling table
otherspelling[wordlist] = 1
end
end
end
-- Mix up letters to make anagram
while getn(tempword1) > 0 do
i = random(getn(tempword1))
currentword = currentword..tremove(tempword1,i)
end
-- Check to see that mixed up letters are not a valid word
-- Still to be done
-- Add spaces inbetween letters of anagram
tempword1 = {}
for i=1,strlen(currentword) do
tempword1 = strsub(currentword,i,i)
end
currentword = tempword1[1]
for i=2, getn(tempword1) do
currentword = currentword.." "..tempword1
end
currentclue = strsub(currentanswer,1,strlen(currentanswer)/2) -- Clue is first half of answer
SendToAll(botname,strupper(currentword))
end
function AnaBotSolve()
SendToAll(botname,"***Il tempo utile per dare la risposta ? trascorso la risposta esatta era: "..strupper(currentanswer)..".")
end
function AnaBotCheck(username)
if (otherspelling[msg] == strlower(currentanswer)) then
--if exist add points
if (PlayerScore[username]) then
PlayerScore[username][2] = PlayerScore[username][2] + strlen(currentanswer) + bonusnum
--else add player to tables
else
PlayerScore[username] = {username , strlen(currentanswer) + bonusnum}
tinsert (RankPlayerScore, PlayerScore[username])
end
-- sort by points
sort (RankPlayerScore, function(a,b) return a[2] > b[2];end)
-- save scores
AnaBotSaveScores()
-- get rank
rank = 0
for i = 1, getn(RankPlayerScore) do
if (RankPlayerScore == PlayerScore[username]) then
rank = i
break
end
end
TextToSend = "***Corretto! "..username.." ottieni "..strlen(currentanswer).." punti"
if (bonusnum > 0) then
TextToSend = TextToSend.." + "..bonusnum.." bonus rapidit?! ;o)"
end
TextToSend = TextToSend..". Total: "..PlayerScore[username][2]..". Position: "..rank.."/"..getn(RankPlayerScore).."."
-- Work out points gap to next person
if (rank == 1) then
-- If only player, say nothing
if (getn(RankPlayerScore) == 1) then
elseif (RankPlayerScore[1][2] == RankPlayerScore[2][2]) then
TextToSend = TextToSend.." ex aequo avec "..RankPlayerScore[2][1]
else
TextToSend = TextToSend.." "..(RankPlayerScore[1][2] - RankPlayerScore[2][2]).." devant "..RankPlayerScore[2][1]
end
elseif (RankPlayerScore[rank][2] == RankPlayerScore[rank-1][2]) then
TextToSend = TextToSend.." ex aequo "..RankPlayerScore[rank-1][1]
else
TextToSend = TextToSend.." "..(RankPlayerScore[rank-1][2] - RankPlayerScore[rank][2]).." derri?re "..RankPlayerScore[rank-1][1]
end
SendToAll(botname,TextToSend)
AnaBotNew()
end
end
function AnaBotClue()
SendToAll(botname,"*Anagram in corso: "..strupper(currentword))
SendToAll(botname,"*Aiuto: "..strupper(currentclue).."...")
end
function AnaBotLoadWords()
wordlist = {} -- blank word list
readfrom(wordfile) -- open file for read
while 1 do
local line = read()
if line == nil then
break
else
tinsert(wordlist, line)
end
end
readfrom() -- close file
SendToAll("???Io conosco + di "..getn(wordlist).." parole !! :o)")
end
function AnaBotSaveScores()
writeto(scorefile) -- open file for writing
-- write each user name and their score to the file
foreach (PlayerScore, function (name,score) write (name.."=="..score[2].."\r\n");end)
writeto() -- close file
end
function AnaBotLoadScores()
readfrom(scorefile) -- open file for read
while 1 do
local line = read()
if line == nil then
break
else
-- break line into username and score
s,e,user,score = strfind(line, "(.+)==(%d+)$")
-- add user to score tables
PlayerScore[user] = {user, tonumber(score)}
tinsert(RankPlayerScore, PlayerScore[user])
end
end
readfrom() -- close file
-- sort scores
sort (RankPlayerScore, function(a,b) return a[2] > b[2];end)
end
function AnaBotScores(user)
TextToSend = "\r\n???Ecco il punteggio:\r\n"
PreTextToSend = ""
r = 0
for i = 1, getn(RankPlayerScore) do
PreTextToSend = PreTextToSend.."\r\nRank: "..i.." - Nom: "..RankPlayerScore[1].." - Score: "..RankPlayerScore[2].."\r\n"
r = r + 1
end
if r == 0 then
TextToSend = "Non,... c'? persona, ancora... :s"
else
TextToSend = TextToSend..PreTextToSend
end
user:SendData(botname, TextToSend)
end
function AnaBotTopTen()
TextToSend = "\r\n???T?P 10:\r\n"
PreTextToSend = ""
r = 0
for i = 1, min(10,getn(RankPlayerScore)) do
PreTextToSend = PreTextToSend..AnaBotEngl
hi boys and possible to modify the writing in lua 5???
Try Here (http://board.univ-angers.fr/thread.php?threadid=4228&boardid=26)
Use the Search button to Find the Origional LUA 4 version to get the associated file.. required
* PLUG *
Anagram game included in XN
click my Sig :P