PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Conversion Requests => Topic started by: F_O_X on 27 February, 2008, 08:22:48

Title: Jumble
Post by: F_O_X on 27 February, 2008, 08:22:48
Please convert a script of this game in new API
Title: Re: Jumble
Post by: [NL]scooby on 27 February, 2008, 17:53:45
--------------------------------------------------------------------------------------
-- This script is converted with PtokaX LUA API Converter v0.9 at 02/16/08 14:01:24 --
--------------------------------------------------------------------------------------
sAuthor = "Donald Duck";
sEmail = "woordmix@kinderspel.nl";
sConversion = "Converted to LUA API by [NL]Scooby? 02/16/2008"
bGameRunning, bGamePaused = nil, nil;
sJumblePhrase, sActualPhrases, NoSpacePhrase = nil,nil,nil;
iLength, iBonusTime = 0, 0;
sScoreOutput, sHintOutput, sHelpOutput, sOpHelpOutput = nil, nil, nil, "";
iTimeCount, iGameNoReplyCount = 0, 0;
tScores, tScoresByName  = { n = 0 }, {};
tChannelUsers, iChannelCount = {}, nil;

-- PtokaX Events --

function OnStartup()
dofile("jumble.cfg");

sScoreFormula = "return "..sScoreFormula;
sBonusFormula = "return "..sBonusFormula;
sHintFormula  = "return "..sHintFormula;

Core.RegBot(sBotName,"","",true);

sHelpOutput = StrReplace(tStringTable[1]);

for cmd,tTemp in pairs(tCommands) do
if(tTemp[1] == DisplayJumbleHelp) then
sJumbleHelp = cmd;
end

sCmd, sDesc = cmd, tTemp[3];

if(tTemp[2] == 0) then
sHelpOutput = sHelpOutput..StrReplace(tStringTable[2]);
else
sOpHelpOutput = sOpHelpOutput..StrReplace(tStringTable[2]);
end
end

OldSendToAll = SendToAll;
UserDisconnected = LeaveJumble;
OpDisconnected = LeaveJumble;



ReadScoresFromFile();
SetScores();
end

function OnTimer(tmr)
if(bGamePaused) then
iTimeCount = iTimeCount + 1;

if(iTimeCount >= iPauseTime) then
StartJumblePhrase();
end
elseif(bGameRunning) then
iTimeCount = iTimeCount + 1;

if(iTimeCount == iHintTime) then
Core.SendToAll("<"..sBotName.."> ".. sHintOutput);
elseif (iTimeCount == iGameEndTime - 10) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[3]));
elseif (iTimeCount == iGameEndTime) then
StopJumblePhrase();
if(iGameNoReplyCount >= 3) then
StopJumbleGame();
end
else

end
else

end
end



function ChatArrival(curUser,sData)
Core.GetUserAllData(curUser)
local s, e, sNewData, chat, cmd, args = string.find(sData, "(%b<>%s+((%S+)%s*([^%|]*)))%|$");
local s, e, sTo = string.find(sData, "^%$To: (%S+)");

if(cmd == nil or (sTo and sTo ~= sBotName)) then return false; end

sUserName = curUser.sNick;

if(sTo) then
curUser.SendData = curUser.SendPM;
end

cmd = string.lower(cmd);

if(tCommands[cmd] and (tCommands[cmd][2] == 0 or curUser.bOperator)) then
Core.SendToNick(curUser.sNick,sData);
tCommands[cmd][1](curUser, args);
return 1;
else
if (not(iChannelCount) and string.sub(sData,1,1) == "<") then
iGameNoReplyCount = 0;
end

if(iChannelCount and string.find(sData, "$To: "..sBotName, 1, 1) == 1 and cmd == "!me") then
sNewData = "* "..sUserName..string.sub(chat, 4);
end

if(bGameRunning and bGamePaused == nil and string.find(string.upper(chat), sActualPhrase, 1, 1)) then
Core.SendToAll(sNewData);

iBonusTime = math.max(iBonusTimeLimit - iTimeCount, 0);

iScore = assert(loadstring(StrReplace(sScoreFormula)))();
iBonus = assert(loadstring(StrReplace(sBonusFormula)))();

if(tScoresByName[sUserName]) then
tScoresByName[sUserName][2] = tScoresByName[sUserName][2] + iScore + iBonus;
else
tScoresByName[sUserName] = {sUserName, iScore + iBonus};
table.insert(tScores, tScoresByName[sUserName]);
end

iRank = SetScores(sUserName);
iRankTotal = #tScores;
iUserScore = tScoresByName[sUserName][2];

WriteScoresToFile();

StopJumblePhrase();

if(iBonusTime ~= 0) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[4]));
else
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[5]));
end

iGameNoReplyCount = 0;

return 1;
else
if(iChannelCount and string.find(sData, "^$To: "..sBotName, 1, 1) == 1) then
if(tChannelUsers[sUserName] == nil) then
JoinJumble(curUser);
else
Core.SendToAll(sNewData);
iGameNoReplyCount = 0;
end

return 1;
end
end

end
end

-- Command Functions --

function StartJumbleGame(curUser, args)
if(bGameRunning) then return; end
if(args ~= "") then
HubGameActive = 2
local s, e, args = string.find(args, "([%w%s]+)$");
args = "Jumble\\"..args..".txt";
local f,e = io.open(args,"r")
if f then
sJumbleWordsFile = args;
f:close(f)
end
end
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[6]));
StartJumblePhrase();
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[7]));

iGameNoReplyCount = 0;
end

function StopJumbleGame(curUser)
if not(bGameRunning) then return; end

if not(bGamePaused) then
StopJumblePhrase();
end

if(curUser) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[8]));
else
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[21]));
-- TODO: fix numbers
end

bGameRunning, bGamePaused = nil, nil;
iBonusTime, iLength = 0, 0;
TmrMan.RemoveTimer(tmr);
end

function DisplayJumblePhrase(curUser)
if(bGameRunning and not bGamePaused) then
if(curUser == nil) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[9]));
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[9]));
end
end
end

function DisplayJumbleHelp(curUser)
if(curUser.bOperator) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHelpOutput..sOpHelpOutput);
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHelpOutput);
end
end

function DisplayJumbleScores(curUser)
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sScoresOutput);
end

function DisplayMyJumbleScore(curUser)
iRank = tScoresByName[curUser.sNick][3];
iRankTotal = #tScores;
iUserScore = tScoresByName[sUserName][2];

if(tScoresByName[curUser.sNick]) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[10]));
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[11]));
end
end

function DisplayJumbleHint(curUser)
if(iTimeCount >= iHintTime) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHintOutput);
end
end

function JoinJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick] == nil) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[12]));

Core.SendPmToNick(curUser.sNick,sBotName, StrReplace(tStringTable[13]));
curUser.SendData = curUser.SendPM;

DisplayJumblePhrase(curUser);
DisplayJumbleHint(curUser);

tChannelUsers[curUser.sNick] = 1;
iChannelCount = iChannelCount + 1;
end
end

function LeaveJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick]) then
sUserName = curUser.sNick;
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[14]));

tChannelUsers[curUser.sNick] = nil;
iChannelCount = iChannelCount - 1;

if(iChannelCount <= 0) then
iChannelCount = 0;
StopJumbleGame();
end
end
end

function SetPrivateJumbleGame(curUser)
if(iChannelCount == nil) then
SendToAll = SendToAllInChannel;
iChannelCount = 0;
end
end

function SetPublicJumbleGame(curUser)
if(iChannelCount) then
SendToAll = OldSendToAll;
tChannelUsers = {};
iChannelCount = nil;
end
end

-- Jumble Phrase Functions --

function JumbleString(str)
local tStr = { n = 0; };
local newString = "";

string.gsub(str, "(.)", function(c) table.insert(tStr, c); end);

math.randomseed(os.clock());

-- Fisher-Yates shuffle
for n = #tStr, 1, -1 do
local pos = math.random(n);
newString = tStr[pos].." "..newString;
tStr[pos] = tStr[n];
end

return newString;
end

function JumblePhrase(phr)
return string.sub(string.gsub(phr, "(%S+)", function (w) return JumbleString(w).."  "; end), 1, -2);
end

function StartJumblePhrase()
local hFile,Er = io.open(sJumbleWordsFile, "r");
if(hFile == nil) then
Core.SendToAll("<"..sBotName.."> Error opening "..sJumbleWordsFile);
return;
end
local _, phr = nil, nil;
local iFileSize = hFile:seek("end")
math.randomseed(os.clock());
while(phr == nil) do
hFile:seek("set", math.random(iFileSize)-1);
_, phr = hFile:read( "*l", "*l");
end
hFile:close();
sActualPhrase = string.upper(phr);
sNoSpacePhrase = string.gsub(sActualPhrase, "%s", "");
sJumblePhrase = JumblePhrase(sActualPhrase);
iLength = string.len(sNoSpacePhrase);

SetJumbleHint();

Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[9]));

iTimeCount = 0;
bGameRunning = 1;
bGamePaused = nil;
TmrMan.RemoveTimer(0);
        tmr = TmrMan.AddTimer(6000);
end

function StopJumblePhrase()
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[15]));
sActualPhrase, sNoSpacePhrase, sJumblePhrase = nil, nil, nil;

iTimeCount = 0;
bGamePaused = 1;
iGameNoReplyCount = iGameNoReplyCount + 1;
end

-- String Functions --

function StrReplace(sString)
sString = string.gsub(sString, "%$(%w+)", function (w)
if(string.sub(w,1,1) == "$") then
return string.sub(w,2,-1);
else
return _G[w] or "nil";
end
end);

return sString;
end

-- Score Functions --

function SetScores(sName)
local iCount = #tScores;
iDisplayCount = iDisplayScores or iCount

if(iDisplayScores) then
sScoresOutput = StrReplace(tStringTable[16]);
else
sScoresOutput = StrReplace(tStringTable[17]);
end

if(iCount == 0) then
sScoresOutput = StrReplace(tStringTable[18]);
else
table.sort(tScores, function(a, b) return a[2] > b[2]; end);

for i = 1, iCount, 1 do
tScores[i][3] = i; -- set ranks for all users
end

for i = 1, math.min(iDisplayCount, iCount) do
iPos = i;
sUserName = tScores[iPos][1];
iUserScore = tScores[iPos][2];
sScoresOutput = sScoresOutput..StrReplace(tStringTable[19]);
end
end

sUserName = sName;

if(sName) then
return tScoresByName[sName][3];
end
end

function ReadScoresFromFile()
dofile(sJumbleScoresFile);

for i = 1, #tScores, 1 do
tScoresByName[tScores[i][1]] = tScores[i];
end
end
function ToArrival(user,data)
Core.GetUserAllData(user)
ChatArrival(user, data)
end
function WriteScoresToFile()
local hFile,Er = io.open(sJumbleScoresFile, "w");
hFile:write("tScores = {\n");
for i = 1, #tScores, 1 do
hFile:write("["..i.."] = {"..string.format("%q", tScores[i][1])..","..tScores[i][2].."},\n");
end
hFile:write("n="..#tScores.."\n");
hFile:write("};");
hFile:close();
end

-- Hint Functions --

function SetJumbleHint()
local tRevealArray = { };

local iReveal = assert(loadstring(StrReplace(sHintFormula)))();

local iPos = nil;

while(iReveal > 0) do
repeat iPos = math.random(iLength); until tRevealArray[iPos] == nil

tRevealArray[iPos] = 1;
iReveal = iReveal - 1;
end

tRevealArray.iPos = 1;

sHint = string.gsub(sActualPhrase, "(%S)", function (c)
if (c == nil) then return ""; end

local out, tTemp = nil, tRevealArray;

if(tTemp[tTemp.iPos]) then
out = c;
else
out = sHintChar;
end

tTemp.iPos = tTemp.iPos + 1;

return out.." ";
end);
sHint = string.sub(sHint, 1, -2);

sHintOutput = StrReplace(tStringTable[20]);
end

-- Channel Functions --

function SendToAllInChannel(sSender, sRawOutput)
if(sSender) then
local _;

if(sRawOutput == nil) then
sRawOutput = sSender;
_, _, sSender = string.find(sRawOutput, "^%<(.-)%>");
else
sRawOutput = "<"..sSender.."> "..sRawOutput;
end

for nick, val in pairs(tChannelUsers) do
if(nick ~= sSender) then
Core.SendToNick(nick, "$To: "..nick.." From: "..sBotName.." $"..sRawOutput);
end
end
end
end
RegDisconnected = UserDisconnected
Title: Re: Jumble
Post by: Gnuff? on 27 February, 2008, 18:23:51
[quote author=[NL]scooby link=topic=7622.msg72293#msg72293 date=1204131225]
--------------------------------------------------------------------------------------
-- This script is converted with PtokaX LUA API Converter v0.9 at 02/16/08 14:01:24 --
--------------------------------------------------------------------------------------
sAuthor = "Donald Duck";
sEmail = "woordmix@kinderspel.nl";
sConversion = "Converted to LUA API by [NL]Scooby? 02/16/2008"
bGameRunning, bGamePaused = nil, nil;
sJumblePhrase, sActualPhrases, NoSpacePhrase = nil,nil,nil;
iLength, iBonusTime = 0, 0;
sScoreOutput, sHintOutput, sHelpOutput, sOpHelpOutput = nil, nil, nil, "";
iTimeCount, iGameNoReplyCount = 0, 0;
tScores, tScoresByName  = { n = 0 }, {};
tChannelUsers, iChannelCount = {}, nil;

-- PtokaX Events --

function OnStartup()
dofile("jumble.cfg");

sScoreFormula = "return "..sScoreFormula;
sBonusFormula = "return "..sBonusFormula;
sHintFormula  = "return "..sHintFormula;

Core.RegBot(sBotName,"","",true);

sHelpOutput = StrReplace(tStringTable[1]);

for cmd,tTemp in pairs(tCommands) do
if(tTemp[1] == DisplayJumbleHelp) then
sJumbleHelp = cmd;
end

sCmd, sDesc = cmd, tTemp[3];

if(tTemp[2] == 0) then
sHelpOutput = sHelpOutput..StrReplace(tStringTable[2]);
else
sOpHelpOutput = sOpHelpOutput..StrReplace(tStringTable[2]);
end
end

OldSendToAll = SendToAll;
UserDisconnected = LeaveJumble;
OpDisconnected = LeaveJumble;



ReadScoresFromFile();
SetScores();
end

function OnTimer(tmr)
if(bGamePaused) then
iTimeCount = iTimeCount + 1;

if(iTimeCount >= iPauseTime) then
StartJumblePhrase();
end
elseif(bGameRunning) then
iTimeCount = iTimeCount + 1;

if(iTimeCount == iHintTime) then
Core.SendToAll("<"..sBotName.."> ".. sHintOutput);
elseif (iTimeCount == iGameEndTime - 10) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[3]));
elseif (iTimeCount == iGameEndTime) then
StopJumblePhrase();
if(iGameNoReplyCount >= 3) then
StopJumbleGame();
end
else

