Skip to content

Commit

Permalink
Correctly bind link uri command to package quality control option; re…
Browse files Browse the repository at this point in the history
…factor SIPackages
  • Loading branch information
VladimirKhil committed Dec 15, 2024
1 parent 5ebee7a commit 9c6f488
Show file tree
Hide file tree
Showing 52 changed files with 887 additions and 1,152 deletions.
43 changes: 1 addition & 42 deletions src/Common/SIPackages/Atom.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using SIPackages.Core;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;

namespace SIPackages;

/// <summary>
/// Defines a question scenario minimal item.
/// </summary>
[Obsolete]
internal sealed class Atom : PropertyChangedNotifier, ITyped, IEquatable<Atom>
internal sealed class Atom
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _type = AtomTypes.Text;
Expand All @@ -36,9 +35,7 @@ public string Type
{
if (_type != value)
{
var oldValue = _type;
_type = value;
OnPropertyChanged(oldValue);
}
}
}
Expand All @@ -54,9 +51,7 @@ public int AtomTime
{
if (_atomTime != value)
{
var oldValue = _atomTime;
_atomTime = value;
OnPropertyChanged(oldValue);
}
}
}
Expand All @@ -71,44 +66,8 @@ public string Text
{
if (_text != value)
{
var oldValue = _text;
_text = value;
OnPropertyChanged(oldValue);
}
}
}

/// <summary>
/// Does the atom text contain specified value.
/// </summary>
/// <param name="value">Text value.</param>
public bool Contains(string value) => _text.IndexOf(value, StringComparison.CurrentCultureIgnoreCase) > -1;

/// <inheritdoc />
public override string ToString()
{
if (_type == AtomTypes.Text)
{
return _text;
}

var res = new StringBuilder();
res.AppendFormat("#{0} ", _type);
res.Append(_text);

return res.ToString();
}

/// <inheritdoc />
public bool Equals(Atom? other) =>
other is not null
&& Type.Equals(other.Type)
&& AtomTime.Equals(other.AtomTime)
&& Text.Equals(other.Text);

/// <inheritdoc />
public override bool Equals(object? obj) => Equals(obj as Atom);

/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Type, AtomTime, Text);
}
47 changes: 0 additions & 47 deletions src/Common/SIPackages/Core/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,5 @@
namespace SIPackages.Core;

/// <summary>
/// Defines search result source kinds.
/// </summary>
public enum ResultKind
{
/// <summary>
/// Object name.
/// </summary>
Name,
/// <summary>
/// Object author.
/// </summary>
Author,
/// <summary>
/// Object source.
/// </summary>
Source,
/// <summary>
/// Object comment.
/// </summary>
Comment,
/// <summary>
/// Object text.
/// </summary>
Text,
/// <summary>
/// Right answers.
/// </summary>
Right,
/// <summary>
/// Wrong answers.
/// </summary>
Wrong,
/// <summary>
/// Question type name.
/// </summary>
TypeName,
/// <summary>
/// Question type parameter name.
/// </summary>
TypeParamName,
/// <summary>
/// Question type parameter value.
/// </summary>
TypeParamValue
}

/// <summary>
/// Defines well-known media types.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/SIPackages/Core/LinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Provides helper methods for working with document links.
/// </summary>
public static class LinkExtensions
internal static class LinkExtensions
{
/// <summary>
/// Extracts link from value.
Expand Down
26 changes: 0 additions & 26 deletions src/Common/SIPackages/Core/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,4 @@ public static string ToCommonString(this List<string> list)

return text.ToString();
}

/// <summary>
/// Checks if any of list items contains the provided pattern.
/// </summary>
/// <param name="list">List to check.</param>
/// <param name="pattern">Pattern to find.</param>
public static bool ContainsQuery(this List<string> list, string pattern) =>
list.Any(item => item.IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase) > -1);

/// <summary>
/// Searches pattern in list values.
/// </summary>
/// <param name="list">List to search.</param>
/// <param name="pattern">Searched pattern.</param>
/// <returns>Search result.</returns>
public static IEnumerable<SearchData> Search(this List<string> list, string pattern)
{
for (int i = 0; i < list.Count; i++)
{
var index = list[i].IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase);
if (index > -1)
{
yield return new SearchData(list[i], index, i);
}
}
}
}
13 changes: 0 additions & 13 deletions src/Common/SIPackages/Core/Named.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ public Named() { }
/// <param name="name">Object name.</param>
public Named(string name) => _name = name;

/// <summary>
/// Detects if the object contains the specified value.
/// </summary>
/// <param name="value">Text value.</param>
public virtual bool Contains(string value) => _name.IndexOf(value, StringComparison.CurrentCultureIgnoreCase) > -1;

/// <summary>
/// Searches a value inside the object.
/// </summary>
/// <param name="value">Value to search.</param>
/// <returns>Search results.</returns>
public virtual IEnumerable<SearchData> Search(string value) => SearchExtensions.Search(ResultKind.Name, _name, value);

/// <inheritdoc />
public override bool Equals(object? obj) => obj is Named named && Name == named.Name;

