Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ControlService webapi calls #92

Merged
merged 12 commits into from Sep 6, 2017
52 changes: 52 additions & 0 deletions hybrasyl/ControlService.cs
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using Hybrasyl.Objects;
using StackExchange.Redis;

namespace Hybrasyl
{
[ServiceContract]
public interface IControlService
{
[OperationContract]
[WebGet(UriTemplate = "/Shutdown/{key}")]
string Shutdown(string key);

[OperationContract]
[WebGet(UriTemplate = "/CurrentUsers")]
List<User> CurrentUsers();

[OperationContract]
[WebGet(UriTemplate = "/User/{name}")]
User User(string name);

}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ControlService : IControlService
{
public string Shutdown(string key)
{
if (key == Constants.ShutdownPassword)
{
World.MessageQueue.Add(new HybrasylControlMessage(ControlOpcodes.ShutdownServer, "build"));
return "Shutdown ControlMessage sent to Server.";
}
return "Shutdown ControlMessage not queued.";
}

public List<User> CurrentUsers() => my BadImageFormatExceptionWorld.ActiveUsers.Select(x => x.Value ).ToList();

public User User(string name)
{
return World.ActiveUsers.All(x => x.Value.Name != name) ? null : World.ActiveUsers.Single(x => x.Value.Name == name).Value;
}
}
}
12 changes: 12 additions & 0 deletions hybrasyl/Game.cs
Expand Up @@ -28,6 +28,9 @@
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using System.Xml;
Expand Down Expand Up @@ -199,6 +202,14 @@ public static void Main(string[] args)
LogLevel = Hybrasyl.Constants.DEFAULT_LOG_LEVEL;
Assemblyinfo = new AssemblyInfo(Assembly.GetEntryAssembly());

//set up service endpoint for ControlService

var host = new WebServiceHost(typeof(ControlService), new Uri($"http://localhost:{Constants.ControlServicePort}/ControlService"));

host.Open();
Logger.InfoFormat($"Starting ControlService on port {Constants.ControlServicePort}");


Constants.DataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Hybrasyl");

if (!Directory.Exists(Constants.DataDirectory))
Expand Down Expand Up @@ -380,6 +391,7 @@ public static void Main(string[] args)
// for World, this triggers a logoff for all logged in users and then terminates. After
// termination, the queue consumer is stopped as well.
// For a true restart we'll need to do a few other things; stop timers, etc.
host.Close();
Lobby.Shutdown();
Login.Shutdown();
World.Shutdown();
Expand Down
3 changes: 3 additions & 0 deletions hybrasyl/Hybrasyl.csproj
Expand Up @@ -60,6 +60,8 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="XmlSchemaClassGenerator, Version=1.0.20.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\XmlSchemaClassGenerator-beta.1.0.20\lib\net45\XmlSchemaClassGenerator.dll</HintPath>
<Private>True</Private>
Expand All @@ -69,6 +71,7 @@
<Compile Include="Book.cs" />
<Compile Include="Board.cs" />
<Compile Include="Compression.cs" />
<Compile Include="ControlService.cs" />
<Compile Include="Objects\Creature.cs" />
<Compile Include="Objects\Door.cs" />
<Compile Include="Objects\Gold.cs" />
Expand Down
2 changes: 1 addition & 1 deletion hybrasyl/Server.cs
Expand Up @@ -212,7 +212,7 @@ public virtual void AcceptConnection(IAsyncResult ar)
}

public void SendCallback(IAsyncResult ar)
{
{

ClientState state = (ClientState) ar.AsyncState;
Client client;
Expand Down
4 changes: 2 additions & 2 deletions hybrasyl/Time.cs
Expand Up @@ -98,8 +98,8 @@ public long HybrasylTicks

public long TerranTicks => HybrasylTicks / 8;

public static string DefaultAge => Game.Config != null ? Game.Config.Time.ServerStart.DefaultAge != string.Empty ? Game.Config.Time.ServerStart.DefaultAge : "Hybrasyl" : "Hybrasyl";
public static int DefaultYear => Game.Config != null ? Game.Config.Time.ServerStart.DefaultYear != 1 ? Game.Config.Time.ServerStart.DefaultYear : 1 : 1;
public static string DefaultAge => Game.Config != null ? Game.Config.Time?.ServerStart?.DefaultAge != string.Empty ? Game.Config.Time?.ServerStart?.DefaultAge : "Hybrasyl" : "Hybrasyl";
public static int DefaultYear => Game.Config != null ? Game.Config.Time?.ServerStart?.DefaultYear != 1 ? Game.Config.Time.ServerStart.DefaultYear : 1 : 1;

public static bool ValidAge(string age)
{
Expand Down
1 change: 1 addition & 0 deletions hybrasyl/Utility.cs
Expand Up @@ -395,6 +395,7 @@ static class Constants
// This is a dirty hack until we have better role / auth support

public const string ShutdownPassword = "batterystaple8!";
public const int ControlServicePort = 4949;

// Dialog settings
// Dialog sequence IDs between 1 and DIALOG_SEQUENCE_SHARED are processed as
Expand Down