public static void GetBuildings(Session session, string bodyID)
        {
            JsonTextWriter req = session.Request("get_buildings",
                                           session.cache.session_id,
                                           bodyID);

              if (session.Post(URL,req) == 0)
              {
            Response.Result result = session.response.result as Response.Result;

            IEnumerator b = result.buildings.GetEnumerator();

            Infrastructure buildings = new Infrastructure();

            buildings.date = result.status.server.time;  // save server time

            while (b.MoveNext())
            {
              KeyValuePair<string,Response.Result.Building> k =
            (KeyValuePair<string,Response.Result.Building>) b.Current;

              k.Value.id = k.Key;

              buildings.Add(k.Value);
            }

            string filename = String.Format("{0}.xml",result.status.body.name);

            XmlSerializer serializer = new XmlSerializer(typeof(Infrastructure));
            Stream stream = new FileStream(filename,FileMode.Create);
            serializer.Serialize(stream,buildings);
            stream.Close();
              }
        }
        public static void GetBuildable(Session session, string bodyID, string x, string y, string tag)
        {
            JsonTextWriter req = session.Request("get_buildable",
                                           session.cache.session_id,
                                           bodyID,x,y,tag);

              session.Post(URL,req);
        }
        public static void Abandon(Session session, string bodyID)
        {
            JsonTextWriter req = session.Request("abandon",
                                           session.cache.session_id,
                                           bodyID);

              session.Post(URL,req);
        }
        public static void AssembleGlyphs(Session session, string buildingID, params string [] ids)
        {
            JsonTextWriter req = session.Request("assemble_glyphs",
                                                 session.cache.session_id,
                                                 buildingID);
            req.WriteStartArray ();
            foreach (string id in ids)
                req.WriteString(id);

            session.Post(URL,req);
        }
        public static void Rename(Session session, string bodyID, string name)
        {
            JsonTextWriter req = session.Request("rename",
                                           session.cache.session_id,
                                           bodyID,name);

              session.Post(URL,req);
        }
        public static void GetStatus(Session session, string bodyID)
        {
            JsonTextWriter req = session.Request("view",
                                           session.cache.session_id,
                                           bodyID);

              session.Post(URL,req);
        }
        public static void Main(string[] args)
        {
            Session session = new Session("Empire Server","https://us1.lacunaexpanse.com");

              //Session session = new Session("Empire Server",null);

            Console.WriteLine(DateTime.Now.ToString("dd MM yyyy HH:mm:ss zzzz"));
            Console.WriteLine(session.cache.time); // 13 02 2012 14:55:58 +0000

            DateTime date;

            DateTime.TryParseExact(session.cache.time,
                                   "dd MM yyyy HH:mm:ss zzzz",
                                   CultureInfo.InvariantCulture,
                                   DateTimeStyles.AssumeLocal,out date);

            Console.WriteLine(date.ToString("dd MM yyyy HH:mm:ss zzzz"));

            if (session.cache.empire_name == null)
              session.cache.empire_name = "name";

            if (session.cache.password == null)
              session.cache.password = "******";

            timer = new System.Timers.Timer(1000); // Setup timer with one second interval
            timer.Elapsed += TimerTick;
            timer.Start();

            start = DateTime.Now;

            // If we already have an active session on server, don't bother with login
            if ((DateTime.Now-date).TotalHours > 2)
            session.Login(session.cache.empire_name, session.cache.password);

              Body.GetBuildings(session,session.cache.home_planet_id);

            session.Close();
            session.Save();
        }
        public static void BuildHalls(Session session, string buildingID, int save)
        {
            int count,total=0;

            foreach (string [] glyph in HallRecipe)
            {
                count = Math.Min(Math.Min(((Stack<string>) glyphs[glyph[0]]).Count,
                                      	((Stack<string>) glyphs[glyph[1]]).Count),
              	           	 Math.Min(((Stack<string>) glyphs[glyph[2]]).Count,
                                      	((Stack<string>) glyphs[glyph[3]]).Count));

                for (int i = count; i > save; i--) {
                    JsonTextWriter req = session.Request("assemble_glyphs",
                                                     session.cache.session_id,
                                                     buildingID);
                    req.WriteStartArray ();

                    for (int j=0; j < glyph.Length; j++)
                        req.WriteString(((Stack<string>) glyphs[glyph[j]]).Pop());

                    if (session.Post(URL,req) != 0) break;

                    total++;
                }
            }

            Console.WriteLine("\n{0} hall(s) assembled!",total);

              IEnumerator pEnum = pType.GetEnumerator();

              foreach (string [] recipe in HallRecipe)
              {
            Console.WriteLine();
            foreach (string glyph in recipe)
            {
              pEnum.MoveNext();
              Console.WriteLine("{0,3:D} {1}-{2}",
                            ((Stack<string>) glyphs[glyph]).Count,
                            pEnum.Current.ToString(),glyph);
            }
              }
        }
        public static void GetGlyphs(Session session, string buildingID)
        {
            JsonTextWriter req = session.Request("get_glyphs",
                                                 session.cache.session_id,
                                                 buildingID);

            //session.onData += ProcessResult;

            if (session.Post(URL,req) == 0) {
            //Next two lines perform same thing old ProcessResult did
            foreach (Result.Glyph glyph in session.response.result.glyphs)
              ((Stack<string>) (glyphs[glyph.type])).Push(glyph.id);

            IEnumerator pEnum = pType.GetEnumerator();
                foreach (string [] recipe in HallRecipe)
                {
                    Console.WriteLine();
                    foreach (string glyph in recipe)
              {
            pEnum.MoveNext();
            Console.WriteLine("{0,3:D} {1}-{2}",
                            ((Stack<string>) glyphs[glyph]).Count,
                            pEnum.Current.ToString(),glyph);
              }
                }

                int count = 0;

            foreach (string [] glyph in HallRecipe)
                    count += Math.Min(Math.Min(((Stack<string>) glyphs[glyph[0]]).Count,
                                           	((Stack<string>) glyphs[glyph[1]]).Count),
                                  	Math.Min(((Stack<string>) glyphs[glyph[2]]).Count,
                                           	((Stack<string>) glyphs[glyph[3]]).Count));

            Console.WriteLine("\nYou can build {0} hall(s)",count);
            }
            //session.onData -= ProcessResult;
        }