🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Untitled

posted in DruinkJournal
Published November 12, 2007
Advertisement
I like my Lua stuff now. I'm working on a client for TA (A talker/MUD client, essentially). And it has cool stuff like HTTP GET support, so I can grab the current weather from rssweather.com:
-- Weather.lua-- Script for /weather command-- Weather formatting and displaying. temp is a numberfunction displayWeather(location, temp, conditions)	-- Ok, we have the temperature and weather, spew it out:	local weatherString = ".me looks out the window at " .. location .. " and feels it's <-c00ff00><-b>" .. temp .. "'C<-0><-cffff00>, <-0><-b>" .. conditions .. " <-c0000ff><-b>O";	for i=1,temp do		weatherString = weatherString .. "_";	end	sendln(weatherString .. "O");end-- httpGet callbackfunction weatherCallback(addr, response, header, content, error)	-- Check for errors	if response ~= 200 then		println("<-cff0000>Failed to get current weather. Error: " .. error);		return;	end	-- Convert content to lower case	local lowerContent = string.lower(content);	-- Loop through the  tags till we find one with the weather string in it	local location, temp, conditions;	local startPos = 1;	local contentLength = string.len(lowerContent);	repeat		-- Find  tag start and end pos		local tagStart = string.find(lowerContent, "", startPos);		if tagStart== nil then			println("<-cff0000>Failed to get current weather. Could not find weather in returned data.");			return;		end		local tagEnd = string.find(lowerContent, "", tagStart+7);		if tagEnd == nil then			println("<-cff0000>Failed to get current weather. Could not find weather in returned data.");			return;		end		startPos = tagEnd+8;		-- Is this the tag we're looking for? (Contains some data after the word Weather		local tag = string.sub(lowerContent, tagStart+7, tagEnd-1);		local weatherPos = string.find(tag, "weather");		if weatherPos ~= nil and string.len(tag)-(weatherPos+7) > 2 then			-- Looks reasonable, grab location (Everything before first comma)			local commaPos = string.find(tag, ",");			if commaPos ~= nil then				location = string.sub(content, tagStart+7, tagStart+7+commaPos-1);			end			-- Trim off everything up to and including "weather :: "			local weatherString = string.sub(content, tagStart+7+weatherPos+10, tagEnd-1);			local weatherStringLwr = string.lower(weatherString);						-- Extract temp			local tempPos = string.find(weatherStringLwr, "c");			if tempPos ~= nil then				temp = tonumber(string.sub(weatherString, 1, tempPos-1));				conditions = string.sub(weatherString, tempPos+2);			end		end	until temp ~= nill;	-- Ok, we have the temperature and weather, spew it out	displayWeather(location, temp, conditions);end-- Command to registerfunction cmdWeather(params)	-- Grab page	local addr = getConfigValue("WeatherURL");	httpGet(addr, "weatherCallback");endregister_command("weather", "cmdWeather");

Yeah, that could probably be neater, but it does the job, and it's a proof of concept. There's also a PHP-style date() function (Well, getTime()), and a getCurrSong() which returns your current Winamp track - which is in a plugin DLL, so I can easily swap in new versions to support stuff like WMP, etc.

Anyway, enough rambling. I'm off to bed.
Previous Entry Untitled
Next Entry Untitled
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement