Adding multiple ite...
 
Notifications
Clear all

Adding multiple items / Slow inventory population

4 Posts
2 Users
0 Reactions
993 Views
(@chickapoowaapaadaao)
Posts: 2
New Member
Topic starter
 

Hi, 

I recently upgraded from V2 to the latest V3 build. I also switched database hosting to a new server (sitegrounds), as the old one was very slow (godaddy).

After going through the setup process for 3 I have everything basically working again under the new version, but it seems like the default items that I am adding for users that register are taking forever to "come through" to the server.

In my testing, I'm registering a new user, adding items to that users inventory upon successful registration, and then about 10 seconds later, I'm trying to query those items from the DB to populate the users in-game inventory. 

Under version 2 this rarely had any issues, but under the new version, it only populates a handful of the items the first time i query the database for the newly registered players inventory. If I log in a little bit later and check that users inventory again, it seems to have more/all of the items.

I'm curious what could be causing this if it's not just a terrible response time from the new DB host. Everything else I hit the host with in terms of response time has been quite fast, so I'd like to think this might not be the case.

When I'm adding items, I'm doing it like this (for about 30 items):

Inventory newPrimary = new Inventory();
newPrimary.name = "A Weapon";

Inventory newPrimary2 = new Inventory();
newPrimary2.name = "Another Weapon";

... etc...

And then I am calling the update function for each item individually, one right after the other:

newPrimary.Update((bool success, string error) => {
    if (success)
        Debug.Log("Success");
    else
        Debug.Log("Failed: " + error);
 });

...

Is this the right/wrong way to handle adding a large amount of items at the same time? Is there any other reason you can think of that items would take so long to get added to the inventory besides the host being slow? Is there a better way to handle default items for newly registered users?

Should I be handling any code differently on the client side now that I moved from V2 to V3? From what I've read so far, it seems like the client side code is mostly identical.

The database is hosted through sitegrounds, maybe there is a better host for a database like this as well. I'm open to any suggestions you might have.

Thanks in advance, and sorry for so many questions!

 
Posted : 27/12/2017 7:13 pm
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

Well, sending so many calls to web server is not good for performance (every web server can handle a limited number of concurrent requests and if it receives more than the number then it will set the new ones like in a pause state until the older have been completely handled).

A good way to make multiple consecutive requests would be to handle this in a coroutine where you wait until each request receives a response, this way you can optimize the server response and don't fill its connections queue.

Here is an example of how I would do it:

IEnumerator AddInventoryItems()
{
	List<Inventory> allItems = new List<Inventory> ();

	Inventory newPrimary = new Inventory ();
	newPrimary.name = "A Weapon";
	allItems.Add (newPrimary);

	Inventory newPrimary2 = new Inventory ();
	newPrimary2.name = "Another Weapon";
	allItems.Add (newPrimary2);

	while (allItems.Count > 0)
	{
		bool wait = true;
		allItems [0].Update ((bool success, string error) => {
			if (success)
				Debug.Log (allItems[0].name + ": Success");
			else
				Debug.Log (allItems [0].name + ": Failed: " + error);
			wait = false;
		});
		while (wait)
		{
			yield return null;
		}
		allItems.RemoveAt (0);
	}
}

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 27/12/2017 10:20 pm
(@chickapoowaapaadaao)
Posts: 2
New Member
Topic starter
 

Thanks, I'll give this a go.

Also, is there an easier way to set default items for a newly registered player? I was thinking I could set up a PHP script to run some SQL on the database to generate the items once all in one go when a user registers, but I haven't really dived into that yet. Any thoughts on whether you think that should work or not?

Also, I'm curious, what database hosting solution did you use to develop Combu?

Thanks again!

 
Posted : 28/12/2017 7:46 pm
(@skaredcreations)
Posts: 805
Prominent Member Admin
 

An easy way would be to create an add-on on your server with a function to be passed to its OnUserCreate where you would use the core server classes so that you won't need to make manual SQL queries.

Just as example, create a folder called myAddon (or any other name of your choice) into /combu/addons of your server and then create a file called addon.php into the new folder with the following code:

<?php

use CombuInventory;

/**
 * Initialize stuff for a newly created Account
 * @param CombuAccount $user
 */
function MyAddon_OnUserCreate ($user) {
    // Add item 1
    $item = new Inventory();
    $item->IdAccount = $user->Id;
    $item->Name = "My Item 1";
    $item->CustomData = json_encode(array(
        "property1" => "value1",
        "property2" => "value2"
    ));
    $item->Quantity = 1;
    $item->Save();
    
    // Add item 2
    $item = new Inventory();
    $item->IdAccount = $user->Id;
    $item->Name = "My Item 2";
    $item->CustomData = json_encode(array(
        "property1" => "value1",
        "property2" => "value2"
    ));
    $item->Quantity = 1;
    $item->Save();
}

$addon->Name = "My add-on";
$addon->Version = "1.0";
$addon->OnUserCreate = "MyAddon_OnUserCreate";

With the above add-on you will have default items automatically added to every new Account created.

More informations about how to create add-ons and database-related classes and web services to be used into it are available at here.

About hosting solution I usually prefer to rent a dedicated server to handle my apps and games (Amazon AWS is usually a good option and not a big deal for price, where you can install both Apache and MySql on a Linux machine), because of much more flexibility and higher performance. As for development I use my local workstation with Apache 2.4+ and MySql 5.x+ installed (or XAMPP if you're running on Windows), that is almost similar to most servers around the web.

FRANCESCO CROCETTI @ SKARED CREATIONS

 
Posted : 28/12/2017 8:26 pm
Share: