Skip to content

Commit 9c6f488

Browse files
committed
Correctly bind link uri command to package quality control option; refactor SIPackages
1 parent 5ebee7a commit 9c6f488

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+887
-1152
lines changed

src/Common/SIPackages/Atom.cs

+1-42
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using SIPackages.Core;
22
using System.ComponentModel;
33
using System.Diagnostics;
4-
using System.Text;
54

65
namespace SIPackages;
76

87
/// <summary>
98
/// Defines a question scenario minimal item.
109
/// </summary>
1110
[Obsolete]
12-
internal sealed class Atom : PropertyChangedNotifier, ITyped, IEquatable<Atom>
11+
internal sealed class Atom
1312
{
1413
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1514
private string _type = AtomTypes.Text;
@@ -36,9 +35,7 @@ public string Type
3635
{
3736
if (_type != value)
3837
{
39-
var oldValue = _type;
4038
_type = value;
41-
OnPropertyChanged(oldValue);
4239
}
4340
}
4441
}
@@ -54,9 +51,7 @@ public int AtomTime
5451
{
5552
if (_atomTime != value)
5653
{
57-
var oldValue = _atomTime;
5854
_atomTime = value;
59-
OnPropertyChanged(oldValue);
6055
}
6156
}
6257
}
@@ -71,44 +66,8 @@ public string Text
7166
{
7267
if (_text != value)
7368
{
74-
var oldValue = _text;
7569
_text = value;
76-
OnPropertyChanged(oldValue);
7770
}
7871
}
7972
}
80-
81-
/// <summary>
82-
/// Does the atom text contain specified value.
83-
/// </summary>
84-
/// <param name="value">Text value.</param>
85-
public bool Contains(string value) => _text.IndexOf(value, StringComparison.CurrentCultureIgnoreCase) > -1;
86-
87-
/// <inheritdoc />
88-
public override string ToString()
89-
{
90-
if (_type == AtomTypes.Text)
91-
{
92-
return _text;
93-
}
94-
95-
var res = new StringBuilder();
96-
res.AppendFormat("#{0} ", _type);
97-
res.Append(_text);
98-
99-
return res.ToString();
100-
}
101-
102-
/// <inheritdoc />
103-
public bool Equals(Atom? other) =>
104-
other is not null
105-
&& Type.Equals(other.Type)
106-
&& AtomTime.Equals(other.AtomTime)
107-
&& Text.Equals(other.Text);
108-
109-
/// <inheritdoc />
110-
public override bool Equals(object? obj) => Equals(obj as Atom);
111-
112-
/// <inheritdoc />
113-
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Type, AtomTime, Text);
11473
}

src/Common/SIPackages/Core/Enums.cs

-47
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,5 @@
11
namespace SIPackages.Core;
22

3-
/// <summary>
4-
/// Defines search result source kinds.
5-
/// </summary>
6-
public enum ResultKind
7-
{
8-
/// <summary>
9-
/// Object name.
10-
/// </summary>
11-
Name,
12-
/// <summary>
13-
/// Object author.
14-
/// </summary>
15-
Author,
16-
/// <summary>
17-
/// Object source.
18-
/// </summary>
19-
Source,
20-
/// <summary>
21-
/// Object comment.
22-
/// </summary>
23-
Comment,
24-
/// <summary>
25-
/// Object text.
26-
/// </summary>
27-
Text,
28-
/// <summary>
29-
/// Right answers.
30-
/// </summary>
31-
Right,
32-
/// <summary>
33-
/// Wrong answers.
34-
/// </summary>
35-
Wrong,
36-
/// <summary>
37-
/// Question type name.
38-
/// </summary>
39-
TypeName,
40-
/// <summary>
41-
/// Question type parameter name.
42-
/// </summary>
43-
TypeParamName,
44-
/// <summary>
45-
/// Question type parameter value.
46-
/// </summary>
47-
TypeParamValue
48-
}
49-
503
/// <summary>
514
/// Defines well-known media types.
525
/// </summary>

