These are chat archives for ikkentim/SampSharp

8th
Jul 2015
Florian van Dillen
@fvandillen
Jul 08 2015 09:00
What I did is create a database session class in my gamemode, like so:
Dean Day
@Bravo-81
Jul 08 2015 09:06
Morning, I'm here..at work. :) But will be able to do bits and bobs.
So you have a session class, do you actually use that for user sessions? Or Database Session stuff?
This gist is what's inside the session class
It contains the database information it needs to connect.
The main difference with regular pawn code, is that in C#/nHibernate, you only connect to your database when you need to do something.
It's like communicating with walkie-talkies, you push to talk, and let go of the button when you are done.
Dean Day
@Bravo-81
Jul 08 2015 09:14
Yeah I get you, just writing the class..
Florian van Dillen
@fvandillen
Jul 08 2015 09:14
A database operation would look like this, somewhere in your code:
using (ISession database = Session.OpenSession())
            {
                using (var tx = database.BeginTransaction())
                {
                    database.Update(this.Account);

                    tx.Commit();
                }
            }
You see how the Session.OpenSession method is used to create the connection to the database, with the information you have in the session class?
Dean Day
@Bravo-81
Jul 08 2015 09:19
Yeah I see how this is working, although I'm missing references..
http://puu.sh/iRoPX/d389a41c8c.png
Florian van Dillen
@fvandillen
Jul 08 2015 09:20
The first reference should be a reference to your player class
Dean Day
@Bravo-81
Jul 08 2015 09:20
I've got a player class with a public Player.
Florian van Dillen
@fvandillen
Jul 08 2015 09:20
In my case, i have using trinity; to resolve that reference
Doh.
Florian van Dillen
@fvandillen
Jul 08 2015 09:21
in your case, do using SampSharpTemplate.Worlds;
And the player reference will resolve.
Dean Day
@Bravo-81
Jul 08 2015 09:21
Awesome, what's the TreatConfiguration?
Florian van Dillen
@fvandillen
Jul 08 2015 09:22
It's the method which is in my session class, however you should visit the gist to see that, because gitter cuts it off
Dean Day
@Bravo-81
Jul 08 2015 09:24
Yup that's good, not I'm missing a ref for SchemaUpdate. -.-
Oh hang on, might of missed a Nhibernate one.
Florian van Dillen
@fvandillen
Jul 08 2015 09:25
using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl;
It's located in NHibernate.Tool.hbm2ddl
Dean Day
@Bravo-81
Jul 08 2015 09:25
Yeah that's the one I missed.
All good.
Florian van Dillen
@fvandillen
Jul 08 2015 09:26
Okay, next part: Mapping
Dean Day
@Bravo-81
Jul 08 2015 09:26
This message was deleted
-.-
Florian van Dillen
@fvandillen
Jul 08 2015 09:26
Mappings first, or else you have no idea what i'm about to explain :P
Dean Day
@Bravo-81
Jul 08 2015 09:27
Nevermind, sure.. :D
Florian van Dillen
@fvandillen
Jul 08 2015 09:27
Mappings are basically classes that describe how certain properties of classes in your gamemode should be stored in the database.
Dean Day
@Bravo-81
Jul 08 2015 09:27
Right.
Florian van Dillen
@fvandillen
Jul 08 2015 09:27
Take a house for example.
In your root namespace, you create a class called House. (you don't actually have to create it).
A house has a Vector3 for the location in the gameworld, a string with the name, and a integer with the PickupModel.
Makes sense?
Dean Day
@Bravo-81
Jul 08 2015 09:28
Yeah sure
Florian van Dillen
@fvandillen
Jul 08 2015 09:29
Pro-tip: I prefer to store this in a folder/namespace called Models, because the House class, is in fact a data model
Dean Day
@Bravo-81
Jul 08 2015 09:30
So for example..
Florian van Dillen
@fvandillen
Jul 08 2015 09:30
Any model needs to be public.
Dean Day
@Bravo-81
Jul 08 2015 09:30
If I have a 'Shop', I can define it by name in /Models/MyShop.cs
With a Vector3, string ShopName, bool Active, etc..
Florian van Dillen
@fvandillen
Jul 08 2015 09:31
Yes, thats a good example
here's some sample code for a house I made
Dean Day
@Bravo-81
Jul 08 2015 09:31
I could reuse this class for every shop I have for example?
Instead of making a new one.
Florian van Dillen
@fvandillen
Jul 08 2015 09:31
(oh, scratch the Vector3, i store x y z seperately
Dean Day
@Bravo-81
Jul 08 2015 09:31
Np.
Florian van Dillen
@fvandillen
Jul 08 2015 09:32
Example:
using SampSharp.Streamer.World;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace trinity
{
    public class House
    {
        public virtual int DatabaseID { get; set; }
        public virtual float x { get; set; }
        public virtual float y { get; set; }
        public virtual float z { get; set; }
        public virtual string Name { get; set; }
        public virtual DynamicPickup createdPickup { get; set; }
    }
}
Let me walk you through
Any questions after seeing this?
Dean Day
@Bravo-81
Jul 08 2015 09:33
You're making your variables here, not sure what Dynamic Pickup is doing?
Florian van Dillen
@fvandillen
Jul 08 2015 09:33
Yes, i'm just making properties for this class here, that we can later use to store the data for this house
Dean Day
@Bravo-81
Jul 08 2015 09:33
As in why it's two lines, does that reference the dynamic pickup code in samp?
DynamicPickup createdPickup
Florian van Dillen
@fvandillen
Jul 08 2015 09:34
the dynamic pickup is basically when you create the house, we will store the pickup in the model, so we can edit it later (move the pickup, change the pickup type)
its Type and variable name
DynamicPickup is so the gamemode knows this is a streamer pickup
and createdPickup is just the name we give it
Also, a property field must be virtual for it to work with nhibernate
And public
Any questions about this class?
Dean Day
@Bravo-81
Jul 08 2015 09:41
Sorry was on a call, I'm a 'Support Guru' for a website company. Would you believe it. xD
Florian van Dillen
@fvandillen
Jul 08 2015 09:41
No worries, i'm at work too :D
Dean Day
@Bravo-81
Jul 08 2015 09:41
Yeah that's perfect, I'm good. :)
Florian van Dillen
@fvandillen
Jul 08 2015 09:42
Allright, moving on
By itself, this does nothing
Dean Day
@Bravo-81
Jul 08 2015 09:42
Ofc
Florian van Dillen
@fvandillen
Jul 08 2015 09:42
Later, you could create methods in this class to draw the actual dynamicpickup, a 3dtextlabel etc..
But thats not relevant right now.
We need to tell nHibernate how it should store this class in the database!
This is called "mapping"
Dean Day
@Bravo-81
Jul 08 2015 09:43
OK.
Florian van Dillen
@fvandillen
Jul 08 2015 09:43
I prefer to store mappings in a namespace called trinity.Database.Mappings
this is a mapping or ClassMap
you can have multiple classes like PropertyMapping in this same class, if they belong together (such as my group system)
but for now, whats important is line 13
Dean Day
@Bravo-81
Jul 08 2015 09:47
So mine would be TeamBasedMissions.Models.MyShop ?
OK I've got the Mpaaing class Property.cs and my Model class MyHouse.cs
Copied your code for now, all working.
Florian van Dillen
@fvandillen
Jul 08 2015 09:51
Yes, yours would be like that
The mapping values used here are quite easy to follow.
x can be used to access properties of your model, allowing you to map them to the database.
Table is the name of the table houses should be stored in
Id is to identify a unique id for this database entry
Having this in place, you can basically do new House();, set properties x, y, z and a name, and then proceed with saving stuff (that is the next part)
Dean Day
@Bravo-81
Jul 08 2015 09:55
I c
But my Mapping doesn't match my model, I need to change that first?
Florian van Dillen
@fvandillen
Jul 08 2015 09:55
How do you mean not match the model?
Dean Day
@Bravo-81
Jul 08 2015 09:55
I've not got a pickup?
Florian van Dillen
@fvandillen
Jul 08 2015 09:56
You don't need to store the actual pickup, because it is created everytime you load the property
All the pickup gives you is a reference to its ID, which is only used when the server is running
In this perspective, the pickup doesnt need to be virtual, which is my mistake
Dean Day
@Bravo-81
Jul 08 2015 09:57
OK, so SAMP will use that value to spawn a Pickup? For example the house icon. But how is that stored?
Florian van Dillen
@fvandillen
Jul 08 2015 09:57
You would still need your own method to spawn a pickup
Example:
I have a mapped integer called "type". It is actually the modelid of the pickup that is used. (See; http://weedarr.wikidot.com/pickups)
In the house model, i have this method:
public virtual void createPickup()
        {
            int modelid = this.type;
            var createdPickup = new DynamicPickup(modelid, 1, new Vector3(this.x, this.y, this.z), 100f);
            this.createdPickup = createdPickup;
            this.type = type;

            createdPickup.PickedUp += (sender, args) =>
            {
                var player = args.Player as Player;
                player.LastPickedUp = this;
                player.SendClientMessage(Color.IndianRed, "[Dev] You are at entrance: " + this.Name);
            };

        }
As you can see, we do store the pickup in the model, but we don't store it into the database because that would be pointless
Dean Day
@Bravo-81
Jul 08 2015 10:02
OK, I understand how it's calling that and everything. But I still don't understand where it's storing the actual value? Does it make a file in the gamemode?
Florian van Dillen
@fvandillen
Jul 08 2015 10:02
What do you mean with value, exactly?
The pickup value?
Dean Day
@Bravo-81
Jul 08 2015 10:02
Yeah
Say Blue House, 1272. How does it know that property is using the value 1272?
If it isn't in my DB.
Florian van Dillen
@fvandillen
Jul 08 2015 10:03
new DynamicPickup returns a reference to the pickup, which is stored in var createdPickup (which is a local var)
It is then stored in the model by doing this.createdPickup = createdPickup
Dean Day
@Bravo-81
Jul 08 2015 10:04
So it physically saves it somewhere in the filesystem?
Florian van Dillen
@fvandillen
Jul 08 2015 10:04
No, just in memory
Dean Day
@Bravo-81
Jul 08 2015 10:04
Meaning it wont still be there after a restart?
Florian van Dillen
@fvandillen
Jul 08 2015 10:04
True, but when the server restarts you will load all houses from the db and call createPickup again
Dean Day
@Bravo-81
Jul 08 2015 10:05
Then where does it get the pickup value? xD
If I've lost it.
Maybe I'm getting confused here.
So from the top
Say I'm in game, I want to save a new Property.
Florian van Dillen
@fvandillen
Jul 08 2015 10:06
Yeah let's say you have a command that creates a new house
Dean Day
@Bravo-81
Jul 08 2015 10:07
I'd use a command we create, like /createproperty Xvalue YValue ZValue MyShopName MyPickupValue
Florian van Dillen
@fvandillen
Jul 08 2015 10:07
for simplicity, let's say we use the players current position
something like /createhouse pickupType houseName
I think you are confusing the pickupType (pickupmodel) with the id the server assigns to it :P
Dean Day
@Bravo-81
Jul 08 2015 10:12
I belive the pickupType should be the integer value for SAMP?
Florian van Dillen
@fvandillen
Jul 08 2015 10:12
Yes
Dean Day
@Bravo-81
Jul 08 2015 10:13
Yeah I get that.
So our value.. 1272 which resembles a blue house.
Florian van Dillen
@fvandillen
Jul 08 2015 10:14
yes
Dean Day
@Bravo-81
Jul 08 2015 10:14
Where does that value go? I'd imagine it should be stored in the Database.
As SAMP & our Gamemode calls that value, to spawn it in game.
Florian van Dillen
@fvandillen
Jul 08 2015 10:14
Yes, we could put it in the Model as a integer field called "type"
And then we simply add it to the mapping, too
Dean Day
@Bravo-81
Jul 08 2015 10:20
OK, now I'm happy. :P
Next?
'Map(x => x.createdPickup);' ?
Also can I ask why we use x?
Florian van Dillen
@fvandillen
Jul 08 2015 10:53
Dean Day
@Bravo-81
Jul 08 2015 11:22
OK awesome, thanks.
Ready to move on.
Florian van Dillen
@fvandillen
Jul 08 2015 11:23
Okay so in recap, we now have the session class in place, the model and we've mapped the model
If you had the createhouse command, you could do something like this
Look at it, run it in your head, and let me know if you have any questions
Dean Day
@Bravo-81
Jul 08 2015 11:29
Nice. I get that. :)
Florian van Dillen
@fvandillen
Jul 08 2015 11:30
The magic is in the database section
Dean Day
@Bravo-81
Jul 08 2015 11:30
So simple.
Florian van Dillen
@fvandillen
Jul 08 2015 11:30
the session can be used to GET data from the database
to insert, update and delete things, you need to use a transaction
There is a little bracket in line 17 you should remove, it shouldnt be there :P
database.Save is to INSERT things, database.Update is to UPDATE things, database.SaveOrUpdate is to save if not exists, update if exists, database.DELETE.. well you get the picture
tx.Commit is magic
the nhibernate library will figure out by itself if it needs to write to the database.
Dean Day
@Bravo-81
Jul 08 2015 11:33
Wow.
Florian van Dillen
@fvandillen
Jul 08 2015 11:33
it will take care of any ID's and num_rows stuff
Dean Day
@Bravo-81
Jul 08 2015 11:33
I miss references for House and ISession.
So I need to ref the Hibernate stuff
Where's House coming from?
Florian van Dillen
@fvandillen
Jul 08 2015 11:34
House is our model
Dean Day
@Bravo-81
Jul 08 2015 11:34
Which is MyHouse for me.
Florian van Dillen
@fvandillen
Jul 08 2015 11:34
Yes, then you'll need to use MyHouse instead
and also, you're missing using nhibernate; and really, you can VERY simply solve references by hovering over the red line
Just try it for ISession
Dean Day
@Bravo-81
Jul 08 2015 11:35
I'm in 2013
I've got a red line for Session
Florian van Dillen
@fvandillen
Jul 08 2015 11:35
hover your mouse over the session red line
Dean Day
@Bravo-81
Jul 08 2015 11:35
Ahh I have to click it
Florian van Dillen
@fvandillen
Jul 08 2015 11:35
Wait for a little down arrow to appear
It'll suggest including nhibernate
Dean Day
@Bravo-81
Jul 08 2015 11:36
Awesome, thanks for that.
Florian van Dillen
@fvandillen
Jul 08 2015 11:36
Visual Studio is aware of your references and will know when you reference stuff you haven't yet included in a namespace
VERY handy feature
Okay, so far, is everything clear and making sense to you?
Dean Day
@Bravo-81
Jul 08 2015 11:38
Yup looks good.
Florian van Dillen
@fvandillen
Jul 08 2015 11:38
Okay, if you have a command to change a house position for example
Dean Day
@Bravo-81
Jul 08 2015 11:39
So now I could type /createhouse 1272 MyHouse
& it would save that to the DB.
Florian van Dillen
@fvandillen
Jul 08 2015 11:39
Yes
Dean Day
@Bravo-81
Jul 08 2015 11:39
Nice.
Florian van Dillen
@fvandillen
Jul 08 2015 11:39
And you didn't have to write a single line of SQL :P
Dean Day
@Bravo-81
Jul 08 2015 11:39
Pretty awesome.
Florian van Dillen
@fvandillen
Jul 08 2015 11:39
And not even create the table structure in the database!
Dean Day
@Bravo-81
Jul 08 2015 11:39
Oh I did that already, but that's a cool feature. :D
Florian van Dillen
@fvandillen
Jul 08 2015 11:40
(you might need to create the db and the empty table, though)
Florian van Dillen
@fvandillen
Jul 08 2015 11:40
It is especially handy when you CHANGE a model/mapping
The best thing is when you get to references, nhibernate will generate foreign keys for you
Dean Day
@Bravo-81
Jul 08 2015 11:41
Oh nice.
Florian van Dillen
@fvandillen
Jul 08 2015 11:48
For your example, this is my loadAccount method which I have in my player class
/// <summary>
        /// Loads the persistent account data of this player.
        /// </summary>
        public void LoadAccount()
        {
            using (ISession database = Session.OpenSession()) 
            {  
                this.Account = database.QueryOver<Account>().Where(x => x.Username == this.Name).SingleOrDefault<Account>();
                if(this.Account != null)
                    this.IsRegistered = true;
            }
        }
Dean Day
@Bravo-81
Jul 08 2015 11:56
Oh yeah, awesome.
Florian van Dillen
@fvandillen
Jul 08 2015 11:56
Does that make sense?
Dean Day
@Bravo-81
Jul 08 2015 11:56
I'm going to have alot to learn, but I understand what it's doing >D
Florian van Dillen
@fvandillen
Jul 08 2015 11:56
You'll find out that it doesnt get more complex than this, when you figure it out
Dean Day
@Bravo-81
Jul 08 2015 11:58
OK cool.
So how do I make SAMP spawn this House? As I can save it.
Well this pickup.
Florian van Dillen
@fvandillen
Jul 08 2015 11:59
Yeah, you can also save coordinates of the interior, so you know where to teleport to
Dean Day
@Bravo-81
Jul 08 2015 12:01
Which I could do by just adding more fields to my model & mapping
:D
Florian van Dillen
@fvandillen
Jul 08 2015 12:01
Yes! You got the picture haha
Dean Day
@Bravo-81
Jul 08 2015 12:01
Sweet. :)
Could we try a simple login system? I think that will get me going.
Like how to detect when a Player joins, give them a screen to login with a Username & Password. Check if it's in the DB or not.
Brb.
Florian van Dillen
@fvandillen
Jul 08 2015 12:03
Sure, I might not have enough time for that today, but i'll try and help you out
Dean Day
@Bravo-81
Jul 08 2015 12:07
OK I'm back.
If you're busy, maybe just go over how to get the server to spawn all houses from the DB?
If you wouldn't mind.
Dean Day
@Bravo-81
Jul 08 2015 12:15
Oh also, what if a user just does /createhouse and doesn't give values, how would we catch errors/
Oh wait, it seems SampSharp does that for you.. :D Looking at the Command Processor now.
Dean Day
@Bravo-81
Jul 08 2015 12:29
I've made a test cmd..
    [Command("hello")]
    public void HelloCommand(Player player)
    {
        player.SendClientMessage("Hello, welcome to our server. This server was created in C# using SampSharp. More to come soon!");
    }
