Exemplo n.º 1
0
        public void RequestCopySelection(UserInRoomInfo usr)
        {
            if (_serverConnection != null && _connectionStatus == ConnectionStatus.SignedIn && usr.CanBeFollowed && usr.SelectionCount > 0)
            {
                //send request
                _currentCopySelectionRequestID = Guid.NewGuid().ToString("N");

                ChatMessage msg = new ChatMessage();
                msg.Name = "reqsel";
                msg.Parameters.Add("reqid", _currentCopySelectionRequestID);
                msg.Parameters.Add("clientid", usr.ID);
                SendMessage(msg);
            }
        }
Exemplo n.º 2
0
        void _serverConnection_DataReceived(object sender, EventArgs e)
        {
            _context.Post(new SendOrPostCallback(delegate(object state)
            {
                try
                {
                    if (_serverConnection != null)
                    {
                        byte[] data = _serverConnection.ReadData();
                        while (data != null)
                        {
                            //parse data
                            ChatMessage msg = ChatMessage.Parse(data);
                            if (msg != null)
                            {
                                //handle data
                                if (msg.Name == "signinfailed")
                                {
                                    closeConnection();
                                    break;
                                }
                                else if (msg.Name == "signinsuccess")
                                {
                                    _retryCount          = 0;
                                    ChatConnectionStatus = ConnectionStatus.SignedIn;
                                }
                                else if (msg.Name == "txt")
                                {
                                    if (NewTextMessage != null)
                                    {
                                        NewTextMessage(this, msg.ID, msg.Parameters["msg"], Color.FromArgb(int.Parse(msg.Parameters["color"])));
                                    }
                                    if (Core.Settings.Default.ChatPlaySounds && _sndMessage != null)
                                    {
                                        _sndMessage.Play();
                                    }
                                }
                                else if (msg.Name == "usersinroom")
                                {
                                    bool newUser = false;
                                    int i        = 0;
                                    foreach (var x in UsersInRoomList)
                                    {
                                        x.present = false;
                                    }
                                    bool wasempty = UsersInRoomList.Count == 0;
                                    while (msg.Parameters.ContainsKey(string.Format("name{0}", i)))
                                    {
                                        string id        = msg.Parameters[string.Format("id{0}", i)];
                                        UserInRoomInfo c = (from x in UsersInRoomList where x.ID == id select x).FirstOrDefault();
                                        if (c == null)
                                        {
                                            c                = new UserInRoomInfo();
                                            c.ID             = id;
                                            c.Username       = msg.Parameters[string.Format("name{0}", i)];
                                            c.present        = true;
                                            c.FollowThisUser = false;
                                            c.SelectionCount = -1;
                                            if (msg.Parameters.ContainsKey(string.Format("sc{0}", i)))
                                            {
                                                string selCount = msg.Parameters[string.Format("sc{0}", i)];
                                                if (!string.IsNullOrEmpty(selCount))
                                                {
                                                    c.SelectionCount = int.Parse(selCount);
                                                }
                                            }
                                            c.CanBeFollowed  = bool.Parse(msg.Parameters[string.Format("cbf{0}", i)]);
                                            c.ActiveGeocache = msg.Parameters[string.Format("agc{0}", i)];
                                            UsersInRoomList.Add(c);
                                            if (!wasempty)
                                            {
                                                if (NewTextMessage != null)
                                                {
                                                    NewTextMessage(this, "-1", string.Format("{0} {1}", c.Username, Localization.TranslationManager.Instance.Translate("JoinedTheRoom") as string), Color.Black);
                                                }
                                            }
                                            newUser = true;
                                        }
                                        else
                                        {
                                            bool cbf         = c.CanBeFollowed;
                                            string agc       = c.ActiveGeocache;
                                            c.CanBeFollowed  = bool.Parse(msg.Parameters[string.Format("cbf{0}", i)]);
                                            c.ActiveGeocache = msg.Parameters[string.Format("agc{0}", i)];
                                            c.present        = true;
                                            c.UpdateText();
                                            if (cbf != c.CanBeFollowed || agc != c.ActiveGeocache)
                                            {
                                                if (UserInfoUpdate != null)
                                                {
                                                    UserInfoUpdate(this, c);
                                                }
                                            }
                                        }
                                        i++;
                                    }
                                    var rmList = from a in UsersInRoomList where !a.present select a;
                                    foreach (var x in rmList)
                                    {
                                        UsersInRoomList.Remove(x);
                                    }

                                    if (newUser && Core.Settings.Default.ChatPlaySounds && _sndWelcome != null)
                                    {
                                        _sndWelcome.Play();
                                    }
                                }
                                else if (msg.Name == "follow")
                                {
                                    UserInRoomInfo[] curUsr = UsersInRoomList.ToArray();
                                    UserInRoomInfo c        = (from x in curUsr where x.ID == msg.ID select x).FirstOrDefault();
                                    if (c != null)
                                    {
                                        c.CanBeFollowed  = bool.Parse(msg.Parameters["canfollow"]);
                                        c.ActiveGeocache = msg.Parameters["cache"];
                                        c.SelectionCount = -1;
                                        if (msg.Parameters.ContainsKey("selected"))
                                        {
                                            string selCount = msg.Parameters["selected"];
                                            if (!string.IsNullOrEmpty(selCount))
                                            {
                                                c.SelectionCount = int.Parse(selCount);
                                            }
                                        }

                                        if (c.FollowThisUser)
                                        {
                                            if (!c.CanBeFollowed)
                                            {
                                                c.FollowThisUser = false;
                                            }
                                        }
                                        c.UpdateText();
                                        if (UserInfoUpdate != null)
                                        {
                                            UserInfoUpdate(this, c);
                                        }
                                    }
                                }
                                else if (msg.Name == "rooms")
                                {
                                    RoomInfo[] curRms = RoomList.ToArray();
                                    foreach (var x in curRms)
                                    {
                                        x.present = false;
                                    }
                                    int i = 0;
                                    while (msg.Parameters.ContainsKey(string.Format("name{0}", i)))
                                    {
                                        string name = msg.Parameters[string.Format("name{0}", i)];
                                        RoomInfo c  = (from x in curRms where x.Name == name select x).FirstOrDefault();
                                        if (c == null)
                                        {
                                            c         = new RoomInfo();
                                            c.present = true;
                                            c.Name    = name;
                                            RoomList.Add(c);
                                        }
                                        else
                                        {
                                            c.present = true;
                                        }
                                        i++;
                                    }
                                    var rmList = from a in RoomList where !a.present select a;
                                    foreach (var x in rmList)
                                    {
                                        RoomList.Remove(x);
                                    }
                                }
                                else if (msg.Name == "reqsel")
                                {
                                    StringBuilder sb        = new StringBuilder();
                                    UserInRoomInfo[] curUsr = UsersInRoomList.ToArray();
                                    UserInRoomInfo c        = (from x in curUsr where x.ID == msg.ID select x).FirstOrDefault();
                                    if (c != null)
                                    {
                                        if (NewTextMessage != null)
                                        {
                                            NewTextMessage(this, "-1", string.Format("{0} {1}", c.Username, Localization.TranslationManager.Instance.Translate("RequestedSelection") as string), Color.Black);
                                        }
                                        if (Core.ApplicationData.Instance.ActiveDatabase != null)
                                        {
                                            var gsel = from Core.Data.Geocache g in Core.ApplicationData.Instance.ActiveDatabase.GeocacheCollection where g.Selected select g;
                                            foreach (var g in gsel)
                                            {
                                                sb.AppendFormat("{0},", g.Code);
                                            }
                                        }
                                    }

                                    ChatMessage bmsg = new ChatMessage();
                                    bmsg.Name        = "reqselresp";
                                    bmsg.Parameters.Add("reqid", msg.Parameters["reqid"]);
                                    bmsg.Parameters.Add("clientid", msg.ID);
                                    bmsg.Parameters.Add("selection", sb.ToString());

                                    SendMessage(bmsg);
                                }
                                else if (msg.Name == "reqselresp")
                                {
                                    if (msg.Parameters["reqid"] == _currentCopySelectionRequestID && _getGeocacheThread == null)
                                    {
                                        //handle response
                                        Core.Storage.Database db = Core.ApplicationData.Instance.ActiveDatabase;
                                        if (db != null)
                                        {
                                            string[] gcCodes = msg.Parameters["selection"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                                            using (Utils.DataUpdater upd = new Utils.DataUpdater(db))
                                            {
                                                foreach (var g in db.GeocacheCollection)
                                                {
                                                    g.Selected = gcCodes.Contains(g.Code);
                                                }
                                            }
                                            //are we missing geocaches?
                                            _importGeocaches.Clear();
                                            if (Core.Settings.Default.LiveAPIMemberTypeId > 0)
                                            {
                                                foreach (string s in gcCodes)
                                                {
                                                    if (db.GeocacheCollection.GetGeocache(s) == null && s.StartsWith("GC"))
                                                    {
                                                        _importGeocaches.Add(s);
                                                    }
                                                }
                                                if (_importGeocaches.Count > 0)
                                                {
                                                    _copySelectionDb   = db;
                                                    _dataUpdater       = new Utils.DataUpdater(db);
                                                    _getGeocacheThread = new Thread(new ThreadStart(getCopiedSelectionGeocacheInbackgroundMethod));
                                                    _getGeocacheThread.IsBackground = true;
                                                    _getGeocacheThread.Start();
                                                }
                                            }
                                        }
                                    }
                                    _currentCopySelectionRequestID = null;
                                }
                            }
                            data = _serverConnection.ReadData();
                        }
                    }
                }
                catch
                {
                    closeConnection();
                }
            }), null);
        }