src/Common/SIPackages/Core/LinkExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <summary>
44
/// Provides helper methods for working with document links.
55
/// </summary>
6-
public static class LinkExtensions
6+
internal static class LinkExtensions
77
{
88
/// <summary>
99
/// Extracts link from value.

src/Common/SIPackages/Core/ListExtensions.cs

-26
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,4 @@ public static string ToCommonString(this List<string> list)
2929

3030
return text.ToString();
3131
}
32-
33-
/// <summary>
34-
/// Checks if any of list items contains the provided pattern.
35-
/// </summary>
36-
/// <param name="list">List to check.</param>
37-
/// <param name="pattern">Pattern to find.</param>
38-
public static bool ContainsQuery(this List<string> list, string pattern) =>
39-
list.Any(item => item.IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase) > -1);
40-
41-
/// <summary>
42-
/// Searches pattern in list values.
43-
/// </summary>
44-
/// <param name="list">List to search.</param>
45-
/// <param name="pattern">Searched pattern.</param>
46-
/// <returns>Search result.</returns>
47-
public static IEnumerable<SearchData> Search(this List<string> list, string pattern)
48-
{
49-
for (int i = 0; i < list.Count; i++)
50-
{
51-
var index = list[i].IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase);
52-
if (index > -1)
53-
{
54-
yield return new SearchData(list[i], index, i);
55-
}
56-
}
57-
}
5832
}

src/Common/SIPackages/Core/Named.cs

-13
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ public Named() { }
3434
/// <param name="name">Object name.</param>
3535
public Named(string name) => _name = name;
3636

37-
/// <summary>
38-
/// Detects if the object contains the specified value.
39-
/// </summary>
40-
/// <param name="value">Text value.</param>
41-
public virtual bool Contains(string value) => _name.IndexOf(value, StringComparison.CurrentCultureIgnoreCase) > -1;
42-
43-
/// <summary>
44-
/// Searches a value inside the object.
45-
/// </summary>
46-
/// <param name="value">Value to search.</param>
47-
/// <returns>Search results.</returns>
48-
public virtual IEnumerable<SearchData> Search(string value) => SearchExtensions.Search(ResultKind.Name, _name, value);
49-
5037
/// <inheritdoc />
5138
public override bool Equals(object? obj) => obj is Named named && Name == named.Name;
5239

