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