Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bartimaeusnek committed Jul 5, 2020
1 parent 5c5dcbe commit 47b1b18
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 60 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ CropApp/bin/
CropApp/obj/
Debug/
/CropApp/wwwroot/BreedingDict10000.json
/CropApp/wwwroot/css/site.min.css
/CropApp/wwwroot/js/site.min.js
*.min.js
*.min.js.map
*.min.css
*.min.css.map
75 changes: 49 additions & 26 deletions CropApp/Backend/CropCalculation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,36 @@ public static class CropCalculation
private static readonly Dictionary<long, int> RatioZmapping = new Dictionary<long, int>();

private static XorShiftRandom _xstr = new XorShiftRandom();


private static int[] ratios = new int[AllCrops.Count * AllCrops.Count];

public static async Task ProcessBreeding()
{
var fi = new FileInfo("wwwroot/BreedingDict.json");
if (fi.Exists)
{
using var r = fi.OpenText();
var json = await r.ReadToEndAsync();
BreedingDict =
JsonConvert.DeserializeObject<Dictionary<(string, string), List<(string, double)>>>(json);
if (await TryLoadCropJsonAsync())
return;
}

var ratios = new int[AllCrops.Count * AllCrops.Count];
PrecalculateRatios();
Console.WriteLine("precalulated ratios");

ParallelProcessCrops();
Console.WriteLine("done with Parallel processing");

Console.WriteLine("Sort Directory");
SortDictionary();
Console.WriteLine("Done sorting Directory");
await WriteCropJsonToDisk();
}

private static async Task WriteCropJsonToDisk()
{
await using var streamWriter = new StreamWriter("wwwroot/BreedingDict.json");
await streamWriter.WriteAsync(JsonConvert.SerializeObject(BreedingDict));
await streamWriter.FlushAsync();
streamWriter.Close();
}

private static void PrecalculateRatios()
{
var counter = 0;

foreach (var cropA in AllCrops)
Expand All @@ -50,10 +66,23 @@ public static async Task ProcessBreeding()
++counter;
}
}

Console.WriteLine("precalulated ratios");

Parallel.ForEach(AllCrops,
}

private static async Task<bool> TryLoadCropJsonAsync()
{
var fi = new FileInfo("wwwroot/BreedingDict.json");
if (!fi.Exists)
return false;

using var r = fi.OpenText();
var json = await r.ReadToEndAsync();
BreedingDict = JsonConvert.DeserializeObject<Dictionary<(string, string), List<(string, double)>>>(json);
return true;
}

private static void ParallelProcessCrops()
{
Parallel.ForEach(AllCrops,
new ParallelOptions {MaxDegreeOfParallelism = 2},
cropA =>
{
Expand Down Expand Up @@ -82,10 +111,11 @@ public static async Task ProcessBreeding()
//Console.WriteLine("done with this task");
});
});

Console.WriteLine("done with Parallel processing");

Console.WriteLine("Sort Directory");
}


private static void SortDictionary()
{
foreach (KeyValuePair<long, ConcurrentBag<int>> kvp in Breeding.AsEnumerable()!
.OrderBy(k => k.Key))
{
Expand All @@ -100,15 +130,8 @@ public static async Task ProcessBreeding()

BreedingDict.Add(kvp.Key.GetCropNamesFromLong(), q.Select(x => (x.Key, x.Value)).ToList());
}

Console.WriteLine("Done sorting Directory");

await using var streamWriter = new StreamWriter("wwwroot/BreedingDict.json");
await streamWriter.WriteAsync(JsonConvert.SerializeObject(BreedingDict));
streamWriter.Flush();
streamWriter.Close();
}

