Jump to content
MakeWebGames

Module Development Tips


KyleMassacre

Recommended Posts

Hi Everyone,

I just wanted to post some module development tip for GLV2 since this engine is gaining more and more traction as the time passes.

I would first like to point people to Here. I think it may be a little unfinished, I assume Chris is a pretty busy guy, but it does give some boilerplate stuff.

I recently wrote a review for a module stating that it seemed unfinished because no SQLs were provided for a column insert. The engine will look for a schema.sql file in the module root directory and it will run it on install. With that being said, I think this could potentially be improved on, so Chris, if you are reading this as well, maybe you could look for a schema-[version].sql file so it runs similar to a migration?

When writing your schema.sql file, PLEASE run some simple checks by using "CREATE IF NOT EXISTS" or some other conditionals for adding columns like:

IF COL_LENGTH('table_name', 'column_name') IS NULL
BEGIN
    ALTER TABLE table_name
    ADD [column_name] INT
END

Auto-ran SQLs can be dangerous so I urge people to take caution when writing up their schema files.

Another tip that I have is hooks. Hooks are great because they allow you or other developers to "hook" into other modules. I recently helped out a developer with creating a hook for their module and told him that he should get in touch with some other devs for their paid modules and ask what hooks they use because he may be able to tap into their module and also make his hook known so that maybe they can use it in their module.

Hooks are easy once you get the hang of it. Basically as a developer you need to create an "Event" for your module in your {moduleName}.inc.php file and another developer can create a "Listener" in their {moduleName}.hook.php file.

For example I will take Dave's chat mod (I have not purchased it so I dont know if he created a hook for it.). But Dave could create a davesChatMod.hook.php that could look similar to:

new hook('insert_chat_line', function($hook){
  global $db;
  $db->query("insert into chat (user_id, text, when) values ($hook['user_id'], $hook['text'], ".now().")");
});

That above is the listener. And now we can get into the event. In another developer inc.php they can run

$chatLine = new hook('insert_chat_line');
$args = ['user_id' => $user_id, 'text' => $text]
$chatLine->run($args);

All the keys provided in the $args array will be available in the the listener via the $hook variable. So when a user completes the action where the hook is ran, Daves listener would then insert a chat line into the database.

 

That is all I have for now. I would like to note, this code probably wont work and it was written fo simplicity. For example $db->query() I think doesnt even exist.

  • Like 2
Link to comment
Share on other sites

2 hours ago, KyleMassacre said:

I would first like to point people to Here. I think it may be a little unfinished, I assume Chris is a pretty busy guy, but it does give some boilerplate stuff.

The wiki is on my radar but I've been focusing my time on community requests.

2 hours ago, KyleMassacre said:

With that being said, I think this could potentially be improved on, so Chris, if you are reading this as well, maybe you could look for a schema-[version].sql file so it runs similar to a migration?

I have already thought about this and TBH this would be a better way to do things. I am working on the ability to upgrade a module at the moment so ill make sure to add something like this.

2 hours ago, KyleMassacre said:

When writing your schema.sql file, PLEASE run some simple checks by using "CREATE IF NOT EXISTS" or some other conditionals for adding columns like:

Some very good advise here, ill certainly do this going forward.

  • Like 2
Link to comment
Share on other sites

8 hours ago, BeNiiiBoiii AKA Avellion said:

Thank you for the solid advice/guidance Kyle!

I will definitely be applying this to future mods and updates 🙂

I found this post really helpful so more like this would be great.. always good to come back to if and when I'm stuck Haha :

Im glad it could help at least one person

8 hours ago, Dayo said:

The wiki is on my radar but I've been focusing my time on community requests.

I have already thought about this and TBH this would be a better way to do things. I am working on the ability to upgrade a module at the moment so ill make sure to add something like this.

Yeah I know your a busy dude and I had a couple of minutes to spare so I went ahead and posted where I could. 
 

Some people may know and some may not know, I used to work a lot with NWE and that engine used a lot of hooks and “migration” type sql files for install/upgrade using version numbers in the file name. And what we would do is, the initial sql file (schema.sql) would be a copy/paste of all sql files including the upgrade ones. The engine would only look for the initial file on module upload and install everything everything from there, but when it was an upgrade, it would look for the module-{version}.sql and run only that one because in theory you should be caught up. 

  • Like 1
Link to comment
Share on other sites

  • 5 weeks later...

 

On 3/11/2020 at 7:38 AM, KyleMassacre said:

$chatLine = new hook('insert_chat_line'); $args = ['user_id' => $user_id, 'text' => $text] $chatLine->run($args)

Is this actually implemented in GL or is the hooks not that advance? I know the code you posted isn't real code but in theory if the code would work, would it work in GL? 

Link to comment
Share on other sites

In theory that would work if there was a listener named “insert_chat_line”. What you posted was the “Event” portion of it. GL will let you create your own listener(s) that will allow other devs to use your event in their code.

So if you created your hook/listener in your “moduleName.hook.php” file called “sims_hook”, I could then create an event and use “sims_hook”, pass in the values you ask for, then your hook would execute the code. Hook names are arbitrary so you are not restricted to hooks created by the engine itself. 
is that kind of what you are asking?

Edited by KyleMassacre
Link to comment
Share on other sites

 

$actionHook = new hook("userAction");
$action = array(
                        "user" => $this->user->id, 
                        "module" => "chase", 
                        "id" => 0, 
                        "success" => true, 
                        "reward" => $winnings
                    );
                    $actionHook->run($action)

I just found this in police chase files. After I made a very similar hook. I can't seem to find the userHook anywhere in any files.

But, if it did exists, how would I call it from any file in GL. I made my own little hook.

<?php
    new hook("userAction", function (
		$mod,  $user, $action,
		 $outcome, $extraData = null) {
			
	global $db;
	
		$insert = $db->prepare("INSERT INTO actionLog 
			(AL_userID, AL_name, AL_action, AL_outcome, AL_extraData, AL_time)
			 VALUES
			 (:userID, :name,  :action, :outcome, :extraData, :timeStamp)");	

			$insert->bindParam(":userID", $user->info->US_id);
            $insert->bindParam(":action", $action);
            $insert->bindParam(":outcome",  $outcome);
			$insert->bindParam(":extraData", $extraData);													
			$insert->bindParam(":timeStamp", time());		
			$insert->bindParam(":name", $mod);			
			$insert->execute ();
			
    });			
?>

I can't seem to call my hook and have it insert the data. Once I can call the hook, I am certain it will insert the data, but in the meantime, it won't. 🙂 If iI cann call this from any file in GL, there isn't many to edit.

 

@Dayo @KyleMassacre @Dave

Edited by Sim
Link to comment
Share on other sites

Ill have a look at the userAction hooks this weekend, it seems like i haven't committed some of the changes. I have some work to do adding more hooks

I also need to document all of the hooks so that other developers can start integrating them.

 

 

Is your hook in the module.hooks.php file, also the hook should be 

new hook("userAction", function ($actionData) {

and not

 

new hook("userAction", function (
		$mod,  $user, $action,
		 $outcome, $extraData = null) {
Edited by Dayo
Link to comment
Share on other sites

Where is the actual userAction hook located at?

A list of all available hooks would also help. 

Edit: I just went threw new database scheme so even if this userAction hook exists, I see it serving no purpose. There is no way to tell what action the user even did. There's no log for it.

Link to comment
Share on other sites

The user action hook is located in code (see example here) not in a database!

So using the example when a user steals a car i will look for any userAction hook within every {module}/{module}.hooks.php file, i will then run the function defined in these files.

If i get time tomorrow/next week i may make a few youtube videos explaining a few concepts on GL like the hooks.

  • Like 1
Link to comment
Share on other sites

I got the wrong idea on what the userAction hook did. Thanks  So someone can just release a small user action log mod, with just the userAction hook and everytime userAction is called, they could update the database with that info? Sounds simple enough 

Or if someone steals a car, someone could have them fight a boss afterwards with there userAction hook. That explains why there is no actual userAction hooks code. 

Thanks, I think you just explained the hooks to me 

🙂

Edited by Sim
Link to comment
Share on other sites

19 minutes ago, Sim said:

I got the wrong idea on what the userAction hook did. Thanks  So someone can just release a small user action log mod, with just the userAction hook and everytime userAction is called, they could update the database with that info? Sounds simple enough 

Or if someone steals a car, someone could have them fight a boss afterwards with there userAction hook. That explains why there is no actual userAction hooks code. 

Thanks, I think you just explained the hooks to me 

🙂

Now you're getting it. Some of the hooks by the engine may serve some purpose but others are there for people like you or I to implement what happens when that hook is ran.

To answer your question

Quote

Thanks  So someone can just release a small user action log mod, with just the userAction hook and everytime userAction is called, they could update the database with that info? Sounds simple enough 

Yep! Lets say for example you want to see what a particular user is doing, i'm sure you can set some sort of flag up in the database to track a user id and if it's there you can enter their actions in some sort of log db table.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...