RPG - storing player data RPG - storing player data php php

RPG - storing player data


Choosing the right method to use

suppose you have this array:

$player_quest_status = array("quest1" => 100,                             "quest2" => 90,                             "quest3" => 50); 

you can have 2 options on storing these array into the database.

  1. using PHP json_encode():

    $player_quest_status_json = json_encode($player_quest_status);//then you can store $player_quest_status_json variable using INSERT.UPDATE 

    statements //array when encoded: {"quest1":100,"quest2":100,"quest3":100}

    use json_decode to convert it back to array after retrieving the value from the database.

  2. using PHP serialize():

    $player_quest_status_json = serialize($player_quest_status);//array when encoded: a:3{s:6:"quest1";i:100;s:6:"quest2";i:100;s:6:"quest3";i:100;}

for more information which function you would like to use:
Preferred method to store PHP arrays (json_encode vs serialize)

though i still recommend json as it is more scalable.


Structuring your Arrays

$player_quest_status = array("player_id" => 1,                             "level"     => 1,                             "quests"    => array( 1/*quest_id*/ => array("percent_completed" => 100,                                                                           "quest_title"       => "the first quest"),                                                   2/*quest_id*/ => array("percent_completed" => 80,                                                                           "quest_title"       => "the second quest"),                                                   3/*quest_id*/ => array("percent_completed" => 50,                                                                           "quest_title"       => "the 3rd quest")                                                  )                             );$player_npc_status = array("npc"  => array( 1 => array("name"  => "lawrence",                                                       "tasks" => array( 1 => array("task_title"   => "title 1",                                                                                    "is_completed" => 1),                                                                         2 => array("task_title"   => "title 2",                                                                                    "is_completed" => 1),                                                                         3 => array("task_title"   => "title 3",                                                                                    "is_completed" => 0))                                                      ),                                            2 => array("name"  => "viscocent",                                                       "tasks" => array( 1 => array("task_title"   => "title 4",                                                                                    "is_completed" => 1),                                                                         2 => array("task_title"   => "title 5",                                                                                    "is_completed" => 2),                                                                         3 => array("task_title"   => "title 6",                                                                                    "is_completed" => 0))                                                      ),                                         )                           );


I suppose independently from the Quest/Task tree stored, it is sufficient to store the progress of the game simply store the currentLevel, currentQuest and the number of tasks done for each NPC of the quest as you've mentioned. Depending on how you have stored it, I would have an array or object that stores the current quest. For example, for user 1, it would look something like

var currLevel = 2;var currQuest = 2;var currNpc   =2var subquests = [3,1,3]; //or {"NPC1":3,"NP2":1,"NPC3":3}//progress tasksubquests[currNpc]= subquests[currNpc] ? subquests[currNpc]+1 : 1//next questcurrQuest++subquests=[]

So depending on your workflow, you can produce a JSON string from an array or object by doing

var str=JSON.stringify(subquests);  //javascript$str=json_encode($array);           //PHP

Which you can store in the database. You can then restore the value either in javascript or php (depending on your workflow) with

var subquests=JSON.parse(json_string);  //javascript$subquests=json_decode($json_string);   //PHP

The database would look something like this sqlfiddle I supposehttp://sqlfiddle.com/#!2/8d436/1/0