Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Randomly generated quests

    Hello,

    I'm learning php + mysql and my motivation is to host a browsergame.
    There is an ready register- and login-system, a character creation wich calculates the attribute points of the class and race of the characters. thats all till now.

    The next thing is to have some fun with quests. There are different types of quests (main quests, daily quest, special quest and so on) so i made two tables for that:
    `quests`
    `quest_types`

    How users do the daylie quests? Thats my problem!

    The should be able to choose between three quest. So first i need three quests or more to let the script randomly choose three out of them.
    Actually there are six daily quests in the table `quests`.

    If we say order by rand() limit 3 every time a user opens the daily quest page, he get three random quests again. thats wrong. he should get three quests once, the he has to finish one of them and the generate three new quests.

    additional, i didnt saved any reward in the `quests` table, because this should be caluclated randomly too, based on the characters level.

    so can you tell me HOW to get three random quests, WHERE to safe them to show them to the user for ever until he finish one of them and WHEN to calculate the reward (gold and xp) to add it to the `characters` table?

    this is so hard for me to do at the moment, pls give me hints.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by esperance View Post
    so can you tell me HOW to get three random quests, WHERE to safe them to show them to the user for ever until he finish one of them and WHEN to calculate the reward (gold and xp) to add it to the `characters` table?
    With cookies or a database. If no cookie is set the user would get the 3 random "quests", which also sets the cookie with that info. If the cookie exists he gets the original quests.

    Alternative to the cookies would be to enter the info into a database.


    --
    Leonard Whistler

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Im still trying to get this quests done, but it seems too hard...

    In this function we proof, if there are entries in the table `character_quests`.

    If no => generate three of them from `quests` and write them in `character_quests` with additional information like reward, characterID, time.

    If yes => display the three quests...

    PHP Code:
            $questID = ;
            
    $questTitle = ;
            
    $questDescription = ;
            
    $characterID = ;
            
    $questMoney getCharacters('levelID') * rand(1,4);
            
    $questExperience getCharacters('levelID') * rand(50,200);
            
    $questTime =  150 rand(1,4);

    function 
    listRandomQuests() {
        global 
    $db$characterID;
        
        if(
    mysqli_query($db"SELECT count(*) FROM character_quests WHERE characterID = $characterID") > 0) {
        
            
    $sql    =   "
                        SELECT
                            questName,
                            questDescription,
                            questGold,
                            questExperience,
                            questTime
                        FROM
                            character_quests
                        WHERE
                            characterID = $characterID
                        ORDER BY
                            questTime ASC
                        "
    ;
            
    $result $db->query($sql);
            
    $rows   = array();
            
            while(
    $row $result->fetch_object()) {
                
    $rows[] = $row;
            }
            
            foreach(
    $rows as $row) {
                echo 
    "<div>";
                echo 
    "<input id='$row->questName' type='radio' name='randomQuest' value='$row->questName' />";
                echo 
    "<label for='$row->questName'>";
                echo 
    "<span class='yellow'>$row->questName</span>";
                echo 
    "<br />";
                echo 
    "$row->questDescription";
                echo 
    "</label>";
                echo 
    "</div>";
            }

        } else {

            
    $sql  "
                    INSERT INTO
                        character_quests(
                            questID,
                            questTitle,
                            questDescription,
                            characterID,
                            questMoney,
                            questExperience,
                            questTime
                        )
                    VALUES
                        (
                            ?, ?, ?, ?, ?, ?, ?
                        )
                    "
    ;
                    
            
    $stmt $db->prepare($sql);
            
    $stmt->bind_param   (
                                
    'issiiii',
                                
    $questID,
                                
    $questTitle,
                                
    $questDescription,
                                
    $characterID,
                                
    $questMoney,
                                
    $questExperience,
                                
    $questTime
                                
    );
            
    $stmt->close();
        
        }

    What i want to know: Is it possible to do like that, or am i going for the wrong way?!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •