This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawRequest.cs
More file actions
157 lines (143 loc) · 4.27 KB
/
DrawRequest.cs
File metadata and controls
157 lines (143 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using SkiaSharp;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace PainterPro
{
public class DrawRequest
{
public string uuid = Guid.NewGuid().ToString().Replace("-", "").ToUpper(); // Generates a UUID 32 characters, containing a mix of digits and capital A-F letters.
public string? payment = null; // Will be generated by swish servers when paid.
public Dictionary<int, PixelRequest> pixels = new Dictionary<int, PixelRequest>();
public string status = "Pending";
public DrawRequest(Dictionary<string, object> fields)
{
// Goes through all fields and adds them to corresponding PixelRequests.
foreach (KeyValuePair<string, object> field in fields)
{
Match keyParts = Regex.Match(field.Key, @"^(?<index>\d*)(?<key>\w*)$");
if (keyParts.Success && int.TryParse(keyParts.Groups["index"].Value, out int index))
{
string key = keyParts.Groups["key"].Value.ToLower();
if (!pixels.ContainsKey(index))
{
pixels.Add(index, new PixelRequest());
}
pixels[index].Add(key, field.Value);
}
}
foreach (KeyValuePair<int, PixelRequest> element in pixels.Where(element => !element.Value.IsComplete()).ToList()) // TODO: Find more elegant solution and handle non-colored pixels.
{
pixels.Remove(element.Key);
}
}
public async Task<HttpResponseMessage?> CreatePaymentRequest()
{
// Creates a JSON response with all values in appsettings.json, as well as some new ones.
Dictionary<string, object> contentFields = new Dictionary<string, object>(Util.appsettings)
{
{ "currency", "SEK" },
{ "amount", "1" },
{ "message", "David testar!" }
};
string contentString = JsonSerializer.Serialize(contentFields).ToString();
StringContent content = new StringContent(contentString, Encoding.UTF8, "application/json");
MyConsole.WriteData(" Sent data", contentString + " PUT " + Util.swishDomain + Util.swishCreatePath + uuid);
HttpClient client = Util.GetSwishClient();
try
{
Task<HttpResponseMessage> swishConnection = client.PutAsync(Util.swishCreatePath + uuid, content);
return await swishConnection;
}
catch (Exception exception)
{
MyConsole.Write(exception.ToString());
return null;
}
}
public async Task<HttpResponseMessage?> GetPaymentQr(int size)
{
Dictionary<string, object> contentFields = new Dictionary<string, object>()
{
{ "token", uuid },
{ "format", "png" },
{ "size", size }
};
string contentString = JsonSerializer.Serialize(contentFields).ToString();
StringContent content = new StringContent(contentString, Encoding.UTF8, "application/json");
MyConsole.WriteData(" Sent data", contentString + " POST " + Util.swishDomain + Util.swishQrPath);
HttpClient client = Util.GetSwishClient("https://mpc.getswish.net");
try
{
Task<HttpResponseMessage> swishConnection = client.PostAsync(Util.swishQrPath, content);
return await swishConnection;
}
catch (Exception exception)
{
MyConsole.Write(exception.ToString());
return null;
}
}
public void Draw()
{
if (pixels.Count > 0)
{
string path = Util.currentDirectory + "\\assets\\images\\painting.png";
using (SKBitmap bitmap = SKBitmap.Decode(path))
using (FileStream stream = File.OpenWrite(path))
{
foreach (PixelRequest pixel in pixels.Values)
{
bitmap.SetPixel((int)pixel.x, (int)pixel.y, SKColor.Parse(pixel.color));
}
SKImage image = SKImage.FromBitmap(bitmap);
SKData data = image.Encode();
data.SaveTo(stream);
}
}
}
public class PixelRequest
{
public int? x;
public int? y;
public string? color;
public void Add(string key, object value)
{
switch (key)
{
case "color":
color = value.ToString();
break;
case "x":
case "y":
int valueInt;
if (value.GetType() == typeof(string))
{
if (!int.TryParse((string)value, out valueInt))
{
break;
}
}
else
{
valueInt = (int)value;
}
switch (key)
{
case "x":
x = valueInt;
break;
case "y":
y = valueInt;
break;
}
break;
}
}
public bool IsComplete()
{
return x != null && y != null && color != null;
}
}
}
}