end
else

end
end



function ChatArrival(curUser,sData)
Core.GetUserAllData(curUser)
local s, e, sNewData, chat, cmd, args = string.find(sData, "(%b<>%s+((%S+)%s*([^%|]*)))%|$");
local s, e, sTo = string.find(sData, "^%$To: (%S+)");

if(cmd == nil or (sTo and sTo ~= sBotName)) then return false; end

sUserName = curUser.sNick;

if(sTo) then
curUser.SendData = curUser.SendPM;
end

cmd = string.lower(cmd);

if(tCommands[cmd] and (tCommands[cmd][2] == 0 or curUser.bOperator)) then
Core.SendToNick(curUser.sNick,sData);
tCommands[cmd][1](curUser, args);
return 1;
else
if (not(iChannelCount) and string.sub(sData,1,1) == "<") then
iGameNoReplyCount = 0;
end

if(iChannelCount and string.find(sData, "$To: "..sBotName, 1, 1) == 1 and cmd == "!me") then
sNewData = "* "..sUserName..string.sub(chat, 4);
end

if(bGameRunning and bGamePaused == nil and string.find(string.upper(chat), sActualPhrase, 1, 1)) then
Core.SendToAll(sNewData);

iBonusTime = math.max(iBonusTimeLimit - iTimeCount, 0);

iScore = assert(loadstring(StrReplace(sScoreFormula)))();
iBonus = assert(loadstring(StrReplace(sBonusFormula)))();

if(tScoresByName[sUserName]) then
tScoresByName[sUserName][2] = tScoresByName[sUserName][2] + iScore + iBonus;
else
tScoresByName[sUserName] = {sUserName, iScore + iBonus};
table.insert(tScores, tScoresByName[sUserName]);
end

iRank = SetScores(sUserName);
iRankTotal = #tScores;
iUserScore = tScoresByName[sUserName][2];

WriteScoresToFile();

StopJumblePhrase();

if(iBonusTime ~= 0) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[4]));
else
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[5]));
end

iGameNoReplyCount = 0;

return 1;
else
if(iChannelCount and string.find(sData, "^$To: "..sBotName, 1, 1) == 1) then
if(tChannelUsers[sUserName] == nil) then
JoinJumble(curUser);
else
Core.SendToAll(sNewData);
iGameNoReplyCount = 0;
end

return 1;
end
end

end
end

-- Command Functions --

function StartJumbleGame(curUser, args)
if(bGameRunning) then return; end
if(args ~= "") then
HubGameActive = 2
local s, e, args = string.find(args, "([%w%s]+)$");
args = "Jumble\\"..args..".txt";
local f,e = io.open(args,"r")
if f then
sJumbleWordsFile = args;
f:close(f)
end
end
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[6]));
StartJumblePhrase();
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[7]));

iGameNoReplyCount = 0;
end

function StopJumbleGame(curUser)
if not(bGameRunning) then return; end

if not(bGamePaused) then
StopJumblePhrase();
end

if(curUser) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[8]));
else
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[21]));
-- TODO: fix numbers
end

bGameRunning, bGamePaused = nil, nil;
iBonusTime, iLength = 0, 0;
TmrMan.RemoveTimer(tmr);
end

function DisplayJumblePhrase(curUser)
if(bGameRunning and not bGamePaused) then
if(curUser == nil) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[9]));
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[9]));
end
end
end

function DisplayJumbleHelp(curUser)
if(curUser.bOperator) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHelpOutput..sOpHelpOutput);
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHelpOutput);
end
end

function DisplayJumbleScores(curUser)
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sScoresOutput);
end

function DisplayMyJumbleScore(curUser)
iRank = tScoresByName[curUser.sNick][3];
iRankTotal = #tScores;
iUserScore = tScoresByName[sUserName][2];

if(tScoresByName[curUser.sNick]) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[10]));
else
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. StrReplace(tStringTable[11]));
end
end

function DisplayJumbleHint(curUser)
if(iTimeCount >= iHintTime) then
Core.SendToNick(curUser.sNick,"<"..sBotName.."> ".. sHintOutput);
end
end

function JoinJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick] == nil) then
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[12]));

Core.SendPmToNick(curUser.sNick,sBotName, StrReplace(tStringTable[13]));
curUser.SendData = curUser.SendPM;

DisplayJumblePhrase(curUser);
DisplayJumbleHint(curUser);

tChannelUsers[curUser.sNick] = 1;
iChannelCount = iChannelCount + 1;
end
end

function LeaveJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick]) then
sUserName = curUser.sNick;
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[14]));

tChannelUsers[curUser.sNick] = nil;
iChannelCount = iChannelCount - 1;

if(iChannelCount <= 0) then
iChannelCount = 0;
StopJumbleGame();
end
end
end

function SetPrivateJumbleGame(curUser)
if(iChannelCount == nil) then
SendToAll = SendToAllInChannel;
iChannelCount = 0;
end
end

function SetPublicJumbleGame(curUser)
if(iChannelCount) then
SendToAll = OldSendToAll;
tChannelUsers = {};
iChannelCount = nil;
end
end

-- Jumble Phrase Functions --

function JumbleString(str)
local tStr = { n = 0; };
local newString = "";

string.gsub(str, "(.)", function(c) table.insert(tStr, c); end);

math.randomseed(os.clock());

-- Fisher-Yates shuffle
for n = #tStr, 1, -1 do
local pos = math.random(n);
newString = tStr[pos].." "..newString;
tStr[pos] = tStr[n];
end

return newString;
end

function JumblePhrase(phr)
return string.sub(string.gsub(phr, "(%S+)", function (w) return JumbleString(w).."  "; end), 1, -2);
end

function StartJumblePhrase()
local hFile,Er = io.open(sJumbleWordsFile, "r");
if(hFile == nil) then
Core.SendToAll("<"..sBotName.."> Error opening "..sJumbleWordsFile);
return;
end
local _, phr = nil, nil;
local iFileSize = hFile:seek("end")
math.randomseed(os.clock());
while(phr == nil) do
hFile:seek("set", math.random(iFileSize)-1);
_, phr = hFile:read( "*l", "*l");
end
hFile:close();
sActualPhrase = string.upper(phr);
sNoSpacePhrase = string.gsub(sActualPhrase, "%s", "");
sJumblePhrase = JumblePhrase(sActualPhrase);
iLength = string.len(sNoSpacePhrase);

SetJumbleHint();

Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[9]));

iTimeCount = 0;
bGameRunning = 1;
bGamePaused = nil;
TmrMan.RemoveTimer(0);
        tmr = TmrMan.AddTimer(6000);
end

function StopJumblePhrase()
Core.SendToAll("<"..sBotName.."> ".. StrReplace(tStringTable[15]));
sActualPhrase, sNoSpacePhrase, sJumblePhrase = nil, nil, nil;

iTimeCount = 0;
bGamePaused = 1;
iGameNoReplyCount = iGameNoReplyCount + 1;
end

-- String Functions --

function StrReplace(sString)
sString = string.gsub(sString, "%$(%w+)", function (w)
if(string.sub(w,1,1) == "$") then
return string.sub(w,2,-1);
else
return _G[w] or "nil";
end
end);

return sString;
end

-- Score Functions --

function SetScores(sName)
local iCount = #tScores;
iDisplayCount = iDisplayScores or iCount

if(iDisplayScores) then
sScoresOutput = StrReplace(tStringTable[16]);
else
sScoresOutput = StrReplace(tStringTable[17]);
end

if(iCount == 0) then
sScoresOutput = StrReplace(tStringTable[18]);
else
table.sort(tScores, function(a, b) return a[2] > b[2]; end);

for i = 1, iCount, 1 do
tScores[i][3] = i; -- set ranks for all users
end

for i = 1, math.min(iDisplayCount, iCount) do
iPos = i;
sUserName = tScores[iPos][1];
iUserScore = tScores[iPos][2];
sScoresOutput = sScoresOutput..StrReplace(tStringTable[19]);
end
end

sUserName = sName;

if(sName) then
return tScoresByName[sName][3];
end
end

function ReadScoresFromFile()
dofile(sJumbleScoresFile);

for i = 1, #tScores, 1 do
tScoresByName[tScores[i][1]] = tScores[i];
end
end
function ToArrival(user,data)
Core.GetUserAllData(user)
ChatArrival(user, data)
end
function WriteScoresToFile()
local hFile,Er = io.open(sJumbleScoresFile, "w");
hFile:write("tScores = {\n");
for i = 1, #tScores, 1 do
hFile:write("["..i.."] = {"..string.format("%q", tScores[i][1])..","..tScores[i][2].."},\n");
end
hFile:write("n="..#tScores.."\n");
hFile:write("};");
hFile:close();
end

-- Hint Functions --

function SetJumbleHint()
local tRevealArray = { };

local iReveal = assert(loadstring(StrReplace(sHintFormula)))();

local iPos = nil;

while(iReveal > 0) do
repeat iPos = math.random(iLength); until tRevealArray[iPos] == nil

tRevealArray[iPos] = 1;
iReveal = iReveal - 1;
end

tRevealArray.iPos = 1;

sHint = string.gsub(sActualPhrase, "(%S)", function (c)
if (c == nil) then return ""; end

local out, tTemp = nil, tRevealArray;

if(tTemp[tTemp.iPos]) then
out = c;
else
out = sHintChar;
end

tTemp.iPos = tTemp.iPos + 1;

return out.." ";
end);
sHint = string.sub(sHint, 1, -2);

sHintOutput = StrReplace(tStringTable[20]);
end

-- Channel Functions --

function SendToAllInChannel(sSender, sRawOutput)
if(sSender) then
local _;

if(sRawOutput == nil) then
sRawOutput = sSender;
_, _, sSender = string.find(sRawOutput, "^%<(.-)%>");
else
sRawOutput = "<"..sSender.."> "..sRawOutput;
end

for nick, val in pairs(tChannelUsers) do
if(nick ~= sSender) then
Core.SendToNick(nick, "$To: "..nick.." From: "..sBotName.." $"..sRawOutput);
end
end
end
end
RegDisconnected = UserDisconnected
[/quote]


Wrapping Code Around makes the post a lot shorter
Title: Re: Jumble
Post by: [NL]scooby on 27 February, 2008, 19:15:18
Thanks Gnuff?  ::)
Title: Re: Jumble
Post by: F_O_X on 28 February, 2008, 06:42:35
With the help of the converter from Huhgarista I too can so to convert.
Mistakes after converting:
1. In PM questions are not sent.
2. Intervals of time between questions and helps changeable.

P.S.
I am sorry for my English.
Title: Re: Jumble
Post by: ]-[?LL?L?L??nG?L on 16 March, 2008, 11:48:45
Im wondering if someone can help me with this Jumble script. When i start it, it starts twice like this...
<]-[?LL?L?L??nG?L> a.start <-------
<????gr?m?M??']['?R?> ]-[?LL?L?L??nG?L has started a new ????gr?m?M??']['?R? game!!!
<????gr?m?M??']['?R?> The Jumble Phrase is: D P G R U E 
<????gr?m?M??']['?R?> Type "a.help" for help.
<]-[?LL?L?L??nG?L> a.start <------
also on stop it does this too, and when you answer theres 2 answers ???
does anyone know why this is, also the timer is taking much longer now since its been converted to the new API
I only have this script in to test for now so i know it isnt any other script conflicting
please help  :-*
Title: Re: Jumble
Post by: ]-[?LL?L?L??nG?L on 31 March, 2008, 18:21:21
Can anyone tell me if they have this problem too? :-\
Title: Re: Jumble
Post by: Madman on 01 April, 2008, 12:53:19

sAuthor = "RabidWombat";
sEmail = "cbarber@dwc.edu";
sConversion = " Converted to LUA 5 by ?˜”??•Hawk•??”˜? 03/03/2005"
bGameRunning, bGamePaused = nil, nil;
sJumblePhrase, sActualPhrases, NoSpacePhrase = nil,nil,nil;
iLength, iBonusTime = 0, 0;
sScoreOutput, sHintOutput, sHelpOutput, sOpHelpOutput = nil, nil, nil, "";
iTimeCount, iGameNoReplyCount = 0, 0;
tScores, tScoresByName  = { n = 0 }, {};
tChannelUsers, iChannelCount = {}, nil;

--[[
Mod by Mutor          07/30/06
        -Converted to LUA 5.11 [LUA 5.02/5.1x compatible]
-Added user commands [right click]
-Added check for file(s), inform ops
]]

-- Converted to API 2 by Madman, 01.04.08

-- PtokaX Events --
function OnStartup()
local errmsg = "Error!: "
if loadfile("jumble.cfg") then
dofile("jumble.cfg");
else
Core.SendToOps("<JumbleFever> Jumble.cfg.txt: No such "..
"file or directory. JumbleFever halted!.")
return true
end
append = sBotName.." halted!"
local f1,e1 = io.open(sJumbleWordsFile)
if f1 then
f1:close()
else
errmsg = errmsg..e1
Core.SendToOps("<"..sBotName.."> "..string.gsub(errmsg,"\n",". ")..append)
return true
end
local f2,e2 = io.open(sJumbleScoresFile)
if f2 then
f2:close()
else
errmsg = errmsg..e2
append = "creating file..."
Core.SendToOps("<"..sBotName.."> "..string.gsub(errmsg,"\n",". ")..append)
WriteScoresToFile()
if loadfile(sJumbleScoresFile) then
append = sJumbleScoresFile.." successfully created."
Core.SendToOps("<"..sBotName.."> "..append)
OnStartup()
else
append = "Failed to create: "..sJumbleScoresFile.."!"
Core.SendToOps("<"..sBotName.."> "..append)
append = nil
return true
end
end
sScoreFormula = "return "..sScoreFormula;
sBonusFormula = "return "..sBonusFormula;
sHintFormula  = "return "..sHintFormula;
Core.RegBot(sBotName,"","",true)
sHelpOutput = StrReplace(tStringTable[1]);
for cmd,tTemp in pairs(tCommands) do
if(tTemp[1] == DisplayJumbleHelp) then
sJumbleHelp = cmd;
end
sCmd, sDesc = cmd, tTemp[3];
if(tTemp[2] == 0) then
sHelpOutput = sHelpOutput..StrReplace(tStringTable[2]);
else
sOpHelpOutput = sOpHelpOutput..StrReplace(tStringTable[2]);
end
end
UserDisconnected = LeaveJumble;
RegDisconnected = LeaveJumble
OpDisconnected = LeaveJumble;
ReadScoresFromFile();
SetScores();
append = nil
collectgarbage("collect")
end

function UserConnected(user)
for i,v in pairs(tCommands) do
if v[2] == 0 or user.bOperator then
Core.SendToUser(user,"$UserCommand 1 3 "..sBotName.."\\"..v[3].."$<%[mynick]> "..i.."&#124;")
end
end
end
OpConnected = UserConnected
RegConnected = UserConnected

function OnTimer()
if(bGamePaused) then
iTimeCount = iTimeCount + 1;
if(iTimeCount >= iPauseTime) then
StartJumblePhrase();
end
elseif(bGameRunning) then
iTimeCount = iTimeCount + 1;
if(iTimeCount == iHintTime) then
Core.SendToAll("<"..sBotName.."> "..sHintOutput);
elseif (iTimeCount == iGameEndTime - 10) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[3]));
elseif (iTimeCount == iGameEndTime) then
StopJumblePhrase();
if(iGameNoReplyCount >= 3) then
StopJumbleGame();
end
else
end
else
end
end

function Send(user,msg)
if sendChk then
Core.SendToUser(user,msg.. "|")
else
Core.SendPmToUser(user,sBotName,msg.. "|")
end
end

function ChatArrival(curUser, sData)
local s, e, sNewData, chat, cmd, args = string.find(sData, "(%b<>%s+((%S+)%s*([^%|]*)))%|$");
local s, e, sTo = string.find(sData, "^%$To: (%S+)");
if(cmd == nil or (sTo and sTo ~= sBotName)) then return false; end
sUserName = curUser.sNick;
if(sTo) then
sendChk = false
--curUser.SendData = curUser.SendPM;
else
sendChk = true
end
cmd = string.lower(cmd);
if(tCommands[cmd] and (tCommands[cmd][2] == 0 or curUser.bOperator)) then
--Send(curUser,sData)
tCommands[cmd][1](curUser, args)
return true
else
if (not(iChannelCount) and string.sub(sData,1,1) == "<") then
iGameNoReplyCount = 0;
end
if(iChannelCount and string.find(sData, "$To: "..sBotName, 1, 1) == 1 and cmd == "!me") then
sNewData = "* "..sUserName..string.sub(chat, 4);
end
if(bGameRunning and bGamePaused == nil and string.find(string.upper(chat), sActualPhrase, 1, 1)) then
Core.SendToAll(sNewData);
iBonusTime = math.max(iBonusTimeLimit - iTimeCount, 0);
iScore = assert(loadstring(StrReplace(sScoreFormula)))();
iBonus = assert(loadstring(StrReplace(sBonusFormula)))();
if(tScoresByName[sUserName]) then
tScoresByName[sUserName][2] = tScoresByName[sUserName][2] + iScore + iBonus;
else
tScoresByName[sUserName] = {sUserName, iScore + iBonus};
table.insert(tScores, tScoresByName[sUserName]);
end
iRank = SetScores(sUserName);
iRankTotal = table.maxn(tScores);
iUserScore = tScoresByName[sUserName][2];
WriteScoresToFile();
StopJumblePhrase();
if(iBonusTime ~= 0) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[4]));
else
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[5]));
end
iGameNoReplyCount = 0;
return true;
else
if(iChannelCount and string.find(sData, "^$To: "..sBotName, 1, 1) == 1) then
if(tChannelUsers[sUserName] == nil) then
JoinJumble(curUser);
else
Core.SendToAll(sNewData);
iGameNoReplyCount = 0;
end
return true;
end
end
end
end

-- Command Functions --
function StartJumbleGame(curUser, args)
if(bGameRunning) then return; end
if(args ~= "") then
HubGameActive = 2
local s, e, args = string.find(args, "([%w%s]+)$");
args = "Jumble\\"..args..".txt";
local f,e = io.open(args,"r")
if f then
sJumbleWordsFile = args;
f:close(f)
end
end
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[6]));
StartJumblePhrase();
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[7]));
iGameNoReplyCount = 0;
end

function StopJumbleGame(curUser)
if not(bGameRunning) then return; end
if not(bGamePaused) then
StopJumblePhrase();
end
if(curUser) then
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[8]));
else
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[21]));
-- TODO: fix numbers
end
bGameRunning, bGamePaused = nil, nil;
iBonusTime, iLength = 0, 0;
end

function DisplayJumblePhrase(curUser)
if(bGameRunning and not bGamePaused) then
if(curUser == nil) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[9]));
else
Send(curUser,StrReplace(tStringTable[9]))
end
end
end

function DisplayJumbleHelp(curUser)
sendChk = true
if(curUser.bOperator) then
Send(curUser,"<"..sBotName.."> "..sHelpOutput..sOpHelpOutput);
else
Send(curUser,"<"..sBotName.."> "..sHelpOutput);
end
end

function DisplayJumbleScores(curUser)
Send(curUser,"<"..sBotName.."> "..sScoresOutput);
end

function DisplayMyJumbleScore(curUser)
iRank = tScoresByName[curUser.sNick][3];
iRankTotal = table.maxn(tScores);
iUserScore = tScoresByName[sUserName][2];
if(tScoresByName[curUser.sNick]) then
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[10]));
else
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[11]));
end
end

function DisplayJumbleHint(curUser)
if(iTimeCount >= iHintTime) then
Send(curUser,"<"..sBotName.."> "..sHintOutput);
end
end

function JoinJumble(curUser)
Core.SendToAll(iChannelCount)
if (iChannelCount and tChannelUsers[curUser.sNick] == nil) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[12]));
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[13]));
DisplayJumblePhrase(curUser);
DisplayJumbleHint(curUser);
tChannelUsers[curUser.sNick] = 1;
iChannelCount = iChannelCount + 1;
end
end

function LeaveJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick]) then
sUserName = curUser.sNick;
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[14]));
tChannelUsers[curUser.sNick] = nil;
iChannelCount = iChannelCount - 1;
if(iChannelCount <= 0) then
iChannelCount = 0;
StopJumbleGame();
end
end
end

function SetPrivateJumbleGame(curUser)
if(iChannelCount == nil) then
Core.SendToAll = SendToAllInChannel;
iChannelCount = 0;
end
end

function SetPublicJumbleGame(curUser)
if(iChannelCount) then
tChannelUsers = {};
iChannelCount = nil;
end
end

-- Jumble Phrase Functions --
function JumbleString(str)
local tStr = { n = 0; };
local newString = "";
string.gsub(str, "(.)", function(c) table.insert(tStr, c); end);
math.randomseed(os.clock());
-- Fisher-Yates shuffle
for n = table.maxn(tStr), 1, -1 do
local pos = math.random(n);
newString = tStr[pos].." "..newString;
tStr[pos] = tStr[n];
end
return newString;
end

function JumblePhrase(phr)
return string.sub(string.gsub(phr, "(%S+)", function (w) return JumbleString(w).."  "; end), 1, -2);
end

function StartJumblePhrase()
local hFile,Er = io.open(sJumbleWordsFile, "r");
if(hFile == nil) then
Core. SendToAll(sBotName, "Error opening "..sJumbleWordsFile);
return;
end
local _, phr = nil, nil;
local iFileSize = hFile:seek("end")
math.randomseed(os.clock());
while(phr == nil) do
hFile:seek("set", math.random(iFileSize)-1);
_, phr = hFile:read( "*l", "*l");
end
hFile:close();
sActualPhrase = string.upper(phr);
sNoSpacePhrase = string.gsub(sActualPhrase, "%s", "");
sJumblePhrase = JumblePhrase(sActualPhrase);
iLength = string.len(sNoSpacePhrase);
SetJumbleHint();
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[9]));
iTimeCount = 0;
bGameRunning = 1;
bGamePaused = nil;
if tmrId then
TmrMan.RemoveTimer(tmrId)
end
tmrId = TmrMan.AddTimer(1000);
end

function StopJumblePhrase()
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[15]));
sActualPhrase, sNoSpacePhrase, sJumblePhrase = nil, nil, nil;
iTimeCount = 0;
bGamePaused = 1;
iGameNoReplyCount = iGameNoReplyCount + 1;
end

-- String Functions --
function StrReplace(sString)
sString = string.gsub(sString, "%$(%w+)", function (w)
if(string.sub(w,1,1) == "$") then
return string.sub(w,2,-1);
else
return _G[w] or "nil";
end
end);
return sString;
end

-- Score Functions --
function SetScores(sName)
local iCount = table.maxn(tScores);
iDisplayCount = iDisplayScores or iCount
if(iDisplayScores) then
sScoresOutput = StrReplace(tStringTable[16]);
else
sScoresOutput = StrReplace(tStringTable[17]);
end
if(iCount == 0) then
sScoresOutput = StrReplace(tStringTable[18]);
else
table.sort(tScores, function(a, b) return a[2] > b[2]; end);
for i = 1, iCount, 1 do
tScores[i][3] = i; -- set ranks for all users
end
for i = 1, math.min(iDisplayCount, iCount), 1 do
iPos = i;
sUserName = tScores[iPos][1];
iUserScore = tScores[iPos][2];
sScoresOutput = sScoresOutput..StrReplace(tStringTable[19]);
end
end
sUserName = sName;
if(sName) then
return tScoresByName[sName][3];
end
end

function ReadScoresFromFile()
dofile(sJumbleScoresFile);
for i = 1, table.maxn(tScores), 1 do
tScoresByName[tScores[i][1]] = tScores[i];
end
end

function ToArrival(user, data)
ChatArrival(user, data)
end

function WriteScoresToFile()
local hFile,Er = io.open(sJumbleScoresFile, "w");
hFile:write("tScores = {\n");
for i = 1, table.maxn(tScores), 1 do
hFile:write("["..i.."] = {"..string.format("%q", tScores[i][1])..","..tScores[i][2].."},\n");
end
hFile:write("n="..table.maxn(tScores).."\n");
hFile:write("};");
hFile:close();
end

-- Hint Functions --
function SetJumbleHint()
local tRevealArray = { };
local iReveal = assert(loadstring(StrReplace(sHintFormula)))();
local iPos = nil;
while(iReveal > 0) do
repeat iPos = math.random(iLength); until tRevealArray[iPos] == nil
tRevealArray[iPos] = 1;
iReveal = iReveal - 1;
end
tRevealArray.iPos = 1;
sHint = string.gsub(sActualPhrase, "(%S)", function (c)
if (c == nil) then return ""; end
local out, tTemp = nil, tRevealArray;
if(tTemp[tTemp.iPos]) then
out = c;
else
out = sHintChar;
end
tTemp.iPos = tTemp.iPos + 1;
return out.." ";
end);
sHint = string.sub(sHint, 1, -2);
sHintOutput = StrReplace(tStringTable[20]);
end

-- Channel Functions --
function SendToAllInChannel(sSender, sRawOutput)
if(sSender) then
local _;
if(sRawOutput == nil) then
sRawOutput = sSender;
_, _, sSender = string.find(sRawOutput, "^%<(.-)%>");
else
sRawOutput = "<"..sSender.."> "..sRawOutput;
end
for nick, val in pairs(tChannelUsers) do
if(nick ~= sSender) then
Core.SendToNick(nick,sBotName,sRawOutput);
end
end
end
end


