SmartObjects Example


using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using iSteam.Sdk;
using iSteam.Sdk.Data;
using iSteam.Sdk.SmartObjects;

namespace SmartObjectsExample
{
    class Program
    {
        private static iSteam.Sdk.SteamToken _token;
        private static iSteam.Sdk.SmartObjects.iUser _user;
        private static bool processing = false;

        static void Main(string[] args)
        {
            _token = new SteamToken();
            var loginStatus = _token.Authenticate("", "", "");

            if (loginStatus != LoginStatus.LoginSuccessful)
            {
                Console.WriteLine(loginStatus);
                Console.ReadLine();
            }

            _user = new iUser(_token, true);
            _user.UpdatedEvent += new iUser.UpdatedEventHandler(user_UpdatedEvent);
            _user.MessageReceivedEvent += _user_MessageReceivedEvent;
            ReadSmartObjects(_user);
        }

        static void _user_MessageReceivedEvent(object sender, OnMessageReceivedEventArgs e)
        {
            var friend = _user.Friends.Single(x => x.Data.SteamId == e.Message.Origin);
            Console.WriteLine(friend.Data.Nickname + ": " + e.Message.MessageText);
            SendMessage(Console.ReadLine());
        }

        static void user_UpdatedEvent(object sender, EventArgs e)
        {
            if (processing) return;
            ReadSmartObjects(_user);
        }

        static void ReadSmartObjects(iUser user)
        {
            var wStopwatch = new Stopwatch();
            processing = true;
            Console.Clear();
            wStopwatch.Start();
            Console.WriteLine("-------------------------------------------------------------------");
            Console.WriteLine("Nick: " + user.Data.Nickname + "(" + user.Data.Status + ")          -" + ((double)wStopwatch.ElapsedMilliseconds)/1000 + "s");
            Console.WriteLine("-------------------------------------------------------------------");

            Console.WriteLine(" -- Friends -- ");
            foreach (var friend in user.Friends)
            {
                Console.WriteLine("Nick: " + friend.Data.Nickname + "(" + friend.Data.Status + ")          -" + ((double)wStopwatch.ElapsedMilliseconds) / 1000 + "s");
            }

            Console.WriteLine(" -- Groups -- ");
            foreach (var group in user.Groups)
            {
                Console.WriteLine("Name: " + group.Data.Name);
                Console.WriteLine("Membership: " + group.Data.Members);
                Console.WriteLine("Users Online: " + group.Data.UsersOnline + "          -" + ((double)wStopwatch.ElapsedMilliseconds) / 1000 + "s");

                //Console.WriteLine(" ---- Members ---- ");
                //foreach (var member in group.Members)
                //{
                //    Console.WriteLine("Nick: " + member.Data.Nickname + "(" + member.Data.Status + ")          -" + ((double)wStopwatch.ElapsedMilliseconds) / 1000 + "s");
                //}
                //Console.WriteLine(" ---- Members ---- ");
            }
            wStopwatch.Stop();
            processing = false;
            

            SendMessage(Console.ReadLine());
        }

        static void SendMessage(string data)
        {
            string[] args = data.Split(':');

            var friend = _user.Friends.SingleOrDefault(x => x.Data.Nickname == args[0]);
            if (friend == null)
            {
                Console.WriteLine("User does not exist!");
                SendMessage(Console.ReadLine());
            }
            if (friend != null)
                _user.SendMessage(args[1], friend.Data);
            
            SendMessage(Console.ReadLine());
        }
    }
}

Help us by donating!

Donate

Last edited Jan 1, 2013 at 7:12 AM by louis_marais, version 4

Comments

No comments yet.