Random Quest Rewards

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
espe
Posts: 32
Joined: Sat Jul 14, 2012 12:46 pm

Random Quest Rewards

Post by espe »

Hey ir,

as you forced me to work on my own "engine", i came up with one of my latest problems.

Imagine you play a game, in wich one of the main leveling part is doing quests and there are two types of quests (history based, random daylies). For leveling, the random daylies are more important because you can do them everywhere and in an larger amount. To make it more interesting, you can choose between three quests wich take some time between 2,5 and 10 minutes. Here is an example:

A wild problem appears
10 minutes
4 gold
62 xp

Talk with your dog
7,5 minutes
2 gold
174 xp

Help yo ol grandma
5 minutes
4 gold
33 xp

As a player, i start deciding what quests will be the most worth for me. Do i need more gold or more xp? Is my guild need money for upgrading the xp-house, from where i get more quest-xp later?

As programer, i have problems: http://www.abload.de/img/mewofmkzl.png

Explain the picture pls...
In the header, you can see a placeholder name and the main navigation.
In the left, you can see a sidebar with some player informations (money, name, stats) and an additional navigation.
In the content, you can see the random daily quests. three of them.

What i want...
Display three different quests with different values.

What i have...
Three same quests are showed with random values.
If i click the button, i get the values but AGAIN RANDOM.
On the picture we see 1312 xp and 32 gold, what we will get is random again, but not that amount.

Here is the code...
(gold reward not included yet, $quest_duration and questBegin(xx) are placeholders)

Code: Select all

<?php
/**
 * Questhandling
 */
 
// global quest vars
$quest_duration = 60; //Placeholder
$quest_duration_left = getCharacters('questDuration') - time();
$quest_experience = getCharacters('levelID') * rand(50,200);
$quest_gold = getCharacters('levelID') * rand(1,4);

if(isset($_POST['quest1_accept'])) {
	questBegin(15);
}
if(isset($_POST['quest2_accept'])) {
	questBegin(30);
}
if(isset($_POST['quest3_accept'])) {
	questBegin(45);
}
if(isset($_POST['quest4_accept'])) {
	questBegin(60);
}

function questBegin($var) {
	global $db, $characterID;
    
	if(getCharacters('questDuration') == 0) {
		$quest_duration = time() + $var;
		mysqli_query($db,
                    "UPDATE
                        characters
                    SET
                        questDuration = $quest_duration
                    WHERE
                        characterID = $characterID"
                    );
	}
}

function questDone() {
	global $db, $characterID, $quest_duration_left, $quest_experience, $quest_gold;
	
	if(getCharacters('questDuration') > 0) {
		if($quest_duration_left < 0) {
			mysqli_query(
                        $db,
                        "UPDATE
                            characters
                        SET 
                            experiencePoints = experiencePoints + $quest_experience,
                            questDuration = 0
						WHERE
                            characterID = $characterID"
                        );
		}
	}
}
?>
More images (database for example) http://www.abload.de/gallery.php?key=jBWLYUvf

Can you help me here?
How and where to safe the values from three quests temporarily, how to make it injection safe and how to generate new quests after the countdown?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Random Quest Rewards

Post by Callan S. »

Nice graphics on your screen shot - with those graphics I would have thought you'd know about session values? So you set session values to be used with something like

Code: Select all

<?php
include_once 'connect.php';
session_start();
at the start,

then you can store the values in something like

Code: Select all

$_SESSION['quest1_gold_value_amount']=whatever value you need;
So if you set that, the page can check if it has a value in it. If it does, use that value!

Also useful is checking if session stored variables have been set up or not.

Code: Select all

if (!isset($_SESSION['quest1_gold_value_amount'])) $_SESSION['quest1_gold_value_amount']=the default starting value for it;
Does that help with what you're talking about, at all?
espe
Posts: 32
Joined: Sat Jul 14, 2012 12:46 pm

Re: Random Quest Rewards

Post by espe »

Hehe I like to make things good lookin', even if they are not good :lol:

Lets see... the sessions and the id for now are:

Code: Select all

op32cf0orptcdorhk1d42evoc3
Array
(
[user_logged] => 1
[username] => Ben
[character_logged] => 1
[characterName] => Pombaer
What means the user is logged in, so he can proceed creating an character or loggin in with an excisting. Like i did with "Pombaer".

For using the random quests, we need more sessions to store the values. They have to be generated and stored once and forever or till a started quest runs out and when a user comes to the random quest site, right?

What do I have to store?
A randomly picked title from a textlist or something?
The right description for the generated title...
Randomly generated values for gold and xp, levelbased.

Specials?
What is, if some random actions can appear like find an item (boots, goldbag, premiumcoin...)
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Random Quest Rewards

Post by vitinho444 »

Hey dude, very nice graphics, and i thought you were just starting, i like your papyrus style panels, where you got them? Did you make those? Im thinking if you can get me some for my engine/game :D
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
espe
Posts: 32
Joined: Sat Jul 14, 2012 12:46 pm

Re: Random Quest Rewards

Post by espe »

I've made the sidebar background and the stony border from content.
rest of papyrus in the content area is copy-pasted from the sidebar background :D

Lets work on my problem here and do some graphics there, yea!
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Random Quest Rewards

Post by Callan S. »

espe wrote:They have to be generated and stored once and forever or till a started quest runs out and when a user comes to the random quest site, right?

What do I have to store?
A randomly picked title from a textlist or something?
The right description for the generated title...
Randomly generated values for gold and xp, levelbased.
If you want to store the values for as long as you want, then you have to make space for them in the database itself and record them there. Are you familiar with adding new values to player entries? If you've already done atleast the first five tutorials, you should have some idea how to add new values. So you'd add quest1name (varchar, say 20 characters in length), quest1timeleft, quest1gold, quest1xp. Then do that for quest2 and quest3. Then it's just a matter of calling them from the DB with your code.
espe
Posts: 32
Joined: Sat Jul 14, 2012 12:46 pm

Re: Random Quest Rewards

Post by espe »

Ooookay, now i have a few important tables for that:

characters (with charid, charname, level ...)
character_random_quests (with charid, questid, gold, xp, time)
character_main_quests (with charid, questid, done, time and idk what else)

then more:

quests (with questid, typeid, questname, questdescription)
quest_type (with questid, questtypeid, questtypename) (for different quests like random, main, mini)

Problem:

Can you show me how to safe three randomly choosen quests from the quests-table in the character_random_quests-table?
If the amount of money and gold (level based) and the questid is stored in the table, its easy to show them to the users and delete after finishing a quest to generate three new and store them again. but how to do that idk :(
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Random Quest Rewards

Post by Callan S. »

I don't think I'd have another table if these are randomly generated - since it seems like the players will do alot of random quests, I'd store them right in the character table, in each players entry.

How familiar are you with manipulating values in a players db entry? Can you make level go up, for example?

If you know how to do that, then you pretty much know how to do this. What's the trigger for generating new random quests? A player clicking a link? Okay, say the link is a $_GET link thats '?generatenewrandomquests'

if (isset($_GET['generatenewrandomquests']) && $player_ranquest1gold==0)
{
$player_ranquest1gold=rand(1,10)+5; // or whatever number

// then write that number to the database. Then do the rest of the numbers, like XP, the time(), etc
}

Anyway, it's not a rhetorical question - do you know how to make a player DB value like 'level' increase?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
espe
Posts: 32
Joined: Sat Jul 14, 2012 12:46 pm

Re: Random Quest Rewards

Post by espe »

For levelup the characters i used the following codes:

This is in character.inc.php, because after an arena fight or completing a quest the user will be redirected to his character page:

Code: Select all

if(getCharacters('experiencePoints') > getCharacter_levels()) {
    mysqli_query($db,
                "UPDATE
                    characters
                SET
                    levelID = levelID + 1
                WHERE
                    characterID = $characterID"
                );
}
The functions getCharacters() and getCharacter_levels() are in functions.php, here:

Code: Select all

function getCharacters($var) {
	global $db, $characterID;
    
    $sql    =   "
                SELECT 
                    $var
                FROM
                    characters
                WHERE
                    characterID = $characterID
                ";
	$result = $db->query($sql);
    $row    = $result->fetch_object();
    return $row->$var;
}

Code: Select all

function getCharacter_levels() {
	global $db, $characterID;
    $sql    =   "
                SELECT 
                    neededExperiencePoints
                FROM
                    character_levels
                JOIN
                    characters
                ON
                    characters.characterID = $characterID
                AND
                    characters.levelID = character_levels.levelID
                ";
	$result = $db->query($sql);
    $row    = $result->fetch_object();
    return $row->neededExperiencePoints;
}
and at least the $characterID:

Code: Select all

function getCharacterID() {
    global $db, $userID;
    
    if(isset($_SESSION['characterName'])) {
        $characterName = mysql_escape_string($_SESSION['characterName']);
    }

    $sql = "SELECT characterID FROM characters WHERE characterName = ? AND userID = ?";
    $stmt = $db->prepare($sql);
    $stmt->bind_param('si', $characterName, $userID);
    $stmt->execute();
    $stmt->bind_result($result);
    $stmt->fetch();
    $stmt->close();
    
    return $result;
}

$characterID = getCharacterID();
This code worksand, but i dont really understand what all these fetch, execute, ... do. :?
Copy pasted and edited it a little bit the make use of it...
Maybe thats the problem, why i have such problems to do these random quests :(


I never used get before. Ive read something about that it is not that safe or something.

For my problem with the random quests I think it is best to use a function, what checks if there are randomQuests in the table `character_random_quests`. If there are no quests, select three of them from `quests` where questTypeID = 2 and insert them with the $characterID, a generated time between 2,5 - 10 minutes and some calculated amount for money and xp * getCharacters('level') into `character_random_quests`.
Now we select the generated and inserted quests and display em (name, description, money, xp, time) plus an button, wich will make the marked (radio list) quest/countdown start.

What do you think about that?!

Its long time ago that i made the login, registration, char crations and all that and as you can see im pretty newbie so i have some trouble reading my own code, that is a few months old :D
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Random Quest Rewards

Post by Callan S. »

Yeah, I don't really understand that fetch stuff either.

Really as a basic start, I'd add something like 'quest1text' to character entries.

Then put somewhere in the code

Code: Select all

echo "<a href='?insertstuff'>Insert some stuff into the players quest1text entry</a><br>";

if (isset($_GET['insertstuff']))
{
$playerquest1text="Guard a Pie";
$updateplayer="update players set quest1text='$playerquest1text' where id='$characterid' limit 1";
mysql_query($updateplayer) or die("Could not update player stats #8");
}
I know that code is simple and doesn't do much, but sometimes people who ask for help don't know how to do that, so I'm trying to establish what you can do. If you already know how the above works and how to do it, or you can figure it out from looking at it, then your pretty much on your way. The rest is more like persperation work!
Post Reply

Return to “Beginner Help and Support”