Commit 85e91bbd authored by Maxime's avatar Maxime
Browse files

switch to full API

parent 78d376ee
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WebAPI.Model;
namespace WebAPI.Controller
......@@ -7,10 +8,80 @@ namespace WebAPI.Controller
[Route("[controller]")]
public class GameController : ControllerBase
{
[HttpPost("CreateSession")]
public Server CreateSession()
[HttpPost("CreateGame")]
public string CreateGame()
{
var game = new Game(Utils.RandomString(GameManager.Instance.Games,3));
GameManager.Instance.Games.Add(game);
Trace.WriteLine($"New Game <{game.Code}>");
return game.Code;
}
[HttpPost("JoinGame")]
public int CreateGame(string code)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return -1;
var id = game.AddPlayer();
return id;
}
[HttpPut("SendPlayer")]
public IActionResult SendPlayer(string code, int id, float x, float y, bool flipX, bool flipY)
{
return new Server("192.168.1.22", 3000, "*****");
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return BadRequest("Game not found");
if (!game.Ready)
return BadRequest("Game not started"); ;
var player = game.Players.Find(x => x.Id == id);
if(player != null)
{
player.X = x;
player.Y = y;
player.FlipX = flipX;
player.FlipY = flipY;
}
return Ok();
}
[HttpGet("GetPlayer")]
public Player GetPlayer(string code, int ownId)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return null;
if (!game.Ready)
return null;
var otherPlayer = game.Players.Find(x => x.Id != ownId);
return otherPlayer;
}
[HttpDelete("RemovePlayer")]
public IActionResult RemovePlayer(string code, int id)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return BadRequest("Game not found");
var player = game.Players.Find(x => x.Id == id);
if (player != null)
game.Players.Remove(player);
return Ok();
}
[HttpGet("GetPlayers")]
public IEnumerable<Player> GetPlayer(string code)
{
var game = GameManager.Instance.Games.Find(x => x.Code == code);
if (game == null)
return Enumerable.Empty<Player>();
return game.Players;
}
}
}
namespace WebAPI.Model
{
public class Game
{
public string Code { get; }
public List<Player> Players { get; }
public bool Ready { get => Players.Count == 2; }
public Game(string code)
{
Code = code;
Players = new List<Player>();
}
public int AddPlayer()
{
if(Players.Count >= 2)
return -1;
var player = new Player(Players.Count);
Players.Add(player);
return player.Id;
}
}
}
namespace WebAPI.Model
{
public class GameManager
{
public List<Game> Games { get;}
public static GameManager Instance
{
get
{
if (instance == null)
instance = new GameManager();
return instance;
}
}
private static GameManager instance = null;
private GameManager() {
Games=new List<Game>();
}
}
}
namespace WebAPI.Model
{
public class Player
{
public int Id { get; }
public float X { get; set; }
public float Y { get; set; }
public bool FlipX { get; set; }
public bool FlipY { get; set; }
public Player(int id)
{
Id = id;
}
}
}
namespace WebAPI.Model
{
public class Server
{
public string Url { get; set; }
public int Port { get; set; }
public string Code { get; set; }
public Server(string url, int port, string code)
{
Url = url;
Port = port;
Code = code;
}
}
}
namespace WebAPI.Model
{
public static class Utils
{
public static Random Random { get; }
private static readonly string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static Utils()
{
Random = new Random();
}
public static string RandomString(List<Game> games, int length)
{
string code = string.Empty;
int i = 0;
do
{
code = new string(Enumerable.Repeat(chars, length).Select(s => s[Random.Next(s.Length)]).ToArray());
i++;
} while (games.Find(x => x.Code == code) != null || i < 50);
return code;
}
}
}
......@@ -4,6 +4,7 @@ namespace ZZZZZZ.Entities.Character
{
public interface ICharacter
{
int CharacterID { get; set; }
(bool X, bool Y) Flip { get; set; }
void Move(Movement movement);
void Move(Vector2 position);
......
......@@ -10,6 +10,7 @@ namespace ZZZZZZ.Objects.Character
{
public class LocalCharacter : Entity, ICharacter
{
public int CharacterID { get; set; }
public float Speed { get; set; }
private Vector2 PrevPos { get; set; }
public bool IsGrounded {get; set;}
......@@ -24,6 +25,7 @@ namespace ZZZZZZ.Objects.Character
public LocalCharacter()
{
CharacterID = -1;
Node.Scale *= 10;
Speed = 15f;
Shape = Node.CreateComponent<CollisionBox2D>();
......
......@@ -5,8 +5,9 @@ using ZZZZZZ.Utils;
namespace ZZZZZZ.Entities.Character
{
internal class RemoteCharacter : Entity, ICharacter
public class RemoteCharacter : Entity, ICharacter
{
public int CharacterID { get; set; }
public (bool X, bool Y) Flip
{
......@@ -19,6 +20,7 @@ namespace ZZZZZZ.Entities.Character
public RemoteCharacter()
{
CharacterID = -1;
Node.Scale *= 10;
Sprites = new List<Sprite2D>
{
......
......@@ -20,8 +20,9 @@ namespace ZZZZZZ.GUI.Navigations
var connectionString = (FindById("ServerIP") as TextInput).Value;
try
{
var (ip, port) = ToolBox.ExtractConnectionString(connectionString);
Task.Run(() => GameManager.Current.ConnectToServer(ip, port));
//var (ip, port) = ToolBox.ExtractConnectionString(connectionString);
//Task.Run(() => GameManager.Current.ConnectToServer(ip, port));
GameManager.Current.ConnectToAPI();
}
catch (ArgumentException e)
{
......
......@@ -30,9 +30,13 @@ namespace ZZZZZZ.GUI.Toasts
messages.Enqueue(text);
return;
}
Text.Value = text;
Text.SetFont(Application.Current.ResourceCache.GetFont(Assets.Fonts.AnonymousPro), MathHelper.Clamp(ToolBox.FontSize(text, Size.X), 20, 60));
Visible = true;
Application.InvokeOnMain(() =>
{
Text.Value = text;
Text.SetFont(Application.Current.ResourceCache.GetFont(Assets.Fonts.AnonymousPro), MathHelper.Clamp(ToolBox.FontSize(text, Size.X), 20, 60));
Visible = true;
});
Task.Run(() => DoAnimation());
Task.Delay(time).ContinueWith(t => DoAnimationBackward());
Task.Delay(time + 1000).ContinueWith(t => { Application.InvokeOnMain(() => Visible = false); });
......
......@@ -25,15 +25,15 @@ namespace ZZZZZZ
public LocalPlayerManager LocalPlayerManager { get; set; }
public RemotePlayerManager RemotePlayerManager { get; set; }
public IServer Server { get; private set; }
public IWebAPI API { get; private set; }
public LevelManager LevelManager { get; private set; }
public Navigation Navigation { get; private set; }
public MusicManager MusicManager { get; private set; }
public SoundManager SoundManager { get; private set; }
public BackgroundParticleEmitter BackgroundParticle { get; private set; }
public GameManager(ApplicationOptions options) : base(options)
{
}
public GameManager() : this(null){ }
public GameManager(ApplicationOptions options) : base(options) { }
protected override void Setup()
{
......@@ -68,9 +68,8 @@ namespace ZZZZZZ
BackgroundParticle.Start();
LevelManager = new LevelManager(Scene.CollisionHandler);
var api = new WebAPI();
api.GetTest();
}
}
}
using ZZZZZZ.Entities.Character;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Urho.Network;
using ZZZZZZ.Entities.Character;
using ZZZZZZ.Inputs;
using ZZZZZZ.Objects.Character;
using ZZZZZZ.Player;
using ZZZZZZ.Server;
using ZZZZZZ.Utils;
using ZZZZZZ.WebAPIs;
namespace ZZZZZZ
{
......@@ -38,5 +43,21 @@ namespace ZZZZZZ
}
return connected;
}
public void ConnectToAPI()
{
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}");
API.JoinGame();
});
}
}
}
......@@ -7,12 +7,14 @@ namespace ZZZZZZ.Player
{
public class RemotePlayerManager : IPlayerManager
{
public int CharacterID { get; set; }
public ICharacter Character { get; private set; }
public LevelManager LevelManager { get; private set; }
public IServer Server { get; }
public RemotePlayerManager(ICharacter character, LevelManager levelManager)
{
CharacterID = -1;
LevelManager = levelManager;
Character = character;
Server = GameManager.Current.Server;
......
using System.Net.Http;
using ZZZZZZ.Entities.Character;
using ZZZZZZ.Objects.Character;
namespace ZZZZZZ.WebAPIs
{
public interface IWebAPI
{
string Code { get; }
HttpClient Client { get; }
bool CreateGame();
bool JoinGame(string code = "");
bool UpdateLocal(LocalCharacter player);
bool UpdateRemote(RemoteCharacter remote);
}
}
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using ZZZZZZ.Entities.Character;
using ZZZZZZ.Objects.Character;
namespace ZZZZZZ.WebAPIs
{
public class WebAPI
public class WebAPI : IWebAPI
{
public string Code { get; private set; }
public string Hook { get; }
public HttpClient Client { get; }
public void GetTest()
public WebAPI(string hook = "https://localhost:5000/")
{
var name = "max";
HttpClient client = new HttpClient();
var r = client.GetAsync($"https://localhost:7117/Character/GetCharacter?name={name}");
r.Wait();
var s = r.Result.Content.ReadAsStringAsync();
s.Wait();
Console.WriteLine(s.Result);
Hook = hook;
Client = new HttpClient();
}
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;
}
public bool 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;
}
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;
}
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;
}
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