GameServer.Database Queries

For any problems with Dawn of Light website or game server, please direct questions and problems here.

Moderator: Support Team

GameServer.Database Queries

Postby Loki » Thu Dec 08, 2016 4:22 pm

Code: Select all
DataObject[] testing = GameServer.Database.SelectObjects<DOLCharacters>("SOME Query HERE")
I know there is an error in that piece of code, can somebody perhaps break it down or point out what I've missed out please

For example, I would like 'testing' to return a player who has played the most hours, so I can reference it later in script.


Hope that makes sense?


Thanks
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby LugusSkye » Fri Dec 09, 2016 1:51 am

I don't use inline code for SQL but if you want the query, this will return what you are asking:
Doing custom SQL inside DOL is a bit inefficient, imo. Open Workbench or other client you are using and run this:

SELECT D.Name, D.PlayedTime
FROM dolcharacters D
WHERE D.PlayedTime = (SELECT MAX(PlayedTime) as MaxPlayedTime FROM dolcharacters);
User avatar
LugusSkye
DOL Initiate
 
Posts: 20
Joined: Fri Sep 09, 2016 4:05 am

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 8:32 am

Code: Select all
var test = GameServer.Database.SelectObjects<DOLCharacters>("ORDER BY `PlayedTime`", new QueryParameter()).FirstOrDefault();

I needed it inside DOL, I can handle outside the core OK. I was'nt sure on the syntax for DOL, but I've been given the above (TY Graveen)

Hopefully I can now make some custom stuff... If not for a testing server! then for others :-) and to gain knowledge

Have a great weekend all

J
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Graveen » Fri Dec 09, 2016 2:47 pm

Original SQL query would be :
Code: Select all
SELECT * FROM dolcharacters ORDER BY `PlayedTime` LIMIT 1;
There is no WHERE clause in this request. But every SelectObjects<T>() start after the WHERE clause (so perhaps correct arg1 is "1 ORDER BY `PlayedTime` LIMIT 1").
2nd argument is blank because you don't need any parameter.

.FirstOrDefault() is the LIMIT 1 part. It is not needed, you can grab ALL objects and just use the myResult[0] to grab the result, or query all and use LINQ to order (but theses are a BAD design decision, there could be many many records - this is why i still suggest LIMIT x).
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12660
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 6:55 pm

I used the Limit 1 in case the result was 2 players with the same hours, not likely but you never know.

Further to this why cant I reference DOLCharacters_ID using this code
Code: Select all
if (res = base.Interact(player)) { TurnTo(player, 5000); var getID = GameServer.Database.SelectObjects<DOLCharacters>("RealmPoints > 0 ORDER BY RealmPoints DESC LIMIT 1", new QueryParameter()); foreach (var t in getID) { t. //here I cant access DOLCHARACTERS_ID } }
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 7:04 pm

Man I feel dumb.. I can use the Name as its unique :-(
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Graveen » Fri Dec 09, 2016 9:40 pm

Look at DOLDatabase\Tables\DOLCharacters.cs.

Every public field can be accessed, but you won't find ID: DOLCharacter inherits from DataObject. Now go to DataObject.cs, and you can see ObjectID which is the field you need. But, you should not need it, except in very special cases.
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12660
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 10:18 pm

I cant use name because then I cant get the players inventory based on the highest RR on server. Name is not present in Inventory, only ownerID which is same as DOLCharacter_ID isnt it?

or am I making this more complicated than it needs to be?
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 10:24 pm

I can see just how much other useful stuff I can access using DOLCharacter more clearly now Ty G :-)
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Graveen » Fri Dec 09, 2016 10:44 pm

Code: Select all
var dbChar = GameServer.Database.SelectObjects<DOLCharacters>("RealmPoints > 0 ORDER BY RealmPoints DESC LIMIT 1", new QueryParameter()).FirstOrDefault; GamePlayer dummyPlayer = new GamePlayer(null, dbChar); var inventory = dummyPlayer.Inventory; dummyPlayer.Dispose();
should work, altough the null should be tested in new GamePlayer()
Image
* pm me to contribute in Dawn of Light: code, database *
User avatar
Graveen
Project Leader
 
Posts: 12660
Joined: Fri Oct 19, 2007 9:22 pm
Location: France

Re: GameServer.Database Queries

Postby Loki » Fri Dec 09, 2016 11:06 pm

As you said :-)... Once I get to Gameplayer it opens so much more.


Thank you G... for the spoon feeding. Ive wrote countless winforms apps and thought I could stand my ground but when taken out of a comfort zone I know shit lol.
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Leodagan » Sat Dec 10, 2016 11:49 am

Just one little thing, try to use "something" in QueryParameter()
Code: Select all
var dbChar = GameServer.Database.SelectObjects<DOLCharacters>("RealmPoints > @RPLimit ORDER BY RealmPoints DESC LIMIT 1", new QueryParameter("@RPLimit", 0)).FirstOrDefault()
This is not really needed here for performances, but using parametrized queries can help database sql compiler to get an execution plan from cache...

And it can be easier to reuse this query with different where clause...

I know it's not the brightest database API, but it was a simple step stone instead of trying to completely implement LINQ-to-DB or Entity Framework...
User avatar
Leodagan
Developer
 
Posts: 1350
Joined: Tue May 01, 2012 9:30 am
Website: https://daoc.freyad.net
Location: Lyon

Re: GameServer.Database Queries

Postby Leodagan » Sat Dec 10, 2016 12:00 pm

If you fear having duplicated "highest RP" players you can use 2 queries...

Performance will not be great but it prevent handling bigger result sets :
Code: Select all
var maxRP = GameServer.Database.SelectObjects<DOLCharacters>("ORDER BY RealmPoints DESC LIMIT 1", new QueryParameter()).Select(dolchar => dolchar.RealmPoints).FirstOrDefault(); var highestPlayers = GameServer.Database.SelectObjects<DOLCharacters>("RealmPoints = @RPMax", new QueryParameter("@RPMax", maxRP));
Remember this can match "0 RP" as being the Max RP, use an if-clause or a Try/Catch using LINQ "First()" to trigger an exception
User avatar
Leodagan
Developer
 
Posts: 1350
Joined: Tue May 01, 2012 9:30 am
Website: https://daoc.freyad.net
Location: Lyon

Re: GameServer.Database Queries

Postby Loki » Sat Dec 10, 2016 10:37 pm

Code: Select all
var playerac = GameServer.Database.SelectObject<DOLCharacters>("RealmPoints > 0 ORDER BY RealmPoints DESC LIMIT 1"); var inv = GameServer.Database.SelectObject<InventoryItem>(playerac.ObjectId = "OwnerID");

this is not returning what I want

By that I mean I thought it should be matching the DOLCharacter_ID with the Inventory_ID
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk

Re: GameServer.Database Queries

Postby Loki » Sat Dec 10, 2016 11:02 pm

Got It
Code: Select all
var playerac = GameServer.Database.SelectObject<DOLCharacters>("RealmPoints > 0 ORDER BY RealmPoints DESC LIMIT 1"); var inv = GameServer.Database.SelectObject<InventoryItem>("OwnerID = '" + playerac.ObjectId + "'");
“ If debugging is the process of removing software bugs, then programming must be the process of putting them in. ”

Join https://discord.gg/r3T2U7S Official DOL Discord Chat
User avatar
Loki
Developer
 
Posts: 468
Joined: Fri Jun 23, 2006 2:14 am
Location: uk


Return to “%s” Support

Who is online

Users browsing this forum: No registered users and 1 guest