Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

The VirtualTrader Class

cuonic edited this page Jul 16, 2012 · 1 revision

The VirtualTrader class is at the heart of the entire system. It handles all purchasing / selling of shares, live price fetching, logging, scoring... Everything (except user authentication, handled by the Auth Class)

It is initiated by including the class and creating a new instance :

<?php

include("virtualtrader.class.php");
$virtualtrader = new VirtualTrader;

?>

On initiation, a database link is created via MySQLi, and the class can then be used using the newly created instance with :

$virtualtrader->FunctionName($functionparams);

The different functions are as follows :

GetStockInfo :

Description :

  • This function is used to fetch stock information from the Google Finance API, based on stock code (Ex: GOOG)

Parameters :

  • $stockcode (string)

Returns :

  • $stockinfo (array) : If stock exists
  • $return (boolean) : If stock doesn't exist

GetStockInfoDB :

Description :

  • This function is used to fetch stock information from the database, based on stock code (Ex: GOOG)

Parameters :

  • $stockcode (string)

Returns :

  • $stockinfo (array) : If stock exists
  • $return (boolean) : If stock doesn't exist

UpdateStockDB :

Description :

  • This function is used to updated the values for each stock in the database based on the GetStockInfo function. It loops through all rows.

Parameters :

  • $stockcode (string)

Returns :

  • $stockinfo (array) : If stock exists
  • $return (boolean) : If stock doesn't exist

CheckStock :

Description :

  • Checks if stock exists in database

Parameters :

  • $stockcode (string)

Returns :

  • $return (boolean)

BuyShare :

Description :

  • Function that verfies user's capacity to purchase a share, and then procedes with purchase (deducting total price from balance, adding purchased quantity to existing quantity etc...)

Parameters :

  • $stockcode (string)
  • $quantity (int)
  • $username (string)

Returns :

  • $return (boolean)

SellShare :

Description :

  • Function that verfies user's capacity to sell a share, and then procedes with sale (adding total price to balance, deducting purchased quantity from existing quantity etc...)

Parameters :

  • $stockcode (string)
  • $quantity (int)
  • $username (string)

Returns :

  • $return (boolean)

LogActivity :

Description :

  • Logs users activity to database on the site based on parameters provided to the function

Parameters :

  • $username (string)
  • $action (string)
  • $additionalinfo (string) = "none"

Returns :

  • $return (boolean)

ShareQty :

Description :

  • Fetches quantity of shares for a given stock for a given user.

Parameters :

  • $username (string)
  • $stockcode (string)

Returns :

  • $quantity (int)

GetStocks :

Description :

  • Provides an array of all stocks in database including name, stock code, price, difference , difference (%)

Parameters :

  • None

Returns :

  • $data (array) : If more than 0 rows
  • $return (boolean) : If 0 rows

GetUserStocks :

Description :

  • Provides an array of stocks owned by a given user in database including name, stock code, price, difference , difference (%)

Parameters :

  • $username (string)

Returns :

  • $data (array) : If more than 0 rows
  • $return (boolean) : If 0 rows

GetTopUsers :

Description :

  • Returns an array of top 10 users (Highest balance) including username and balance

Parameters :

  • None

Returns :

  • $data (array) : If more than 0 rows
  • $return (boolean) : If 0 rows

GetUserBalance :

Description :

  • Returns the balance of a given username

Parameters :

  • $username (string)

Returns :

  • $balance (double) : If user exists
  • $return (boolean) : If user doesn't exist

Error and success messages are also handled by the class. These exist for the moment in two languages : English and French and can be modified in the inc/lang.php. You can echo these errors out with the following code :

if(isset($virtualtrader->errormsg)) { echo "<span style=\"color: red\">"; foreach ($virtualtrader->errormsg as $emsg) { echo "$emsg<br/>"; } echo "</span><br/>"; }
if(isset($virtualtrader->successmsg)) { echo "<span style=\"color: green\">"; foreach ($virtualtrader->successmsg as $smsg) { echo "$smsg<br/>"; } echo "</span><br/>"; }  

Pages are accessed indirectely via a "proxy" file (index.php). You choose the page with the "page" GET variable (Ex: index.php?page=home). Pages are defined in the index.php and are selected via a switch. >hilst in development the "page" variable is visible, but can be hidden with a nice bit of ReWrite Engine in a .htaccess, such as :

RewriteEngine on  
RewriteBase /  
RewriteRule ^([A-Za-z0-9\-]+)$ /index.php?page=$1 [QSA,L]