One Armed Bandit v3
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

One Armed Bandit v3

Started by Herodes, 17 September, 2006, 19:05:56

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Herodes

Had fun in the weekend reviving this one ...
--- One-Armed-Bandit v1
--- by Herodes
--- :: 17/9/2006 ::

-- based on a script by FlipDeluxe with the same name as this (10/5/2003)
-- that undergone a modification from Herodes (27/7/2004)

--- Commands
-- !insert 					- start the game
-- !hit 						- pull the arm
-- !oknow					- grab the credits and go
-- !showtop <p|c|pt|cp|cpt>	- to show the top ten (p=plays, c=credits won, pt=playing time, cp=credits per play, cpt=credits per second )

--- Configuration
-- just give the bot a name...
sBot = "Bandit"
---


--- Please don't touch below here,... if you touch don't blame either of the authors .. :)
sPlayer, iCredit, iStart, bPlay ='', 5, 0, nil

function Main()
	frmHub:RegBot(sBot)
end


function ChatArrival( user, data )
	local cmd, args = string.match( string.sub(data, 1, -2) , '%b<>%s*[%!%?%+%-%.%#](%S*)%s*(.*)' )
	if cmd then
		local tCmds = {
			['insert'] = function ( user )
				if sPlayer == user.sName then user:SendData( sBot, "You are playing already, type '!hit' in Main Chat" );return 1; end
				if bPlay then user:SendData( sBot, "My apologies, you must wait until "..sPlayer.." is done with his round.");return 1; end
				iStart, bPlay, sPlayer = os.time(), true, user.sName
				SendToAll( sBot, "Alright!, "..user.sName.." just threw in a buck! Let's see what he can do..." )
				user:SendData( sBot, "Started! Pull with '!hit' in main chat!");return 1;
			end,
			['hit'] = function ( user, args )
				if (sPlayer ~= '') and ( user.sName ~= sPlayer ) then
					user:SendData( sBot, "My apologies, you must wait until "..sPlayer.." is done with his round.")
					return 1;
				end
				if ( iCredit == 0 ) or ( bPlay == nil ) then
					bPlay = nil
					SendToAll(sBot, 'Better insert a coin if you want to play with me,...' );
					return 1;
				end
				iCredit = iCredit - 1
				user:SendData( sBot, 'Remaining Credits: '..iCredit );
				local tDraw, tFigs, tRewards = 	{ math.random(1,8), math.random(1,8), math.random(1,8) },
												{'@', '$', '#', '&', 'X', 'O', '%', '*'},
												{  	{10, "Cool " ..sPlayer.. ", you just won 10 bucks!" },
													{20, "Great!! " ..sPlayer.." 20 bucks for you!" },
													{30, sPlayer..", just won 30 bucks..." },
													{40, "Wow! "..sPlayer.." it must be your lucky day, 40 bucks!!" },
													{50, "Yeah!! " ..sPlayer.." cha-ching 50 bucks!!!" },
													{70, "Woohoo!! " ..sPlayer.." You're on a winning spree!! 70 bucks!!!" },
													{90, "Wow, you won 90 bucks " ..sPlayer.."!!" },
													{100, "Awesome!! " ..sPlayer.." You just hit the jackpot! cha-ching 100 bucks!!!" },
													{1, "Great!! " ..sPlayer.." a buck for you!" } };
				SendToAll(
					"\r\n\t...----------------o-0-o----------------...\r\n"..
					"\t¦        One-Armed-Bandit        ¦\r\n"..
					"\t¦      >>>  "..tFigs[tDraw[1]].."  -  "..tFigs[tDraw[2]].."  -  "..tFigs[tDraw[3]].."  <<<      ¦\r\n"..
					"\t```----------------o-0-o----------------```")

				if ( tDraw[1] == tDraw[2] ) and ( tDraw[2] == tDraw[3] ) then
					iCredit = iCredit + tRewards[tDraw[1]][1]
					SendToAll( tRewards[tDraw[1]][2] );
				elseif ( tDraw[1] == tDraw[2] ) or ( tDraw[2] == tDraw[3] ) or ( tDraw[1] == tDraw[3] ) then
					iCredit = iCredit + 1
					SendToAll( sBot, tRewards[9][2] );
				else
					SendToAll(sBot,"Ahhh, too bad "..sPlayer.." you won nothing,..");
					if ( iCredit == 0 ) then
						SaveLog()
						SendToAll( sBot, "Outta credits, insert coin.");
					end;
				end
				return 1;
			end,
			['showtop'] = function ( user, args )
				local f,e = io.open('one-armed-bandit.dat' )
				if not f then user:SendData( sBot, 'Sorry no plays are logged to this moment,.. no statistics to show!' );return 1; end; f:close()
				local div = function( a, b )
					if a == 0 then a = 1 end;
					if b == 0 then b = 1 end;
					return a/b;
				end
				local tOpt = {
					['p'] = {head = 'Showing the Top Ten of the Most Times Played Users',sortby = 2,func = function( x ) return x..' times'; end },
					['c'] = {head = 'Showing the Top Ten of the Biggest Winners',sortby = 3,func = function( x ) return x..' credits won'; end },
					['pt'] = {head = 'Showing the Top Ten of the Longest Playing Time Users',sortby = 4,func = function( x ) return x..' seconds in total'; end },
					['cp'] = {head = 'Showing the Top Ten of the Most Profitable Per Play Users',sortby = 5,func = function( x, y ) return x..' credits won in '..y..' plays, around '..string.format( '%0.3f', div(x, y) )..' credits won per play'; end },
					['cpt'] = {head = 'Showing the Top Ten of the Most Profitable Per Playtime Users',sortby = 6,func = function( x, y ) return x..' credits won in '..y..' seconds, around '..string.format( '%0.3f', div(x, y) )..'  credits won per seconds'; end },
				}
				local what = string.match( args, '^(%S+)' )
				if what and tOpt[what] then
					local GetList = function( what )
						dofile( 'one-armed-bandit.dat' )
						local tTops = {}
						for i,v in pairs( tPlays ) do
							local c, t = 0,0;
							for val in ipairs(v) do t = t + (v[val][2] - v[val][1]); c = c + v[val][3];end
							table.insert( tTops, { i, #v, c, t } )
						end
						for idx in ipairs( tTops ) do
							table.insert( tTops[idx], 5, div( tTops[idx][3], tTops[idx][2] ) );
							table.insert( tTops[idx], 6, div( tTops[idx][4], tTops[idx][3] ) )
						end
						--- doing the credit per play top ten
						table.sort( tTops, function( a, b ) return a[what] > b[what];end)
						return tTops
					end
					local mode = tOpt[what]
					local m, tRes = '\r\n -:: '..tOpt[what].head..' ::-', GetList( tOpt[what].sortby )
					for t in ipairs( tRes ) do
						if t > 10 then break; end
						local s = nil;
						if tOpt[what].sortby == 5 then s = tOpt[what].func( tRes[t][3], tRes[t][2] )
						elseif tOpt[what].sortby == 6 then s = tOpt[what].func( tRes[t][4], tRes[t][3] )
						end
						m = m..'\r\n\t'..t..'. '..tRes[t][1]..':\t'..(s or tOpt[what].func( tOpt[what].sortby ))
					end
					user:SendData( sBot, m )
				else user:SendData( sBot, "Syntax Error!, use: !showtop <p/c/pt/cp/cpt>");
				end
				return 1;
			end,
			['oknow'] = function( user, args )
				if user.sName ~=sPlayer then user:SendData( sBot, 'Nope,.. you are not playing :P' );return 1; end
				SaveLog(); return 1;
			end,
		}
		if tCmds[cmd] then return tCmds[cmd]( user, args ); end
	end
end

function SaveLog ()
	local f,e = io.open( 'one-armed-bandit.dat' )
	if f then dofile('one-armed-bandit.dat');f:close();end;
	tPlays = tPlays or {}
	tPlays[sPlayer] = tPlays[sPlayer] or {}
	table.insert( tPlays[sPlayer], { iStart, os.time(), iCredit } )
	f,e = io.open( 'one-armed-bandit.dat', 'w+' )
	f:write( '--- Generated by One Armed Bandit\n--- Do not modify!!\n'..Serialize( tPlays, 'tPlays' ) )
	f:close()
	iCredit, sPlayer, bPlay, tPlays = 5, '';
end

function Serialize(tTable, sTableName, sTab)
	sTab = sTab or "";
	local ret = sTab..sTableName.." = {\n"
	for key, value in pairs(tTable) do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
		if (type(value) == "table") then
			ret = ret..Serialize(value, sKey, sTab.."\t")
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
			ret = ret..sTab.."\t"..sKey.." = "..sValue
		end
		ret = ret..",\n"
	end
	return ret..sTab.."}"
end

SMF spam blocked by CleanTalk