Dean Day
@Bravo-81
Jul 08 2015 12:44
Will test it when I'm home, seems simple enough.
Dean Day
@Bravo-81
Jul 08 2015 13:01
I've gone ahead and added a some default values to the House command and also a 'Cost' field.
Tim Potze
@ikkentim
Jul 08 2015 14:26
Make the method static (the command)
Dean Day
@Bravo-81
Jul 08 2015 14:36
Have done, what's the difference Mr I'm on Holiday?
Tim Potze
@ikkentim
Jul 08 2015 14:37
I'm leaving this evening
Dean Day
@Bravo-81
Jul 08 2015 14:38
Haha fair enough. :+1:

You can see my server here.. 185.43.211.186:7767
So far I have /hello :)

You should also be able to do /makemeadmin then /createhouse

Also /ah
Dean Day
@Bravo-81
Jul 08 2015 15:45
OK leaving work, enjoy your holiday Tim.
Dean Day
@Bravo-81
Jul 08 2015 19:21

Hmm this errors:
public override void OnConnected(EventArgs e)
{
base.OnConnected(e);

        Console.WriteLine(Name + " connected.");

        SendClientMessage("Welcome to {88AA88}T{FFFFFF}eam {88AA88}B{FFFFFF}ased {88AA88}M{FFFFFF}issions!");
        GameText("~w~Team Based Missions", 4000, 4);
    }

It's from the gamemode conversion, states I cannot convert a Player to a GTAPlayer. Is it the 'Name'?

Marco Acierno
@marcoacierno
Jul 08 2015 20:49
What is Name?
You have created your own Player class?
Dean Day
@Bravo-81
Jul 08 2015 20:52
Nah its from a converted game mode example. I imagine it isn't referenced. How would I call the players name?