Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Saturday, March 20, 2010

How to integrate recent news stories into interactive fiction

I'm writing an interactive novel on Facebook, which besides integrating the reader and the reader's Facebook friends into the novel, incorporating the current time, day, and location of the reader, and even using a Google map of the reader's location as the setting, integrates real-world news headlines. Here's how:


1. Google news. Google news makes it easy to search for headlines on a specific topic. For example, type the url http://news.google.com/news?q=j.d. salinger
(it works even with the space between words!)


As you can see, this generates a search listing of the latest news articles on the subject, in descending chronological order. If you want, you can even refine the search by adding other parameters to the url (like news source, region, time period, etc).


2. Use RSS to parse (extract) the title (or other parts of the news story, for that matter).


Quite simply: http://news.google.com/news?q=j.d. salinger&output=rss

With this type of formatting, you can use PHP to pick out the bits of the article you want. In my case, I wanted only the titles. This part of the programming is very standard. Many websites aggregrate content using customized RSS Feeds.


3. Allow the user/reader to pick the topic


This involves some pretty straightforward programming: store a user-inputed string as a variable and put that variable where it needs to go:


$newsstart = "http://news.google.com/news?q=";

$news = $userinput;

$newsend = "&output=rss";

$newsall = $newsstart . $news . $newsend;


4. Do some fancy PHP XML display stuff, and that's it. You get something like:



Sally Salamander looked down at the soggy newspaper on the park bench. It was a [name of newspaper dynamically inserted here!], the [date of the article here!!] edition, yellowed from the sun. The headline said, simply [headline here!!!]

Making a User-Targeted Open Source Videogame Map

Step 1: Find a way to dynamically display a map using url parsing.
http://mapki.com/wiki/Google_Map_Parameters:

<a href="http://maps.google.com/?q=Los%20Angeles">http://maps.google.com?q=Los Angeles</a>

Step 2: Integrate it with your application
http://wiki.developers.facebook.com/index.php/Fb:iframe


<fb:iframe src="http://maps.google.com/?q=Los%20Angeles&output=embed" smartsize="true"></fb:iframe>

Step 3: Use a php variable to customize it for each player:
<?php echo $city; ?>

End result:

View Larger Map

Wednesday, January 13, 2010

PHP Code snippet:

The below code does a few interesting things:
  • Checks that the story is part of the 1st ten pages (<10)
  • Determines whether the male or female version of the chapter should be displayed
  • replaces placeholder character names with user-generated Facebook friends
  • allows the story to be displayed with PHP code (i.e., allowing the facebook friend names to show up)

// evaluates the story as a mixture of php and html
if ($page_id < 10 && $user_gender =='f')
{
$thestory = $femstory;
}
else
{
$thestory = $story;
}
$thestory = str_replace($plaintext, $markup, $thestory);
eval("\$thestory=$thestory;");
echo $thestory;

PHP FBML Code Snippet: User-generated details

The below php code retrieves user data from their Facebook profile (assigning default values if the user hasn't provided the info, & spits in out in a nice, customized story.

$user_details = $facebook->api_client->users_getInfo($user_id, 'current_location, education_history, pic');
if (empty($user_details[0]['education_history'][0]['name'])){
$university = "Yale";
$graduation = "2005";
}
else
{
$university = $user_details[0]['education_history'][0]['name'];
$graduation = $user_details[0]['education_history'][0]['year'];
}
if (empty($user_details[0]['current_location']['city'])){
$city = "Poughkeepsie";
$state = "New York";
}
else
{
$city = $user_details[0]['current_location']['city'];
$state = $user_details[0]['current_location']['state'];
}
echo "
Brendan attended ";
echo $city;
echo " City College, and worked at the First Bank of ";
echo $state;

Wednesday, December 30, 2009

How to write a novel that doesn't fall apart

When I originally began working on the idea for an interactive novel, I always envisioned that, at some point, I'd have to do a bunch of gruntwork to take my novel and mark it up with a bunch of code, to do a painstaking page-by-page find/replace

It occurred to me that the program could do this for me, on the fly, whenever anyone requests [that's programmer speak, I think] a page of the novel. All I'd ever see would be my original novel. All the reader would see? The custom-built canary. Here's how it works (using PHP):

$plaintext = array("Ana", "Brendan", "Charlie", "Daniel", "Elle");
$markup = array("$anahere", "$brendanhere", "$charliehere", "$danielhere", "$ellehere");
$story = str_replace($plaintext, $markup, $story);

Getting the SQL to communicate with the PHP and the FBML makes it a bit more complex, but that's the basic idea.

Tuesday, December 22, 2009

Almost Fully Functional


My new interactive novel is almost functional, and here's a sneak preview

User Mania = X + (100-X)/2, where X <100


Last night I figured out what, for me, was a bit of challenging math. Here's the story. In my interactive novel, each reader has stats. Each choice readers make on a page makes the stats go up or down. One of the stats is mania/depression. When the novel starts, you're at 50%/50%. I had to figure out a way to have the stat go up or down without reaching 100. One way to do this would be with two variables, $mania & $depression, where each could be incremented based on the user's choice, then averaged to find the percentage. For example:

$mania = 50
$depression = 50

User's choice:
$depression = $depression + 10

Percentage for new depression = ($depression / ($depression+$mania))*100

That, however, requires two variables, and it will likely never give very extreme percentages, which is always more fun. So I came up with these formulae to use one variable that'll never reach 0 or 100:

to increase: $depression = $depression + (100-$depression)/2
to decrease: $depression = $depression - $depression/2