Esempio n. 1
0
 /// <summary>
 /// Handles the response from the server.
 /// </summary>
 /// <typeparam name="Y">Response type</typeparam>
 /// <param name="command">Command</param>
 /// <param name="ctor">Response object constructor</param>
 /// <param name="taskSource">Task source</param>
 /// <returns>Action<string></returns>
 private Action <string> ResponseMessageHandler <Y>(APICommand command, Func <string, Y> ctor, TaskCompletionSource <Y> taskSource) where Y : APIResponse
 {
     return((message) =>
     {
         try
         {
             taskSource.SetResult(ctor(message));
         }
         catch (APIErrorResponseException ex)
         {
             if (ShouldRedirect(command, ex))
             {
                 var loginContext = new LoginContext(taskSource as TaskCompletionSource <LoginResponse>, ex, command, ctor as Func <string, LoginResponse>);
                 ExecuteRedirect(loginContext);
             }
             else if (IsRedirectRecord(ex.ErrorResponse))
             {
                 taskSource.SetException(new APICommunicationException("Too many redirects."));
             }
             else
             {
                 taskSource.SetException(ex);
             }
         }
     });
 }
        void Login(APICommand command)
        {
            var    form   = (command.Params[0] as LoginningForm);
            object player = null;

            try
            { player = database.ReadObject(form.Login); }
            catch
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Wrong login")));
                return;
            }
            if ((player is PlayerInfo))
            {
                if ((player as PlayerInfo).Password == form.Password)
                {
                    if (ContinueSession(player as PlayerInfo))
                    {
                        SendObject(new APIAnswer(command, (player as PlayerInfo).SharedPlayerInfo));
                    }
                    else
                    {
                        SendObject(new APIAnswer(command, null, new ArgumentException("Already in game")));
                    }
                }
                else
                {
                    SendObject(new APIAnswer(command, null, new ArgumentException("Wrong password")));
                }
            }
            else
            {
                throw new ArgumentException("Wrong database answer");
            }
        }
Esempio n. 3
0
        public IEnumerator Request(APICommand c, System.Action <string> callback)
        {
            Debug.Log("Doing action");
            UnityWebRequest www = UnityWebRequest.Get(ConstructURL(c));

            if (loginCredential != null)
            {
                string cookiehead = "PHPSESSID=" + loginCredential;
                www.SetRequestHeader("Cookie", cookiehead);
            }

            yield return(www.SendWebRequest());

            // if we have an error, log it
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            // if not, show what we got in the request
            else
            {
                string sensorXml = www.downloadHandler.text;
                Debug.Log(sensorXml);
                callback(sensorXml);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Resets redirect counter if needed.
 /// </summary>
 /// <typeparam name="Y">Response type</typeparam>
 /// <param name="command">Command</param>
 /// <param name="taskSource">Task source</param>
 private void ResetRedirectCounterIfFirstLoginTry <Y>(APICommand command, TaskCompletionSource <Y> taskSource)
 {
     if (command is LoginCommand && taskSource == null)
     {
         redirectCounter = 0;
     }
 }
Esempio n. 5
0
        void MakeMove(APICommand command)
        {
            var f = command.Params[0] as CardSessionShared.SessionCommand;

            if (player == null)
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Not loginned"))); return;
            }
            var s = player.Sessions.Find(q => q.ID == f.SessionID);

            if (s == null)
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Not my session"))); return;
            }
            try
            {
                if (f.TargetPosition == null)
                {
                    player.MakeMove(f.SessionID, f.SubjectID, f.Command, new object[] { f.TargetID });
                }
                else
                {
                    player.MakeMove(f.SessionID, f.SubjectID, f.Command, new object[] { f.TargetPosition });
                }
            }
            catch (Exception e) { SendObject(new APIAnswer(command, null, new ArgumentException(e.Message))); }
        }
Esempio n. 6
0
        public APICommand ListNode(int jobID)
        {
            var p = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("jobID", jobID.ToString())
            };

            var c = new APICommand("listNode", p);

            return(c);
        }
Esempio n. 7
0
        public APICommand ListSensor(int nodeID)
        {
            var p = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("nodeID", nodeID.ToString())
            };

            var c = new APICommand("listSensor", p);

            return(c);
        }
Esempio n. 8
0
 void LoadPlayerInfo(APICommand command)
 {
     supervisor.FindPlayer((string)command.Params[0], out Pair <CardEnvironmentShared.PlayerInfo, bool> p, out Exception e);
     if (e != null)
     {
         SendObject(new APIAnswer(command, null, e));
     }
     else if (p != null)
     {
         SendObject(new APIAnswer(command, p));
     }
 }
Esempio n. 9
0
        public APICommand LoginUser(string user, string pass)
        {
            var p = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("user_username", user),
                new KeyValuePair <string, string>("user_password", pass)
            };

            var c = new APICommand("login", p);

            return(c);
        }
Esempio n. 10
0
        public APICommand ListSensorData(int sensorID, int startDate, int endDate)
        {
            var p = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("sensorID", sensorID.ToString()),
                new KeyValuePair <string, string>("startDate", startDate.ToString()),
                new KeyValuePair <string, string>("endDate", endDate.ToString())
            };

            var c = new APICommand("listSensorData", p);

            return(c);
        }
Esempio n. 11
0
    public bool AddCommand(string methodName, params object[] arguments)
    {
        if (APICommandBuffer.GetAPIMethod(methodName) == null)
        {
            return(false);
        }

        APICommand command = new APICommand(methodName, arguments);

        _Commands.Add(command);

        return(true);
    }
Esempio n. 12
0
        void LoadNewDeck(APICommand command)
        {
            var f = command.Params[0] as CardEnvironmentShared.PlayerInfo;

            if (player == null)
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Not loginned")));
            }
            else
            {
                player.PlayerInfo.SharedPlayerInfo.CurrentSoliders = f.CurrentSoliders;
                player.PlayerInfo.SharedPlayerInfo.CurrentSpells   = f.CurrentSpells;
            }
        }
Esempio n. 13
0
    public void LoginUser(string username, string password)
    {
        APICommand c = api.Login(username, password);

        StartCoroutine(api.Request(c, s =>
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(s);

            XmlNodeList elemList = xDoc.GetElementsByTagName("PHPSESSID");
            if (elemList[0] != null)
            {
                api.LoginCredential = (elemList[0].InnerXml);
            }
        }));
    }
Esempio n. 14
0
    /// <summary>
    /// Send one command to the source
    /// </summary>
    /// <param name="litterRobotId"></param>
    /// <param name="command"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    private async Task <object?> SendAsync(string litterRobotId, string command,
                                           CancellationToken cancellationToken = default)
    {
        this.Logger.LogDebug("Started sending command {command} - {litterRobotId} to LR", command, litterRobotId);

        var(userid, token) = await this.LoginAsync(cancellationToken);

        var baseUrl    = string.Format(COMMANDURL, userid, litterRobotId);
        var apiCommand = new APICommand {
            command = command, litterRobotId = litterRobotId,
        };
        var request = this.Request(HttpMethod.Post, baseUrl, apiCommand, token);
        var resp    = await this.Client.SendAsync(request, cancellationToken);

        resp.EnsureSuccessStatusCode();

        this.Logger.LogDebug("Finished sending command {command} - {litterRobotId} to LR", command, litterRobotId);
        return(new object());
    }
Esempio n. 15
0
        void Registration(APICommand command)
        {
            if (player != null)
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Already loginned")));
            }
            var f = (LoginningForm)command.Params[0];

            supervisor.Registration(f.Login, f.Password, out player, out Exception e);
            if (e != null)
            {
                SendObject(new APIAnswer(command, null, e));
            }
            else if (player != null)
            {
                SendObject(new APIAnswer(command, player.PlayerInfo.SharedPlayerInfo));
            }
            player.Send = SendObject;
        }
Esempio n. 16
0
        /// <summary>
        /// Executes command.
        /// </summary>
        /// <typeparam name="Y">Response type</typeparam>
        /// <param name="command">Command</param>
        /// <param name="ctor">Response object constructor</param>
        /// <param name="taskSource">Task source</param>
        /// <returns>Task<Response type></returns>
        public Task <Y> Execute <Y>(APICommand command, Func <string, Y> ctor, TaskCompletionSource <Y> taskSource = null) where Y : APIResponse
        {
            if (taskSource == null)
            {
                taskSource = new TaskCompletionSource <Y>();
            }

            ResetRedirectCounterIfFirstLoginTry(command, taskSource);

            try
            {
                Send(command.JSONString, ResponseMessageHandler <Y>(command, ctor, taskSource));
            }
            catch (APICommunicationException ex)
            {
                taskSource.SetException(ex);
            }

            return(taskSource.Task);
        }
Esempio n. 17
0
        void Registration(APICommand command)
        {
            var form = (command.Params[0] as LoginningForm);

            if (database.Exists(form.Login))
            {
                SendObject(new APIAnswer(command, null, new ArgumentException("Login in use")));
            }
            else
            {
                var player = new PlayerInfo(new CardCollectiveSharedSide.PlayerInfo()
                {
                    Name = form.Login
                }, form.Password);

                database.WriteObject(form.Login, player);

                ContinueSession(player);
            }
        }
Esempio n. 18
0
 void LoadChats(APICommand command)
 {
 }
Esempio n. 19
0
 private string ConstructURL(APICommand c)
 {
     return(apiURL + c.ToString());
 }
Esempio n. 20
0
        public APICommand Logout()
        {
            var c = new APICommand("logout", null);

            return(c);
        }
Esempio n. 21
0
 void LoadPlayerInfo(APICommand command)
 {
 }
Esempio n. 22
0
 void StartSession(APICommand command)
 {
 }
Esempio n. 23
0
 void MakeMove(APICommand command)
 {
 }
Esempio n. 24
0
 void LeaveSession(APICommand command)
 {
 }
Esempio n. 25
0
 public APIJob(APICommand command, Action<object, List<string>> callback, params object[] arguments)
 {
     this.Command = command;
     this.Callback = callback;
     this.Parameters = arguments;
 }
Esempio n. 26
0
 void StartChat(APICommand command)
 {
 }
Esempio n. 27
0
 void LeaveChat(APICommand command)
 {
 }
Esempio n. 28
0
 /// <summary>
 /// Verifies if should redirect.
 /// </summary>
 /// <param name="command">Command</param>
 /// <param name="ex">Exception</param>
 /// <returns>Bool</returns>
 private bool ShouldRedirect(APICommand command, APIErrorResponseException ex)
 {
     return(command is LoginCommand && redirectCounter <= MAX_REDIRECT_NUMBER && IsRedirectRecord(ex.ErrorResponse));
 }
Esempio n. 29
0
 void MakeMessageInChat(APICommand command)
 {
 }
Esempio n. 30
0
 void Unlogin(APICommand command)
 {
 }
Esempio n. 31
0
 public APIJob(APICommand command, Action<object, List<string>> action)
 {
     // TODO: Complete member initialization
     this.Command = command;
     this.Callback = action;
 }
Esempio n. 32
0
 void LoadSessions(APICommand command)
 {
 }