Expand Down
3 changes: 2 additions & 1 deletion src/Common/SIPackages/Core/QuestionTypeParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/// <summary>
/// Provides well-known question type parameters.
/// </summary>
public static class QuestionTypeParams
[Obsolete]
internal static class QuestionTypeParams
{
/// <summary>
/// Question special theme.
Expand Down
8 changes: 4 additions & 4 deletions src/Common/SIPackages/Core/QuestionTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class QuestionTypes
/// Stake question type.
/// </summary>
[Obsolete("Use Stake question type")]
public const string Auction = "auction";
internal const string Auction = "auction";

/// <summary>
/// Stake question type.
Expand All @@ -35,7 +35,7 @@ public static class QuestionTypes
/// Secret question type.
/// </summary>
[Obsolete("Use Secret question type")]
public const string Cat = "cat";
internal const string Cat = "cat";

/// <summary>
/// Secret question type.
Expand All @@ -56,13 +56,13 @@ public static class QuestionTypes
/// Extended secret question type.
/// </summary>
[Obsolete("Use Secret question type")]
public const string BagCat = "bagcat";
internal const string BagCat = "bagcat";

/// <summary>
/// No-risk question.
/// </summary>
[Obsolete("Use NoRisk question type")]
public const string Sponsored = "sponsored";
internal const string Sponsored = "sponsored";

/// <summary>
/// No-risk question.
Expand Down
32 changes: 32 additions & 0 deletions src/Common/SIPackages/Helpers/InfoOwnerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace SIPackages.Helpers;

internal static class InfoOwnerExtensions
{
/// <summary>
/// Copies info from source object.
/// </summary>
/// <param name="target">Target object.</param>
/// <param name="infoOwner">Source object.</param>
internal static void SetInfoFromOwner(this InfoOwner target, InfoOwner infoOwner)
{
foreach (string s in infoOwner.Info.Authors)
{
target.Info.Authors.Add(s);
}

foreach (string s in infoOwner.Info.Sources)
{
target.Info.Sources.Add(s);
}

target.Info.Comments.Text = infoOwner.Info.Comments.Text;

if (infoOwner.Info.ShowmanComments != null)
{
target.Info.ShowmanComments ??= new Comments();
target.Info.ShowmanComments.Text = infoOwner.Info.ShowmanComments.Text;
}

target.Info.Extension = infoOwner.Info.Extension;
}
}
25 changes: 25 additions & 0 deletions src/Common/SIPackages/Helpers/PackageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace SIPackages.Helpers;

/// <summary>
/// Provides package helper methods.
/// </summary>
public static class PackageExtensions
{
/// <summary>
/// Creates a new round.
/// </summary>
/// <param name="package">Package.</param>
/// <param name="type">Round type.</param>
/// <param name="name">Round name.</param>
public static Round CreateRound(this Package package, string type, string? name)
{
var round = new Round
{
Name = name ?? (package.Rounds.Count + 1).ToString(),
Type = type
};

package.Rounds.Add(round);
return round;
}
}
19 changes: 19 additions & 0 deletions src/Common/SIPackages/Helpers/RoundExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace SIPackages.Helpers;

/// <summary>
/// Provides extension methods for rounds.
/// </summary>
public static class RoundExtensions
{
/// <summary>
/// Creates a new theme.
/// </summary>
/// <param name="round">Round.</param>
/// <param name="name">Theme name.</param>
public static Theme CreateTheme(this Round round, string? name)
{
var theme = new Theme { Name = name ?? "" };
round.Themes.Add(theme);
return theme;
}
}
70 changes: 70 additions & 0 deletions src/Common/SIPackages/Helpers/ThemeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using SIPackages.Core;

namespace SIPackages.Helpers;

/// <summary>
/// Provides extension methods for the <see cref="Theme"/> class.
/// </summary>
public static class ThemeExtensions
{
/// <summary>
/// Creates a new question in the theme.
/// </summary>
/// <param name="theme">Theme.</param>
/// <param name="price">Question price.</param>
/// <param name="isFinal">Does the question belong to the final round.</param>
/// <param name="text">Question text.</param>
public static Question CreateQuestion(this Theme theme, int price = -1, bool isFinal = false, string text = "")
{
int qPrice = DetectQuestionPrice(theme, price, isFinal);

var quest = new Question
{
Price = qPrice
};

quest.Parameters[QuestionParameterNames.Question] = new StepParameter
{
Type = StepParameterTypes.Content,
ContentValue = new List<ContentItem>
{
new() { Type = ContentTypes.Text, Value = text },
}
};

quest.Right.Add("");
theme.Questions.Add(quest);

return quest;
}

private static int DetectQuestionPrice(Theme theme, int price, bool isFinal)
{
if (price != -1)
{
return price;
}

var validQuestions = theme.Questions.Where(q => q.Price != Question.InvalidPrice).ToList();

var questionCount = validQuestions.Count;

if (questionCount > 1)
{
var stepValue = validQuestions[1].Price - validQuestions[0].Price;
return Math.Max(0, validQuestions[questionCount - 1].Price + stepValue);
}

if (questionCount > 0)
{
return validQuestions[0].Price * 2;
}

if (isFinal)
{
return 0;
}

return 100;
}
}
Loading

0 comments on commit 9c6f488

Please sign in to comment.