155 lines
5.9 KiB
C#
155 lines
5.9 KiB
C#
using Gameboard.Shogi.Api.ServiceModels.Messages;
|
|
using Gameboard.ShogiUI.Sockets.Models;
|
|
using Gameboard.ShogiUI.Sockets.Repositories.Utility;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Repositories
|
|
{
|
|
public interface IGameboardRepository
|
|
{
|
|
Task DeleteGame(string gameName);
|
|
Task<Session> GetGame(string gameName);
|
|
Task<GetSessionsResponse> GetGames();
|
|
Task<GetSessionsResponse> GetGames(string playerName);
|
|
Task<List<Move>> GetMoves(string gameName);
|
|
Task<string> PostSession(PostSession request);
|
|
Task<string> PostJoinPrivateSession(PostJoinPrivateSession request);
|
|
Task<bool> PutJoinPublicSession(PutJoinPublicSession request);
|
|
Task PostMove(string gameName, PostMove request);
|
|
Task<string> PostJoinCode(string gameName, string userName);
|
|
Task<Player> GetPlayer(string userName);
|
|
Task<bool> 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<Session> GetGame(string gameName)
|
|
{
|
|
var uri = $"Session/{gameName}";
|
|
var response = await client.GetAsync(Uri.EscapeUriString(uri));
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
return null;
|
|
}
|
|
return new Session(JsonConvert.DeserializeObject<GetSessionResponse>(json).Session);
|
|
}
|
|
|
|
public async Task DeleteGame(string gameName)
|
|
{
|
|
var uri = $"Session/{gameName}";
|
|
await client.DeleteAsync(Uri.EscapeUriString(uri));
|
|
}
|
|
|
|
public async Task<string> 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).SessionName;
|
|
}
|
|
|
|
public async Task<bool> 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).JoinSucceeded;
|
|
}
|
|
|
|
public async Task<string> 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();
|
|
var deserialized = JsonConvert.DeserializeObject<PostJoinPrivateSessionResponse>(json);
|
|
if (deserialized.JoinSucceeded)
|
|
{
|
|
return deserialized.SessionName;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public async Task<List<Move>> GetMoves(string gameName)
|
|
{
|
|
var uri = $"Session/{gameName}/Moves";
|
|
var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
|
var json = await get.Content.ReadAsStringAsync();
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
return new List<Move>();
|
|
}
|
|
var response = JsonConvert.DeserializeObject<GetMovesResponse>(json);
|
|
return response.Moves.Select(m => new Move(m)).ToList();
|
|
}
|
|
|
|
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<string> 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).JoinCode;
|
|
}
|
|
|
|
public async Task<Player> GetPlayer(string playerName)
|
|
{
|
|
var uri = $"Player/{playerName}";
|
|
var get = await client.GetAsync(Uri.EscapeUriString(uri));
|
|
var content = await get.Content.ReadAsStringAsync();
|
|
if (!string.IsNullOrWhiteSpace(content))
|
|
{
|
|
var response = JsonConvert.DeserializeObject<GetPlayerResponse>(content);
|
|
return new Player(response.Player.Name);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public async Task<bool> PostPlayer(PostPlayer request)
|
|
{
|
|
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, MediaType);
|
|
var response = await client.PostAsync(PlayerRoute, content);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
}
|
|
}
|