massive checkpoint
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
using System;
|
||||
using Gameboard.ShogiUI.Sockets.Models;
|
||||
using System;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
{
|
||||
public class UserDocument : CouchDocument
|
||||
{
|
||||
public enum LoginPlatform
|
||||
{
|
||||
Microsoft,
|
||||
Guest
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public LoginPlatform Platform { get; set; }
|
||||
public WhichLoginPlatform Platform { get; set; }
|
||||
/// <summary>
|
||||
/// The browser session ID saved via Set-Cookie headers.
|
||||
/// Only used with guest accounts.
|
||||
@@ -31,8 +27,8 @@ namespace Gameboard.ShogiUI.Sockets.Repositories.CouchModels
|
||||
Name = name;
|
||||
WebSessionId = webSessionId;
|
||||
Platform = WebSessionId.HasValue
|
||||
? LoginPlatform.Guest
|
||||
: LoginPlatform.Microsoft;
|
||||
? WhichLoginPlatform.Guest
|
||||
: WhichLoginPlatform.Microsoft;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,12 +7,13 @@ using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
{
|
||||
public interface IGameboardRepository
|
||||
{
|
||||
Task<bool> CreateBoardState(string sessionName, Models.Shogi shogi);
|
||||
Task<bool> CreateBoardState(Models.Session session);
|
||||
Task<bool> CreateSession(Models.SessionMetadata session);
|
||||
Task<bool> CreateUser(Models.User user);
|
||||
Task<IList<Models.SessionMetadata>> ReadSessionMetadatas();
|
||||
@@ -46,11 +47,16 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
var content = new StringContent(JsonConvert.SerializeObject(q), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync("_find", content);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
var sessions = JsonConvert.DeserializeObject<CouchFindResult<SessionDocument>>(responseContent).docs;
|
||||
var results = JsonConvert.DeserializeObject<CouchFindResult<SessionDocument>>(responseContent);
|
||||
if (results != null)
|
||||
{
|
||||
return results
|
||||
.docs
|
||||
.Select(s => new Models.SessionMetadata(s.Name, s.IsPrivate, s.Player1, s.Player2))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return sessions
|
||||
.Select(s => new Models.SessionMetadata(s.Name, s.IsPrivate, s.Player1, s.Player2))
|
||||
.ToList();
|
||||
return new List<Models.SessionMetadata>(0);
|
||||
}
|
||||
|
||||
public async Task<Models.Session?> ReadSession(string name)
|
||||
@@ -113,9 +119,12 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
return new Models.Shogi(moves);
|
||||
}
|
||||
|
||||
public async Task<bool> CreateBoardState(string sessionName, Models.Shogi shogi)
|
||||
/// <summary>
|
||||
/// Saves a snapshot of board state and the most recent move.
|
||||
/// </summary>
|
||||
public async Task<bool> CreateBoardState(Models.Session session)
|
||||
{
|
||||
var boardStateDocument = new BoardStateDocument(sessionName, shogi);
|
||||
var boardStateDocument = new BoardStateDocument(session.Name, session.Shogi);
|
||||
var content = new StringContent(JsonConvert.SerializeObject(boardStateDocument), Encoding.UTF8, ApplicationJson);
|
||||
var response = await client.PostAsync(string.Empty, content);
|
||||
return response.IsSuccessStatusCode;
|
||||
@@ -198,14 +207,23 @@ namespace Gameboard.ShogiUI.Sockets.Repositories
|
||||
|
||||
public async Task<Models.User?> ReadUser(string userName)
|
||||
{
|
||||
var document = new UserDocument(userName);
|
||||
var response = await client.GetAsync(document.Id);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
if (response.IsSuccessStatusCode)
|
||||
try
|
||||
{
|
||||
var user = JsonConvert.DeserializeObject<UserDocument>(responseContent);
|
||||
var document = new UserDocument(userName);
|
||||
var uri = new Uri(client.BaseAddress!, HttpUtility.UrlEncode(document.Id));
|
||||
var response = await client.GetAsync(HttpUtility.UrlEncode(document.Id));
|
||||
var response2 = await client.GetAsync(uri);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var user = JsonConvert.DeserializeObject<UserDocument>(responseContent);
|
||||
|
||||
return new Models.User(user.Name);
|
||||
return new Models.User(user.Name);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user