mirror of
https://github.com/SoDOff-Project/sodoff-mmo.git
synced 2025-10-11 08:18:49 -07:00
add an `AllClients
list and implement
UserUpdateController
`
This commit is contained in:
parent
4198480e53
commit
75824931d3
75
src/API/Controllers/UserUpdateController.cs
Normal file
75
src/API/Controllers/UserUpdateController.cs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using sodoffmmo.Core;
|
||||||
|
using sodoffmmo.Data;
|
||||||
|
|
||||||
|
namespace sodoffmmo.API.Controllers
|
||||||
|
{
|
||||||
|
[Route("mmo/update")]
|
||||||
|
[ApiController]
|
||||||
|
public class UserUpdateController : ControllerBase
|
||||||
|
{
|
||||||
|
[HttpPost]
|
||||||
|
[Route("SendPacketToPlayer")]
|
||||||
|
public IActionResult SendPacketToPlayer([FromForm] string apiToken, [FromForm] string userId, [FromForm] string cmd, [FromForm] string serializedArgs)
|
||||||
|
{
|
||||||
|
Client? client = Server.AllClients.FirstOrDefault(e => e.PlayerData.UNToken == apiToken);
|
||||||
|
Client? receivingClient = Server.AllClients.FirstOrDefault(e => e.PlayerData.Uid == userId);
|
||||||
|
|
||||||
|
if (client == null) return Unauthorized();
|
||||||
|
|
||||||
|
// send string array packet to player
|
||||||
|
string[]? deserializedArgs = JsonSerializer.Deserialize<string[]>(serializedArgs);
|
||||||
|
if(deserializedArgs == null) return BadRequest("No Data");
|
||||||
|
if(receivingClient != null) receivingClient.Send(Utils.ArrNetworkPacket(deserializedArgs, cmd));
|
||||||
|
else return BadRequest("Client Not Found");
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Route("SendPacketToRoom")]
|
||||||
|
public IActionResult SendPacketToRoom([FromForm] string apiToken, [FromForm] string roomName, [FromForm] string cmd, [FromForm] string serializedArgs)
|
||||||
|
{
|
||||||
|
Client? client = Server.AllClients.FirstOrDefault(e => e.PlayerData.UNToken == apiToken);
|
||||||
|
Room? room = Room.Get(roomName);
|
||||||
|
|
||||||
|
if(client == null) return Unauthorized();
|
||||||
|
else if (room == null) return BadRequest("Room Not Found");
|
||||||
|
|
||||||
|
// send string array packet to room
|
||||||
|
string[]? deserializedArgs = JsonSerializer.Deserialize<string[]>(serializedArgs);
|
||||||
|
if(deserializedArgs == null) return BadRequest("No Data");
|
||||||
|
room.Send(Utils.ArrNetworkPacket(deserializedArgs, cmd));
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Route("UpdateRoomVarsInRoom")]
|
||||||
|
public IActionResult UpdateRoomVarsInRoom([FromForm] string apiToken, [FromForm] string roomName, [FromForm] string serializedVars)
|
||||||
|
{
|
||||||
|
// vars are stored as key value pairs in dictionaries, grab a dictionary instead of a string array
|
||||||
|
Dictionary<string, string>? vars = JsonSerializer.Deserialize<Dictionary<string, string>>(serializedVars);
|
||||||
|
Room? room = Room.Get(roomName);
|
||||||
|
Client? client = Server.AllClients.FirstOrDefault(e => e.PlayerData.UNToken == apiToken);
|
||||||
|
|
||||||
|
if (client == null) return Unauthorized();
|
||||||
|
|
||||||
|
if (vars == null) return BadRequest("No Data");
|
||||||
|
if (room != null)
|
||||||
|
{
|
||||||
|
if (room.RoomVariables == null) room.RoomVariables = new(); // create a new room variables list for the room if one does not exist
|
||||||
|
foreach(var rVar in vars)
|
||||||
|
{
|
||||||
|
room.RoomVariables.Add(NetworkArray.VlElement(rVar.Key, rVar.Value, isPersistent: true)); // hardcoding persistance
|
||||||
|
}
|
||||||
|
NetworkPacket packet = Utils.VlNetworkPacket(room.RoomVariables, room.Id);
|
||||||
|
room.Send(packet);
|
||||||
|
return Ok();
|
||||||
|
} else return BadRequest("Room Not Found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,7 @@ public class Server {
|
|||||||
readonly IPAddress ipAddress;
|
readonly IPAddress ipAddress;
|
||||||
readonly bool IPv6AndIPv4;
|
readonly bool IPv6AndIPv4;
|
||||||
ModuleManager moduleManager = new();
|
ModuleManager moduleManager = new();
|
||||||
|
public static List<Client> AllClients { get; set; } = new();
|
||||||
|
|
||||||
public Server(IPAddress ipAdress, int port, bool IPv6AndIPv4) {
|
public Server(IPAddress ipAdress, int port, bool IPv6AndIPv4) {
|
||||||
this.ipAddress = ipAdress;
|
this.ipAddress = ipAdress;
|
||||||
@ -57,6 +58,7 @@ public class Server {
|
|||||||
|
|
||||||
private async Task HandleClient(Socket handler) {
|
private async Task HandleClient(Socket handler) {
|
||||||
Client client = new(handler);
|
Client client = new(handler);
|
||||||
|
AllClients.Add(client);
|
||||||
try {
|
try {
|
||||||
while (client.Connected) {
|
while (client.Connected) {
|
||||||
await client.Receive();
|
await client.Receive();
|
||||||
@ -69,6 +71,7 @@ public class Server {
|
|||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
client.SetRoom(null);
|
client.SetRoom(null);
|
||||||
|
AllClients.Remove(client);
|
||||||
} catch (Exception) { }
|
} catch (Exception) { }
|
||||||
client.Disconnect();
|
client.Disconnect();
|
||||||
Console.WriteLine("Socket disconnected IID: " + client.ClientID);
|
Console.WriteLine("Socket disconnected IID: " + client.ClientID);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user