Skip to content

Commit 813481c

Browse files
add VariableValueHelper and improve variable value handling (#2786)
1 parent c4a912b commit 813481c

File tree

5 files changed

+138
-34
lines changed

5 files changed

+138
-34
lines changed

Intersect.Server/Web/Controllers/Api/V1/GuildController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,19 @@ public IActionResult GuildVariableSet(Guid guildId, Guid variableId, [FromBody]
369369

370370
var variable = guild.GetVariable(variableDescriptor.Id, true);
371371

372-
var changed = false;
373-
if (variable?.Value != null)
372+
if (variable?.Value == null)
374373
{
375-
if (variable.Value.Value != valueBody.Value)
376-
{
377-
variable.Value.Value = valueBody.Value;
378-
changed = true;
379-
}
374+
return InternalServerError("Variable value storage is missing.");
380375
}
381376

382-
// ReSharper disable once InvertIf
383-
if (changed)
377+
if (!VariableValueHelper.TryConvertValue(variableDescriptor.DataType, valueBody.Value, out object convertedValue, out string error))
384378
{
379+
return BadRequest(error);
380+
}
381+
382+
if (!VariableValueHelper.Equals(variableDescriptor.DataType, variable.Value, convertedValue))
383+
{
384+
variable.Value.Value = convertedValue;
385385
guild.StartCommonEventsWithTriggerForAll(CommonEventTrigger.GuildVariableChange, string.Empty, variableId.ToString());
386386
_ = guild.UpdatedVariables.AddOrUpdate(
387387
variableId,

Intersect.Server/Web/Controllers/Api/V1/PlayerController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -693,19 +693,19 @@ public IActionResult PlayerVariableSet(LookupKey lookupKey, Guid variableId, [Fr
693693

694694
var variable = player.GetVariable(variableDescriptor.Id, true);
695695

696-
var changed = false;
697-
if (variable?.Value != null)
696+
if (variable?.Value == null)
698697
{
699-
if (variable.Value.Value != valueBody.Value)
700-
{
701-
variable.Value.Value = valueBody.Value;
702-
changed = true;
703-
}
698+
return InternalServerError("Variable value storage is missing.");
699+
}
700+
701+
if (!VariableValueHelper.TryConvertValue(variableDescriptor.DataType, valueBody.Value, out object convertedValue, out string error))
702+
{
703+
return BadRequest(error);
704704
}
705705

706-
// ReSharper disable once InvertIf
707-
if (changed)
706+
if (!VariableValueHelper.Equals(variableDescriptor.DataType, variable.Value, convertedValue))
708707
{
708+
variable.Value.Value = convertedValue;
709709
player.StartCommonEventsWithTrigger(CommonEventTrigger.PlayerVariableChange, string.Empty, variableId.ToString());
710710
using var context = DbInterface.CreatePlayerContext(false);
711711
_ = context.Update(player);

Intersect.Server/Web/Controllers/Api/V1/UserController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,19 @@ public IActionResult UserVariableSet(LookupKey lookupKey, Guid variableId, [From
626626

627627
var variable = user.GetVariable(variableDescriptor.Id, true);
628628

629-
var changed = false;
630-
if (variable?.Value != null)
629+
if (variable?.Value == null)
631630
{
632-
if (variable.Value.Value != valueBody.Value)
633-
{
634-
variable.Value.Value = valueBody.Value;
635-
changed = true;
636-
}
631+
return InternalServerError("Variable value storage is missing.");
632+
}
633+
634+
if (!VariableValueHelper.TryConvertValue(variableDescriptor.DataType, valueBody.Value, out object convertedValue, out string error))
635+
{
636+
return BadRequest(error);
637637
}
638638

639-
// ReSharper disable once InvertIf
640-
if (changed)
639+
if (!VariableValueHelper.Equals(variableDescriptor.DataType, variable.Value, convertedValue))
641640
{
641+
variable.Value.Value = convertedValue;
642642
user.StartCommonEventsWithTriggerForAll(CommonEventTrigger.UserVariableChange, string.Empty, variableId.ToString());
643643
_ = user.UpdatedVariables.AddOrUpdate(
644644
variableId,
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Globalization;
2+
using Intersect.Enums;
3+
using Intersect.Framework.Core.GameObjects.Variables;
4+
5+
namespace Intersect.Server.Web.Controllers.Api.V1;
6+
7+
internal static class VariableValueHelper
8+
{
9+
private const string InvalidValueErrorFormat = "Invalid value for variable of type {0}. Received: {1}";
10+
11+
public static bool Equals(VariableDataType dataType, VariableValue variableValue, object? value)
12+
{
13+
if (value == null)
14+
{
15+
return variableValue.Value == null;
16+
}
17+
18+
return dataType switch
19+
{
20+
VariableDataType.Boolean => value is bool booleanValue && variableValue.Boolean == booleanValue,
21+
VariableDataType.Integer => value is long longValue && variableValue.Integer == longValue,
22+
VariableDataType.Number => value is double doubleValue && variableValue.Number.Equals(doubleValue),
23+
VariableDataType.String => value is string stringValue && string.Equals(variableValue.String ?? string.Empty, stringValue, StringComparison.Ordinal),
24+
_ => Equals(variableValue.Value, value),
25+
};
26+
}
27+
28+
public static bool TryConvertValue(
29+
VariableDataType dataType,
30+
object? value,
31+
out object convertedValue,
32+
out string? error
33+
)
34+
{
35+
convertedValue = string.Empty;
36+
error = null;
37+
38+
if (value == null)
39+
{
40+
if (dataType == VariableDataType.String)
41+
{
42+
return true;
43+
}
44+
45+
error = FormatError(dataType, "null");
46+
return false;
47+
}
48+
49+
switch (dataType)
50+
{
51+
case VariableDataType.Boolean:
52+
if (value is bool booleanValue)
53+
{
54+
convertedValue = booleanValue;
55+
return true;
56+
}
57+
58+
error = FormatError(dataType, value?.GetType()?.Name ?? "null");
59+
return false;
60+
61+
case VariableDataType.Integer:
62+
if (long.TryParse(value.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedInt))
63+
{
64+
convertedValue = parsedInt;
65+
return true;
66+
}
67+
68+
error = FormatError(dataType, value?.GetType()?.Name ?? "null");
69+
return false;
70+
71+
case VariableDataType.Number:
72+
if (double.TryParse(value.ToString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var parsedDouble))
73+
{
74+
convertedValue = parsedDouble;
75+
return true;
76+
}
77+
78+
error = FormatError(dataType, value?.GetType()?.Name ?? "null");
79+
return false;
80+
81+
case VariableDataType.String:
82+
if (value is string stringValue)
83+
{
84+
convertedValue = stringValue;
85+
return true;
86+
}
87+
88+
error = FormatError(dataType, value?.GetType()?.Name ?? "null");
89+
return false;
90+
91+
default:
92+
error = FormatError(dataType, value?.GetType()?.Name ?? "null");
93+
return false;
94+
}
95+
}
96+
97+
private static string FormatError(VariableDataType dataType, string received)
98+
{
99+
return string.Format(CultureInfo.InvariantCulture, InvalidValueErrorFormat, dataType, received);
100+
}
101+
}

Intersect.Server/Web/Controllers/Api/V1/VariablesController.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,19 @@ public IActionResult ServerVariableSet(Guid variableId, [FromBody] VariableValue
102102
return NotFound($@"No server variable with id '{variableId}'.");
103103
}
104104

105-
var changed = false;
106-
if (variable.Value != null && variable.Value.Value != variableValue.Value)
105+
if (variable.Value == null)
107106
{
108-
variable.Value.Value = variableValue.Value;
109-
changed = true;
107+
return InternalServerError("Variable value storage is missing.");
110108
}
111109

112-
// ReSharper disable once InvertIf
113-
if (changed)
110+
if (!VariableValueHelper.TryConvertValue(variable.DataType, variableValue.Value, out object convertedValue, out string error))
114111
{
112+
return BadRequest(error);
113+
}
114+
115+
if (!VariableValueHelper.Equals(variable.DataType, variable.Value, convertedValue))
116+
{
117+
variable.Value.Value = convertedValue;
115118
Player.StartCommonEventsWithTriggerForAll(
116119
CommonEventTrigger.ServerVariableChange,
117120
"",
@@ -123,4 +126,4 @@ public IActionResult ServerVariableSet(Guid variableId, [FromBody] VariableValue
123126

124127
return Ok(variable);
125128
}
126-
}
129+
}

0 commit comments

Comments
 (0)