xabbo-scripts/Scripts/Shroom.csx
Administrator 7a548130a3 Move all scripts into Scripts/ subfolder
Keeps the repo root clean - only README.md visible on landing page.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 09:49:37 +01:00

213 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
string BACKEND_URL = "http://localhost:3000/api/room-data";
HttpClient httpClient = new HttpClient();
Log("🚀 Room Data Collector v2.0 started");
async Task SendRoomUpdate()
{
try
{
var data = new
{
roomId = RoomId,
roomName = Room?.Name ?? "Unknown",
timestamp = DateTime.UtcNow.ToString("o"),
furniture = new List<object>(),
users = new List<object>()
};
if (FloorItems != null)
{
foreach (var item in FloorItems)
{
try
{
if (item?.Location == null) continue;
string itemName = "Unknown";
try
{
itemName = item.GetName() ?? $"Item_{item.Kind}";
}
catch
{
itemName = $"Furni_{item.Kind}";
}
data.furniture.Add(new
{
id = item.Id,
className = item.Kind,
name = itemName,
x = item.Location.X,
y = item.Location.Y,
z = item.Location.Z,
direction = item.Direction,
state = item.State,
owner = item.OwnerName ?? "Unknown"
});
}
catch (Exception ex)
{
Log($"❌ Error processing floor item {item?.Id}: {ex.Message}");
}
}
}
if (WallItems != null)
{
foreach (var item in WallItems)
{
try
{
if (item?.Location == null) continue;
string itemName = "Unknown";
try
{
itemName = item.GetName() ?? $"WallItem_{item.Kind}";
}
catch
{
itemName = $"Wall_{item.Kind}";
}
data.furniture.Add(new
{
id = item.Id,
className = item.Kind + 10000,
name = itemName,
x = item.Location.WX,
y = item.Location.WY,
z = 0.0,
direction = 0,
state = item.State,
owner = item.OwnerName ?? "Unknown",
isWallItem = true
});
}
catch (Exception ex)
{
Log($"❌ Error processing wall item {item?.Id}: {ex.Message}");
}
}
}
if (Users != null)
{
foreach (var user in Users)
{
try
{
if (user?.Location == null) continue;
data.users.Add(new
{
id = user.Id,
name = user.Name ?? "Unknown",
x = user.Location.X,
y = user.Location.Y,
z = user.Location.Z,
figure = user.Figure ?? "hr-100-61.hd-180-1.ch-210-66",
direction = user.Direction,
motto = user.Motto ?? "",
gender = user.Gender.ToString()
});
}
catch (Exception ex)
{
Log($"❌ Error processing user {user?.Name}: {ex.Message}");
}
}
}
var json = System.Text.Json.JsonSerializer.Serialize(data);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(BACKEND_URL, content);
if (response.IsSuccessStatusCode)
{
Log($"✅ Sent: {data.furniture.Count} furniture, {data.users.Count} users to browser");
}
else
{
Log($"❌ Failed: {response.StatusCode}");
}
}
catch (Exception ex)
{
Log($"❌ Error: {ex.Message}");
}
}
OnEnteredRoom(async e =>
{
Log($"🏠 Entered room: {Room?.Name} (ID: {RoomId})");
await Task.Delay(2000);
await SendRoomUpdate();
});
OnIntercept(In["ObjectAdd"], async e =>
{
Log("🪑 Furniture added");
await Task.Delay(500);
await SendRoomUpdate();
});
OnIntercept(In["ObjectRemove"], async e =>
{
Log("🗑️ Furniture removed");
await SendRoomUpdate();
});
OnIntercept(In["ObjectUpdate"], async e =>
{
Log("🔄 Furniture updated");
await SendRoomUpdate();
});
OnIntercept(In["Users"], async e =>
{
Log("👥 Users list updated");
await Task.Delay(500);
await SendRoomUpdate();
});
OnIntercept(In["UserRemove"], async e =>
{
Log("👋 User left");
await SendRoomUpdate();
});
OnIntercept(In["UserUpdate"], async e =>
{
await SendRoomUpdate();
});
while (Run)
{
try
{
if (IsInRoom)
{
await SendRoomUpdate();
}
await Task.Delay(15000);
}
catch (Exception ex)
{
Log($"❌ Main loop error: {ex.Message}");
await Task.Delay(5000);
}
}
httpClient.Dispose();
Log("🛑 Room Data Collector stopped");