Page 1 of 1

.NET API: CreateRoom problems

Posted: 19 May 2009, 22:46
by cBroadbo
After having some exceptions thrown by the .NET API when calling CreateRoom, I loaded up the .NET API project and stepped through the code to see what was going on. Here is some code from CreateRoom that is causing trouble:

Code: Select all

            string isGame = (bool)roomObj["isGame"] ? "1" : "0";
            string exitCurrentRoom = "1";
            string maxUsers = roomObj.ContainsKey("maxUsers") ? Convert.ToString(roomObj["maxUsers"]) : "0";
            string maxSpectators = roomObj.ContainsKey("maxSpectators") ? Convert.ToString(roomObj["maxSpectators"]) : "0";

            if ((bool)roomObj["isGame"] && roomObj["exitCurrent"] != null)
                exitCurrentRoom = (bool)roomObj["exitCurrent"] ? "1" : "0";

            string xmlMsg = "<room tmp='1' gam='" + isGame + "' spec='" + maxSpectators + "' exit='" + exitCurrentRoom + "'>";

            xmlMsg += "<name><![CDATA[" + (roomObj["name"] == null ? "" : roomObj["name"]) + "]]></name>";
            xmlMsg += "<pwd><![CDATA[" + (roomObj["password"] == null ? "" : roomObj["password"]) + "]]></pwd>";
            xmlMsg += "<max>" + maxUsers + "</max>";

            if (roomObj["uCount"] != null)
                xmlMsg += "<uCnt>" + ((bool)roomObj["uCount"] ? "1" : "0") + "</uCnt>";

            // Set extension for room
            if (roomObj["extension"] != null)
            {
                xmlMsg += "<xt n='" + ((Hashtable)roomObj["extension"])["name"];
                xmlMsg += "' s='" + ((Hashtable)roomObj["extension"])["script"] + "' />";
            }

            // Set Room Variables on creation
            if (roomObj["vars"] == null)
                xmlMsg += "<vars></vars>";


The central issue is that this code expects various keys to be present in the incoming HashTable. The absence of certain fields causes an exception: Here is and example:

Code: Select all

string isGame = (bool)roomObj["isGame"] ? "1" : "0";


Other parts of this code perform better by checking for the existance of the key prior to accessing it:

Code: Select all

roomObj.ContainsKey("maxUsers")


Seems like this should be done consistantly throughout this section.

The only other issue I found is that there is an inconsistancy between the documentation in the .NET API and the code for the "exitCurrentRoom". The docs list it as "exitCurrentRoom" whereas the code is looking for "exitCurrent".

You can successfully create a room despite these issues by specifying most, if not all the fields in the HashTable, similar to this:

Code: Select all

                Hashtable roomObj = new Hashtable();
                roomObj.Add("name", "myRoomName");
                roomObj.Add("password", "myPassword);
                roomObj.Add("exitCurrentRoom", false);
                roomObj.Add("exitCurrent", false);
                roomObj.Add("maxUsers", 20);
                roomObj.Add("uCount", true);
                roomObj.Add("maxSpectators", 0);
                roomObj.Add("isGame", false);
                roomObj.Add("extension", null);
                roomObj.Add("vars", null);


Hope this helps!

Craig

Posted: 20 May 2009, 05:59
by Lapo
Moved in the proper API section.

I was pointing out some bugs

Posted: 20 May 2009, 07:48
by cBroadbo
Hi Lapo,

I put the post in the bug section because I was pointing out some issues that I think are bugs.

It does make sense to have it here because anyone having issues with CreateRoom could benefit from the post.

I just wanted to make sure you guys knew about the issues/bugs.

Craig

Posted: 20 May 2009, 14:49
by Lapo
Sure, thanks .
We are taking a look asap

Posted: 20 May 2009, 19:51
by ThomasLund
Hi!

I am leaving on vacation veeeery soon - extended weekend. So hopefully we can solve this without a 1.2.2 "double brown bag" release.

For CreateRoom I really hate the Hashtable approach (leftover from how AS3 does it), and that is why I deprecated it completely in 1.2.

You should instead be using the new CreateRoom that takes an NewRoomDescriptor object instead.

With that said - the variables that the code is requiring you to insert into the hashtable are the required variables for CreateRoom. So yes - the code *could* be nice and check it, but in the end its a matter of not having used required variables. The ones that are not required do not need to be specified. That works in my example tutorials, so should also work for everyone else (but code can be spooky sometimes)

I would really like to not make any changes to the Hashtable CreateRoom code anymore - simply because it will be removed in the next bigger update. So a waste of time really.

Good point on the exitCurrent / exitCurrentRoom though. Missed that one - but also fixed in the object based approach. So I would also here not really want to fix anything, but rather delete the problem next update :-)

Hope thats satisfactory

Best
Thomas

Agreed

Posted: 20 May 2009, 20:08
by cBroadbo
No worries Thomas. I have it working and don't need any modifications. I just wanted to make sure you knew the issues and perhaps help anyone who might be having difficulties.

If that approach is being depricated then you are right. There is no point in fixing it.

Enjoy your vacation!

Craig

Posted: 21 May 2009, 05:11
by ThomasLund
Thanks Craig!!

Really appreciate it - but yes - please please (everyone) move to the new object based room creation. This piece of code WILL be removed next time and break your games if you didnt notice the warnings in the console + read the documentation.

And it takes some of the magic guesswork out of the API (figuring out what keys to use in a Hashtable isnt really a nice or easy way). Sorry to even have introduced it

:-)

/Thomas