public async Task <NetworkResponse <CreateGroupResponse> > CreateGroup(int gameRoom, List <Player> players) { // Concatenate all the player IDs string playerIds = ""; foreach (Player p in players) { playerIds += p.GetId().ToString() + ","; } // Trim the last comma playerIds.Substring(0, playerIds.Length - 1); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), new KeyValuePair <string, string>("room_id", gameRoom.ToString()), new KeyValuePair <string, string>("participants[]", playerIds), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/rooms/{gameRoom}/groups", formContent); return(await NetworkResponse <CreateGroupResponse> .FromHttpResponse(response)); }
public async Task <NetworkResponse <BlockPlayerResponse> > GetBlockList() { string getEndpoint = $"{Url}/api/blocks?session_id={_sessionId}"; HttpResponseMessage response = await _sendRequest(HttpMethod.Get, getEndpoint, null); return(await NetworkResponse <BlockPlayerResponse> .FromHttpResponse(response)); }
/// <summary> /// Gets a list of all ongoing rooms that the user is a member of /// </summary> /// <returns>A list of ongoing game rooms</returns> public async Task <NetworkResponse <GameRoomResponse> > GetOngoingRooms() { string getEndpoint = $"{Url}/api/rooms?session_id={_sessionId}&room_status=ongoing&filter_player=true"; HttpResponseMessage response = await _sendRequest(HttpMethod.Get, getEndpoint, null); return(await NetworkResponse <GameRoomResponse> .FromHttpResponse(response)); }
///////////////////////////////////////////////////////////////// // // Messaging Methods // ///////////////////////////////////////////////////////////////// public async Task <NetworkResponse <GroupMessageListResponse> > GetGroupMessages(int gameRoom, int GroupNumber) { string getEndpoint = $"{Url}/api/rooms/{gameRoom}/groups/{GroupNumber}/messages?session_id={_sessionId}"; HttpResponseMessage response = await _sendRequest(HttpMethod.Get, getEndpoint, null); return(await NetworkResponse <GroupMessageListResponse> .FromHttpResponse(response)); }
/// <summary> /// Gets a list of all of the GameEvents for the specified gameroom /// </summary> /// <param name="gameRoom">The id of the game room to fetch events for.</param> /// <returns>A list of game events</returns> public async Task <NetworkResponse <GameEventResponse> > GetGameEvents(int gameRoom) { string getEndpoint = $"{Url}/api/rooms/{gameRoom}/events?session_id={_sessionId}&filter=tick&filter_arg=0"; HttpResponseMessage response = await _sendRequest(HttpMethod.Get, getEndpoint, null); return(await NetworkResponse <GameEventResponse> .FromHttpResponse(response)); }
public async Task <NetworkResponse <UnblockPlayerResponse> > UnblockPlayer(int BlockId) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Delete, $"{Url}/api/blocks/{BlockId}", formContent); return(await NetworkResponse <UnblockPlayerResponse> .FromHttpResponse(response)); }
///////////////////////////////////////////////////////////////// // // Social Methods // ///////////////////////////////////////////////////////////////// public async Task <NetworkResponse <BlockPlayerResponse> > BlockPlayer(Player player) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), new KeyValuePair <string, string>("other_player_id", player.GetId().ToString()), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/blocks", formContent); return(await NetworkResponse <BlockPlayerResponse> .FromHttpResponse(response)); }
/// <summary> /// Starts a game early if the room's capacity is not filled. /// This action can only be performed by the creator of the lobby. /// </summary> /// <param name="roomId">The room id to start early.</param> /// <returns>the StartLobbyEarlyResponse</returns> public async Task <NetworkResponse <StartLobbyEarlyResponse> > StartLobbyEarly(int roomId) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/rooms/{roomId}/start", formContent); return(await NetworkResponse <StartLobbyEarlyResponse> .FromHttpResponse(response)); }
///////////////////////////////////////////////////////////////// // // Authentication Methods // ///////////////////////////////////////////////////////////////// /// <summary> /// Register a new account. /// </summary> /// <param name="username">The username to register</param> /// <param name="password">The password to register</param> /// <param name="email">The email address to register</param> /// <returns>The RegisterResponse</returns> public async Task <NetworkResponse <RegisterResponse> > RegisterAccount(string username, string password, string email) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password), new KeyValuePair <string, string>("email", email), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, Url + "/api/register", formContent); return(await NetworkResponse <RegisterResponse> .FromHttpResponse(response)); }
public async Task <NetworkResponse <SendMessageResponse> > SendMessage(int gameRoom, int groupId, string message) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), new KeyValuePair <string, string>("message", message), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/rooms/{gameRoom}/groups/{groupId}/messages", formContent); return(await NetworkResponse <SendMessageResponse> .FromHttpResponse(response)); }
/// <summary> /// Static method to create a new Network Response. /// </summary> /// <param name="responseMessage">The HttpResponseMessage that is recieved from the HttpClient</param> /// <returns>A new instance of a NetworkResponse</returns> public static async Task <NetworkResponse <T> > FromHttpResponse(HttpResponseMessage responseMessage) { NetworkResponse <T> response = new NetworkResponse <T>(); response.HttpResponse = responseMessage; // Read the response string responseContent = await responseMessage.Content.ReadAsStringAsync(); response.ResponseContent = responseContent; // Deserialize the response if it was successful. if (responseMessage.IsSuccessStatusCode) { T responseTemplate = default(T); if (responseContent.StartsWith("[")) { // Response is an array, convert it to an object with a key. responseContent = $"{{ 'array': {responseContent} }}"; } // At this point, the JSON deserialization can throw an error. // However, if there is a successful network response but it cannot parse the JSON // then this is a fault of the developer. // Thus, letting the error be thrown here is fine. responseTemplate = JsonConvert.DeserializeObject <T>(responseContent); response.Response = responseTemplate; } else { NetworkError error; try { Console.WriteLine("Request returned error: " + responseContent); error = JsonConvert.DeserializeObject <NetworkError>(responseContent); } catch (JsonException e) { error = new NetworkError(); error.Message = responseContent; } response.ErrorContent = error; } return(response); }
/// <summary> /// Submits a game event to the a game room. Note: The player must be in the game room /// to be able to submit an event to it and the GameEvent must be a valid game event. /// </summary> /// <param name="gameEvent">The GameEvent to submit to the server</param> /// <param name="gameRoom">The id of the game room to submit an event to.</param> /// <returns>The SubmitEventResponse</returns> public async Task <NetworkResponse <SubmitEventResponse> > SubmitGameEvent(GameEvent gameEvent, int gameRoom) { Int32 unixTimestamp = (Int32)(gameEvent.GetTick().GetDate().Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), new KeyValuePair <string, string>("occurs_at", unixTimestamp.ToString()), new KeyValuePair <string, string>("event_msg", '"' + gameEvent.ToJson() + '"'), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/rooms/{gameRoom}/events", formContent); return(await NetworkResponse <SubmitEventResponse> .FromHttpResponse(response)); }
/// <summary> /// Creates a new game lobby /// </summary> /// <param name="title">The title of the lobby</param> /// <param name="max_players">The maximum number of players</param> /// <param name="min_rating">The minimum rating</param> /// <param name="rated">If the game is a ranked game</param> /// <param name="anonymous">If the game is anonymous</param> /// <param name="goal">The goal of the game</param> /// <param name="map">The map that the game is played on</param> /// <returns>the CreateLobbyResponse</returns> public async Task <NetworkResponse <CreateLobbyResponse> > CreateLobby(string title, int maxPlayers, int minRating, bool rated, bool anonymous, string goal, int map) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("session_id", _sessionId), new KeyValuePair <string, string>("description", title), new KeyValuePair <string, string>("max_players", maxPlayers.ToString()), new KeyValuePair <string, string>("min_rating", minRating.ToString()), new KeyValuePair <string, string>("rated", rated ? "1" : "0"), new KeyValuePair <string, string>("anonymity", anonymous ? "1" : "0"), new KeyValuePair <string, string>("goal", goal), new KeyValuePair <string, string>("map", map.ToString()), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, $"{Url}/api/rooms", formContent); return(await NetworkResponse <CreateLobbyResponse> .FromHttpResponse(response)); }
/// <summary> /// Logs the user into the API. If successful, stores the user's session token /// in the Api object so that future requests don't need to accept the token. /// Note: creating a `new Api()` instance will have the user's token persist. /// </summary> /// <param name="username">The user's username</param> /// <param name="password">The user's password</param> /// <returns>The login response</returns> public async Task <NetworkResponse <LoginResponse> > Login(string username, string password) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password), }); HttpResponseMessage response = await _sendRequest(HttpMethod.Post, Url + "/api/login", formContent); NetworkResponse <LoginResponse> loginResponse = await NetworkResponse <LoginResponse> .FromHttpResponse(response); if (loginResponse.IsSuccessStatusCode()) { _sessionId = loginResponse.Response.Token; } return(loginResponse); }