src/Common/SIPackages/Core/QuestionTypeParams.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/// <summary>
44
/// Provides well-known question type parameters.
55
/// </summary>
6-
public static class QuestionTypeParams
6+
[Obsolete]
7+
internal static class QuestionTypeParams
78
{
89
/// <summary>
910
/// Question special theme.

src/Common/SIPackages/Core/QuestionTypes.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static class QuestionTypes
1919
/// Stake question type.
2020
/// </summary>
2121
[Obsolete("Use Stake question type")]
22-
public const string Auction = "auction";
22+
internal const string Auction = "auction";
2323

2424
/// <summary>
2525
/// Stake question type.
@@ -35,7 +35,7 @@ public static class QuestionTypes
3535
/// Secret question type.
3636
/// </summary>
3737
[Obsolete("Use Secret question type")]
38-
public const string Cat = "cat";
38+
internal const string Cat = "cat";
3939

4040
/// <summary>
4141
/// Secret question type.
@@ -56,13 +56,13 @@ public static class QuestionTypes
5656
/// Extended secret question type.
5757
/// </summary>
5858
[Obsolete("Use Secret question type")]
59-
public const string BagCat = "bagcat";
59+
internal const string BagCat = "bagcat";
6060

6161
/// <summary>
6262
/// No-risk question.
6363
/// </summary>
6464
[Obsolete("Use NoRisk question type")]
65-
public const string Sponsored = "sponsored";
65+
internal const string Sponsored = "sponsored";
6666

6767
/// <summary>
6868
/// No-risk question.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace SIPackages.Helpers;
2+
3+
internal static class InfoOwnerExtensions
4+
{
5+
/// <summary>
6+
/// Copies info from source object.
7+
/// </summary>
8+
/// <param name="target">Target object.</param>
9+
/// <param name="infoOwner">Source object.</param>
10+
internal static void SetInfoFromOwner(this InfoOwner target, InfoOwner infoOwner)
11+
{
12+
foreach (string s in infoOwner.Info.Authors)
13+
{
14+
target.Info.Authors.Add(s);
15+
}
16+
17+
foreach (string s in infoOwner.Info.Sources)
18+
{
19+
target.Info.Sources.Add(s);
20+
}
21+
22+
target.Info.Comments.Text = infoOwner.Info.Comments.Text;
23+
24+
if (infoOwner.Info.ShowmanComments != null)
25+
{
26+
target.Info.ShowmanComments ??= new Comments();
27+
target.Info.ShowmanComments.Text = infoOwner.Info.ShowmanComments.Text;
28+
}
29+
30+
target.Info.Extension = infoOwner.Info.Extension;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace SIPackages.Helpers;
2+
3+
/// <summary>
4+
/// Provides package helper methods.
5+
/// </summary>
6+
public static class PackageExtensions
7+
{
8+
/// <summary>
9+
/// Creates a new round.
10+
/// </summary>
11+
/// <param name="package">Package.</param>
12+
/// <param name="type">Round type.</param>
13+
/// <param name="name">Round name.</param>
14+
public static Round CreateRound(this Package package, string type, string? name)
15+
{
16+
var round = new Round
17+
{
18+
Name = name ?? (package.Rounds.Count + 1).ToString(),
19+
Type = type
20+
};
21+
22+
package.Rounds.Add(round);
23+
return round;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace SIPackages.Helpers;
2+
3+
/// <summary>
4+
/// Provides extension methods for rounds.
5+
/// </summary>
6+
public static class RoundExtensions
7+
{
8+
/// <summary>
9+
/// Creates a new theme.
10+
/// </summary>
11+
/// <param name="round">Round.</param>
12+
/// <param name="name">Theme name.</param>
13+
public static Theme CreateTheme(this Round round, string? name)
14+
{
15+
var theme = new Theme { Name = name ?? "" };
16+
round.Themes.Add(theme);
17+
return theme;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using SIPackages.Core;
2+
3+
namespace SIPackages.Helpers;
4+
5+
/// <summary>
6+
/// Provides extension methods for the <see cref="Theme"/> class.
7+
/// </summary>
8+
public static class ThemeExtensions
9+
{
10+
/// <summary>
11+
/// Creates a new question in the theme.
12+
/// </summary>
13+
/// <param name="theme">Theme.</param>
14+
/// <param name="price">Question price.</param>
15+
/// <param name="isFinal">Does the question belong to the final round.</param>
16+
/// <param name="text">Question text.</param>
17+
public static Question CreateQuestion(this Theme theme, int price = -1, bool isFinal = false, string text = "")
18+
{
19+
int qPrice = DetectQuestionPrice(theme, price, isFinal);
20+
21+
var quest = new Question
22+
{
23+
Price = qPrice
24+
};
25+
26+
quest.Parameters[QuestionParameterNames.Question] = new StepParameter
27+
{
28+
Type = StepParameterTypes.Content,
29+
ContentValue = new List<ContentItem>
30+
{
31+
new() { Type = ContentTypes.Text, Value = text },
32+
}
33+
};
34+
35+
quest.Right.Add("");
36+
theme.Questions.Add(quest);
37+
38+
return quest;
39+
}
40+
41+
private static int DetectQuestionPrice(Theme theme, int price, bool isFinal)
42+
{
43+
if (price != -1)
44+
{
45+
return price;
46+
}
47+
48+
var validQuestions = theme.Questions.Where(q => q.Price != Question.InvalidPrice).ToList();
49+
50+
var questionCount = validQuestions.Count;
51+
52+
if (questionCount > 1)
53+
{
54+
var stepValue = validQuestions[1].Price - validQuestions[0].Price;
55+
return Math.Max(0, validQuestions[questionCount - 1].Price + stepValue);
56+
}
57+
58+
if (questionCount > 0)
59+
{
60+
return validQuestions[0].Price * 2;
61+
}
62+
63+
if (isFinal)
64+
{
65+
return 0;
66+
}
67+
68+
return 100;
69+
}
70+
}

0 commit comments

Comments
 (0)