0 votes
by (120 points)
I want to create a poll on Twine.

So if the poll, for example, asks, What is your favorite city? and the answers are A) Boston, B) New York, C) Chicago. The user would pick one, and after, the user would see which choice is winning at the time the user voted. So the user's choice should be competing against all the people who have played the game and have voted before.

Thus if the majority so far has chosen BOSTON, then the next part of the story would be about BOSTON regardless if the user chose, say, Chicago.

I'm assuming I need to create a database and a .php file, but I don't know how to integrate the .php file in Twine. I've been studying a way to do this for two weeks with no results. PLEASE HELP!

I'm using the latest version of Twine and Harlowe.

Thanks in advance!

1 Answer

0 votes
by (159k points)

comments:
a. PHP is a server side scripting language, this means that you will need to either correctly setup a web-server with PHP installed, or have access to one that has been pre-configured for you. This topic is beyond the scope of this Q/A site.

b. You don't make it clear if you are planing to host the story on the same web-server or If you are planing to allow the end-user to download and play the story on their local machine. If the later then you may need to handle issues like Cross-Origin Resource Sharing, so I would suggest doing the former.

c. You will need to implement a method of uniquely identifying the individual end-users playing your game, this could be as simple as asking them within the game to enter an email address, to as complex as getting them to create an account on your web-site.

d. Harlowe doesn't have a documented Javascript API and the story format has been deliberately designed to restrict access to the internals of it's Javascript engine, because of this it isn't the best choice if you are planing to use Javascript to interact with story variables or the contents of passages.

But to answer you question, I would suggests using the jQuery $.ajax function to communicate between your story and your web-server, using a method similar to how vicgeb did in their Passing Twine variables to PHP question on the Old Forum Archive.

warning: Without further information about the story variables, the structure of your story, and the web-server the following examples need to be generic. Also because I don't know which method you will be using to identify the end-users the following examples will not include any code related to Authentication or Credentials, And finally because I don't have access to a PHP configured web-server the following examples have NOT been tested, (eg. I'm being lazy.)

You would need to implement at least two Javascript functions within your Story Javascript area:

1. A function to send this end-user's data to the relevant PHP file on the web-server.

window.sendOpinionToPoll = function () {
	$.ajax({
		method	: "POST",
		url	: "receive-opinion-ajax.php",
		data	: { /* a copy of the relevant story variables goes here! */ }
	})
		.done(function () {
			/* What should happen if the data was sent successfully. */
			alert("success");
		})
		.fail(function () {
			/* What should happen if the data transmision failed. */
			alert("fail");
		});
}

2. A function to retrieve the current Poll result(s) from the relevant PHP file on the web-server.

window.retrievePollResults = function () {
	$.ajax({
		method	: "GET",
		url	: "retrieve-results-ajax.php",
		data	: { /* If you want the result for a particular question you would indicate that here! */ }
	})
		.done(function (data) {
			/* What should happen if the data was retrieve successfully. */
			alert("success");
		})
		.fail(function () {
			/* What should happen if the retrieval failed. */
			alert("fail");
		});
}

 

You could use script elements like those below within your Passages to execute each of the above Javascript functions, exactly where in your Passages you do that depends greatly on how you've implemented your story.

<script>sendOpinionToPoll();</script>

<script>retrievePollResults();</script>

 

...