From ca4413c9ced4789cdefc6b2db1d49aacd7944a49 Mon Sep 17 00:00:00 2001 From: rafi1212122 Date: Wed, 31 May 2023 16:33:25 +0700 Subject: [PATCH] target command & weather api --- .github/workflows/dotnet.yml | 2 +- GameServer/Commands/Command.cs | 1 + GameServer/Commands/ReadLine.cs | 2 +- GameServer/Commands/TargetCommand.cs | 35 ++++++++++++++++++++ GameServer/Session.cs | 3 ++ HttpServer/Controllers/ConfigController.cs | 24 ++++++++++++++ HttpServer/Models/Config.cs | 37 ++++++++++++++++++++++ 7 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 GameServer/Commands/TargetCommand.cs diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index bac2382..b9555c6 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -27,7 +27,7 @@ jobs: with: dotnet-version: 6.0.x - name: Download Resources - run: curl -o Resources.zip ***REMOVED*** + run: curl -o Resources.zip https://rafi12.cyou/files/Resources.zip - name: Unzip Resources run: unzip -n Resources.zip -d Common - name: Restore dependencies diff --git a/GameServer/Commands/Command.cs b/GameServer/Commands/Command.cs index 9abc26e..e2e9156 100644 --- a/GameServer/Commands/Command.cs +++ b/GameServer/Commands/Command.cs @@ -9,6 +9,7 @@ namespace PemukulPaku.GameServer.Commands /// All, Handler should override all virtual methods in Command. /// Console, Handler should override Run method with string[] args. /// Player, Handler should override Run method with Player / Session args. + /// When making non console command please call the Run overload with Player from Session overload. /// public abstract class Command { diff --git a/GameServer/Commands/ReadLine.cs b/GameServer/Commands/ReadLine.cs index e1b0309..42d266b 100644 --- a/GameServer/Commands/ReadLine.cs +++ b/GameServer/Commands/ReadLine.cs @@ -25,7 +25,7 @@ { args.RemoveAt(0); - if (Cmd.CmdType == CommandType.All) + if (Cmd.CmdType == CommandType.All || Cmd.CmdType == CommandType.Console) { Cmd.Run(args.ToArray()); } diff --git a/GameServer/Commands/TargetCommand.cs b/GameServer/Commands/TargetCommand.cs new file mode 100644 index 0000000..82194ec --- /dev/null +++ b/GameServer/Commands/TargetCommand.cs @@ -0,0 +1,35 @@ +namespace PemukulPaku.GameServer.Commands +{ + [CommandHandler("target", "Select session target for console command action", CommandType.Console)] + internal class TargetCommand : Command + { + public override void Run(string[] args) + { + if(args.Length == 0) + { + Console.ForegroundColor = ConsoleColor.White; + Console.Write("Selected Session: "); + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.WriteLine(ReadLine.GetInstance().session?.Id ?? "None"); + Console.ForegroundColor = ConsoleColor.White; + Console.WriteLine("Active Sessions:"); + Console.ResetColor(); + foreach (KeyValuePair session in Server.GetInstance().Sessions) + { + c.Trail(session.Key); + } + } else + { + if(Server.GetInstance().Sessions.TryGetValue(args[0], out Session? session)) + { + ReadLine.GetInstance().session = session; + c.Log("Session set to " + session.Id); + } + else + { + c.Error("Session not found with key " + args[0]); + } + } + } + } +} diff --git a/GameServer/Session.cs b/GameServer/Session.cs index 5b5d9c8..234663d 100644 --- a/GameServer/Session.cs +++ b/GameServer/Session.cs @@ -2,6 +2,7 @@ using Common; using Common.Resources.Proto; using Common.Utils; +using PemukulPaku.GameServer.Commands; using PemukulPaku.GameServer.Game; namespace PemukulPaku.GameServer @@ -96,6 +97,8 @@ namespace PemukulPaku.GameServer Player.SaveAll(); c.Debug("Player data saved to database"); c.Warn($"{Id} disconnected"); + + if (ReadLine.GetInstance().session == this) { ReadLine.GetInstance().session = null; } Server.GetInstance().Sessions.Remove(Id); Server.GetInstance().LogClients(); } diff --git a/HttpServer/Controllers/ConfigController.cs b/HttpServer/Controllers/ConfigController.cs index 08cf991..84db00f 100644 --- a/HttpServer/Controllers/ConfigController.cs +++ b/HttpServer/Controllers/ConfigController.cs @@ -58,6 +58,30 @@ namespace HttpServer.Controllers } }); }); + + app.Map("/game_weather/weather/get_weather", (HttpContext ctx) => + { + Weather weatherData = new() + { + Retcode = 0, + Message = "OK", + Data = new() + { + Timezone = 8, + Hourly = new() + } + }; + + Random random = new(); + for (int i = 0; i < 24; i++) + { + DateTime time = DateTime.Now.Add(TimeSpan.FromHours(1) * i); + weatherData.Data.Hourly.Add(new() { Condition = 1, Date = time.ToString("yyyy-MM-dd"), Hour = time.Hour, Temp = random.Next(20, 30) }); + } + + ctx.Response.Headers.Add("Content-Type", "application/json"); + return ctx.Response.WriteAsync(JsonConvert.SerializeObject(weatherData)); + }); app.Map("/bh3_os/mdk/shield/api/loadConfig", (HttpContext ctx) => { diff --git a/HttpServer/Models/Config.cs b/HttpServer/Models/Config.cs index 2fa78f3..e19f2c2 100644 --- a/HttpServer/Models/Config.cs +++ b/HttpServer/Models/Config.cs @@ -20,5 +20,42 @@ namespace HttpServer.Models [JsonProperty("minimum")] public string Minimum { get; set; } } + + public partial class Weather + { + [JsonProperty("retcode")] + public int Retcode { get; set; } + + [JsonProperty("message")] + public string Message { get; set; } + + [JsonProperty("data")] + public WeatherData Data { get; set; } + } + + public partial class WeatherData + { + [JsonProperty("timezone")] + public int Timezone { get; set; } + + [JsonProperty("hourly")] + public List Hourly { get; set; } + + public partial class HourlyData + { + [JsonProperty("condition")] + public long Condition { get; set; } + + [JsonProperty("hour")] + public long Hour { get; set; } + + [JsonProperty("date")] + public string Date { get; set; } + + [JsonProperty("temp")] + public long Temp { get; set; } + } + } + } #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. \ No newline at end of file