[TC] Gaming Forums
A quick start with C# - Printable Version

+- [TC] Gaming Forums (https://forum.city-driving.co.uk)
+-- Forum: General Information (/forumdisplay.php?fid=1)
+--- Forum: General Discussions (/forumdisplay.php?fid=4)
+---- Forum: Developer Forum (/forumdisplay.php?fid=221)
+---- Thread: A quick start with C# (/showthread.php?tid=27257)



A quick start with C# - Adorable - 2017-08-29 02:02

Using [TC]s new API inside C# Applications:

NOTE: Json.NET Library needed for converting from JSON -> .NET
Code:
class tcApi
    {
        string key = "&key=YourKey";
        string baseUrl = "https://api.tc-gaming.co.uk/";

        System.Net.WebClient cl = new System.Net.WebClient();

        public dynamic getData (string a)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject(cl.DownloadString(baseUrl + a + key));
        }
    }

    static class Actions
    {
        /// <summary>
        /// Returns detailed informations about each [TC] Server
        /// </summary>
        public static string Servers()
        {
            return "citydriving/live/status?server=all";
        }
        /// <summary>
        /// Gets the profile of a player (by username)
        /// </summary>
        /// <param name="getExtraInfo">Get informations relative to the licenses, cars and stats</param>
        /// /// <param name="getInSimUI">!setopt command settings</param>
        public static string Profile(string Username, bool getExtraInfo = false, bool getInSimUI = false)
        {
            string ret = "citydriving/profile/get?username=" + Username;
            if (getExtraInfo) ret += "&stats=1&cars=1&licenses=1";
            if (getInSimUI) ret = "citydriving/profile/getopts?username=" + Username;
            return ret;
        }
        /// <summary>
        /// Gets informations relative to a car
        /// </summary>
        /// <param name="vin">The vehicle unique identifier</param>
        public static string Vehicle(string vin)
        {
            return "citydriving/carinfo?vin=" + vin;
        }
        /// <summary>
        /// Gets informations relative to a car
        /// </summary>
        /// <param name="rows">Number of tickets to request, Pass a 0 if you dont want to use this param</param>
        public static string Tickets(int rows)
        {
            string ret = "citydriving/stats/last_tickets?";
            // 150 is perfect
            if (rows < 0)
            {
                ret += "&rows=" + rows;
            }
            return ret;
        }
        /// <summary>
        /// Gets cars listed for sale
        /// </summary>
        /// <param name="carCode">LFS car code eg. UF1</param>
        public static string Market(string carCode = "")
        {
            string ret = "citydriving/market/getlist?";
            if (carCode != "" && carCode.Length == 3) { ret += "type=" + carCode; }
            return ret;
        }
        static string Sanitize(string Nickname)
        {
            return new System.Text.RegularExpressions.Regex(@"\^[0-9]").Replace(Nickname, string.Empty);
        }
    }

Obtaining Json.NET
I recommend to anyone coding C# to use Visual Studio (A cross platform IDE), its just a matter of executing a command in the NuGeT Package manager console to obtain the library: Install-Package Newtonsoft.Json
You do not need to download, drag and drop or link with the compiler. the program does it for you!

Usage
Add the classes "tcApi" and "Actions" to your project

Make new instance of the class tcApi
Use the method "getData" and pass in a string from the Action class, a deserialized dynamic object will be returned.

Code:
tcApi api = new tcApi();
var data = api.getData(Actions.Profile("racerss", true));
Console.WriteLine("Travelled Distance: " + data["stats"]["drivenDistance"]);



NOTE: I covered the most important end points, rest will be added soon.
Always refer to the documentation for the exact key value pairs property names as they are case sensitive, it is also important to surround your code with try {} and catch {} to avoid possible errors / crashes (eg: a mispelled username).


RE: A quick start with C# - Kyle - 2017-08-29 07:27

Not like I understand any of this, but it looks nice! Good work Biggrin


RE: A quick start with C# - Pete - 2017-08-29 11:39

For a fetching function, like the examples already in the docs, that function should do all of the query building.

All of the necessary data to run the request should be passed in to your main tcApi class. In your example you end up building the query strings in your helper class code, outside of the fetching function.

The examples in the docs take in a very basic object which contains all of the necessary data to make the request, for example the config in the JS example:

Code:
var key = 'YOUR_KEY';
var request = {
  api: 'citydriving/profile/get',
  params: {
    key: key,
    username: 'USERNAME'
  }
};

Your example should do the same so that the user of your function does not need to build query strings in order to make requests for data.

If you'd like to have a go at making your main request function as fully featured as the PHP and JS examples in the docs, I can add it to that.