Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Implement game logic to your web application

Gamify class can help you to implement game logic to your PHP aplications or websites. This class can create needed tables for storing information on most popular database platforms using PDO. It also helps you to add users, define levels and achievements and generate user statistics and tops.

Then all you have to do is to bind gamify class functions to actions on your website, which would provide experience or achievement actions for your players.

Contents

Download

Example codes

<?php
include("./gamify.php");
//create class instance with database connection
$g = new gamify("localhost", "root", "password", "gamify");

//output pdo errors
$g->debug();

//create sql tables (only for the first time)
$g->install();

//create new user
//providing username
$g->create_user("ar2rsawseen");

//create new level
//providing level name and experience needed
$g->create_level("First level", 100);

//create new achievement for click every 24 hours
//providing achievement name, actions needed, 
//time period in seconds between actions, 
//optional achievement badge and description
$g->create_achievement("Clicker", 100, 60*60*24, "./cbadge.png", "Do 100 clicks");

echo "<pre>";
//add experience to created user and output new gained level
//by providing username and amount of experience
print_r($g->add_experience("ar2rsawseen", 100));

//automatically create user if it doesn't exist and add experience
//by providing username and amount of experience
print_r($g->add_experience("ar2rs", 50));

//output user information and errors if any
//output information about users
print_r($g->get_user("ar2rsawseen"));
print_r($g->get_user("ar2rs"));
//output errors
print_r($g->get_errors());
echo "</pre>";
?>

Examples in action

Example scripts provided with package in action:

Method list

Constructor

Back to method list

Method namenew gamify($host, $user, $pass, $db, $type = "mysql", $pref = "gamify_")
DescriptionCreate instance of class using provided database connection and settings
Input parameters

string $host - database host

string $user - username for database

string $pass - password for database

string $db - database name

string $type - database type/platform

string $pref - prefix for generated tables

Example input
new gamify("localhost", "root", "password", "game", "mysql", "gamify");

Generate needed tables

Back to method list

Method nameinstall()
DescriptionExecutes SQL which generates needed tables

Debug mode

Back to method list

Method namedebug()
DescriptionSets PDO::ERRMODE_WARNING, which outputs errors and warnings

Get errors

Back to method list

Method nameget_errors()
DescriptionReturns array containing errors
Example output
Array
(
	[0] => Can't delete default level. It can only be edited
)

Create new user

Back to method list

Method namecreate_user($username)
DescriptionCreate new user for game
Input parameters

string $username - username to identify user

Example inputcreate_user("ar2rsawseen")

Get information about user

Back to method list

Method nameget_user($username)
DescriptionReturns users level, amount of experience and information about all user's achievements, achievements with status completed are completed, with status active - user is trying to complete them
Input parameters

string $username - username by which to identify user

Example inputget_user("ar2rsawseen")
Example output
Array
(
    [ID] => 2
    [username] => ar2rsawseen
    [experience] => 256
    [level] => Second level
    [achievements] => Array
        (
            [0] => Array
                (
                    [achievement_name] => 1 hundret
                    [badge_src] => ./badge.png
                    [amount_got] => 10
                    [amount_needed] => 10
                    [time] => 1310562182
                    [status] => completed
                )

            [1] => Array
                (
                    [achievement_name] => Small clicker
                    [badge_src] => ./sm.jpg
                    [amount_got] => 10
                    [amount_needed] => 1
                    [time] => 1310562274
                    [status] => completed
                )

            [2] => Array
                (
                    [achievement_name] => Mega clicker
                    [badge_src] => ./mega.jpg
                    [amount_got] => 1
                    [amount_needed] => 1000
                    [time] => 1310846733
                    [status] => active
                )
        )
)

Get list of users

Back to method list

Method nameget_users($ord = "", $desc = false, $limit = 0)
DescriptionReturns array with users and their amount of experience and level
Input parameters

string $ord - how to order users, accepts two strings "username" or "experience"

bool $desc - should users appear in reverse (descending) order (default: ascending order)

int $limit - how many users to return (default: all users)

Example inputget_users("experience", true, 2)
Example output
Array
(
    [0] => Array
        (
            [ID] => 2
            [username] => ar2rsawseen
            [experience] => 256
            [level] => Second level
        )

    [1] => Array
        (
            [ID] => 4
            [username] => ar2rs
            [experience] => 100
            [level] => First level
        )

)

Edit user information

Back to method list

Method nameedit_user($id, $username = "", $experience = "", $level = "")
DescriptionEdit user information by ID
Input parameters

int $id - internal user identifier, can be retrieved using get_user method

string $username - new username, if empty current username is used

int $experience - new amount of experience, if empty current value is used

int $level - internal identifier of level, can be retrieved using get_levels method, if empty current value is used

Delete user

Back to method list

Method namedelete_user($username)
DescriptionDeletes user and all statistics
Input parameters

int $id - internal user identifier, can be retrieved using get_user method

Create new level

Back to method list

Method namecreate_level($name, $exp)
DescriptionCreates new level. By default there is already one level created automatically for users with 0 experience
Input parameters

string $name - level name

int $exp - experience needed to level up

Example inputcreate_level("First level", 100)

Get information about level

Back to method list

Method nameget_level($id)
DescriptionReturns level name and amount of experience needed to level up
Input parameters

int $id - internal identifier of level, can be retrieved using get_levels method

Example inputget_level(2)
Example output
Array
(
    [ID] => 2
    [level_name] => Starter
    [experience_needed] => 50
)

Get list of levels

Back to method list

Method nameget_levels($ord = "", $desc = false, $limit = 0)
DescriptionReturns array with level names and amount of experience needed to level up
Input parameters

string $ord - how to order levels, accepts two string "level_name" or "experience_needed"

bool $desc - should levels appear in reverse (descending) order (default: ascending order)

int $limit - how many levels to return (default: all levels)

Example inputget_levels("experience_needed", true, 1)
Example output
Array
(
    [0] => Array
        (
            [ID] => 7
            [level_name] => Fifth level
            [experience_needed] => 1000
        )
)

Edit level information

Back to method list

Method nameedit_level($id, $name = "", $experience = "")
DescriptionEdit level information by ID
Input parameters

int $id - internal level identifier, can be retrieved using get_levels method

string $name - new level name, if empty current level name is used

int $experience - new amount of experience needed to level up, if empty current value is used

Delete level

Back to method list

Method namedelete_level($id)
DescriptionDeletes level. Default level with ID = 1 can not be deleted, only edited. All users who had deleted level will revert back to default level
Input parameters

int $id - internal level identifier, can be retrieved using get_levels method

Create new achievement

Back to method list

Method namecreate_achievement($name, $amount, $period = 0, $badge = "")
DescriptionCreates new achievement
Input parameters

string $name - achievement name

int $amount - amount of actions to perform to get achievement

int $period - time in second needed between two action (default: no time limit)

string $badge - optional image as badge for achievement

string $description - optional description of achievement

Example inputcreate_achievement("Clicker", 100, 60*60*24, "./clicker_badge.png", "Do 100 clicks on a button")

Get information about achievement

Back to method list

Method nameget_achievement($id)
DescriptionReturns achievement name, amount of actions needed, time period between actions, badge, description and status
Input parameters

int $id - internal identifier of achievement, can be retrieved using get_achievements method

Example inputget_achievement(1)
Example output
Array
(
    [ID] => 1
    [achievement_name] => 1 hundret
    [badge_src] => ./badge.png
    [description] => 
    [amount_needed] => 100
    [time_period] => 10
    [status] => active
)

Get list of achievements

Back to method list

Method nameget_achievements($ord = "", $desc = false, $limit = 0)
DescriptionReturns array with achievement name, amount of actions needed, time period between actions, badge, description and status
Input parameters

string $ord - how to order achievements, accepts two strings "achievement_name" or "amount_needed"

bool $desc - should achievements appear in reverse (descending) order (default: ascending order)

int $limit - how many achievements to return (default: all achievements)

Example inputget_achievements("achievement_name", false, 1)
Example output
Array
(
    [0] => Array
        (
            [ID] => 1
            [achievement_name] => 1 hundret
            [badge_src] => ./badge.png
            [description] => 
            [amount_needed] => 10
            [time_period] => 10
            [status] => active
        )
)

Edit achievement information

Back to method list

Method nameedit_achievement($id, $name = "", $amount = "", $period = "", $badge = "", $description = "", $status = "active")
DescriptionEdit achievement information by ID
Input parameters

int $id - internal achievement identifier, can be retrieved using get_achievements method

string $name - achievement name, if empty current achievement name is used

int $amount - amount of actions to perform to get achievement, if empty current value is used

int $period - time in second needed between two action, if empty current value is used

string $badge - optional image as badge for achievement, if empty current value is used

string $description - optional description of achievement, if empty current value is used

Delete achievement

Back to method list

Method namedelete_achievement($id)
DescriptionDeletes achievement
Input parameters

int $id - internal achievement identifier, can be retrieved using get_achievements method

Disable achievement

Back to method list

Method namedisable_achievement($id)
DescriptionDisables achievement, so none of actions will count
Input parameters

int $id - internal achievement identifier, can be retrieved using get_achievements method

Enable achievement

Back to method list

Method nameenable_achievement($id)
DescriptionEnable achievement, so actions towards achievement will count. Achievements are enabled by default
Input parameters

int $id - internal achievement identifier, can be retrieved using get_achievements method

Add experience to user

Back to method list

Method nameadd_experience($username, $exp)
DescriptionAdd experience to user for achieving new levels. If user achieves new level, information about new level is returned, same as from get_level method, otherwise returns false. If username doesn't exist, user will be automatically created
Input parameters

string $username - username to identify user

int $exp - amount of experience to add to user

Example inputadd_experience("ar2rsawseen", 100)

Perform action for achievement

Back to method list

Method nameaction($username, $achievement, $amount = 1)
DescriptionPerform an action towards achievement. If needed amount of actions for user are collected, then information about gained achievement is returned, same as from get_achievement method, otherwise returns false. If username doesn't exist, user will be automatically created
Input parameters

string $username - username to identify user

int $achievement - internal achievement identifier

int $amount - amount of actions to add to user

Example inputaction("ar2rsawseen", 1)

Possible error messages

List of all errors and meanings

Error textMeaningSolution
There are no user with username %username%You tried to get information about user that doesn't existCreate user using create_user method
There are no usersYou haven't created any userCreate user using create_user method
User with ID %id%' does not existYou are trying to edit user, that doesn't existCreate user using create_user method
There are no level with ID %id%You are trying to get or edit level, that doesn't existCreate level using create_level method
There are no levelsYou haven't created any levelCreate level using create_level method
Can't delete default level. It can only be editedYou are trying to delete level with ID 1You may only edit this level using edit_level method to suit your needs, but note that changing it's needed experience amount, might cause problems
There are no achievements with ID %id%You are trying to get, edit or perform action on achievement, that doesn't existCreate achievement using create_achievement method
There are no achievementsYou haven't created any achievementsCreate achievement using create_achievement method

Latest changes

None for now

Awards

Gamify class was nominated to August Innovation Award and got 3rd place, thank you everyone for your support.


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus