Files
Shogi/Gameboard.ShogiUI.Sockets/Repositories/GameboardRepository.cs

132 lines
5.5 KiB
C#

using Gameboard.Shogi.Api.ServiceModels.Messages;
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Gameboard.ShogiUI.Sockets.Repositories
{
public interface IGameboardRepository
{
Task DeleteGame(string gameName);
Task<GetSessionResponse> GetGame(string gameName);
Task<GetSessionsResponse> GetGames();
Task<GetSessionsResponse> GetGames(string playerName);
Task<GetMovesResponse> GetMoves(string gameName);
Task<PostSessionResponse> PostSession(PostSession request);
Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request);
Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request);
Task PostMove(string gameName, PostMove request);
Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName);
Task<GetPlayerResponse> GetPlayer(string userName);
Task<HttpResponseMessage> PostPlayer(PostPlayer request);
}
public class GameboardRepository : IGameboardRepository
{
private const string GetSessionsRoute = "Sessions";
private const string PostSessionRoute = "Session";
private const string JoinSessionRoute = "Session/Join";
private const string PlayerRoute = "Player";
private const string MediaType = "application/json";
private readonly IAuthenticatedHttpClient client;
public GameboardRepository(IAuthenticatedHttpClient client)
{
this.client = client;
}
public async Task<GetSessionsResponse> GetGames()
{
var response = await client.GetAsync(GetSessionsRoute);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
}
public async Task<GetSessionsResponse> GetGames(string playerName)
{
var uri = $"Sessions/{playerName}";
var response = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetSessionsResponse>(json);
}
public async Task<GetSessionResponse> GetGame(string gameName)
{
var uri = $"Session/{gameName}";
var response = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetSessionResponse>(json);
}
public async Task DeleteGame(string gameName)
{
var uri = $"Session/{gameName}";
await client.DeleteAsync(Uri.EscapeUriString(uri));
}
public async Task<PostSessionResponse> PostSession(PostSession request)
{
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync(PostSessionRoute, content);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostSessionResponse>(json);
}
public async Task<PutJoinPublicSessionResponse> PutJoinPublicSession(PutJoinPublicSession request)
{
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PutAsync(JoinSessionRoute, content);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PutJoinPublicSessionResponse>(json);
}
public async Task<PostJoinPrivateSessionResponse> PostJoinPrivateSession(PostJoinPrivateSession request)
{
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
var response = await client.PostAsync(JoinSessionRoute, content);
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
}
public async Task<GetMovesResponse> GetMoves(string gameName)
{
var uri = $"Session/{gameName}/Moves";
var response = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetMovesResponse>(json);
}
public async Task PostMove(string gameName, PostMove request)
{
var uri = $"Session/{gameName}/Move";
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
await client.PostAsync(Uri.EscapeUriString(uri), content);
}
public async Task<PostJoinCodeResponse> PostJoinCode(string gameName, string userName)
{
var uri = $"JoinCode/{gameName}";
var serialized = JsonConvert.SerializeObject(new PostJoinCode { PlayerName = userName });
var content = new StringContent(serialized, Encoding.UTF8, MediaType);
var json = await (await client.PostAsync(Uri.EscapeUriString(uri), content)).Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<PostJoinCodeResponse>(json);
}
public async Task<GetPlayerResponse> GetPlayer(string playerName)
{
var uri = $"Player/{playerName}";
var response = await client.GetAsync(Uri.EscapeUriString(uri));
var json = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GetPlayerResponse>(json);
}
public async Task<HttpResponseMessage> PostPlayer(PostPlayer request)
{
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
return await client.PostAsync(PlayerRoute, content);
}
}
}