Advertisement
Tarusov

Untitled

Aug 7th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.51 KB | None | 0 0
  1. using System;
  2. using GameAdminPanel.Models;
  3. using GameAdminPanel.ViewModels;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.EntityFrameworkCore;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12.  
  13.     public class BalanceController : Controller
  14.     {
  15.         private readonly ApplicationContext context;
  16.         private readonly ProjectModel project;
  17.  
  18.         public BalanceController(ApplicationContext context)
  19.         {
  20.             this.context = context;
  21.             var tmp = context.Projects.ToArrayAsync().Result;
  22.             project = tmp[0];
  23.         }
  24.  
  25.         public async Task<IActionResult> FormsEditor()
  26.         {
  27.             FormsModel configForm;
  28.  
  29.             configForm = JsonConvert.DeserializeObject<FormsModel>(project.Forms);
  30.            
  31.             var formsHtml = CreateFormsHtml(configForm);
  32.             var viewModel = new FormsEditorViewModel()
  33.             {
  34.                 FormCache = formsHtml,
  35.                 Forms = configForm
  36.             };
  37.             return View(viewModel);
  38.         }
  39.  
  40.         [HttpPost]
  41.         public async Task<IActionResult> SaveFormsEditor(FormsEditorViewModel viewModel)
  42.         {
  43.             project.ConfigGame = JsonConvert.SerializeObject(viewModel.Forms);
  44.  
  45.             await context.SaveChangesAsync();
  46.  
  47.             return View("FormsEditor", viewModel);
  48.         }
  49.  
  50.         //Строит схему форм из json
  51.         public FormsModel ConvertJsonToConfigFormScheme(JToken data, bool isObject = false, string oldPath = "")
  52.         {
  53.             if (data.Type == JTokenType.Property)
  54.             {
  55.                 foreach (var it in data)
  56.                 {
  57.                     return ConvertJsonToConfigFormScheme(it, isObject, oldPath);
  58.                 }
  59.             }
  60.  
  61.             var result = new FormsModel {Attachments = new List<FormsModel>()};
  62.  
  63.             if (isObject)
  64.             {
  65.                 var path = data.Path.Replace(oldPath + ".", string.Empty);
  66.                 result.JsonId = path;
  67.             }
  68.             else
  69.             {
  70.                 result.JsonId = data.Path;
  71.             }
  72.  
  73.             result.Title = result.JsonId;
  74.             switch (data.Type)
  75.             {
  76.                 case JTokenType.Array:
  77.                 {
  78.                     result.Type = TypeForms.Array;
  79.                     if (data.HasValues)
  80.                     {
  81.                         result.Attachments.Add(ConvertJsonToConfigFormScheme(data[0]));
  82.                         result.Attachments[0].JsonId = null;
  83.                     }
  84.  
  85.                     break;
  86.                 }
  87.  
  88.                 case JTokenType.Object:
  89.                 {
  90.                     result.Type = TypeForms.Object;
  91.  
  92.                     if (!data.HasValues)
  93.                         return result;
  94.  
  95.                     foreach (var it in data)
  96.                     {
  97.                         result.Attachments.Add(ConvertJsonToConfigFormScheme(it, true, data.Path));
  98.                     }
  99.  
  100.                     break;
  101.                 }
  102.  
  103.                 case JTokenType.Boolean:
  104.                     result.Type = TypeForms.Boolean;
  105.                     result.Value = data.ToString();
  106.                     break;
  107.                 case JTokenType.Integer:
  108.                 case JTokenType.Float:
  109.                     result.Type = TypeForms.Number;
  110.                     result.Value = data.ToString();
  111.                     break;
  112.                 default:
  113.                     result.Type = Regex.IsMatch(data.ToString(), "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") ? TypeForms.Color : TypeForms.String;
  114.                     result.Value = data.ToString();
  115.                     break;
  116.             }
  117.  
  118.             return result;
  119.         }
  120.  
  121.         //Строит формы на основе схемы и json
  122.         public FormsModel ConvertJsonToConfigForm(JToken data, FormsModel formsScheme)
  123.         {
  124.             if (data.Type == JTokenType.Property)
  125.             {
  126.                 foreach (var it in data)
  127.                 {
  128.                     return ConvertJsonToConfigForm(it, formsScheme);
  129.                 }
  130.             }
  131.             var result = new FormsModel
  132.             {
  133.                 StyleClass = formsScheme.StyleClass,
  134.                 JsonId = formsScheme.JsonId,
  135.                 Type = formsScheme.Type,
  136.                 Title = formsScheme.Title,
  137.                 Attachments = new List<FormsModel>()
  138.             };
  139.  
  140.             System.Diagnostics.Debug.WriteLine($"{formsScheme.JsonId} - {data.Path}");
  141.             if (data.Type == JTokenType.Array)
  142.             {
  143.                 if (data.HasValues)
  144.                 {
  145.                     foreach (var it in data)
  146.                     {
  147.                         result.Attachments.Add(ConvertJsonToConfigForm(it, formsScheme.Attachments[0]));
  148.                     }
  149.                 }
  150.             }
  151.             else if (data.Type == JTokenType.Object)
  152.             {
  153.                 if (data.HasValues)
  154.                 {
  155.                     int i = 0;
  156.                     foreach (var it in data)
  157.                     {
  158.                         result.Attachments.Add(ConvertJsonToConfigForm(it, formsScheme.Attachments[i]));
  159.                         i++;
  160.                     }
  161.                 }
  162.             }
  163.             else
  164.             {
  165.                 result.Value = data.ToString();
  166.             }
  167.  
  168.             return result;
  169.         }
  170.  
  171.  
  172.         public string ConvertConfigFormToJson(List<FormsModel> forms, bool isArray = false)
  173.         {
  174.             string result = "";
  175.             foreach (var it in forms)
  176.             {
  177.                 if (it.Type == TypeForms.Object)
  178.                 {
  179.                     if (!isArray)
  180.                     {
  181.                         result += $"\"{it.JsonId}\":";
  182.                     }
  183.  
  184.                     result += "{";
  185.                     result += ConvertConfigFormToJson(it.Attachments);
  186.                     result.Remove(result.Length - 1, 1);
  187.                     result += "},";
  188.                 }
  189.                 else if (it.Type == TypeForms.Array)
  190.                 {
  191.                     if (!isArray)
  192.                     {
  193.                         result += $"\"{it.JsonId}\":";
  194.                     }
  195.  
  196.                     result += "[";
  197.                     result += ConvertConfigFormToJson(it.Attachments, true);
  198.                     result += "],";
  199.                 }
  200.                 else
  201.                 {
  202.                     if (!isArray)
  203.                     {
  204.                         result += $"\"{it.JsonId}\":";
  205.                     }
  206.  
  207.                     if (string.IsNullOrEmpty(it.Value))
  208.                     {
  209.                         result += $"null,";
  210.                     }
  211.                     else
  212.                     {
  213.                         if (it.Type == TypeForms.String)
  214.                         {
  215.                             result += $"\"{it.Value}\",";
  216.                         }
  217.                         else
  218.                         {
  219.                             result += $"{it.Value},";
  220.                         }
  221.                     }
  222.                 }
  223.             }
  224.             result = result.Remove(result.Length - 1, 1);
  225.             return result;
  226.         }
  227.  
  228.         private string CreateFormsHtml(FormsModel forms)
  229.         {
  230.             StringBuilder result = new StringBuilder();
  231.  
  232.                 result.Append($"<div class=\"{forms.StyleClass}\" title=\"{forms.JsonId}\">");
  233.                 result.Append($"<h2>{ forms.Title}</h2>");
  234.  
  235.                 for (int x = 0; x < forms.Attachments.Count; x++)
  236.                 {
  237.                     result.Append(CreateFormsBlock(forms.Attachments[x], $"Forms.Attachments[{x}]"));
  238.                 }
  239.  
  240.                 result.Append("</div>");
  241.            
  242.  
  243.             return result.ToString();
  244.         }
  245.  
  246.         private string CreateFormsBlock(FormsModel form, string pathVariable)
  247.         {
  248.             string result = "";
  249.             string replacePathVariable = pathVariable.Replace('[', '_').Replace(']', '_').Replace('.', '_');
  250.             if (form.Type == TypeForms.Boolean)
  251.             {
  252.                 result+=
  253.                     $"<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/>";
  254.             }
  255.             else if (form.Type == TypeForms.String)
  256.             {
  257.                 result+=
  258.                     $"<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/>";
  259.             }
  260.             else if (form.Type == TypeForms.Number)
  261.             {
  262.                 result+=$"<div class=\"formNumber {form.StyleClass}\"><span>{form.Title}</span>";
  263.                 result+=$"<input type=\"number\" value=\"{form.Value}\" class=\"form-control\" id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/>";
  264.                 result+=$"</div><br/>";
  265.             }
  266.             else if (form.Type == TypeForms.Color)
  267.             {
  268.                 result+=
  269.                     $"<div class=\"formColor {form.StyleClass}\"><span>{form.Title}</span><input type=\"color\" value=\"{form.Value}\" id=\"{replacePathVariable}_Value\" name=\"{pathVariable}.Value\"/></div><br/>";
  270.             }
  271.             else if (form.Type == TypeForms.Array)
  272.             {
  273.                 result+=$"<div class=\"formArray {form.StyleClass}\"><span title=\"{form.JsonId}\">{form.Title}</span>";
  274.                 for (int i = 0; i < form.Attachments.Count; i++)
  275.                 {
  276.                     result+=CreateFormsBlock(form.Attachments[i], pathVariable + $".Attachments[{i}]");
  277.                 }
  278.  
  279.                 result+=$"</div><br/>";
  280.             }
  281.             else if (form.Type == TypeForms.Object)
  282.             {
  283.                 result+=$"<div class=\"formObject {form.StyleClass}\"><span title=\"{form.JsonId}\">{form.Title}</span>";
  284.                 for (int i = 0; i < form.Attachments.Count; i++)
  285.                 {
  286.                     result+=CreateFormsBlock(form.Attachments[i], pathVariable + $".Attachments[{i}]");
  287.                 }
  288.  
  289.                 result+=$"</div><br/>";
  290.             }
  291.  
  292.             return result;
  293.         }
  294.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement