Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RPG - storing player data

Given the following tree structure, where each player logged in can have data for current and completed levels, quests per level, NPCs per quest, and multiple tasks per NPC... I'm trying to figure out the best way to store the current and completed data per player.

enter image description here

I asked the question before, albeit with too much detail. It was requested that I generalize the question... So if you'd like more context, see here:

RPG - storing player data for semi-complex tree structure

Some information about my RPG structure:

  • Levels, Quests per Level, and Tasks per NPC are completed in chronological order.
  • NPC dialog and task dialog data is stored in a js object in a npc_dialog_1.js file in the following structure:

    var dialog = {
    quests : {
        questName : {   
            NPCName: {
                TaskName:  {
                     "english" : 
                      [
                          "Hello, here is some dialog",
                      ],//more items per task
                }, //more tasks per NPC
            }, //more NPCs per quest
        }, //more quests options per "quests"
    }, //more options in dialog besides "quests" if I want
    };
    

    I am storing each dialog object in a separate npc_dialog_n.js file per map, that I'm retrieving with requireJS. This will reduce clutter in a single npc_dialog file.

  • NPCs per Quest, however, can be started in any order... this is trying to mimic a GTA-style quest queue, as the game follows a general linear progression, yet for each quest you can talk to multiple NPCs and start, pause, and resume the NPC's tasks at any point.

Since the player can start, pause, and resume tasks per any NPC at a given time, I'm trying to figure out the best way to store the current and completed data per player.

mfirdaus recommended the following DB table with M:M relationship b/t user & npc... yet this would add up quickly as each player can have multiple NPCs per quest, and multiple tasks started per NPC... Any way around this set up?

enter image description here

My current db schemaenter image description here:

Thank you

like image 531
user3871 Avatar asked May 11 '14 23:05

user3871


2 Answers

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))
                                                      ),
                                         )

                           );
like image 58
Viscocent Avatar answered Nov 14 '22 00:11

Viscocent


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   =2
var subquests = [3,1,3]; //or {"NPC1":3,"NP2":1,"NPC3":3}

//progress task
subquests[currNpc]= subquests[currNpc] ? subquests[currNpc]+1 : 1

//next quest
currQuest++
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 suppose http://sqlfiddle.com/#!2/8d436/1/0

like image 1
mfirdaus Avatar answered Nov 14 '22 02:11

mfirdaus