Let me know if it don't work as it should...
Title: Re: Jumble
Post by: ]-[?LL?L?L??nG?L on 01 April, 2008, 14:17:15
Is working absolutely perfectly hun just as it should Thank you  :P :P :P
Title: Re: Jumble
Post by: Madman on 16 April, 2009, 13:58:47

sAuthor = "RabidWombat";
sEmail = "cbarber@dwc.edu";
sConversion = " Converted to LUA 5 by ?˜”??•Hawk•??”˜? 03/03/2005"
bGameRunning, bGamePaused = nil, nil;
sJumblePhrase, sActualPhrases, NoSpacePhrase = nil,nil,nil;
iLength, iBonusTime = 0, 0;
sScoreOutput, sHintOutput, sHelpOutput, sOpHelpOutput = nil, nil, nil, "";
iTimeCount, iGameNoReplyCount = 0, 0;
tScores, tScoresByName  = { n = 0 }, {};
tChannelUsers, iChannelCount = {}, nil;

--[[
Mod by Mutor          07/30/06
-Converted to LUA 5.11 [LUA 5.02/5.1x compatible]
-Added user commands [right click]
-Added check for file(s), inform ops
Mod by Madman 16.04.09
-Fixed path error
]]

-- Converted to API 2 by Madman, 01.04.08

-- PtokaX Events --
function OnStartup()
local errmsg = "Error!: "
if loadfile(Core.GetPtokaXPath().."scripts/jumble.cfg") then
dofile(Core.GetPtokaXPath().."scripts/jumble.cfg");
else
Core.SendToOps("<JumbleFever> Jumble.cfg.txt: No such "..
"file or directory. JumbleFever halted!.")
return true
end
append = sBotName.." halted!"
local f1,e1 = io.open(Core.GetPtokaXPath().."scripts/"..sJumbleWordsFile)
if f1 then
f1:close()
else
errmsg = errmsg..e1
Core.SendToOps("<"..sBotName.."> "..string.gsub(errmsg,"\n",". ")..append)
return true
end
local f2,e2 = io.open(Core.GetPtokaXPath().."scripts/"..sJumbleScoresFile)
if f2 then
f2:close()
else
errmsg = errmsg..e2
append = "creating file..."
Core.SendToOps("<"..sBotName.."> "..string.gsub(errmsg,"\n",". ")..append)
WriteScoresToFile()
if loadfile(Core.GetPtokaXPath().."scripts/"..sJumbleScoresFile) then
append = sJumbleScoresFile.." successfully created."
Core.SendToOps("<"..sBotName.."> "..append)
OnStartup()
else
append = "Failed to create: "..sJumbleScoresFile.."!"
Core.SendToOps("<"..sBotName.."> "..append)
append = nil
return true
end
end
sScoreFormula = "return "..sScoreFormula;
sBonusFormula = "return "..sBonusFormula;
sHintFormula  = "return "..sHintFormula;
Core.RegBot(sBotName,"","",true)
sHelpOutput = StrReplace(tStringTable[1]);
for cmd,tTemp in pairs(tCommands) do
if(tTemp[1] == DisplayJumbleHelp) then
sJumbleHelp = cmd;
end
sCmd, sDesc = cmd, tTemp[3];
if(tTemp[2] == 0) then
sHelpOutput = sHelpOutput..StrReplace(tStringTable[2]);
else
sOpHelpOutput = sOpHelpOutput..StrReplace(tStringTable[2]);
end
end
UserDisconnected = LeaveJumble;
RegDisconnected = LeaveJumble
OpDisconnected = LeaveJumble;
ReadScoresFromFile();
SetScores();
append = nil
collectgarbage("collect")
end

function UserConnected(user)
for i,v in pairs(tCommands) do
if v[2] == 0 or user.bOperator then
Core.SendToUser(user,"$UserCommand 1 3 "..sBotName.."\\"..v[3].."$<%[mynick]> "..i.."&#124;")
end
end
end
OpConnected = UserConnected
RegConnected = UserConnected

function OnTimer()
if(bGamePaused) then
iTimeCount = iTimeCount + 1;
if(iTimeCount >= iPauseTime) then
StartJumblePhrase();
end
elseif(bGameRunning) then
iTimeCount = iTimeCount + 1;
if(iTimeCount == iHintTime) then
Core.SendToAll("<"..sBotName.."> "..sHintOutput);
elseif (iTimeCount == iGameEndTime - 10) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[3]));
elseif (iTimeCount == iGameEndTime) then
StopJumblePhrase();
if(iGameNoReplyCount >= 3) then
StopJumbleGame();
end
else
end
else
end
end

function Send(user,msg)
if sendChk then
Core.SendToUser(user,msg.. "|")
else
Core.SendPmToUser(user,sBotName,msg.. "|")
end
end

function ChatArrival(curUser, sData)
local s, e, sNewData, chat, cmd, args = string.find(sData, "(%b<>%s+((%S+)%s*([^%|]*)))%|$");
local s, e, sTo = string.find(sData, "^%$To: (%S+)");
if(cmd == nil or (sTo and sTo ~= sBotName)) then return false; end
sUserName = curUser.sNick;
if(sTo) then
sendChk = false
--curUser.SendData = curUser.SendPM;
else
sendChk = true
end
cmd = string.lower(cmd);
if(tCommands[cmd] and (tCommands[cmd][2] == 0 or curUser.bOperator)) then
--Send(curUser,sData)
tCommands[cmd][1](curUser, args)
return true
else
if (not(iChannelCount) and string.sub(sData,1,1) == "<") then
iGameNoReplyCount = 0;
end
if(iChannelCount and string.find(sData, "$To: "..sBotName, 1, 1) == 1 and cmd == "!me") then
sNewData = "* "..sUserName..string.sub(chat, 4);
end
if(bGameRunning and bGamePaused == nil and string.find(string.upper(chat), sActualPhrase, 1, 1)) then
Core.SendToAll(sNewData);
iBonusTime = math.max(iBonusTimeLimit - iTimeCount, 0);
iScore = assert(loadstring(StrReplace(sScoreFormula)))();
iBonus = assert(loadstring(StrReplace(sBonusFormula)))();
if(tScoresByName[sUserName]) then
tScoresByName[sUserName][2] = tScoresByName[sUserName][2] + iScore + iBonus;
else
tScoresByName[sUserName] = {sUserName, iScore + iBonus};
table.insert(tScores, tScoresByName[sUserName]);
end
iRank = SetScores(sUserName);
iRankTotal = table.maxn(tScores);
iUserScore = tScoresByName[sUserName][2];
WriteScoresToFile();
StopJumblePhrase();
if(iBonusTime ~= 0) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[4]));
else
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[5]));
end
iGameNoReplyCount = 0;
return true;
else
if(iChannelCount and string.find(sData, "^$To: "..sBotName, 1, 1) == 1) then
if(tChannelUsers[sUserName] == nil) then
JoinJumble(curUser);
else
Core.SendToAll(sNewData);
iGameNoReplyCount = 0;
end
return true;
end
end
end
end

-- Command Functions --
function StartJumbleGame(curUser, args)
if(bGameRunning) then return; end
if(args ~= "") then
HubGameActive = 2
local s, e, args = string.find(args, "([%w%s]+)$");
args = "Jumble\\"..args..".txt";
local f,e = io.open(args,"r")
if f then
sJumbleWordsFile = args;
f:close(f)
end
end
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[6]));
StartJumblePhrase();
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[7]));
iGameNoReplyCount = 0;
end

function StopJumbleGame(curUser)
if not(bGameRunning) then return; end
if not(bGamePaused) then
StopJumblePhrase();
end
if(curUser) then
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[8]));
else
Core.SendToAll("<"..sBotName..">"..StrReplace(tStringTable[21]));
-- TODO: fix numbers
end
bGameRunning, bGamePaused = nil, nil;
iBonusTime, iLength = 0, 0;
end

function DisplayJumblePhrase(curUser)
if(bGameRunning and not bGamePaused) then
if(curUser == nil) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[9]));
else
Send(curUser,StrReplace(tStringTable[9]))
end
end
end

function DisplayJumbleHelp(curUser)
sendChk = true
if(curUser.bOperator) then
Send(curUser,"<"..sBotName.."> "..sHelpOutput..sOpHelpOutput);
else
Send(curUser,"<"..sBotName.."> "..sHelpOutput);
end
end

function DisplayJumbleScores(curUser)
Send(curUser,"<"..sBotName.."> "..sScoresOutput);
end

function DisplayMyJumbleScore(curUser)
iRank = tScoresByName[curUser.sNick][3];
iRankTotal = table.maxn(tScores);
iUserScore = tScoresByName[sUserName][2];
if(tScoresByName[curUser.sNick]) then
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[10]));
else
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[11]));
end
end

function DisplayJumbleHint(curUser)
if(iTimeCount >= iHintTime) then
Send(curUser,"<"..sBotName.."> "..sHintOutput);
end
end

function JoinJumble(curUser)
Core.SendToAll(iChannelCount)
if (iChannelCount and tChannelUsers[curUser.sNick] == nil) then
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[12]));
Send(curUser,"<"..sBotName.."> "..StrReplace(tStringTable[13]));
DisplayJumblePhrase(curUser);
DisplayJumbleHint(curUser);
tChannelUsers[curUser.sNick] = 1;
iChannelCount = iChannelCount + 1;
end
end

function LeaveJumble(curUser)
if(iChannelCount and tChannelUsers[curUser.sNick]) then
sUserName = curUser.sNick;
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[14]));
tChannelUsers[curUser.sNick] = nil;
iChannelCount = iChannelCount - 1;
if(iChannelCount <= 0) then
iChannelCount = 0;
StopJumbleGame();
end
end
end

function SetPrivateJumbleGame(curUser)
if(iChannelCount == nil) then
Core.SendToAll = SendToAllInChannel;
iChannelCount = 0;
end
end

function SetPublicJumbleGame(curUser)
if(iChannelCount) then
tChannelUsers = {};
iChannelCount = nil;
end
end

-- Jumble Phrase Functions --
function JumbleString(str)
local tStr = { n = 0; };
local newString = "";
string.gsub(str, "(.)", function(c) table.insert(tStr, c); end);
math.randomseed(os.clock());
-- Fisher-Yates shuffle
for n = table.maxn(tStr), 1, -1 do
local pos = math.random(n);
newString = tStr[pos].." "..newString;
tStr[pos] = tStr[n];
end
return newString;
end

function JumblePhrase(phr)
return string.sub(string.gsub(phr, "(%S+)", function (w) return JumbleString(w).."  "; end), 1, -2);
end

function StartJumblePhrase()
local hFile,Er = io.open(sJumbleWordsFile, "r");
if(hFile == nil) then
Core. SendToAll(sBotName, "Error opening "..sJumbleWordsFile);
return;
end
local _, phr = nil, nil;
local iFileSize = hFile:seek("end")
math.randomseed(os.clock());
while(phr == nil) do
hFile:seek("set", math.random(iFileSize)-1);
_, phr = hFile:read( "*l", "*l");
end
hFile:close();
sActualPhrase = string.upper(phr);
sNoSpacePhrase = string.gsub(sActualPhrase, "%s", "");
sJumblePhrase = JumblePhrase(sActualPhrase);
iLength = string.len(sNoSpacePhrase);
SetJumbleHint();
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[9]));
iTimeCount = 0;
bGameRunning = 1;
bGamePaused = nil;
if tmrId then
TmrMan.RemoveTimer(tmrId)
end
tmrId = TmrMan.AddTimer(1000);
end

function StopJumblePhrase()
Core.SendToAll("<"..sBotName.."> "..StrReplace(tStringTable[15]));
sActualPhrase, sNoSpacePhrase, sJumblePhrase = nil, nil, nil;
iTimeCount = 0;
bGamePaused = 1;
iGameNoReplyCount = iGameNoReplyCount + 1;
end

-- String Functions --
function StrReplace(sString)
sString = string.gsub(sString, "%$(%w+)", function (w)
if(string.sub(w,1,1) == "$") then
return string.sub(w,2,-1);
else
return _G[w] or "nil";
end
end);
return sString;
end

-- Score Functions --
function SetScores(sName)
local iCount = table.maxn(tScores);
iDisplayCount = iDisplayScores or iCount
if(iDisplayScores) then
sScoresOutput = StrReplace(tStringTable[16]);
else
sScoresOutput = StrReplace(tStringTable[17]);
end
if(iCount == 0) then
sScoresOutput = StrReplace(tStringTable[18]);
else
table.sort(tScores, function(a, b) return a[2] > b[2]; end);
for i = 1, iCount, 1 do
tScores[i][3] = i; -- set ranks for all users
end
for i = 1, math.min(iDisplayCount, iCount), 1 do
iPos = i;
sUserName = tScores[iPos][1];
iUserScore = tScores[iPos][2];
sScoresOutput = sScoresOutput..StrReplace(tStringTable[19]);
end
end
sUserName = sName;
if(sName) then
return tScoresByName[sName][3];
end
end

function ReadScoresFromFile()
dofile(Core.GetPtokaXPath().."scripts/"..sJumbleScoresFile);
for i = 1, table.maxn(tScores), 1 do
tScoresByName[tScores[i][1]] = tScores[i];
end
end

function ToArrival(user, data)
ChatArrival(user, data)
end

function WriteScoresToFile()
local hFile,Er = io.open(Core.GetPtokaXPath().."scripts/"..sJumbleScoresFile, "w");
hFile:write("tScores = {\n");
for i = 1, table.maxn(tScores), 1 do
hFile:write("["..i.."] = {"..string.format("%q", tScores[i][1])..","..tScores[i][2].."},\n");
end
hFile:write("n="..table.maxn(tScores).."\n");
hFile:write("};");
hFile:close();
end

-- Hint Functions --
function SetJumbleHint()
local tRevealArray = { };
local iReveal = assert(loadstring(StrReplace(sHintFormula)))();
local iPos = nil;
while(iReveal > 0) do
repeat iPos = math.random(iLength); until tRevealArray[iPos] == nil
tRevealArray[iPos] = 1;
iReveal = iReveal - 1;
end
tRevealArray.iPos = 1;
sHint = string.gsub(sActualPhrase, "(%S)", function (c)
if (c == nil) then return ""; end
local out, tTemp = nil, tRevealArray;
if(tTemp[tTemp.iPos]) then
out = c;
else
out = sHintChar;
end
tTemp.iPos = tTemp.iPos + 1;
return out.." ";
end);
sHint = string.sub(sHint, 1, -2);
sHintOutput = StrReplace(tStringTable[20]);
end

-- Channel Functions --
function SendToAllInChannel(sSender, sRawOutput)
if(sSender) then
local _;
if(sRawOutput == nil) then
sRawOutput = sSender;
_, _, sSender = string.find(sRawOutput, "^%<(.-)%>");
else
sRawOutput = "<"..sSender.."> "..sRawOutput;
end
for nick, val in pairs(tChannelUsers) do
if(nick ~= sSender) then
Core.SendToNick(nick,sBotName,sRawOutput);
end
end
end
end


Updated with path fix, should work ;p
Title: Re: Jumble
Post by: Madman on 17 April, 2009, 07:40:33
Well.. i'm guessing you have less life then me, so are you up for the task? ;p
I'm busy with life and T2G, so it will take me alot of time to fix it....
Title: Re: Jumble
Post by: Madman on 18 April, 2009, 11:42:59
I based that on that, you are the one that answer most request of us two.
No I don't live in Se, I just happend to be born and grow up here.