Exemplo n.º 1
0
        protected override void challengerHandler(UserThread client, CSMessage msg)
        {
            try
            {
                ServerThread to;
                CSUser       user = (CSUser)msg.get(MessageType.USER);

                if ((to = findClient(user)) != null && !to.playing())
                {
                    if (msg.get(MessageType.REQUEST) != null)
                    {
                        if (!user.Equals(client.To) && !client.Froms.Contains(user))
                        {
                            abortRequest(client, client.ClientUser);
                            client.To = to.ClientUser;
                            to.Froms.Add(client.ClientUser);
                            msg.add(MessageType.USER, client.ClientUser);
                            to.send(msg);
                        }
                    }
                    else if (msg.get(MessageType.RESPONSE) != null)
                    {
                        if (client.Froms.Contains(user) && to.To.Equals(client.ClientUser))
                        {
                            abortRequest(client, client.ClientUser);
                            client.To = null;
                            client.Froms.Remove(to.ClientUser);
                            to.To = null;
                            foreach (CSUser u in client.Froms)
                            {
                                abortRequest(u, client.ClientUser);
                            }
                            foreach (CSUser u in to.Froms)
                            {
                                abortRequest(u, to.ClientUser);
                            }
                            client.Froms.Clear();
                            to.Froms.Clear();

                            beginGame((ServerThread)client, to);
                        }
                    }
                    else if (msg.get(MessageType.ABORT) != null)
                    {
                        if (user.Equals(client.To))
                        {
                            client.To = null;
                            to.Froms.Remove(client.ClientUser);
                        }
                        else
                        {
                            to.To = null;
                            client.Froms.Remove(user);
                        }
                        msg.add(MessageType.USER, client.ClientUser);
                        to.send(msg);
                    }
                }
                else
                {
                    msg.clear();
                    msg.Type = MessageType.CHALLENGING;
                    msg.add(MessageType.ABORT, true).add(MessageType.USER, user);
                    client.To = null;
                    client.Froms.Remove(user);
                    client.send(msg);
                }
            }
            catch (Exception e)
            {
                log(Severiry.ERROR, "Controller[PARTY] - " + e.Message);
            }
        }
Exemplo n.º 2
0
        protected override void loginHandler(UserThread client, CSMessage msg)
        {
            try
            {
                CSUser user     = (CSUser)msg.get(MessageType.USER);
                CSUser tempUser = new CSUser(client.ClientUser.Login);
                bool   error    = true;

                if (msg.get(MessageType.REGISTRATION) != null)
                {
                    msg.clear();
                    if (manager.exist(user.Login))
                    {
                        msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Login '" + user.Login + "' already exists !");
                    }
                    else
                    {
                        manager.create(user);
                        if (view != null && view.isOpened())
                        {
                            view.update(client.ClientUser.Login, user);
                        }
                        client.ClientUser.Login = user.Login;
                        msg.add(MessageType.STATE, true).add(MessageType.USER, client.ClientUser);
                        error = false;
                    }
                }
                else if (findClient(user) == null)
                {
                    msg.clear();
                    if ((user = manager.find(user)) != null)
                    {
                        client.ClientUser.Points    = user.Points;
                        client.ClientUser.NbParties = user.NbParties;
                        if (view != null && view.isOpened())
                        {
                            view.update(client.ClientUser.Login, user);
                        }
                        client.ClientUser.Login = user.Login;
                        msg.add(MessageType.STATE, true).add(MessageType.USER, client.ClientUser);
                        error = false;
                    }
                    else
                    {
                        msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Incorrect login or password !");
                    }
                }
                else
                {
                    msg.add(MessageType.STATE, false).add(MessageType.ERROR, "Login already connected !");
                }

                if (!error)
                {
                    abortRequest(client, tempUser);
                    foreach (CSUser u in client.Froms)
                    {
                        abortRequest(u, tempUser);
                    }
                    client.To = null;
                    client.Froms.Clear();
                    CSMessage clearmsg = new CSMessage(MessageType.CLEAR);
                    client.send(clearmsg);
                }

                client.send(msg);
                if (!error)
                {
                    broadcast();
                }
            }
            catch (Exception e)
            {
                log(Severiry.ERROR, "Manager[" + manager + "] - " + e.Message);
            }
        }