Skip to content

Commit

Permalink
Merge pull request #16 from MrDave1999/feat/ranks
Browse files Browse the repository at this point in the history
feat: Create the rank module
  • Loading branch information
MrDave1999 authored Aug 8, 2024
2 parents 45115b3 + ade68f5 commit 2cf5ad9
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 101 deletions.
4 changes: 4 additions & 0 deletions src/Application/CTF.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<PackageReference Include="SampSharp.Entities" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="CTF.Application.Tests" />
</ItemGroup>

<ItemGroup>
<Compile Update="Common\Resources\Messages.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
9 changes: 9 additions & 0 deletions src/Application/Common/Resources/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

216 changes: 119 additions & 97 deletions src/Application/Common/Resources/Messages.resx
Original file line number Diff line number Diff line change
@@ -1,101 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="InvalidRank" xml:space="preserve">
<value>Invalid range has been passed</value>
</data>
</root>
10 changes: 10 additions & 0 deletions src/Application/Players/Ranks/IRank.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CTF.Application.Players.Ranks;

internal interface IRank
{
public RankId Id { get; }
public string Name { get; }
public int RequiredKills { get; }
public bool IsMax();
public bool IsNotMax();
}
64 changes: 64 additions & 0 deletions src/Application/Players/Ranks/RankCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
namespace CTF.Application.Players.Ranks;

internal class RankCollection
{
private static readonly Rank[] s_ranks =
[
new(RankId.Noob, 0),
new(RankId.Medium, 50),
new(RankId.Junior, 100),
new(RankId.SemiAdvance, 150),
new(RankId.Advanced, 200),
new(RankId.Hitman, 250),
new(RankId.Extreme, 300),
new(RankId.Annihilator, 350),
new(RankId.Maniac, 400),
new(RankId.Invincible, 450),
new(RankId.Senior, 500),
new(RankId.GameMaster, 550),
new(RankId.Professional, 600),
new(RankId.SuperPro, 650),
new(RankId.Legendary, 700)
];

private RankCollection() { }
public static int Max => s_ranks.Length;
public static IEnumerable<IRank> GetAll() => s_ranks;
public static Result<IRank> GetById(RankId id)
{
if ((int)id < 0 || (int)id >= Max)
return Result<IRank>.Failure(Messages.InvalidRank);

Rank rank = s_ranks[(int)id];
return Result<IRank>.Success(rank);
}
public static Result<IRank> GetNextRank(RankId previous)
{
if ((int)previous < 0 || (int)previous >= Max)
return Result<IRank>.Failure(Messages.InvalidRank);

Rank rank = ((int)previous + 1 == Max) ?
Rank.None :
s_ranks[(int)previous + 1];

return Result<IRank>.Success(rank);
}

private class Rank : IRank
{
public static readonly Rank None = new();
public RankId Id { get; } = (RankId)(-1);
public string Name { get; } = "None";
public int RequiredKills { get; }

public Rank() { }
public Rank(RankId id, int requiredKills)
{
Id = id;
Name = id.ToString();
RequiredKills = requiredKills;
}
public bool IsMax() => Max == (int)Id + 1;
public bool IsNotMax() => !IsMax();
}
}
20 changes: 20 additions & 0 deletions src/Application/Players/Ranks/RankId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace CTF.Application.Players.Ranks;

internal enum RankId
{
Noob,
Medium,
Junior,
SemiAdvance,
Advanced,
Hitman,
Extreme,
Annihilator,
Maniac,
Invincible,
Senior,
GameMaster,
Professional,
SuperPro,
Legendary
}
24 changes: 24 additions & 0 deletions src/Application/Players/Ranks/RankSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace CTF.Application.Players.Ranks;

internal class RankSystem : ISystem
{
[PlayerCommand("ranks")]
public void ViewRanks(Player player, IDialogService dialogService)
{
var columnHeaders = new[]
{
"Rank",
"Total Required Kills"
};
var dialog = new TablistDialog(
caption: "Ranks",
button1: "Close",
button2: null,
columnHeaders);
var ranks = RankCollection.GetAll();
foreach (IRank rank in ranks)
dialog.Add(rank.Name, rank.RequiredKills.ToString());

dialogService.Show(player, dialog);
}
}
5 changes: 4 additions & 1 deletion src/Application/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
global using SampSharp.Entities;
global using SampSharp.Entities.SAMP;
global using SampSharp.Entities.SAMP.Commands;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection;

global using CTF.Application.Common;
global using CTF.Application.Common.Resources;
4 changes: 2 additions & 2 deletions src/Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public void Configure(IServiceCollection services)
services.AddPersistenceSQLiteServices(configuration);
services.AddApplicationServices();

// TODO: Add services and systems to the services collection
services.AddSystem<System1>();
// Add systems to the services collection
services.AddSystemsInAssembly(typeof(ApplicationServicesExtensions).Assembly);
}

public void Configure(IEcsBuilder builder)
Expand Down
Loading

0 comments on commit 2cf5ad9

Please sign in to comment.