Spaces:
Build error
Build error
Sahil commited on
Update Program.cs
Browse files- Program.cs +76 -63
Program.cs
CHANGED
|
@@ -1,71 +1,84 @@
|
|
| 1 |
using Microsoft.AspNetCore.Builder;
|
| 2 |
using Microsoft.AspNetCore.Http;
|
| 3 |
-
using
|
| 4 |
-
using System.
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
{
|
| 8 |
-
public
|
| 9 |
-
{
|
| 10 |
-
|
| 11 |
-
{
|
| 12 |
-
var builder = WebApplication.CreateBuilder(args);
|
| 13 |
-
var app = builder.Build();
|
| 14 |
-
|
| 15 |
-
var mapSize = 15;
|
| 16 |
-
var map = new string[mapSize, mapSize];
|
| 17 |
-
var player = new Player();
|
| 18 |
-
var troops = new List<Troop>();
|
| 19 |
-
|
| 20 |
-
app.UseDefaultFiles();
|
| 21 |
-
app.UseStaticFiles();
|
| 22 |
-
|
| 23 |
-
app.MapGet("/player", () => player);
|
| 24 |
-
app.MapGet("/map", () => map);
|
| 25 |
-
app.MapGet("/troops", () => troops);
|
| 26 |
-
|
| 27 |
-
app.MapPost("/place-building/{x}/{y}/{name}", (int x, int y, string name) =>
|
| 28 |
-
{
|
| 29 |
-
if (x >= 0 && x < mapSize && y >= 0 && y < mapSize && map[y, x] == null)
|
| 30 |
-
{
|
| 31 |
-
map[y, x] = name;
|
| 32 |
-
player.Buildings.Add(name);
|
| 33 |
-
}
|
| 34 |
-
return new { map, player };
|
| 35 |
-
});
|
| 36 |
-
|
| 37 |
-
app.MapPost("/collect-gold", () => { player.Gold += 10; return player; });
|
| 38 |
-
|
| 39 |
-
app.MapPost("/train-troop/{type}/{x}/{y}", (string type, int x, int y) =>
|
| 40 |
-
{
|
| 41 |
-
troops.Add(new Troop { Type = type, X = x, Y = y, TargetX = mapSize - 1, TargetY = y });
|
| 42 |
-
return troops;
|
| 43 |
-
});
|
| 44 |
-
|
| 45 |
-
app.MapPost("/tick", () =>
|
| 46 |
-
{
|
| 47 |
-
foreach (var troop in troops)
|
| 48 |
-
if (troop.X < mapSize - 1) troop.X++;
|
| 49 |
-
return troops;
|
| 50 |
-
});
|
| 51 |
-
|
| 52 |
-
app.Run("http://0.0.0.0:7860");
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
{
|
| 65 |
-
|
| 66 |
-
public int X { get; set; }
|
| 67 |
-
public int Y { get; set; }
|
| 68 |
-
public int TargetX { get; set; }
|
| 69 |
-
public int TargetY { get; set; }
|
| 70 |
}
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
using Microsoft.AspNetCore.Builder;
|
| 2 |
using Microsoft.AspNetCore.Http;
|
| 3 |
+
using System.Text.Json;
|
| 4 |
+
using System.Text.Json.Serialization;
|
| 5 |
|
| 6 |
+
var builder = WebApplication.CreateBuilder(args);
|
| 7 |
+
var app = builder.Build();
|
| 8 |
+
|
| 9 |
+
// ===== Game State =====
|
| 10 |
+
var mapSize = 15;
|
| 11 |
+
|
| 12 |
+
// Jagged array for serializable map
|
| 13 |
+
string[][] map = new string[mapSize][];
|
| 14 |
+
for (int i = 0; i < mapSize; i++)
|
| 15 |
+
map[i] = new string[mapSize];
|
| 16 |
+
|
| 17 |
+
// Player info
|
| 18 |
+
var player = new Player { Gold = 0, Elixir = 0 };
|
| 19 |
+
|
| 20 |
+
// Troops list
|
| 21 |
+
var troops = new List<Troop>();
|
| 22 |
+
|
| 23 |
+
// ===== Classes =====
|
| 24 |
+
public class Player
|
| 25 |
{
|
| 26 |
+
public int Gold { get; set; }
|
| 27 |
+
public int Elixir { get; set; }
|
| 28 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
public class Troop
|
| 31 |
+
{
|
| 32 |
+
public string Type { get; set; }
|
| 33 |
+
public int X { get; set; }
|
| 34 |
+
public int Y { get; set; }
|
| 35 |
+
}
|
| 36 |
|
| 37 |
+
// ===== Endpoints =====
|
| 38 |
+
app.UseDefaultFiles();
|
| 39 |
+
app.UseStaticFiles();
|
| 40 |
+
|
| 41 |
+
// Get player info
|
| 42 |
+
app.MapGet("/player", () => player);
|
| 43 |
+
|
| 44 |
+
// Get map info
|
| 45 |
+
app.MapGet("/map", () => map);
|
| 46 |
+
|
| 47 |
+
// Get troops
|
| 48 |
+
app.MapGet("/troops", () => troops);
|
| 49 |
+
|
| 50 |
+
// Collect gold
|
| 51 |
+
app.MapPost("/collect-gold", () => {
|
| 52 |
+
player.Gold += 10;
|
| 53 |
+
return Results.Ok();
|
| 54 |
+
});
|
| 55 |
+
|
| 56 |
+
// Place building
|
| 57 |
+
app.MapPost("/place-building/{x}/{y}/{name}", (int x, int y, string name) =>
|
| 58 |
+
{
|
| 59 |
+
if (x < 0 || x >= mapSize || y < 0 || y >= mapSize)
|
| 60 |
+
return Results.BadRequest("Invalid coordinates");
|
| 61 |
+
map[y][x] = name;
|
| 62 |
+
return Results.Ok();
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
// Train troop
|
| 66 |
+
app.MapPost("/train-troop/{type}/{x}/{y}", (string type, int x, int y) =>
|
| 67 |
+
{
|
| 68 |
+
if (x < 0 || x >= mapSize || y < 0 || y >= mapSize)
|
| 69 |
+
return Results.BadRequest("Invalid coordinates");
|
| 70 |
+
troops.Add(new Troop { Type = type, X = x, Y = y });
|
| 71 |
+
return Results.Ok();
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
// Advance troops (example simple movement)
|
| 75 |
+
app.MapPost("/tick", () =>
|
| 76 |
+
{
|
| 77 |
+
foreach (var t in troops)
|
| 78 |
{
|
| 79 |
+
if (t.X < mapSize - 1) t.X += 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
+
return Results.Ok();
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
app.Run();
|