private static CropModel AttemptCrossing(Span<int> ratioz, IReadOnlyCollection<int> cropIds)
{
if (cropIds.Count > 4 || cropIds.Count < 2)
Expand Down
12 changes: 12 additions & 0 deletions CropApp/CropApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Update="wwwroot\js\popper.min.js">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Update="wwwroot\js\popper.min.js.map">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Update="wwwroot\js\CropPath.js">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 21 additions & 9 deletions CropApp/Pages/CropPath.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
Layout = "_Layout";
ViewData["Title"] = "Path";
}
<environment include="Development">
@section Scripts
{
<script src="~/js/CropList.js"></script>
<link rel="stylesheet" href="~/css/gridStyles.css"/>
}
<environment include="Development">
@section Scripts
{
<script src="~/js/CropList.js"></script>
<script src="~/js/CropPath.js"></script>
<link rel="stylesheet" href="~/css/gridStyles.css"/>
}
</environment>
<environment exclude="Development">
@section Scripts
{
<script src="~/js/CropPath.min.js"></script>
}
</environment>
@{
var ignoredList = ViewContext.HttpContext.Request.Query["ign"].ToString().Split(',').ToList();
var crop = ViewContext.HttpContext.Request.Query["crop"];
}
<body>
<h1>@crop</h1>
<div class="wrapper-single-box">
<div class="wrapper-box-left" onclick="toggleMode()" id="toggle">
Toggle Mode
</div>
</div>
<div class="wrapper">
@foreach (var (parent1, parent2, chance) in Model.Crops)
{
Expand All @@ -31,11 +43,11 @@
.Replace(" ", "%20");
<div class="a wrapper-single-box color-crops-wrapper color-crops-wrapper-border">
<div class="wrapper-double-box">
<a onclick="toCropList(event)" class="wrapper-box-left color-crops-wrapper color-crops-wrapper-border" href=@hrefparent1>
<a data-toggle="tooltip" data-placement="bottom" title="Ignore this crop" onclick="toCropList(event)" class="wrapper-box-left color-crops-wrapper color-crops-wrapper-border" href=@hrefparent1>
Parent 1:
@parent1
</a>
<a onclick="toCropList(event)" class="wrapper-box-right color-crops-wrapper color-crops-wrapper-border" href=@hrefparent2>
<a data-toggle="tooltip" data-placement="bottom" title="Ignore this crop" onclick="toCropList(event)" class="wrapper-box-right color-crops-wrapper color-crops-wrapper-border" href=@hrefparent2>
Parent 2:
@parent2
</a>
Expand All @@ -58,7 +70,7 @@
.Aggregate("/CropPath?crop=" + ViewContext.HttpContext.Request.Query["crop"], (current, ignStr) => current + "&ign=" + ignStr)
.Replace(" ", "%20");
<div class="a wrapper-single-box">
<a data-toggle="tooltip" class="wrapper-box-unignore" href=@hrefFilter>
<a data-toggle="tooltip" data-placement="bottom" title="Unignore this crop" class="wrapper-box-unignore" href=@hrefFilter>
@ignoredCrop
</a>
</div>
Expand Down
26 changes: 23 additions & 3 deletions CropApp/bundleconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"wwwroot/css/site.css",
"wwwroot/css/globalStyles.css",
"wwwroot/css/gridStyles.css"
]
],
"minify": {
"enabled": true,
"colorNames": "hex",
"adjustRelativePaths": true
}
},
{
"outputFileName": "wwwroot/js/site.min.js",
Expand All @@ -15,8 +20,23 @@
],
"minify": {
"enabled": true,
"renameLocals": true
"renameLocals": true,
"alwaysEscapeNonAscii": true,
"termSemicolons": false
},
"sourceMap": true
},
{
"outputFileName": "wwwroot/js/CropPath.min.js",
"inputFiles": [
"wwwroot/js/CropPath.js"
],
"minify": {
"enabled": true,
"renameLocals": true,
"alwaysEscapeNonAscii": true,
"termSemicolons": false
},
"sourceMap": false
"sourceMap": true
}
]
14 changes: 0 additions & 14 deletions CropApp/wwwroot/js/CropList.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,4 @@ function comparer(index) {
}
function getCellValue(row, index) {
return $(row).children("td").eq(index).text()
}

function toCropList(e) {
if (e.ctrlKey) {
e.preventDefault();
$(e.target).href=""
const text = $(e.target)
.text()
.replace(/\n {20}Parent \d:/,"")
.replace(/\n\s/,"")
.replace(/\n\s/,"")
.trim();
window.location.href = window.location.href.replace(/crop=(?:.*[^&])/,`crop=${text}`);
}
}
65 changes: 65 additions & 0 deletions CropApp/wwwroot/js/CropPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
let toggle = false;

function toCropList(e) {
if ((e.ctrlKey && toggle)){
e.preventDefault();
window.location.href = e.target.href;
return;
}
if ((toggle && !e.ctrlKey) || (e.ctrlKey && !toggle)) {
e.preventDefault();
$(e.target).href=""
const text = $(e.target)
.text()
.replace(/\n {20}Parent \d:/,"")
.replace(/\n\s/,"")
.replace(/\n\s/,"")
.trim();
window.location.href = window.location.href.replace(/crop=(?:.*[^&])/,`crop=${text}`);
}
}

function toggleMode() {
toggle = !toggle;
const element = document.getElementById("toggle");
const tooltips = document.getElementsByClassName("wrapper-box-left color-crops-wrapper color-crops-wrapper-border")
if (toggle) {
element.textContent = "Go to crop mode. Click/Tap here to toggle!"
for (const div of tooltips){
div.setAttribute("data-original-title","Go to crop")
}
}
else {
element.textContent = "Ignore this crop mode. Click/Tap here to toggle!"
for (const div of tooltips){
div.setAttribute("data-original-title","Ignore this crop")
}
}
}

$(document).keydown(
function(ev) {
if (ev.ctrlKey) {
const tooltips = document.body.getElementsByClassName("tooltip-inner");
for (const e of tooltips) {
if (!toggle)
e.textContent = "Go to crop";
else
e.textContent = "Ignore this crop";
}
}
}
)
$(document).keyup(
function(ev) {
if (!ev.ctrlKey) {
const tooltips = document.body.getElementsByClassName("tooltip-inner");
for (const e of tooltips) {
if (!toggle)
e.textContent = "Ignore this crop";
else
e.textContent = "Go to crop";
}
}
}
)
4 changes: 2 additions & 2 deletions CropApp/wwwroot/js/popper.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CropApp/wwwroot/js/popper.min.js.map

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions CropApp/wwwroot/js/site.js
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your Javascript code.


0 comments on commit 47b1b18

Please sign in to comment.