Editing strings in a MySQL database in C#?

11/30/2012 22:58 ~*NewDuuDe*~#1
This is somewhat noobish, but I really can't figure this out.

I created a database entry in the character table that inserts strings into a field. Whenever you start a quest or progress in a quest it adds another string so that I can check for that on the next part of the quest. So, my problem.

For adding strings/checking it would be:
Code:
user.quests += "sss";
 if (user.quests.contains("sss")
but I realize that running multiple if statements checking for values in this on an npc won't work, as it can only run one if-statement with a certain value and several strings will still be in the database that it has checked for before, so I have to have a way of removing or replacing it.

I tried:
Code:
user.quests -=(which doesn't work for strings), user.quest.remove("sss") // doesnt do anything
user.quest.replace("sss", "aaa") // doesn't do anything either.
.replace could probably work if i deleted text from a certain index([Only registered and activated users can see links. Click Here To Register...]), but that wont work as people wont do the quests in the same order, so I cant set it to a certain index without it being replaced at some point.

Any ideas? At the moment I can only add strings to it, but not remove.
11/30/2012 23:22 pro4never#2
This is NOT how to handle this...

I cannot think of any more inefficient way to handle it to be honest (not to be rude... just a fact)

String comparisons are quite costly operations and scale horrendously poorly (might be 'acceptable' with a user having 5 quests but what if they have 100?)


A more scalable system would be a quests table following a structure somewhere along the line of...


PlayerID
QuestID
var1
var2
var3
CompletionEnum


That way you can do something like

SELECT * FROM QUESTS WHERE PlayerID=X AND CompletionEnum=Active

UPDATE QUESTS var1=mobKills WHERE PlayerID=X


Simple updates like that.

You then can do a QuestType table which links quest id to further information (times available, description, rewards, etc).

Break tables into their most simple yet functional format is generally the right course of action.
11/30/2012 23:39 Korvacs#3
Couldnt have said it better myself pro, that string system is such a fundamentally flawed system its not even funny =x
11/30/2012 23:43 ~*NewDuuDe*~#4
I do appreciate the feedback, but I wasn't actually going to use this. I was doing this solely for learning. So I'd still like to have an answer from anyone who can bother.
12/01/2012 00:09 Korvacs#5
So you want to learn how to use a system which you will never use, and should never use in the future?

Why bother?

Sigh well in any case, .remove and .replace are doing something, but they return the new modified string, so you would need to do

Example = Example.Replace("sss", "yyy");

Intellisense is something you should be paying attention to while coding.
12/01/2012 01:16 pro4never#6
Small example..

lets say you have a string variable as part of your character class called "QuestString".

When checking if you have a quest active you would have to do something like.

if(user.QuestString.Contains("UniqueQuestName"))
//logic


All of the string logic (adding new quests, removing old quests, checking if quest exists) should be done source side and the database value should only be referenced when loading the character and when saving to the database (when user logs out or server shuts down)


Keep in mind that such a string system is not only more computationally expensive the longer the string is (more characters to check) but you're also required to give quests a unique name.

EG:

QuestString = "PheasantHunting1PheasantHunting10"

These are two stages to one quest (lets assume you have 10 quests. just an example) but using

if(user.QuestString.Conatains("PheasantHunting1") will return quest even if you do not current have the first stage of the quest because PheasantHunting10 contains the string PheasantHunting1.

it's just a realllyyy bad approach.
12/01/2012 01:26 Ultimation#7
shouldnt you be using a enumeration like pro says. its cheaper for the CPU to process and its easier to handle...

Code:
Switch (user.Quest)
{
case UserQuest.PheasantHunting:
  {
    //code block here
    break; 
  }
}

public enum UserQuest
{
None=0,
PheasantHunting=1,
DragonHunting=2
//etc etc
}
and if you want to store it into the database as a string for easy reading you could do

reading:
Code:
var datareader = new DataReader("SELECT quest FROM QUESTS WHERE PlayerID=X AND CompletionEnum=Active")
user.Quest = (UserQuest)Enum.parse(datareader["Quest"],typeof(UserQuest);
writing

Code:
sql.execute(string.format("Update Quests Set Quest='{0}' where PlayerID={1}",user.Quest.ToString(),User.ID);
Something like that

and in the database the value for PlayerID XXX should be the Enum Name.. i.e PheasantHunting


if you wanted each quest type to have different steps.. like stage1, stage2, stage 3 have another

you could do..
Code:
 
user.Quest = (FieldValue / 100);
user.QuestStage=(FieldValue % 100)
so if you was doing the pheasant quest and on stage 2 you would have in the database the ID 102
result being:
Code:
 
user.Quest = PheasantHunting
User.QuestStage = 2
its basic math, ill let you work out the rest.
12/01/2012 17:43 JohnHeatz#8
#Closed as requested