-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
210 lines (172 loc) · 7.31 KB
/
Program.cs
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
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using var cts = new CancellationTokenSource();
var receiverOptions = new ReceiverOptions{
AllowedUpdates = Array.Empty<UpdateType>()
};
///////////////////////////////////////////////////////////////////////////////////////////////////////
IClient telegram= new TlgClient("5747520451:AAFUXAYgxTJK7tU4m3HLk3N5ec-5Ks0xGDs");
//////////////////////////////////////////////////////////////////////////////////////////////////////
List<party> parties=new List<party>();
Dictionary<int,int>player_party=new Dictionary<int, int>();
DataAdventure Data= new DataAdventure(telegram);
int current_party=1000000;
void add_party(int leader,string adv_name,string leader_name, string leader_user){
parties.Add(new party(telegram,current_party,adv_name,leader,leader_name,leader_user));
player_party.Add(leader,current_party-1000000);
current_party++;
}
void add_member(int member, string name,string user, int party_id){
player_party.Add(member,party_id-1000000);
parties[party_id-1000000].add_member( member, name, user);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
((TlgClient)telegram).botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
);
var me = await ((TlgClient)telegram).botClient.GetMeAsync();
Console.WriteLine($"Server started correctly");
((TlgClient)telegram).notifyAdmins(new ClientParams("Server started correctly"));
Console.ReadLine();
cts.Cancel();
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken){
try{
if (update.Message is not { } message) { //CallBackHandling
if(update.CallbackQuery is not {} callb) return;
var cId=callb.Message.Chat.Id;
Console.WriteLine($"{cId}({callb.Message.Chat.FirstName}): {callb.Data}");
if(callb.Data.StartsWith("/choose_hero")){
parties[player_party[(int)cId]].choose_hero((int)cId,int.Parse(callb.Data.Substring(13)) );
}
if(callb.Data.StartsWith("/new_adv")){
string adv_name=callb.Data.Substring(9);
add_party((int)cId,adv_name,callb.Message.Chat.FirstName,callb.Message.Chat.Username);
telegram.notify(
(int)cId,
new ClientParams(
$"Adventure created. Send to your friends the following command to join the adventure /join {current_party-1}",
rM: callb.Message.MessageId)
);
}
if (callb.Data.StartsWith("/do") ){
string mess=callb.Data.Substring(3);
parties[player_party[(int)cId]].do_action((int)cId,int.Parse(mess));
return;
}
return;
}
if (message.Text is not { } messageText)return;
var chatId = message.Chat.Id;
Console.WriteLine($"{chatId}({message.From.FirstName}): {messageText}");
if (messageText.StartsWith("/run") ){// Only for developers
if( (int)chatId==789850916 || (int)chatId==639646249 ){
string script= messageText.Substring(5);
parties[player_party[(int)chatId]].run_script(script);
}
return;
}
if (messageText.StartsWith("/new_adventure")){
if(player_party.ContainsKey((int)chatId)){
telegram.notify(
(int)chatId,
new ClientParams(
"You are already in one adventure, if you want to host a new one /quit the current",
rM: (int)message.MessageId
)
);
}
Data.printAllAdventures((int)chatId, message.MessageId);
return;
}
if (messageText.StartsWith("/join") ){
int party=int.Parse(messageText.Substring(6,7));
if(player_party.ContainsKey((int)chatId) && player_party[(int)chatId]==(int)party ){
telegram.notify(
(int)chatId,
new ClientParams("You are already in this adventure")
);
return;
}
if(parties[party-1000000].isStarted){
telegram.notify(
(int)chatId,
new ClientParams("That adventure is already started, try another or create a new one")
);
return;
}
add_member((int)chatId, message.From.FirstName, message.From.Username , party);
return;
}
if(messageText=="/start_adventure"){
if( !player_party.ContainsKey((int)chatId) ){
telegram.notify(
(int)chatId,
new ClientParams(
"You have to host an adventure first, type /new_adventure to host it",
rM:(int)message.MessageId
)
);
return;
}
parties[player_party[(int)chatId]].start_adventure();
return;
}
if(!player_party.ContainsKey((int)chatId)){
telegram.notify(
(int)chatId,
new ClientParams(
"You are not in any adventure, create one or join one",
rM: message.MessageId
)
);
return;
}
if(messageText.StartsWith("/variables") ){
parties[player_party[(int)chatId]].print_vars((int)chatId);
return;
}
if(messageText.StartsWith("/actions") ){
parties[player_party[(int)chatId]].actions((int)chatId);
return;
}
if(messageText.StartsWith("/end_adventure") ){
foreach(player member in parties[player_party[(int)chatId]].members){
player_party.Remove(member.chat_id);
}
return;
}
if( !messageText.StartsWith("/") ){
if(messageText=="") return;
parties[player_party[(int)chatId]].chat($"@{message.From.Username}: {messageText}", (int)chatId);
return;
}
telegram.notify(
(int)chatId,
new ClientParams("Unknown command")
);
}catch (Exception e){
Console.WriteLine("{0} Exception caught.", e);
if (update.Message is not { } message)return;
if (message.Text is not { } messageText)return;
var chatId = message.Chat.Id;
telegram.notify(
(int)chatId,
new ClientParams ("Bad command ussage or error")
);
}
}
Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken){
var ErrorMessage = exception switch{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}