Commit ce1ea277 authored by Maxime's avatar Maxime
Browse files

add cleaning of unused games in API

parent 85e91bbd
......@@ -19,12 +19,13 @@ namespace WebAPI.Controller
}
[HttpPost("JoinGame")]
public int CreateGame(string code)
public int JoinGame(string code)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return -1;
var id = game.AddPlayer();
Trace.WriteLine($"Player <{id}> joined Game <{game.Code}>");
return id;
}
......@@ -47,7 +48,7 @@ namespace WebAPI.Controller
return Ok();
}
[HttpGet("GetPlayer")]
public Player GetPlayer(string code, int ownId)
......@@ -60,7 +61,16 @@ namespace WebAPI.Controller
var otherPlayer = game.Players.Find(x => x.Id != ownId);
return otherPlayer;
}
[HttpGet("IsGameReady")]
public bool IsGameReady(string code)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return false;
return game.Ready;
}
[HttpDelete("RemovePlayer")]
public IActionResult RemovePlayer(string code, int id)
......@@ -83,5 +93,14 @@ namespace WebAPI.Controller
return game.Players;
}
[HttpGet("GetGameCodes")]
public IEnumerable<string> GetGameCodes()
{
return GameManager.Instance.Games.Select(x => x.Code).ToList();
}
}
}
namespace WebAPI.Model
using System.Diagnostics;
namespace WebAPI.Model
{
public class GameManager
{
public List<Game> Games { get;}
public List<Game> Games { get; }
public static GameManager Instance
......@@ -19,8 +21,26 @@
private static GameManager instance = null;
private GameManager() {
Games=new List<Game>();
private GameManager()
{
Games = new List<Game>();
CleanGames();
}
private void CleanGames()
{
Task.Run(() =>
{
while (true)
{
Task.Delay(10000).Wait();
var unusedGames = Games.Count(x => x.Players.Count == 0);
Trace.WriteLine($"Cleaning {unusedGames} unused game(s)");
Games.RemoveAll(x => x.Players.Count == 0);
}
});
}
}
}
<menu>
<title text="ZZZZZZ" scale="70" x="50" y="15"/>
<input id="ServerIP" text="localhost:3000" scale="20" x="50" y="50" />
<button text="CONNECT" scale="20" x="50" y="70" onClick="MultiplayerConnectClicked" />
<input id="ServerCode" text="enter code" scale="20" x="70" y="50" />
<button text="JOIN" scale="20" x="50" y="50" onClick="MultiplayerJoinClicked" />
<button text="CREATE" scale="20" x="50" y="70" onClick="MultiplayerCreateClicked" />
<image src="back.png" scale="10" x="10" y="80" onClick="MultiplayerBackClicked"/>
</menu>
\ No newline at end of file
......@@ -23,9 +23,9 @@ namespace ZZZZZZ.Objects.Character
private readonly List<Sprite2D> Sprites;
private float internalTimer;
public LocalCharacter()
public LocalCharacter(int id = -1)
{
CharacterID = -1;
CharacterID = id;
Node.Scale *= 10;
Speed = 15f;
Shape = Node.CreateComponent<CollisionBox2D>();
......
......@@ -34,14 +34,13 @@ namespace ZZZZZZ.GUI.Inputs
private void LineEdit_TextChanged(TextChangedEventArgs obj)
{
if (Text != null)
{
realText = obj.Text;
if (obj.Text.Length > 15)
{
if (realText.Length < obj.Text.Length) position++;
if (realText.Length > obj.Text.Length) position--;
realText = obj.Text;
Text.Value = realText.Substring(position);
}
else
......
......@@ -13,15 +13,26 @@ namespace ZZZZZZ.GUI.Navigations
ButtonSound();
Load(Assets.UI.Menus.Main);
}
public void MultiplayerConnectClicked()
public void MultiplayerJoinClicked()
{
ButtonSound();
var connectionString = (FindById("ServerIP") as TextInput).Value;
try
{
//var (ip, port) = ToolBox.ExtractConnectionString(connectionString);
//Task.Run(() => GameManager.Current.ConnectToServer(ip, port));
var code = (FindById("ServerCode") as TextInput).Value;
GameManager.Current.ConnectToAPI(code);
}
catch (ArgumentException e)
{
GameManager.Current.Navigation.Toast.Show(e.Message);
}
}
public void MultiplayerCreateClicked()
{
ButtonSound();
try
{
GameManager.Current.ConnectToAPI();
}
catch (ArgumentException e)
......
......@@ -45,16 +45,48 @@ namespace ZZZZZZ
}
public void ConnectToAPI()
public void ConnectToAPI(string gamecode = "")
{
Task.Run(() =>
{
API = new WebAPI();
var code = API.CreateGame();
System.Console.WriteLine($"Created game with ID {API.Code}");
Navigation.Toast.Show($"Created game with ID {API.Code}");
if (string.IsNullOrEmpty(gamecode))
{
API.CreateGame();
System.Console.WriteLine($"Created game with ID {API.Code}");
Navigation.Toast.Show($"Created game with ID {API.Code}");
}
else
{
API.Code = gamecode;
}
var joinedID = API.JoinGame();
if (joinedID != -1)
{
int counter = 0;
while (counter < 600 && !API.IsGameReady())
{
System.Console.WriteLine($"Game is not ready, try {counter}");
Task.Delay(1000).Wait();
counter++;
}
API.JoinGame();
InvokeOnMain(() =>
{
LevelManager.Load(Assets.Levels.World0);
LocalPlayerManager = new LocalPlayerManager(new LocalCharacter(joinedID), new JoystickInput(), LevelManager);
Navigation.Load(Assets.UI.Menus.Ingame);
});
Task.Delay(500).Wait();
while (true)
{
System.Console.WriteLine($"UPDATE {LocalPlayerManager.Character.CharacterID}");
API.UpdateLocal(LocalPlayerManager.Character as LocalCharacter);
Task.Delay(50).Wait();
}
}
});
}
......
......@@ -8,13 +8,15 @@ namespace ZZZZZZ.WebAPIs
public interface IWebAPI
{
string Code { get; }
string Code { get; set; }
HttpClient Client { get; }
bool CreateGame();
bool JoinGame(string code = "");
int JoinGame(string code = "");
bool UpdateLocal(LocalCharacter player);
bool UpdateRemote(RemoteCharacter remote);
bool IsGameReady();
}
......
......@@ -10,7 +10,7 @@ namespace ZZZZZZ.WebAPIs
{
public class WebAPI : IWebAPI
{
public string Code { get; private set; }
public string Code { get; set; }
public string Hook { get; }
public HttpClient Client { get; }
......@@ -22,43 +22,92 @@ namespace ZZZZZZ.WebAPIs
public bool CreateGame()
{
var post = Client.PostAsync($"{Hook}Game/CreateGame", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
Code = content.Result;
return post.Result.StatusCode == HttpStatusCode.OK;
try
{
var post = Client.PostAsync($"{Hook}Game/CreateGame", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
Code = content.Result;
return post.Result.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool JoinGame(string code)
public int JoinGame(string code)
{
if (code == string.Empty)
code = Code;
var post = Client.PostAsync($"{Hook}Game/JoinGame?code={code}", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
return post.Result.StatusCode == HttpStatusCode.OK;
try
{
if (code == string.Empty)
code = Code;
var post = Client.PostAsync($"{Hook}Game/JoinGame?code={code}", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
return int.Parse(content.Result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return -1;
}
}
public bool UpdateLocal(LocalCharacter player)
{
var post = Client.PostAsync($"{Hook}Game/SendPlayer?code={Code}&id={player.ID}&x={FormatFloat(player.Node.Position2D.X)}&y={FormatFloat(player.Node.Position2D.Y)}&flipX={player.Flip.X}&flipY={player.Flip.Y}", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
return post.Result.StatusCode == HttpStatusCode.OK;
try
{
var post = Client.PutAsync($"{Hook}Game/SendPlayer?code={Code}&id={player.CharacterID}&x={FormatFloat(player.Node.Position2D.X)}&y={FormatFloat(player.Node.Position2D.Y)}&flipX={player.Flip.X}&flipY={player.Flip.Y}", null);
post.Wait();
var content = post.Result.Content.ReadAsStringAsync();
content.Wait();
return post.Result.StatusCode == HttpStatusCode.OK;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool UpdateRemote(RemoteCharacter remote)
{
var get = Client.GetAsync($"{Hook}Game/GetPlayer?code={Code}&ownId={remote.ID}");
get.Wait();
var content = get.Result.Content.ReadAsStringAsync();
content.Wait();
var result = content.Result;
Console.WriteLine(result);
return true;
try
{
var get = Client.GetAsync($"{Hook}Game/GetPlayer?code={Code}&ownId={remote.ID}");
get.Wait();
var content = get.Result.Content.ReadAsStringAsync();
content.Wait();
var result = content.Result;
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
public bool IsGameReady()
{
try
{
var get = Client.GetAsync($"{Hook}Game/IsGameReady?code={Code}");
get.Wait();
var content = get.Result.Content.ReadAsStringAsync();
content.Wait();
var result = content.Result;
return bool.Parse(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
private string FormatFloat(float value) => value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment