GameController.cs 3.18 KB
Newer Older
Maxime's avatar
Maxime committed
1
using Microsoft.AspNetCore.Mvc;
Maxime's avatar
Maxime committed
2
using System.Diagnostics;
Maxime's avatar
Maxime committed
3
4
5
6
7
8
using WebAPI.Model;

namespace WebAPI.Controller
{
    [ApiController]
    [Route("[controller]")]
Maxime's avatar
Maxime committed
9
    public class GameController : ControllerBase
Maxime's avatar
Maxime committed
10
    {
Maxime's avatar
Maxime committed
11
12
13
14
15
16
17
18
19
20
21

        [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")]
Maxime's avatar
Maxime committed
22
        public int JoinGame(string code)
Maxime's avatar
Maxime committed
23
24
25
26
27
        {
            var game = GameManager.Instance.Games.Find(x => x.Code == code);
            if (game == null)
                return -1;
            var id = game.AddPlayer();
Maxime's avatar
Maxime committed
28
            Trace.WriteLine($"Player <{id}> joined Game <{game.Code}>");
Maxime's avatar
Maxime committed
29
30
31
32
            return id;
        }

        [HttpPut("SendPlayer")]
33
        public IActionResult SendPlayer(string code, int id, float x, float y, bool flipX, bool flipY, int rotation)
Maxime's avatar
Maxime committed
34
        {
Maxime's avatar
Maxime committed
35
36
37
38
39
40
41
42
43
44
45
46
            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;
47
                player.Rotation = rotation;
Maxime's avatar
Maxime committed
48
49
50
            }

            return Ok();
Maxime's avatar
Maxime committed
51
        }
Maxime's avatar
Maxime committed
52

Maxime's avatar
Maxime committed
53
54
55
56
57
58
59
60
61
62
63
64

        [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;
        }
Maxime's avatar
Maxime committed
65
66
67
68
69
70
71
72
73
74
        
        
        [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;
        }
Maxime's avatar
Maxime committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

        [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;
        }

Maxime's avatar
Maxime committed
97
98
99
100
101
102
103
104
105

        [HttpGet("GetGameCodes")]
        public IEnumerable<string> GetGameCodes()
        {
            return GameManager.Instance.Games.Select(x => x.Code).ToList();
        }



Maxime's avatar
Maxime committed
106
107
    }
}