Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using GameAdminPanel.Models;
- using GameAdminPanel.ViewModels;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.EntityFrameworkCore;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- public class BalanceController : Controller
- {
- private readonly ApplicationContext context;
- private readonly ProjectModel project;
- public BalanceController(ApplicationContext context)
- {
- this.context = context;
- var tmp = context.Projects.ToArrayAsync().Result;
- project = tmp[0];
- }
- public async Task<IActionResult> FormsEditor()
- {
- FormsModel configForm;
- configForm = JsonConvert.DeserializeObject<FormsModel>(project.Forms);
- var formsHtml = CreateFormsHtml(configForm);
- var viewModel = new FormsEditorViewModel()
- {
- FormCache = formsHtml,
- Forms = configForm
- };
- return View(viewModel);
- }
- [HttpPost]
- public async Task<IActionResult> SaveFormsEditor(FormsEditorViewModel viewModel)
- {
- project.ConfigGame = JsonConvert.SerializeObject(viewModel.Forms);
- await context.SaveChangesAsync();
- return View("FormsEditor", viewModel);
- }
- //Строит схему форм из json
- public FormsModel ConvertJsonToConfigFormScheme(JToken data, bool isObject = false, string oldPath = "")
- {
- if (data.Type == JTokenType.Property)
- {
- foreach (var it in data)
- {
- return ConvertJsonToConfigFormScheme(it, isObject, oldPath);
- }
- }
- var result = new FormsModel {Attachments = new List<FormsModel>()};
- if (isObject)
- {
- var path = data.Path.Replace(oldPath + ".", string.Empty);
- result.JsonId = path;
- }
- else
- {
- result.JsonId = data.Path;
- }
- result.Title = result.JsonId;
- switch (data.Type)
- {
- case JTokenType.Array:
- {
- result.Type = TypeForms.Array;
- if (data.HasValues)
- {
- result.Attachments.Add(ConvertJsonToConfigFormScheme(data[0]));
- result.Attachments[0].JsonId = null;
- }
- break;
- }
- case JTokenType.Object:
- {
- result.Type = TypeForms.Object;
- if (!data.HasValues)
- return result;
- foreach (var it in data)
- {
- result.Attachments.Add(ConvertJsonToConfigFormScheme(it, true, data.Path));
- }
- break;
- }
- case JTokenType.Boolean:
- result.Type = TypeForms.Boolean;
- result.Value = data.ToString();
- break;
- case JTokenType.Integer:
- case JTokenType.Float:
- result.Type = TypeForms.Number;
- result.Value = data.ToString();
- break;
- default:
- result.Type = Regex.IsMatch(data.ToString(), "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") ? TypeForms.Color : TypeForms.String;
- result.Value = data.ToString();
- break;
- }
- return result;
- }
- //Строит формы на основе схемы и json
- public FormsModel ConvertJsonToConfigForm(JToken data, FormsModel formsScheme)
- {
- if (data.Type == JTokenType.Property)
- {
- foreach (var it in data)
- {
- return ConvertJsonToConfigForm(it, formsScheme);
- }
- }
- var result = new FormsModel
- {
- StyleClass = formsScheme.StyleClass,
- JsonId = formsScheme.JsonId,
- Type = formsScheme.Type,
- Title = formsScheme.Title,
- Attachments = new List<FormsModel>()
- };
- System.Diagnostics.Debug.WriteLine($"{formsScheme.JsonId} - {data.Path}");
- if (data.Type == JTokenType.Array)
- {
- if (data.HasValues)
- {
- foreach (var it in data)
- {
- result.Attachments.Add(ConvertJsonToConfigForm(it, formsScheme.Attachments[0]));
- }
- }
- }
- else if (data.Type == JTokenType.Object)
- {
- if (data.HasValues)
- {
- int i = 0;
- foreach (var it in data)
- {
- result.Attachments.Add(ConvertJsonToConfigForm(it, formsScheme.Attachments[i]));
- i++;
- }
- }
- }
- else
- {
- result.Value = data.ToString();
- }
- return result;
- }
- public string ConvertConfigFormToJson(List<FormsModel> forms, bool isArray = false)
- {
- string result = "";
- foreach (var it in forms)
- {
- if (it.Type == TypeForms.Object)
- {
- if (!isArray)
- {
- result += $"\"{it.JsonId}\":";
- }
- result += "{";
- result += ConvertConfigFormToJson(it.Attachments);
- result.Remove(result.Length - 1, 1);
- result += "},";
- }
- else if (it.Type == TypeForms.Array)
- {
- if (!isArray)
- {
- result += $"\"{it.JsonId}\":";
- }
- result += "[";
- result += ConvertConfigFormToJson(it.Attachments, true);
- result += "],";
- }
- else
- {
- if (!isArray)
- {
- result += $"\"{it.JsonId}\":";
- }
- if (string.IsNullOrEmpty(it.Value))
- {
- result += $"null,";
- }
- else
- {
- if (it.Type == TypeForms.String)
- {
- result += $"\"{it.Value}\",";
- }
- else
- {
- result += $"{it.Value},";
- }
- }
- }
- }
- result = result.Remove(result.Length - 1, 1);
- return result;
- }
- private string CreateFormsHtml(FormsModel forms)
- {
- StringBuilder result = new StringBuilder();
- result.Append($"<div class=\"{forms.StyleClass}\" title=\"{forms.JsonId}\">");
- result.Append($"<h2>{ forms.Title}</h2>");
- for (int x = 0; x < forms.Attachments.Count; x++)
- {
- result.Append(CreateFormsBlock(forms.Attachments[x], $"Forms.Attachments[{x}]"));
- }
- result.Append("</div>");
- return result.ToString();
- }
- private string CreateFormsBlock(FormsModel form, string pathVariable)
- {
- string result = "";
- string replacePathVariable = pathVariable.Replace('[', '_').Replace(']', '_').Replace('.', '_');
- if (form.Type == TypeForms.Boolean)
- {
- result+=
- $"<div class=\"formBoolean checkbox {form.StyleClass}\"><label><div class=\"checker\"><span><input type=\"checkbox\" value=\"{form.Value}\" {(form.Value == "true" ? "checked=\"checked\"" : "")} id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/></span></div>{form.Title}</label></div><br/>";
- }
- else if (form.Type == TypeForms.String)
- {
- result+=
- $"<div class=\"formString {form.StyleClass}\"><span>{form.Title}</span><input type=\"text\" value=\"{form.Value}\" class=\"form-control\" id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/></div><br/>";
- }
- else if (form.Type == TypeForms.Number)
- {
- result+=$"<div class=\"formNumber {form.StyleClass}\"><span>{form.Title}</span>";
- result+=$"<input type=\"number\" value=\"{form.Value}\" class=\"form-control\" id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/>";
- result+=$"</div><br/>";
- }
- else if (form.Type == TypeForms.Color)
- {
- result+=
- $"<div class=\"formColor {form.StyleClass}\"><span>{form.Title}</span><input type=\"color\" value=\"{form.Value}\" id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/></div><br/>";
- }
- else if (form.Type == TypeForms.Array)
- {
- result+=$"<div class=\"formArray {form.StyleClass}\"><span title=\"{form.JsonId}\">{form.Title}</span>";
- for (int i = 0; i < form.Attachments.Count; i++)
- {
- result+=CreateFormsBlock(form.Attachments[i], pathVariable + $".Attachments[{i}]");
- }
- result+=$"</div><br/>";
- }
- else if (form.Type == TypeForms.Object)
- {
- result+=$"<div class=\"formObject {form.StyleClass}\"><span title=\"{form.JsonId}\">{form.Title}</span>";
- for (int i = 0; i < form.Attachments.Count; i++)
- {
- result+=CreateFormsBlock(form.Attachments[i], pathVariable + $".Attachments[{i}]");
- }
- result+=$"</div><br/>";
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement