-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
296 lines (236 loc) · 10.7 KB
/
Copy pathProgram.cs
File metadata and controls
296 lines (236 loc) · 10.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.Linq;
using Mastonet;
using dotenv.net;
namespace TownBuilderBot
{
static class Program
{
public class Point : IEquatable<Point>{
public int X;
public int Y;
bool IEquatable<Point>.Equals(Point other)
{
return X == other.X && Y == other.Y;
}
}
public class Post
{
public string body;
public IEnumerable<string> pollOptions;
}
const string QuestionMark = "❓";
public static System.IO.Stream awsLambdaHandler(System.IO.Stream inputStream)
{
Console.WriteLine("starting via lambda");
Main(new string[0]);
return inputStream;
}
public static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine("Beginning program");
bool isReadOnly = args.Contains("readonly");
bool forceZoningMode = args.Contains("zoning");
DotEnv.Load();
MastodonClient client = MakeClient();
Mastonet.Entities.Status latestStatus = GetLatestStatus(client);
Random rand = new Random();
Post post = MakeNormalPost(latestStatus, 10, rand);
if (isReadOnly) {
PrintPost(post);
} else {
PublishPoll(client, post);
}
}
private static MastodonClient MakeClient()
{
string instance = Environment.GetEnvironmentVariable("mastodonInstance");
string accessToken = Environment.GetEnvironmentVariable("mastodonAccessToken");
return new MastodonClient(instance, accessToken);
}
private static Mastonet.Entities.Status GetLatestStatus(MastodonClient client)
{
string accountId = Environment.GetEnvironmentVariable("mastodonAccountId");
var statuses = client.GetAccountStatuses(accountId, new ArrayOptions(){ Limit = 1 }).Result;
return statuses.First();
}
private static void PublishPoll(MastodonClient client, Post post)
{
Console.WriteLine("Publishing Post");
Mastonet.Entities.PollParameters poll = new Mastonet.Entities.PollParameters()
{
Options = post.pollOptions,
ExpiresIn = System.TimeSpan.FromDays(1),
};
var _ = client.PublishStatus(post.body, poll: poll).Result;
}
private static void PrintPost(Post post)
{
Console.WriteLine(post.body);
foreach (string option in post.pollOptions)
{
Console.WriteLine(option);
}
}
public static Post MakeNormalPost(Mastonet.Entities.Status latestStatus, int gridWidth, Random rand)
{
string latestStatusAsCharacters = ReplaceHTMLWithCharacters(latestStatus.Content);
string latestStatusWithoutHashtags = RemoveHashTags(latestStatusAsCharacters);
string newGrid = UpdateGrid(latestStatusWithoutHashtags, latestStatus.Poll, gridWidth, rand);
string newGridWithHashtags = newGrid + "\n#hachybots #bot";
IEnumerable<string> pollOptions = EmojiIndex.GetRandomPollOptions(rand);
return new Post(){ body = newGridWithHashtags, pollOptions = pollOptions };
}
public static string RemoveHashTags(string postBody) {
if (!postBody.Contains('#')) {
return postBody;
}
string[] tokens = postBody.Split('\n');
IEnumerable<string> allButLastRow = tokens.Take(tokens.Length - 1);
return String.Join('\n', allButLastRow);
}
private static string UpdateGrid(string oldGrid, Mastonet.Entities.Poll poll, int gridWidth, Random rand)
{
Point oldQuestionMarkLocation = GetGridCoordinates(oldGrid, gridWidth, QuestionMark);
string winningEmoji = GetPollWinningElement(poll, rand);
string tickedGrid = TickGridElements(oldGrid, gridWidth);
string newGridWithoutQuestionMark = ReplaceElement(tickedGrid, gridWidth, oldQuestionMarkLocation, winningEmoji);
List<Point> possibleLocations = GetPossibleTargetLocations(gridWidth, oldQuestionMarkLocation);
Point newTargetLocation = possibleLocations[rand.Next(possibleLocations.Count)];
return ReplaceElement(newGridWithoutQuestionMark, gridWidth, newTargetLocation, QuestionMark);
}
public static string TickGridElements(string grid, int gridWidth) {
grid = NormalizeEmojiRepresentation(grid);
foreach (EmojiIndex.EmojiData elementData in EmojiIndex.All) {
if (elementData.TickFunction == null) {
continue;
}
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridWidth; y++) {
int index = y * (gridWidth + 1) + x;
System.Globalization.StringInfo stringInfo = new System.Globalization.StringInfo(grid);
string element = stringInfo.SubstringByTextElements(index, 1);
if (element == elementData.Emoji) {
grid = elementData.TickFunction(grid, gridWidth, new Point(){X = x, Y = y});
}
}
}
}
return grid;
}
public static string GetElement(string grid, int width, Point location)
{
if (location.X < 0 || width <= location.X || location.Y < 0 || width <= location.Y)
{
return null;
}
int index = location.Y * (width + 1) + location.X;
System.Globalization.StringInfo stringInfo = new System.Globalization.StringInfo(grid);
return stringInfo.SubstringByTextElements(index, 1);
}
private static string ReplaceHTMLWithCharacters(string input)
{
return input.Replace("<p>", "").Replace("<br />", "\n").Replace("</p>", "");
}
public static string NormalizeEmojiRepresentation(string input)
{
string output = "";
char emojiSelector = Centvrio.Emoji.VariationSelectors.VS16.ToCharArray().Last();
var enumerator = System.Globalization.StringInfo.GetTextElementEnumerator(input);
while (enumerator.MoveNext())
{
string inputElement = enumerator.GetTextElement();
if (inputElement == "\n" || inputElement == "\r" || inputElement.ToCharArray().Last() == emojiSelector) {
output += inputElement;
}
else
{
output += inputElement + emojiSelector;
}
}
return output;
}
public static string ReplaceElement(string inGrid, int width, Point location, Centvrio.Emoji.UnicodeString newString)
{
return ReplaceElement(inGrid, width, location.X, location.Y, (newString + Centvrio.Emoji.VariationSelectors.VS16).ToString());
}
public static string ReplaceElement(string inGrid, int width, Point location, string newString)
{
return ReplaceElement(inGrid, width, location.X, location.Y, newString);
}
public static string ReplaceElement(string inGrid, int width, int x, int y, string newString)
{
if (x < 0 || y < 0 || x >= width || y >= width) {
return inGrid;
}
int index = y * (width + 1) + x;
System.Globalization.StringInfo stringInfo = new System.Globalization.StringInfo(inGrid);
string prefix = stringInfo.SubstringByTextElements(0, index);
string suffix;
if (stringInfo.LengthInTextElements == index + 1) {
suffix = "";
} else {
suffix = stringInfo.SubstringByTextElements(index + 1);
}
return prefix + newString + suffix;
}
public static Point GetGridCoordinates(string inGrid, int width, string target)
{
var enumerator = System.Globalization.StringInfo.GetTextElementEnumerator(inGrid);
for (int textElementIndex = 0; enumerator.MoveNext(); textElementIndex++)
{
if (enumerator.GetTextElement() == target)
{
return new Point
{
X = textElementIndex % (width + 1),
Y = textElementIndex / (width + 1)
};
}
}
return null;
}
public static List<Point> GetPossibleTargetLocations(int width, Point locationToExclude)
{
List<Point> possibleLocations = new List<Point>();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < width; y++)
{
if (!(x == locationToExclude.X && y == locationToExclude.Y))
{
possibleLocations.Add(new Point{ X = x, Y = y});
}
}
}
return possibleLocations;
}
public static string GetPollWinningElement(Mastonet.Entities.Poll poll, Random rand) {
if (poll == null)
{
Console.WriteLine("Couldn't find poll. Default option.");
return "🌳";
}
int maxVotes = poll.Options.Max(o => o.VotesCount ?? 0);
IEnumerable<Mastonet.Entities.PollOption> winningOptions = poll.Options.Where(o => o.VotesCount == maxVotes);
int randIndex = rand.Next(winningOptions.Count());
Mastonet.Entities.PollOption winningOption = winningOptions.ElementAt(randIndex);
System.Globalization.StringInfo stringInfo = new System.Globalization.StringInfo(winningOption.Title);
return stringInfo.SubstringByTextElements(0, 1);
}
public static string GetZoneGrid(string elementsGrid, int gridWidth)
{
string zoneGrid = elementsGrid;
for (int x = 0; x < gridWidth; x++) {
for (int y = 0; y < gridWidth; y++) {
string element = GetElement(elementsGrid, gridWidth, new Point{X = x, Y = y});
EmojiIndex.EmojiData data = EmojiIndex.GetData(element);
zoneGrid = ReplaceElement(zoneGrid, gridWidth, x, y, EmojiIndex.GetZoneEmoji(data.Zone));
}
}
return zoneGrid;
}
}
}