Add create game api (#241)

This commit is contained in:
js6pak authored and GitHub committed 2021-03-04 09:11:50 +00:00
1 parent be19b1524b
commit e769b874c8
11 files changed
+78 -52

No files matched your search

@@ -8,7 +8,7 @@ namespace Impostor.Api.Games
{
public static int GetGameCount(this IGameManager manager, MapFlags map)
{
return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << game.Options.MapId)));
return manager.Games.Count(game => map.HasFlag((MapFlags)(1 << (byte)game.Options.Map)));
}
}
}
+13 -1
View File
@@ -24,10 +24,15 @@ namespace Impostor.Api.Games
int PlayerCount { get; }
IClientPlayer Host { get; }
IClientPlayer? Host { get; }
bool IsPublic { get; }
/// <summary>
/// Gets or sets display name on game list.
/// </summary>
string? DisplayName { get; set; }
IDictionary<object, object> Items { get; }
int HostId { get; }
@@ -55,6 +60,13 @@ namespace Impostor.Api.Games
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
ValueTask SyncSettingsAsync();
/// <summary>
/// Sets game's privacy.
/// </summary>
/// <param name="isPublic">Privacy to set.</param>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous operation.</returns>
ValueTask SetPrivacyAsync(bool isPublic);
/// <summary>
/// Send the message to all players.
/// </summary>
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Impostor.Api.Innersloth;
namespace Impostor.Api.Games.Managers
{
@@ -7,5 +9,7 @@ namespace Impostor.Api.Games.Managers
IEnumerable<IGame> Games { get; }
IGame? Find(GameCode code);
ValueTask<IGame> CreateAsync(GameOptionsData options);
}
}
+25 -40
View File
@@ -14,112 +14,97 @@ namespace Impostor.Api.Innersloth
/// <summary>
/// Gets or sets host's version of the game.
/// </summary>
public byte Version { get; set; }
public byte Version { get; set; } = LatestVersion;
/// <summary>
/// Gets or sets the maximum amount of players for this lobby.
/// </summary>
public byte MaxPlayers { get; set; }
public byte MaxPlayers { get; set; } = 10;
/// <summary>
/// Gets or sets the language of the lobby as per <see cref="GameKeywords"/> enum.
/// </summary>
public GameKeywords Keywords { get; set; }
public GameKeywords Keywords { get; set; } = GameKeywords.English;
/// <summary>
/// Gets or sets the MapId selected for this lobby
/// Gets or sets the Map selected for this lobby.
/// </summary>
/// <remarks>
/// Skeld = 0, MiraHQ = 1, Polus = 2.
/// </remarks>
internal byte MapId { get; set; }
/// <summary>
/// Gets or sets the map selected for this lobby
/// </summary>
public MapTypes Map
{
get => (MapTypes)MapId;
set => MapId = (byte)value;
}
public MapTypes Map { get; set; } = MapTypes.Skeld;
/// <summary>
/// Gets or sets the Player speed modifier.
/// </summary>
public float PlayerSpeedMod { get; set; }
public float PlayerSpeedMod { get; set; } = 1f;
/// <summary>
/// Gets or sets the Light modifier for the players that are members of the crew as a multiplier value.
/// </summary>
public float CrewLightMod { get; set; }
public float CrewLightMod { get; set; } = 1f;
/// <summary>
/// Gets or sets the Light modifier for the players that are Impostors as a multiplier value.
/// </summary>
public float ImpostorLightMod { get; set; }
public float ImpostorLightMod { get; set; } = 1f;
/// <summary>
/// Gets or sets the Impostor cooldown to kill in seconds.
/// </summary>
public float KillCooldown { get; set; }
public float KillCooldown { get; set; } = 15f;
/// <summary>
/// Gets or sets the number of common tasks.
/// </summary>
public int NumCommonTasks { get; set; }
public int NumCommonTasks { get; set; } = 1;
/// <summary>
/// Gets or sets the number of long tasks.
/// </summary>
public int NumLongTasks { get; set; }
public int NumLongTasks { get; set; } = 1;
/// <summary>
/// Gets or sets the number of short tasks.
/// </summary>
public int NumShortTasks { get; set; }
public int NumShortTasks { get; set; } = 2;
/// <summary>
/// Gets or sets the maximum amount of emergency meetings each player can call during the game in seconds.
/// </summary>
public int NumEmergencyMeetings { get; set; }
public int NumEmergencyMeetings { get; set; } = 1;
/// <summary>
/// Gets or sets the cooldown between each time any player can call an emergency meeting in seconds.
/// </summary>
public int EmergencyCooldown { get; set; }
public int EmergencyCooldown { get; set; } = 15;
/// <summary>
/// Gets or sets the number of impostors for this lobby.
/// </summary>
public int NumImpostors { get; set; }
public int NumImpostors { get; set; } = 1;
/// <summary>
/// Gets or sets a value indicating whether ghosts (dead crew members) can do tasks.
/// </summary>
public bool GhostsDoTasks { get; set; }
public bool GhostsDoTasks { get; set; } = true;
/// <summary>
/// Gets or sets the Kill as per values in <see cref="KillDistances"/>.
/// </summary>
/// <remarks>
/// Short = 0, Normal = 1, Long = 2.
/// </remarks>
public KillDistances KillDistance { get; set; }
public KillDistances KillDistance { get; set; } = KillDistances.Normal;
/// <summary>
/// Gets or sets the time for discussion before voting time in seconds.
/// </summary>
public int DiscussionTime { get; set; }
public int DiscussionTime { get; set; } = 15;
/// <summary>
/// Gets or sets the time for voting in seconds.
/// </summary>
public int VotingTime { get; set; }
public int VotingTime { get; set; } = 120;
/// <summary>
/// Gets or sets a value indicating whether an ejected player is an impostor or not.
/// </summary>
public bool ConfirmImpostor { get; set; }
public bool ConfirmImpostor { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether players are able to see tasks being performed by other players.
@@ -127,7 +112,7 @@ namespace Impostor.Api.Innersloth
/// <remarks>
/// By being set to true, tasks such as Empty Garbage, Submit Scan, Clear asteroids, Prime shields execution will be visible to other players.
/// </remarks>
public bool VisualTasks { get; set; }
public bool VisualTasks { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the vote is anonymous.
@@ -137,12 +122,12 @@ namespace Impostor.Api.Innersloth
/// <summary>
/// Gets or sets the task bar update mode as per values in <see cref="Innersloth.TaskBarUpdate"/>.
/// </summary>
public TaskBarUpdate TaskBarUpdate { get; set; }
public TaskBarUpdate TaskBarUpdate { get; set; } = TaskBarUpdate.Always;
/// <summary>
/// Gets or sets a value indicating whether the GameOptions are the default ones.
/// </summary>
public bool IsDefaults { get; set; }
public bool IsDefaults { get; set; } = true;
/// <summary>
/// Deserialize a packet/message to a new GameOptionsData object.
@@ -166,7 +151,7 @@ namespace Impostor.Api.Innersloth
writer.Write((byte)version);
writer.Write((byte)MaxPlayers);
writer.Write((uint)Keywords);
writer.Write((byte)MapId);
writer.Write((byte)Map);
writer.Write((float)PlayerSpeedMod);
writer.Write((float)CrewLightMod);
writer.Write((float)ImpostorLightMod);
@@ -223,7 +208,7 @@ namespace Impostor.Api.Innersloth
Version = bytes.ReadByte();
MaxPlayers = bytes.ReadByte();
Keywords = (GameKeywords)bytes.ReadUInt32();
MapId = bytes.ReadByte();
Map = (MapTypes)bytes.ReadByte();
PlayerSpeedMod = bytes.ReadSingle();
CrewLightMod = bytes.ReadSingle();
+1 -1
View File
@@ -1,6 +1,6 @@
namespace Impostor.Api.Innersloth
{
public enum MapTypes
public enum MapTypes : byte
{
Skeld = 0,
MiraHQ = 1,
@@ -25,10 +25,10 @@ namespace Impostor.Api.Net.Messages.S2C
writer.Write(game.PublicIp.Address);
writer.Write((ushort)game.PublicIp.Port);
writer.Write(game.Code);
writer.Write(game.Host.Client.Name);
writer.Write(game.DisplayName ?? game.Host?.Client.Name ?? string.Empty);
writer.Write((byte)game.PlayerCount);
writer.WritePacked(1); // TODO: What does Age do?
writer.Write((byte)game.Options.MapId);
writer.Write((byte)game.Options.Map);
writer.Write((byte)game.Options.NumImpostors);
writer.Write((byte)game.Options.MaxPlayers);
writer.EndMessage();
@@ -43,4 +43,4 @@ namespace Impostor.Api.Net.Messages.S2C
throw new System.NotImplementedException();
}
}
}
}
+12 -3
View File
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using Impostor.Api.Games.Managers;
using Impostor.Api.Innersloth;
using Impostor.Api.Plugins;
using Microsoft.Extensions.Logging;
@@ -12,16 +14,23 @@ namespace Impostor.Plugins.Example
public class ExamplePlugin : PluginBase
{
private readonly ILogger<ExamplePlugin> _logger;
private readonly IGameManager _gameManager;
public ExamplePlugin(ILogger<ExamplePlugin> logger)
public ExamplePlugin(ILogger<ExamplePlugin> logger, IGameManager gameManager)
{
_logger = logger;
_gameManager = gameManager;
}
public override ValueTask EnableAsync()
public override async ValueTask EnableAsync()
{
_logger.LogInformation("Example is being enabled.");
return default;
var game = await _gameManager.CreateAsync(new GameOptionsData());
game.DisplayName = "Example game";
await game.SetPrivacyAsync(true);
_logger.LogInformation("Created game {0}.", game.Code.Code);
}
public override ValueTask DisableAsync()
+1 -1
View File
@@ -332,7 +332,7 @@ namespace Impostor.Server.Net
{
using var message = MessageWriter.Get(MessageType.Reliable);
var games = _gameManager.FindListings((MapFlags)options.MapId, options.NumImpostors, options.Keywords);
var games = _gameManager.FindListings((MapFlags)options.Map, options.NumImpostors, options.Keywords);
var skeldGameCount = _gameManager.GetGameCount(MapFlags.Skeld);
var miraHqGameCount = _gameManager.GetGameCount(MapFlags.MiraHQ);
@@ -98,7 +98,7 @@ namespace Impostor.Server.Net.Manager
x.Value.PlayerCount < x.Value.Options.MaxPlayers))
{
// Check for options.
if (!map.HasFlag((MapFlags)(1 << game.Options.MapId)))
if (!map.HasFlag((MapFlags)(1 << (byte)game.Options.Map)))
{
continue;
}
+15 -1
View File
@@ -6,13 +6,15 @@ using Impostor.Api.Games;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Messages;
using Impostor.Hazel;
using Impostor.Server.Net.Inner;
namespace Impostor.Server.Net.State
{
internal partial class Game : IGame
{
IClientPlayer IGame.Host => Host;
IClientPlayer? IGame.Host => Host;
IGameNet IGame.GameNet => GameNet;
@@ -44,5 +46,17 @@ namespace Impostor.Server.Net.State
await FinishRpcAsync(writer);
}
}
public async ValueTask SetPrivacyAsync(bool isPublic)
{
IsPublic = isPublic;
using (var writer = MessageWriter.Get(MessageType.Reliable))
{
WriteAlterGameMessage(writer, false, IsPublic);
await SendToAllAsync(writer);
}
}
}
}
+2
View File
@@ -60,6 +60,8 @@ namespace Impostor.Server.Net.State
public bool IsPublic { get; private set; }
public string? DisplayName { get; set; }
public int HostId { get; private set; }
public GameStates GameState { get; private set; }