Coconut / Program.cs
Sahil
Update Program.cs
0329f2c verified
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();