Behavior, Content, Money – 3 Things you should never give away for free!!!

BCmoney MobileTV

Moving a command-line PHP Rock Paper Scissors game to the Browser

Posted by bcmoney on December 1, 2016 in Gaming, PHP with No Comments


No Gravatar
Rock-paper-scissors chart

Rock-paper-scissors chart (Photo credit: Wikipedia)

In the first part we saw an example for creating a PHP Rock Paper Scissors game that waits for user input via the command-line and then evaluates the output of the game. Next we see an example for porting our basic command-line PHP Rock Paper Scissors game to the browser to reach a broader audience, since well, not that many people play command-line games these days.


Benefits of the Browser

With a Browser we have a few more useful tools at our disposal, as we can start to keep track of games played (wins/losses) over the lifetime of a particular user session. We also have the potential to open up the game to multiplayer more easily, as a player could play against others if we added a login mechanism (while a proper multi-user session persistence and secure login/logout mechanism is outside the scope of this example, a simple demo will be presented for enabling “play against a friend” mode with unique Game IDs).


Sessions

Sessions in PHP combine the use of short-term server-side persistence which lasts the lifetime of the page our code sits on, in combination with Cookies to extend that lifetime beyond an accidental page reload or tab close so that a session can be resumed. When you use a Session you’ll get a session ID of the name:

PHPSESSID

With a long unique alphanumeric identifier, such as:

49cdf231ddf8502e307c437ef98fd632

The Cookie representing this name/value pair looks roughly as follows (as viewed in the CookieManager+ plugin for FireFox):
In raw text that Cookie would look simply like this:

PHPSESSID=hfdvcdrj8hr6p6blkg9tt00av6; path=/; domain=gtk.php.net

And the value will be passed with each request from the client to the server along with the regular HTTP Headers (User-Agent, etc), in order to identify our device/browser/OS combination. It looks like this:

Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie PHPSESSID=hfdvcdrj8hr6p6blkg9tt00av6; LAST_LANG=en; COUNTRY=NA%2C156.34.241.222
Host gtk.php.net
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0


Multiplayer

Multiplayer in the traditional sense would require a scalable, dedicated server for connecting large pools of users or a sophisticated P2P routing algorithm. Ideally, these days, a P2P networking solution would be used to enable direct communication between the parties playing a game together.

 

Read the rest of this entry »

BC$ = Behavior, Content, Money

The goal of the BC$ project is to raise awareness and make changes with respect to the three pillars of information freedom - Behavior (pursuit of interests and passions), Content (sharing/exchanging ideas in various formats), Money (fairness and accessibility) - bringing to light the fact that:

1. We regularly hand over our browser histories, search histories and daily online activities to companies that want our money, or, to benefit from our use of their services with lucrative ad deals or sales of personal information.

2. We create and/or consume interesting content on their services, but we aren't adequately rewarded for our creative efforts or loyalty.

3. We pay money to be connected online (and possibly also over mobile), yet we lose both time and money by allowing companies to market to us with unsolicited advertisements, irrelevant product offers and unfairly structured service pricing plans.

  • Calendar

    • December 2016
      M T W T F S S
       1234
      567891011
      12131415161718
      19202122232425
      262728293031  
  • Archives