Skip to content

Commit

Permalink
Get players games
Browse files Browse the repository at this point in the history
  • Loading branch information
kochol committed Aug 12, 2020
1 parent 8f3b8fd commit 617443b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
11 changes: 10 additions & 1 deletion Server/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ public async Task<ActionResult<Player>> Get()
/// Player calls this method to get a lobby ID
/// </summary>
/// <returns></returns>
[HttpGet("lobby")]
[HttpPost("lobby")]
[HttpGet("lobby")]
public async Task<ActionResult<Lobby>> GetLobby()
{
return await LobbyManager.AutoJoin(long.Parse(User.Identity.Name));
}

[HttpPost("games/{offset}/{count}")]
public async Task<ActionResult<List<Game>>> GetGames(int offset, int count)
{
return await DataContext.Games.GetPlayerGames(
long.Parse(User.Identity.Name), offset, count
);
}
}
}
3 changes: 2 additions & 1 deletion Server/Data/DatabaseName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class DatabaseName
{
public static readonly string Players = "0";
public static readonly string Devices = "1";
public static readonly string Games = "2";
public static readonly string Lobbies = "2";
public static readonly string Games = "3";
}
}
40 changes: 39 additions & 1 deletion Server/Data/GameData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MessagePack;
using ArdbSharp;
using MessagePack;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -28,5 +29,42 @@ public async ValueTask AddGame(Game game)
await db.Value.ListRightPushAsync("p:g:" + ps.PlayerId, game.Id);
}
}

public async ValueTask<Game> GetGameById(long game_id)
{
using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Games);

var game_data = await db.Value.StringGetAsync("g:" + game_id);

if (game_data == null)
return null;

return MessagePackSerializer.Deserialize<Game>((byte[])game_data);
}
public async ValueTask<List<Game>> GetPlayerGames(long player_Id, int offset, int count)
{
if (offset < 0 || count < 1 || player_Id < 1)
return null;

using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Players);

var len = Database.ToLong(await db.Value.ListLenghtAsync("p:g:" + player_Id));
if (offset >= len)
return null;

// Sort the games descending
var end = len - offset - 1;
var start = end - count;
var game_ids = await db.Value.ListRangeAsync("p:g:" + player_Id, start, end);

var res = new List<Game>(count);

for (int i = game_ids.Length - 1; i >= 0; i--)
{
res.Add(await GetGameById(Database.ToLong(game_ids[i])));
}

return res;
}
}
}
12 changes: 6 additions & 6 deletions Server/Data/LobbyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class LobbyData
{
public async ValueTask<long> AddLobby(Lobby lobby)
{
using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Games);
using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Lobbies);
lobby.Id = await db.Value.StringIncr("l:id", 1);

await db.Value.StringSetAsync("l:" + lobby.Id, MessagePackSerializer.Serialize(lobby));
Expand All @@ -36,16 +36,16 @@ public async Task DeleteLobbyAsync(long lobby_id)
{
foreach (var p in t)
{
FireAndForget.KeyDelete(DatabaseName.Games, "l:p:" + p);
FireAndForget.KeyDelete(DatabaseName.Lobbies, "l:p:" + p);
}
}

FireAndForget.KeyDelete(DatabaseName.Games, "l:" + lobby_id);
FireAndForget.KeyDelete(DatabaseName.Lobbies, "l:" + lobby_id);
}

public async ValueTask<Lobby> GetLobbyById(long lobby_id)
{
using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Games);
using var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Lobbies);
var l = await db.Value.StringGetAsync("l:" + lobby_id);
if (l == null)
return null;
Expand All @@ -63,7 +63,7 @@ public async ValueTask<Lobby> GetLobbyById(long lobby_id)
public async ValueTask<Lobby> GetPlayerLobby(long player_id)
{
long lobby_id = 0;
using (var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Games))
using (var db = await DataContext.Db.GetDatabaseAsync(DatabaseName.Lobbies))
lobby_id = Database.ToLong(await db.Value.StringGetAsync("l:p:" + player_id));

if (lobby_id == 0)
Expand All @@ -73,7 +73,7 @@ public async ValueTask<Lobby> GetPlayerLobby(long player_id)
if (lobby == null)
{
// delete player from deleted lobby
FireAndForget.KeyDelete(DatabaseName.Games, "l:p:" + player_id);
FireAndForget.KeyDelete(DatabaseName.Lobbies, "l:p:" + player_id);
}

return lobby;
Expand Down
2 changes: 1 addition & 1 deletion ardb-sharp

0 comments on commit 617443b

Please sign in to comment.