-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMessageparser.cs
More file actions
269 lines (221 loc) · 10.8 KB
/
Copy pathMessageparser.cs
File metadata and controls
269 lines (221 loc) · 10.8 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Auction.mod
{
class Messageparser
{
//private List<Auction> addingcards = new List<Auction>();
Helpfunktions helpf;
private Regex priceregexOffer = new Regex(@".*[0-9]{1,9}[g]?.*");
private Regex priceregex = new Regex(@".*[^x0-9]+[0-9]{2,9}[g]?[^x0-9]+.*");
private Regex priceregexpriceonname = new Regex(@"[^x0-9]{2,}[0-9]{2,9}[g]?[^x0-9]+.*");
Regex numberregx = new Regex(@"[0-9]{2,9}");
Regex numberregxOffer = new Regex(@"[0-9]{1,9}");
public List<nickelement> searchscrollsnicks = new List<nickelement>();
// private int cardnametoid(string name) { return helpf.cardids[Array.FindIndex(helpf.cardnames, element => element.Equals(name))]; }
private static Messageparser instance;
public static Messageparser Instance
{
get
{
if (instance == null)
{
instance = new Messageparser();
}
return instance;
}
}
private Messageparser()
{
this.helpf = Helpfunktions.Instance;
}
private string pricetestfirst(string price)
{
string result = "";
//Console.WriteLine("PREIS: " + price);
Match match = this.priceregexpriceonname.Match(" " + price + " ");
if (match.Success) { result = match.Value; }
return result;
}
private string pricetest(string price)
{
string result = "";
//Console.WriteLine("PREIS: " + price);
Match match = priceregex.Match(" " + price + " ");
if (match.Success) { result = match.Value; }
return result;
}
/*private void additemtolist(Card c, string from, int gold, bool wts, string wholemsg)
{
if (wts)
{
this.addingcards.Add(new Auction(from, DateTime.Now, Auction.OfferType.SELL, c, wholemsg, gold));
}
else
{
this.addingcards.Add(new Auction(from, DateTime.Now, Auction.OfferType.BUY, c, wholemsg, gold));
}
}*/
private static void GetAuctionsFromShortIntoList(string shortList, string from, Auction.OfferType offerType, List<Auction> outList)
{
string[] words = shortList.Split(';');
for (int i = 0; i < words.Length; i++)
{
if (words[i] == "" || words[i] == " ") break;
string price;
string ids;
if (words[i].Contains(' '))
{
price = words[i].Split(' ')[1];
if (price == "") { price = "0"; }
ids = words[i].Split(' ')[0];
}
else
{
price = "0";
ids = words[i];
}
string[] ideen = ids.Split(','); //When string does not contain a , the whole string is the first element of the returned array.
foreach (string idd in ideen)
{
int id = Convert.ToInt32(idd);
CardType type = CardTypeManager.getInstance().get(id);
Card card = new Card(id, type, true);
outList.Add(new Auction(from, DateTime.Now, offerType, card, "(Network Auction)", Convert.ToInt32(price)));
}
}
}
public static List<Auction> GetAuctionsFromShortMessage(string msg, string from)
{
List<Auction> addingcards = new List<Auction>();
//bool wts = true;
string secmsg = "";
if (msg.StartsWith("aucs "))
{
//wts = true;
msg = msg.Remove(0, 5);
if (msg.Contains("aucb "))
{
secmsg = (msg.Split(new string[] { "aucb " }, StringSplitOptions.None))[1];
msg = (msg.Split(new string[] { "aucb " }, StringSplitOptions.None))[0];
GetAuctionsFromShortIntoList(secmsg, from, Auction.OfferType.BUY, addingcards);
}
GetAuctionsFromShortIntoList(msg, from, Auction.OfferType.SELL, addingcards);
}
if (msg.StartsWith("aucb "))
{
msg = msg.Remove(0, 5);
if (msg.Contains("aucs "))
{
secmsg = (msg.Split(new string[] { "aucs " }, StringSplitOptions.None))[1];
msg = (msg.Split(new string[] { "aucs " }, StringSplitOptions.None))[0];
GetAuctionsFromShortIntoList(secmsg, from, Auction.OfferType.SELL, addingcards);
}
GetAuctionsFromShortIntoList(msg, from, Auction.OfferType.BUY, addingcards);
}
return addingcards;
}
public List<Auction> GetAuctionsFromMessage(string msgg, string from, string room)
{
string msg = Regex.Replace(msgg, @"(<color=#[A-Za-z0-9]{0,6}>)|(</color>)", String.Empty);
// todo: delete old msgs from author
if (msg.StartsWith("aucs ") || msg.StartsWith("aucb ")) { return GetAuctionsFromShortMessage(msg, from); }
//if (msg.StartsWith("aucc ")) { respondtocommand(msg,from); return; }
Auction.OfferType currentOfferType = Auction.OfferType.BUY; //Will be overwritten
//string[] words=msg.Split(' ');
List<Auction> addingAuctions = new List<Auction>();
List<char> delimiters = new List<char>();
//{ '\r', '\n', ' ', ',', ';' };
foreach (char a in msg)
{
if (!char.IsLetterOrDigit(a)) delimiters.Add(a);
}
string[] words = msg.Split(delimiters.ToArray(), StringSplitOptions.RemoveEmptyEntries);
//words = Regex.Split(msg, @"");
if (!msg.ToLower().Contains("wts") && !msg.ToLower().Contains("wtb") && !msg.ToLower().Contains("sell") && !msg.ToLower().Contains("buy")) return addingAuctions;
bool wtxfound = false;
DateTime now1 = DateTime.Now;
for (int i = 0; i < words.Length; i++)
{
Card c; int price = 0;
string word = words[i].ToLower();
// save in wts or wtb?
if (word.Contains("wts") || word.Contains("sell")) { currentOfferType = Auction.OfferType.SELL; wtxfound = true; };
if (word.Contains("wtb") || word.Contains("buy")) { currentOfferType = Auction.OfferType.BUY; wtxfound = true; };
if (!wtxfound) continue;// if no wts or wtb was found, skip card search
//int arrindex = Array.FindIndex(this.cardnames, element => word.Contains(element.Split(' ')[0])); // changed words[i] and element!
int arrindex = this.searchscrollsnicks.FindIndex(element => word.Contains(element.nick.Split(' ')[0]));
int iadder = 0;
if (arrindex >= 0) // wort in cardlist enthalten
{
//Console.WriteLine(word + " " + arrindex);
//string[] possiblecards = Array.FindAll(this.cardnames, element => word.Contains(element.Split(' ')[0]));
List<nickelement> possibnics = this.searchscrollsnicks.FindAll(element => word.Contains(element.nick.Split(' ')[0]));
bool findcard = false;
string foundedcard = "";
string textplace = "";
for (int ii = 0; ii < possibnics.Count; ii++)
{
//string match = possiblecards[ii].ToLower();
string match = possibnics[ii].nick.ToLower();
int posleng = Math.Min(match.Split(' ').Length, words.Length - i);
string searchob = string.Join(" ", words, i, posleng).ToLower();
if (searchob.Contains(match)) { findcard = true; foundedcard = possibnics[ii].cardname.ToLower(); iadder = posleng; textplace = searchob; break; };
}
//
i = i + iadder;
if (findcard)
{
//CardType type = CardTypeManager.getInstance().get(cardnametoid(foundedcard.ToLower()));
CardType type = CardTypeManager.getInstance().get(helpf.cardnamesToID[foundedcard.ToLower()]);
//int realarrindex = Array.FindIndex(helpf.cardnames, element => foundedcard.Equals(element));
int realarrindex = helpf.cardnameToArrayIndex(foundedcard);
c = new Card(helpf.cardids[realarrindex], type, true);
//Console.WriteLine("found " + foundedcard + " in " + textplace);
string tmpgold = pricetestfirst((textplace.Split(' '))[(textplace.Split(' ')).Length - 1]);
if (!(tmpgold == "")) // && iadder >1
{ // case: cardnamegold
//Console.WriteLine("found " + this.numberregx.Match(tmpgold).Value);
price = Convert.ToInt32(this.numberregx.Match(tmpgold).Value);
}
else if (i < words.Length)
{
int j = i;
tmpgold = pricetest(words[j]);
while (tmpgold == "")
{
if (j + 1 < words.Length)
{
j++;
tmpgold = pricetest(words[j]);
}
else { tmpgold = "fail"; }
}
if (!(tmpgold == "fail"))
{ // cardname gold
//Console.WriteLine("found gold " + this.numberregx.Match(tmpgold).Value);
price = Convert.ToInt32(this.numberregx.Match(tmpgold).Value);
}
}
now1=now1.AddMilliseconds(1);
addingAuctions.Add(new Auction(from, now1, currentOfferType, c, msgg, price));
//additemtolist(c, from, price, wts, msgg);
i--;
}//if (find) ende
}
}
return addingAuctions;
}
private string pricetestOffer(string price)
{
string result = "";
//Console.WriteLine("PREIS: " + price);
Match match = priceregexOffer.Match(" " + price + " ");
if (match.Success) { result = match.Value; }
return result;
}
}
}