Skip to content

Commit a119d36

Browse files
committed
リファクタリング
1 parent f67a8f3 commit a119d36

File tree

1 file changed

+88
-70
lines changed

1 file changed

+88
-70
lines changed

MildomSitePlugin/MessageProvider.cs

Lines changed: 88 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,87 @@ class OnChatMessage : IInternalMessage
4141
public string UserImg { get; internal set; }
4242
public DateTime PostedAt { get; internal set; }
4343
public string Raw { get; set; }
44+
public static IInternalMessage CreateChat(string raw, Dictionary<int, string> imageDict, dynamic d)
45+
{
46+
IInternalMessage internalMessage;
47+
//{"area": 2000, "cmd": "onChat", "fansBgPic": null, "fansGroupType": null, "fansLevel": null, "fansName": null, "level": 7, "medals": null, "msg": "うめえぇぇえ", "reqId": 0, "roomAdmin": 0, "roomId": 10038336, "toId": 10038336, "toName": "Nephrite【ネフライト】", "type": 3, "userId": 10088625, "userImg": "https://vpic.mildom.com/download/file/jp/mildom/nnphotos/10088625/5F0AB42E-8BF4-4A3A-9E70-FC6A9A49AAF0.jpg", "userName": "FSーSavage"}
48+
var messageItems = PlainTextToCommentAndStamp(imageDict, d);
49+
//2020/08/27 userIdの無いコメントを確認。ゲストユーザでもコメント投稿できるのだろうか?定型文だけ?
50+
//{"area": 2000, "cmd": "onChat", "msg": "よくやった", "msgId": "1598498460835_0_8192", "reqId": 0, "roomId": 10007428, "time": "1598498460835", "toId": 10007428, "toName": "*", "type": 3, "userName": "guest737168"}
51+
//2020/08/29 onAddでは匿名ユーザーのユーザーIDは0になっている。統一したい。
52+
string userId;
53+
if (d.IsDefined("userId"))
54+
{
55+
userId = ((long)d.userId).ToString();
56+
}
57+
else
58+
{
59+
//匿名ユーザー。userNameが"guest737168"のような形式だからこれを流用してみる。
60+
userId = d.userName;
61+
}
62+
//userIdが無い場合はuserImgも無さそう。
63+
string userImg;
64+
if (d.IsDefined("userImg"))
65+
{
66+
userImg = d.userImg;
67+
}
68+
else
69+
{
70+
userImg = null;
71+
}
72+
internalMessage = new OnChatMessage
73+
{
74+
MessageItems = messageItems,
75+
UserId = userId,
76+
UserName = d.userName,
77+
UserImg = userImg,
78+
PostedAt = Utils.GetCurrentDateTime(),
79+
Raw = raw,
80+
};
81+
return internalMessage;
82+
}
83+
/// <summary>
84+
/// コメントの中に[/7]のような形式でスタンプが埋め込まれているから分離する
85+
/// </summary>
86+
/// <param name="imageDict"></param>
87+
/// <param name="rawComment"></param>
88+
/// <returns></returns>
89+
private static List<IMessagePart> PlainTextToCommentAndStamp(Dictionary<int, string> imageDict, string rawComment)
90+
{
91+
var messageItems = new List<IMessagePart>();
92+
var arr = Regex.Split(rawComment, "\\[/(\\d+)\\]");
93+
for (int i = 0; i < arr.Length; i++)
94+
{
95+
var item = arr[i];
96+
if (i % 2 == 0)
97+
{
98+
if (string.IsNullOrEmpty(item))
99+
{
100+
continue;
101+
}
102+
messageItems.Add(Common.MessagePartFactory.CreateMessageText(item));
103+
}
104+
else
105+
{
106+
if (int.TryParse(item, out int n))
107+
{
108+
if (imageDict.TryGetValue(n, out var emotUrl))
109+
{
110+
messageItems.Add(new Common.MessageImage
111+
{
112+
Alt = $"[/{item}]",
113+
Height = 40,
114+
Width = 40,
115+
Url = emotUrl,
116+
X = null,
117+
Y = null,
118+
});
119+
}
120+
}
121+
}
122+
}
123+
return messageItems;
124+
}
44125
}
45126
class OnBroadcast : IInternalMessage
46127
{
@@ -100,12 +181,15 @@ public static OnUserCountMessage Create(dynamic json, string raw)
100181
};
101182
}
102183
}
103-
class MessageParser
184+
static class Utils
104185
{
105186
public static DateTime GetCurrentDateTime()
106187
{
107188
return DateTime.Now;
108189
}
190+
}
191+
class MessageParser
192+
{
109193
public static IInternalMessage Parse(string raw, Dictionary<int, string> imageDict)
110194
{
111195
IInternalMessage internalMessage;
@@ -118,70 +202,7 @@ public static IInternalMessage Parse(string raw, Dictionary<int, string> imageDi
118202
break;
119203
case "onChat":
120204
{
121-
//{"area": 2000, "cmd": "onChat", "fansBgPic": null, "fansGroupType": null, "fansLevel": null, "fansName": null, "level": 7, "medals": null, "msg": "うめえぇぇえ", "reqId": 0, "roomAdmin": 0, "roomId": 10038336, "toId": 10038336, "toName": "Nephrite【ネフライト】", "type": 3, "userId": 10088625, "userImg": "https://vpic.mildom.com/download/file/jp/mildom/nnphotos/10088625/5F0AB42E-8BF4-4A3A-9E70-FC6A9A49AAF0.jpg", "userName": "FSーSavage"}
122-
var messageItems = new List<IMessagePart>();
123-
var arr = Regex.Split(d.msg, "\\[/(\\d+)\\]");
124-
for (int i = 0; i < arr.Length; i++)
125-
{
126-
if (i % 2 == 0)
127-
{
128-
if (string.IsNullOrEmpty(arr[i]))
129-
{
130-
continue;
131-
}
132-
messageItems.Add(Common.MessagePartFactory.CreateMessageText(arr[i]));
133-
}
134-
else
135-
{
136-
if (int.TryParse(arr[i], out int n))
137-
{
138-
if (imageDict.TryGetValue(n, out var emotUrl))
139-
{
140-
messageItems.Add(new Common.MessageImage
141-
{
142-
Alt = $"[/{item}]",
143-
Height = 40,
144-
Width = 40,
145-
Url = emotUrl,
146-
X = null,
147-
Y = null,
148-
});
149-
}
150-
}
151-
}
152-
}
153-
//2020/08/27 userIdの無いコメントを確認。ゲストユーザでもコメント投稿できるのだろうか?定型文だけ?
154-
//{"area": 2000, "cmd": "onChat", "msg": "よくやった", "msgId": "1598498460835_0_8192", "reqId": 0, "roomId": 10007428, "time": "1598498460835", "toId": 10007428, "toName": "*", "type": 3, "userName": "guest737168"}
155-
//2020/08/29 onAddでは匿名ユーザーのユーザーIDは0になっている。統一したい。
156-
string userId;
157-
if (d.IsDefined("userId"))
158-
{
159-
userId = ((long)d.userId).ToString();
160-
}
161-
else
162-
{
163-
//匿名ユーザー。userNameが"guest737168"のような形式だからこれを流用してみる。
164-
userId = d.userName;
165-
}
166-
//userIdが無い場合はuserImgも無さそう。
167-
string userImg;
168-
if (d.IsDefined("userImg"))
169-
{
170-
userImg = d.userImg;
171-
}
172-
else
173-
{
174-
userImg = null;
175-
}
176-
internalMessage = new OnChatMessage
177-
{
178-
MessageItems = messageItems,
179-
UserId = userId,
180-
UserName = d.userName,
181-
UserImg = userImg,
182-
PostedAt = GetCurrentDateTime(),
183-
Raw = raw,
184-
};
205+
internalMessage =OnChatMessage. CreateChat(raw, imageDict, d);
185206
}
186207
break;
187208
case "onAdd":
@@ -208,7 +229,7 @@ public static IInternalMessage Parse(string raw, Dictionary<int, string> imageDi
208229
UserId = userId,
209230
UserName = username,
210231
UserImg = d.userImg,
211-
PostedAt = GetCurrentDateTime(),
232+
PostedAt = Utils.GetCurrentDateTime(),
212233
Raw = raw,
213234
};
214235
}
@@ -233,7 +254,7 @@ public static IInternalMessage Parse(string raw, Dictionary<int, string> imageDi
233254
UserId = (long)d.userId,
234255
UserName = d.userName,
235256
UserImg = d.userImg,
236-
PostedAt = GetCurrentDateTime(),
257+
PostedAt = Utils.GetCurrentDateTime(),
237258
Raw = raw,
238259
};
239260
break;
@@ -271,9 +292,6 @@ public static IInternalMessage Parse(string raw, Dictionary<int, string> imageDi
271292
return internalMessage;
272293
}
273294
}
274-
275-
276-
277295
class MessageProvider : IProvider
278296
{
279297
public IProvider Master { get; set; }

0 commit comments

Comments
 (0)