/// <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"); } }
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); } }
/// <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; } }
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))); } }
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); }
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); }
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)); } }
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); }
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); }
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); }
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; } }
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); } })); }
/// <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()); }
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; }
/// <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); }
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); } }
void LoadChats(APICommand command) { }
private string ConstructURL(APICommand c) { return(apiURL + c.ToString()); }
public APICommand Logout() { var c = new APICommand("logout", null); return(c); }
void LoadPlayerInfo(APICommand command) { }
void StartSession(APICommand command) { }
void MakeMove(APICommand command) { }
void LeaveSession(APICommand command) { }
public APIJob(APICommand command, Action<object, List<string>> callback, params object[] arguments) { this.Command = command; this.Callback = callback; this.Parameters = arguments; }
void StartChat(APICommand command) { }
void LeaveChat(APICommand command) { }
/// <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)); }
void MakeMessageInChat(APICommand command) { }
void Unlogin(APICommand command) { }
public APIJob(APICommand command, Action<object, List<string>> action) { // TODO: Complete member initialization this.Command = command; this.Callback = action; }
void LoadSessions(APICommand command) { }