105 lines
2.5 KiB
GDScript
105 lines
2.5 KiB
GDScript
class_name CommandResult
|
|
extends RefCounted
|
|
|
|
enum Code {
|
|
OK,
|
|
INVALID_COMMAND,
|
|
INVALID_PAYLOAD,
|
|
DUPLICATE_COMMAND_ID,
|
|
DUPLICATE_IDEMPOTENCY_KEY,
|
|
NOT_FOUND,
|
|
VALIDATION_FAILED,
|
|
TRANSACTION_CLOSED,
|
|
TRANSACTION_APPLY_FAILED,
|
|
EXECUTOR_FAILED,
|
|
}
|
|
|
|
var code: Code = Code.OK
|
|
var command_id := 0
|
|
var target_id: StringName = &""
|
|
var message := ""
|
|
var resolution_hints: Array[String] = []
|
|
var details: Dictionary = {}
|
|
var is_replay := false
|
|
|
|
|
|
func _init(
|
|
p_code: Code = Code.OK,
|
|
p_command_id: int = 0,
|
|
p_message: String = "",
|
|
p_details: Dictionary = {}
|
|
) -> void:
|
|
code = p_code
|
|
command_id = p_command_id
|
|
message = p_message
|
|
details = p_details.duplicate(true)
|
|
|
|
|
|
static func succeeded(
|
|
p_command_id: int = 0,
|
|
p_details: Dictionary = {}
|
|
) -> CommandResult:
|
|
return CommandResult.new(Code.OK, p_command_id, "", p_details)
|
|
|
|
|
|
static func failed(
|
|
p_code: Code,
|
|
p_command_id: int,
|
|
p_message: String,
|
|
p_details: Dictionary = {}
|
|
) -> CommandResult:
|
|
assert(p_code != Code.OK, "Use CommandResult.succeeded() for a successful result.")
|
|
return CommandResult.new(p_code, p_command_id, p_message, p_details)
|
|
|
|
|
|
func is_success() -> bool:
|
|
return code == Code.OK
|
|
|
|
|
|
func copy() -> CommandResult:
|
|
var result := CommandResult.new(code, command_id, message, details)
|
|
result.target_id = target_id
|
|
result.resolution_hints.assign(resolution_hints)
|
|
result.is_replay = is_replay
|
|
return result
|
|
|
|
|
|
func replay_copy() -> CommandResult:
|
|
var result := copy()
|
|
result.is_replay = true
|
|
return result
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return {
|
|
"code": Code.keys()[code],
|
|
"command_id": command_id,
|
|
"target_id": String(target_id),
|
|
"message": message,
|
|
"resolution_hints": resolution_hints.duplicate(),
|
|
"details": details.duplicate(true),
|
|
"is_replay": is_replay,
|
|
}
|
|
|
|
|
|
static func from_dictionary(data: Dictionary) -> CommandResult:
|
|
var code_name := String(data.get("code", "INVALID_COMMAND"))
|
|
var parsed_code := Code.INVALID_COMMAND
|
|
var code_names := Code.keys()
|
|
var code_index := code_names.find(code_name)
|
|
if code_index >= 0:
|
|
parsed_code = code_index as Code
|
|
var result := CommandResult.new(
|
|
parsed_code,
|
|
int(data.get("command_id", 0)),
|
|
String(data.get("message", "")),
|
|
data.get("details", {}) as Dictionary
|
|
)
|
|
result.target_id = StringName(data.get("target_id", ""))
|
|
var saved_hints: Variant = data.get("resolution_hints", [])
|
|
if saved_hints is Array:
|
|
for hint: Variant in saved_hints:
|
|
result.resolution_hints.append(String(hint))
|
|
result.is_replay = bool(data.get("is_replay", false))
|
|
return result
|