Skip to content

Commit c58d033

Browse files
committed
SIQuester 5.9.0
Package tags property is always visible. Secret question cost editor added. Flat view enhancements. Improved text import.
1 parent 7833100 commit c58d033

Some content is hidden

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

51 files changed

+2959
-973
lines changed

src/Common/SIPackages/Core/BagCatHelper.cs

-48
This file was deleted.

src/Common/SIPackages/Core/BagCatInfo.cs src/Common/SIPackages/Core/NumberSet.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using System.ComponentModel;
22
using System.Runtime.CompilerServices;
3+
using SIPackages.TypeConverters;
34

45
namespace SIPackages.Core;
56

7+
// TODO: sealed
8+
69
/// <summary>
7-
/// Defines a secret question info.
10+
/// Defines a set of numbers.
811
/// </summary>
9-
public class BagCatInfo : INotifyPropertyChanged
12+
[TypeConverter(typeof(NumberSetTypeConverter))]
13+
public class NumberSet : INotifyPropertyChanged
1014
{
1115
private int _minimum = 0;
1216

1317
/// <summary>
14-
/// Minimum stake value.
18+
/// Minimum value.
1519
/// </summary>
1620
public int Minimum
1721
{
@@ -22,7 +26,7 @@ public int Minimum
2226
private int _maximum = 0;
2327

2428
/// <summary>
25-
/// Maximum stake value.
29+
/// Maximum value.
2630
/// </summary>
2731
public int Maximum
2832
{
@@ -33,7 +37,7 @@ public int Maximum
3337
private int _step = 0;
3438

3539
/// <summary>
36-
/// Step (a minimum distance between two possible stakes) value.
40+
/// Step (a minimum distance between two possible nubmbers) value.
3741
/// </summary>
3842
public int Step
3943
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.ComponentModel;
2+
using System.Globalization;
3+
using System.Text.RegularExpressions;
4+
using SIPackages.Core;
5+
6+
namespace SIPackages.TypeConverters;
7+
8+
/// <summary>
9+
/// Provides helper method for parsing <see cref="NumberSet" /> type.
10+
/// </summary>
11+
/// <inheritdoc />
12+
public sealed class NumberSetTypeConverter : TypeConverter
13+
{
14+
private static readonly Regex NumberSetRegex = new(@"\[(?'min'\d+);(?'max'\d+)\](/(?'step'\d+))?", RegexOptions.Compiled);
15+
16+
/// <inheritdoc />
17+
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) =>
18+
sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
19+
20+
/// <inheritdoc />
21+
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) =>
22+
value is string stringValue ? ParseNumberSet(stringValue) : base.ConvertFrom(context, culture, value);
23+
24+
/// <inheritdoc />
25+
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) =>
26+
destinationType == typeof(string) && base.CanConvertTo(context, destinationType);
27+
28+
/// <inheritdoc />
29+
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) =>
30+
destinationType == typeof(string) && value is NumberSet numberSet
31+
? SerializeNumberSet(numberSet)
32+
: base.ConvertTo(context, culture, value, destinationType);
33+
34+
/// <summary>
35+
/// Parses number set from serialized syntax.
36+
/// Number set syntax examples are "[100;500]" or "[200;1000]/200".
37+
/// </summary>
38+
/// <param name="value">Number set value.</param>
39+
/// <returns>Parsed number set.</returns>
40+
public static NumberSet? ParseNumberSet(string value)
41+
{
42+
if (int.TryParse(value, out var singleNumber))
43+
{
44+
return new NumberSet
45+
{
46+
Minimum = singleNumber,
47+
Maximum = singleNumber,
48+
Step = 0
49+
};
50+
}
51+
52+
var match = NumberSetRegex.Match(value);
53+
54+
if (!match.Success)
55+
{
56+
return null;
57+
}
58+
59+
_ = int.TryParse(match.Groups["min"].Value, out var minimum);
60+
_ = int.TryParse(match.Groups["max"].Value, out var maximum);
61+
var stepString = match.Groups["step"].Value;
62+
63+
return new NumberSet
64+
{
65+
Minimum = minimum,
66+
Maximum = maximum,
67+
Step = GetStepValue(minimum, maximum, stepString)
68+
};
69+
}
70+
71+
/// <summary>
72+
/// Converts number set to string.
73+
/// </summary>
74+
/// <param name="numberSet">Number set to convert.</param>
75+
/// <returns>Number set string representation.</returns>
76+
public static string SerializeNumberSet(NumberSet numberSet) =>
77+
numberSet.Minimum == numberSet.Maximum
78+
? numberSet.Minimum.ToString()
79+
: ((numberSet.Step == numberSet.Maximum - numberSet.Minimum || numberSet.Step == 0)
80+
? $"[{numberSet.Minimum};{numberSet.Maximum}]"
81+
: $"[{numberSet.Minimum};{numberSet.Maximum}]/{numberSet.Step}");
82+
83+
private static int GetStepValue(int minimum, int maximum, string stepString) =>
84+
stepString.Length > 0 && int.TryParse(stepString, out var step) ? step : maximum - minimum;
85+
}

src/SICore/SICore/Clients/Game/GameData.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ internal int ChooserIndex
132132
/// </summary>
133133
internal QuestionType Type { get; set; }
134134

135-
private BagCatInfo _catInfo = null;
135+
private NumberSet? _catInfo = null;
136136

137-
public BagCatInfo CatInfo
137+
public NumberSet? CatInfo
138138
{
139139
get => _catInfo;
140140
set

src/SICore/SICore/Clients/Game/GameLogic.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SIPackages;
88
using SIPackages.Core;
99
using SIPackages.Providers;
10+
using SIPackages.TypeConverters;
1011
using SIUI.Model;
1112
using System;
1213
using System.Collections.Generic;
@@ -4489,7 +4490,7 @@ private void PrintSecretQuestionInfo()
44894490
{
44904491
_data.CurPriceWrong = _data.CurPriceRight = questionPrice;
44914492

4492-
_data.CatInfo = new BagCatInfo
4493+
_data.CatInfo = new NumberSet
44934494
{
44944495
Minimum = _data.CurPriceRight,
44954496
Maximum = _data.CurPriceRight
@@ -4499,15 +4500,15 @@ private void PrintSecretQuestionInfo()
44994500
}
45004501
else if (_data.Type.Name == QuestionTypes.Cat)
45014502
{
4502-
_data.CatInfo = new BagCatInfo();
4503+
_data.CatInfo = new NumberSet();
45034504
add = _data.Question.Price.ToString();
45044505
_data.CurPriceRight = _data.Question.Price;
45054506
_data.CurPriceWrong = _data.CurPriceRight;
45064507
}
45074508
else
45084509
{
45094510
_data.CurPriceRight = -1;
4510-
_data.CatInfo = BagCatHelper.ParseCatCost(cost);
4511+
_data.CatInfo = NumberSetTypeConverter.ParseNumberSet(cost);
45114512

45124513
if (_data.CatInfo != null)
45134514
{
@@ -4528,7 +4529,7 @@ private void PrintSecretQuestionInfo()
45284529
}
45294530
else
45304531
{
4531-
var catInfo = _data.CatInfo = new BagCatInfo();
4532+
var catInfo = _data.CatInfo = new NumberSet();
45324533

45334534
catInfo.Minimum = -1;
45344535
catInfo.Maximum = 0;

src/SICore/SICore/Data/StakeInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SICore
44
{
5-
public sealed class StakeInfo : BagCatInfo
5+
public sealed class StakeInfo : NumberSet
66
{
77
private int _stake = 0;
88

src/SIQuester/QTxtConverter/CombinedString.cs

+35-46
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,73 @@
33
namespace QTxtConverter;
44

55
/// <summary>
6-
/// Строка-комбинация других строк (задаваемых номерами)
6+
/// Represents a string which is a result of combinations (intersectons) of a set of soure strings.
77
/// </summary>
88
public sealed class CombinedString
99
{
10-
private string _content;
10+
private string _value;
1111

1212
/// <summary>
13-
/// Получить список номеров строк-источников
13+
/// Source strings indicies in source array.
1414
/// </summary>
15-
public List<int> Sources { get; } = new List<int>();
15+
public List<int> Sources { get; } = new();
1616

1717
/// <summary>
18-
/// Создание комбинированной строки
18+
/// Initializes a new instance of <see cref="CombinedString" /> class.
1919
/// </summary>
20-
/// <param name="s">Содержание</param>
21-
/// <param name="num">Список номеров строк-источников</param>
22-
public CombinedString(string s, params int[] num)
20+
/// <param name="value">String value.</param>
21+
/// <param name="sourceIndicies">Source strings indicies in source array.</param>
22+
public CombinedString(string value, params int[] sourceIndicies)
2323
{
24-
_content = s;
25-
26-
foreach (var n in num)
27-
{
28-
Sources.Add(n);
29-
}
24+
_value = value;
25+
Sources.AddRange(sourceIndicies);
3026
}
3127

3228
/// <summary>
33-
/// Создание комбинированной строки
29+
/// Initializes a new instance of <see cref="CombinedString" /> class based on other combined strings.
3430
/// </summary>
35-
/// <param name="s">Список строк-источников</param>
36-
public CombinedString(params CombinedString[] s)
31+
/// <param name="sources">Source strings.</param>
32+
public CombinedString(params CombinedString[] sources)
3733
{
38-
var index = 0;
39-
40-
foreach (CombinedString str in s)
34+
if (sources.Length == 0)
4135
{
42-
if (index == 0)
43-
{
44-
_content = str.ToString();
36+
throw new ArgumentException("sources must not be empty", nameof(sources));
37+
}
4538

46-
foreach (int n in str.Sources)
47-
{
48-
Sources.Add(n);
49-
}
50-
}
51-
else
52-
{
53-
CombineWith(str);
54-
}
39+
_value = sources[0].ToString();
5540

56-
index++;
41+
foreach (var index in sources[0].Sources)
42+
{
43+
Sources.Add(index);
44+
}
45+
46+
for (int i = 1; i < sources.Length; i++)
47+
{
48+
CombineWith(sources[i]);
5749
}
5850
}
5951

60-
private void CombineWith(CombinedString str)
52+
private void CombineWith(CombinedString combinedString)
6153
{
62-
int len = _content.Length;
54+
var length = _value.Length;
6355

64-
_content = len > 0
56+
_value = length > 0
6557
? StringManager.BestCommonSubString(
66-
_content,
67-
str._content,
58+
_value,
59+
combinedString.ToString(),
6860
new StringManager.StringNorm(StringManager.TemplateSearchingNorm),
6961
true)
7062
: "";
7163

72-
foreach (int n in str.Sources)
64+
foreach (var index in combinedString.Sources)
7365
{
74-
if (!Sources.Contains(n))
66+
if (!Sources.Contains(index))
7567
{
76-
Sources.Add(n);
68+
Sources.Add(index);
7769
}
7870
}
7971
}
8072

81-
/// <summary>
82-
/// Содержание строки
83-
/// </summary>
84-
/// <returns>Содержание строки</returns>
85-
override public string ToString() => _content;
73+
/// <inheritdoc />
74+
override public string ToString() => _value;
8675
}

0 commit comments

Comments
 (0)