<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Master Baboon &#187; server-side</title>
	<atom:link href="http://www.masterbaboon.com/tag/server-side/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.masterbaboon.com</link>
	<description>The sea of the simulation</description>
	<lastBuildDate>Mon, 14 Nov 2011 08:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Guess 2/3 game: sending data from Flash to a remote database</title>
		<link>http://www.masterbaboon.com/2009/02/guess-23-game-sending-data-from-flash-to-a-remote-database/</link>
		<comments>http://www.masterbaboon.com/2009/02/guess-23-game-sending-data-from-flash-to-a-remote-database/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 20:56:37 +0000</pubDate>
		<dc:creator>pietro</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[server-side]]></category>

		<guid isPermaLink="false">http://www.masterbaboon.com/?p=146</guid>
		<description><![CDATA[I wrote a simple game to learn how to store on and retrieve data from a server-side database. The rules are simple: you have to guess 2/3 of the average of the guesses of all the players. So, for example, if you think that the other players guessed on average 100, your guess should be [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a simple game to learn how to store on and retrieve data from a server-side database. The rules are simple: you have to guess 2/3 of the average of the guesses of all the players. So, for example, if you think that the other players guessed on average 100, your guess should be 67. The 2/3 rule is there to avoid the trivial strategy of  submitting the same number over and over again. According to game theory, the game has a Nash equilibrium at 0, which in this case does not correspond to a rational strategy. Wikipedia has <a href="http://en.wikipedia.org/wiki/Guess_2/3_of_the_average" target="_blank">some information and links about the game</a>. Give it a try:</p>

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_guess23game_1383234307"
			class="flashmovie"
			width="550"
			height="400">
	<param name="movie" value="/flashes/guess23game.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="/flashes/guess23game.swf"
			name="fm_guess23game_1383234307"
			width="550"
			height="400">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p>The score you see after submitting is the distance from your guess to the true 2/3 of the average. Since you get some information about your and others&#8217; score, it should be possible to find a strategy to win in a small number of trials&#8230; Let me know if you find one!</p>
<h4>Communicating with the server</h4>
<p>To implement the game, I created a MySQL table in a database on the server that is used to store the players&#8217; name and guess. The database is accessed by calling a server-side PHP script from the game Actionscript client.</p>
<p>The PHP script gets the player&#8217;s information and stores it in the database; then it computes the 2/3 of the average of all entries, and returns the score and rank of the current player, along with the top scores in the database. The script receives the data as a HTTP request (the <em>?var1=value1&amp;var2=value2 </em>thing you see in many dynamical web pages), and returns an XML fragment.</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">\n<span style="color: #0000ff;">&quot;;<br />
// top scores<br />
while ((<span style="color: #006699; font-weight: bold;">$row</span> = mysql_fetch_array(<span style="color: #006699; font-weight: bold;">$hit</span>)) &amp;amp;&amp;amp; (<span style="color: #006699; font-weight: bold;">$currentrank</span>==0 || <span style="color: #006699; font-weight: bold;">$rank</span>&amp;lt;=<span style="color: #006699; font-weight: bold;">$ntop</span>)) {<br />
&nbsp; if (<span style="color: #006699; font-weight: bold;">$rank</span>&amp;lt;=<span style="color: #006699; font-weight: bold;">$ntop</span>) {<br />
&nbsp; &nbsp; print &quot;</span>\n<span style="color: #0000ff;">&quot;;<br />
&nbsp; &nbsp; print &quot;</span>\t<span style="color: #009900;">&#123;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span>\n<span style="color: #0000ff;">&quot;;<br />
&nbsp; &nbsp; print &quot;</span>\t<span style="color: #009900;">&#123;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'diff'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span>\n<span style="color: #0000ff;">&quot;;<br />
&nbsp; &nbsp; print &quot;</span>\n<span style="color: #0000ff;">&quot;;<br />
&nbsp; }<br />
&nbsp; if (<span style="color: #006699; font-weight: bold;">$row</span>['time'] == <span style="color: #006699; font-weight: bold;">$time</span>) {<br />
&nbsp; &nbsp; <span style="color: #006699; font-weight: bold;">$currentrank</span> = <span style="color: #006699; font-weight: bold;">$rank</span>;<br />
&nbsp; &nbsp; <span style="color: #006699; font-weight: bold;">$currentscore</span> = <span style="color: #006699; font-weight: bold;">$row</span>['diff'];<br />
&nbsp; }<br />
&nbsp; <span style="color: #006699; font-weight: bold;">$rank</span> = <span style="color: #006699; font-weight: bold;">$rank</span> + 1;<br />
}<br />
<br />
// current score<br />
print &quot;</span>\n\t<span style="color: #009900;">&#123;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#125;</span>\n\t<span style="color: #009900;">&#123;</span><span style="color: #000088;">$currentscore</span><span style="color: #009900;">&#125;</span>\n<span style="color: #0000ff;">&quot;;<br />
print &quot;</span><br />
<br />
\n<span style="color: #0000ff;">&quot;;<br />
<br />
mysql_close();<br />
<br />
?&amp;gt;</span></div></td></tr></tbody></table></div>
</pre>
<p>Calling the script from AS3 is relativelty easy. The remote call is done by a URLLoader object; the actual request is stored in a URLRequest object, and the request variables are wrapped in a URLVariables instance. I had an annoying problem with testing the code: for some reason, Flash always caches the requests. I looked around and it seems that the only workaround is to add a dummy variable to the request with a value that changes constantly, e.g., the current time.</p>
<p>This is the code for the communication with the server:</p>
<pre>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public function submitGuess(name:String, guess:uint, ntop:int):void {<br />
&nbsp; &nbsp; var request:URLRequest = new URLRequest(&quot;http://www.masterbaboon.com/php/guess23/guess23submit.php&quot;);<br />
&nbsp; &nbsp; var httpHeader : URLRequestHeader = new URLRequestHeader(&quot;pragma&quot;, &quot;no-cache&quot;);<br />
&nbsp; &nbsp; request.requestHeaders.push(httpHeader);<br />
<br />
&nbsp; &nbsp; // request variables: name of player, guess, number of top scores to return<br />
&nbsp; &nbsp; var vars:URLVariables = new URLVariables();<br />
&nbsp; &nbsp; vars.name = name;<br />
&nbsp; &nbsp; vars.guess = guess;<br />
&nbsp; &nbsp; vars.ntop = ntop;<br />
&nbsp; &nbsp; // this is to trick AS3 into not caching the URL request<br />
&nbsp; &nbsp; vars.nocache = Number(new Date().getTime());<br />
<br />
&nbsp; &nbsp; request.method = URLRequestMethod.GET;<br />
&nbsp; &nbsp; request.data = vars;<br />
<br />
&nbsp; &nbsp; // send to server and call loadingComplete when complete<br />
&nbsp; &nbsp; var loader:URLLoader = new URLLoader();<br />
<br />
&nbsp; &nbsp; // the results of the request is not returned immediately, but<br />
&nbsp; &nbsp; // we can monitor the COMPLETE event dispatched by the loader<br />
&nbsp; &nbsp; loader.addEventListener(Event.COMPLETE, loadingComplete);<br />
&nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; loader.load(request);<br />
&nbsp; &nbsp; } catch (e:Error) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace(&quot;An error has occurred while communicating with the server.&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace(e);<br />
&nbsp; &nbsp; }<br />
}<br />
<br />
public function loadingComplete(e:Event):void {<br />
&nbsp; &nbsp; // this is how to access the data returned from the request<br />
&nbsp; &nbsp; data = XML(e.target.data);<br />
&nbsp; &nbsp; // switch frame<br />
&nbsp; &nbsp; gotoAndStop(&quot;highScoreFrame&quot;);<br />
}</div></td></tr></tbody></table></div>
</pre>
<p>You&#8217;re welcome to <a href="http://www.masterbaboon.com/flashes/guess23.zip">download the whole code</a>.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-5929969675637192";
google_ad_slot = "7572213318";
google_ad_width = 468;
google_ad_height = 15;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.masterbaboon.com/2009/02/guess-23-game-sending-data-from-flash-to-a-remote-database/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

