demonstration script

Post Reply
Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

demonstration script

Post by Nyyrazzilyss » Mon Dec 12, 2016 9:21 pm

There was a request on gitter in regards to a demonstration script that requires token ring functionality with a known event ordering in sysInterProfileEvent (dev mudlet). Include the below in 3 profiles running at the same time, then type 'lua start()' on each to begin.

(edit) Rewritten for latest commit, and echoes added to track event flow
(edit) tempTimer restored
Code: [show] | [select all] lua
players = players or {}

-- This function is called to start the script running
function start()
	echo("Starting script...\n")

	registerAnonymousEventHandler("sysInterProfileEvent", "interProfileEvent")

	players[getProfileName()] = true

	sendHello()
end

function interProfileEvent(a,b, eventString)
	local event=yajl.to_value(eventString)

	local action=event.action
	local sender=event.sender

	local fname="receive" .. event.action

	echo("Received: " .. event.action .. " From: " .. event.sender .. "\n")

	if _G[fname] ~= nil then	
		_G[fname](event)
	else
		echo("invalid functionname: ".. fname .. "\n")
	end
end

function sendHello()
	local event={["action"]="Hello", 
					 ["sender"]=getProfileName()}


	echo("Sending: " .. event.action .. " From: " .. event.sender .. "\n")

	sendGlobalEvent( yajl.to_string(event) ) 
end

function sendHi(event)
	local event={["action"]="Hi", 
					 ["sender"]=getProfileName()}

	echo("Sending: " .. event.action .. " From: " .. event.sender .. "\n")

	sendGlobalEvent( yajl.to_string(event) ) 
end

function receiveHello(event)
	if event.sender ~= getProfileName() then
		echo("A new player has just joined or reconnected: " .. event.sender .. "\n")

		players[event.sender] = true

		-- send hi response
		tempTimer(0, [[sendHi()]])

		echo("Player list:\n")

		display(players)
	else
		echo("Discarding Hello from self\n")

		if table.size(players) == 1 then
			echo("Currently single player only.\n")
		end
	end
end

function receiveHi(event)
	players[event.sender] = true

	display(players)

	echo("Total number of players: " .. table.size(players) .. "\n")

	-- initiate desired test if 3 players joined

	if table.size(players) == 3 and not testrun then
		testrun=true

		if failedtest then
			echo("I already failed to be first before I even started!\n")
		else
			echo("3 players have now joined. Initiating demonstration.\n")
			echo("Only one (the first to attempt) profile will be able to perform the next action.\n")

			tempTimer(0, [[sendTest()]])
		end
	end
end

function sendTest(event)
	local event={["action"]="Test", 
					 ["sender"]=getProfileName()}

	echo("Sending: " .. event.action .. " From: " .. event.sender .. "\n")

	sendGlobalEvent( yajl.to_string(event) ) 
end

function receiveTest(event)
	if event.sender == getProfileName() and not failedtest then
		echo("I can perform the action!\n")
		success=true
	else
		if not success and not failedtest then
			echo("Someone else is performing this event before me. I failed.\n")
			failedtest=true
		end
	end
end

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest