-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathConnectedUser.cs
More file actions
833 lines (687 loc) · 30.6 KB
/
Copy pathConnectedUser.cs
File metadata and controls
833 lines (687 loc) · 30.6 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LobbyClient;
using PlasmaShared;
using ZkData;
namespace ZkLobbyServer
{
public class ConnectedUser : ICommandSender
{
public ConcurrentDictionary<ClientConnection, bool> Connections = new ConcurrentDictionary<ClientConnection, bool>();
private ServerBattle myBattle;
public ServerBattle MyBattle
{
get { return myBattle; }
set
{
myBattle = value;
var bid = value?.BattleID;
if (User.BattleID != bid)
{
User.BattleID = bid;
Interlocked.Increment(ref User.SyncVersion);
}
}
}
private ZkLobbyServer server;
private DateTime chatWait = DateTime.UtcNow;
public User User = new User();
public HashSet<string> FriendBy { get; set; }
public HashSet<string> FriendNames { get; set; }
public List<FriendEntry> FriendEntries { get; set; } = new List<FriendEntry>();
public HashSet<string> IgnoredBy { get; set; }
public HashSet<string> Ignores { get; set; }
public ConcurrentDictionary<string, int> HasSeenUserVersion { get; set; } = new ConcurrentDictionary<string, int>();
public bool IsLoggedIn => (User != null) && (User.AccountID != 0);
public string Name => User.Name;
private PartyManager partyManager => server.PartyManager;
public ConnectedUser(ZkLobbyServer server, User user)
{
this.server = server;
User = user;
LoadFriendsIgnores();
}
public async Task SendCommand<T>(T data)
{
try
{
var line = server.Serializer.SerializeToLine(data);
await SendLine(line);
}
catch (Exception ex)
{
Trace.TraceError("{0} error sending {1} : {2}", data, ex);
}
}
public async Task SendLine(string line)
{
await Task.WhenAll(Connections.Keys.Select(async (con) => { await con.SendLine(line); }));
}
public void LoadFriendsIgnores()
{
using (var db = new ZkDataContext())
{
var rels =
db.AccountRelations.Where(x => (x.TargetAccountID == User.AccountID) || (x.OwnerAccountID == User.AccountID))
.Select(x => new { OwnerAccountID = x.OwnerAccountID, Owner = x.Owner.Name, Target = x.Target.Name, Relation = x.Relation, SteamID = x.Target.SteamID })
.ToList();
FriendNames =
new HashSet<string>(rels.Where(x => (x.Relation == Relation.Friend) && (x.OwnerAccountID == User.AccountID)).Select(x => x.Target));
FriendBy =
new HashSet<string>(rels.Where(x => (x.Relation == Relation.Friend) && (x.OwnerAccountID != User.AccountID)).Select(x => x.Owner));
FriendEntries = new List<FriendEntry>(rels.Where(x => (x.Relation == Relation.Friend) && (x.OwnerAccountID == User.AccountID)).Select(x => new FriendEntry() { Name = x.Target, SteamID = x.SteamID?.ToString() }));
Ignores =
new HashSet<string>(rels.Where(x => (x.Relation == Relation.Ignore) && (x.OwnerAccountID == User.AccountID)).Select(x => x.Target));
IgnoredBy =
new HashSet<string>(rels.Where(x => (x.Relation == Relation.Ignore) && (x.OwnerAccountID != User.AccountID)).Select(x => x.Owner));
}
}
public async Task Process(MatchMakerQueueRequest queueRequest)
{
await server.MatchMaker.QueueRequest(this, queueRequest);
}
public async Task Process(PwJoinPlanet args)
{
await server.PlanetWarsMatchMaker.OnJoinPlanet(this, args);
}
public async Task Process(PwCancel args)
{
await server.PlanetWarsMatchMaker.OnCancel(this);
}
public async Task Process(KickFromBattle batKick)
{
if (!IsLoggedIn) return;
if ((batKick.BattleID == null) && (MyBattle != null)) batKick.BattleID = MyBattle.BattleID;
ServerBattle bat;
if (server.Battles.TryGetValue(batKick.BattleID.Value, out bat))
{
if ((bat.FounderName != Name) && !User.IsAdmin && (Name != batKick.Name))
{
await Respond("No rights to do a kick");
return;
}
await bat.KickFromBattle(batKick.Name, batKick.Reason);
}
}
public async Task Process(ForceJoinBattle forceJoin)
{
if (!IsLoggedIn) return;
if (!User.IsAdmin)
{
await Respond("No rights for force join");
return;
}
ServerBattle bat;
if (server.Battles.TryGetValue(forceJoin.BattleID, out bat)) await server.ForceJoinBattle(forceJoin.Name, bat);
}
public async Task Process(KickFromChannel chanKick)
{
if (!IsLoggedIn) return;
Channel channel;
User user;
if (server.Channels.TryGetValue(chanKick.ChannelName, out channel) && channel.Users.TryGetValue(chanKick.UserName, out user))
{
if (!User.IsAdmin)
{
await Respond("No rights to execute kick");
return;
}
var client = server.ConnectedUsers[chanKick.UserName];
await client.Respond(string.Format("You were kicked from channel {0} by {1} : {2}", chanKick.ChannelName, Name, chanKick.Reason));
await client.Process(new LeaveChannel() { ChannelName = chanKick.ChannelName });
}
}
public async Task Process(KickFromServer kick)
{
if (!IsLoggedIn) return;
ConnectedUser connectedUser;
if (server.ConnectedUsers.TryGetValue(kick.Name, out connectedUser))
{
if (!User.IsAdmin)
{
await Respond("No rights to execute kick");
return;
}
await server.KickFromServer(Name, kick.Name, kick.Reason);
}
}
public async Task Process(ForceJoinChannel forceJoin)
{
if (!IsLoggedIn) return;
if (!User.IsAdmin)
{
await Respond("No rights to execute forcejoin");
return;
}
ConnectedUser connectedUser;
if (server.ConnectedUsers.TryGetValue(forceJoin.UserName, out connectedUser))
{
Channel channel;
server.Channels.TryGetValue(forceJoin.ChannelName, out channel);
await
connectedUser.Process(new JoinChannel()
{
ChannelName = forceJoin.ChannelName,
Password = channel != null ? channel.Password : null
});
}
}
public async Task Process(JoinChannel joinChannel)
{
if (!IsLoggedIn) return;
if (!await server.ChannelManager.CanJoin(User.AccountID, joinChannel.ChannelName))
{
await
SendCommand(new JoinChannelResponse()
{
Success = false,
Reason = "you don't have permission to join this channel",
ChannelName = joinChannel.ChannelName
});
return;
}
var channel = server.Channels.GetOrAdd(joinChannel.ChannelName, (n) => new Channel() { Name = joinChannel.ChannelName, });
if (!string.IsNullOrEmpty(channel.Password) && (channel.Password != joinChannel.Password))
{
await SendCommand(new JoinChannelResponse() { Success = false, Reason = "invalid password", ChannelName = joinChannel.ChannelName });
return;
}
var added = channel.Users.TryAdd(Name, User);
if (!added)
{
await
SendCommand(new JoinChannelResponse()
{
Success = false,
Reason = "You are already in this channel",
ChannelName = joinChannel.ChannelName
});
return;
}
var visibleUsers = !channel.IsDeluge ? channel.Users.Keys.ToList() : channel.Users.Keys.Where(x => server.CanUserSee(Name, x)).ToList();
var canSeeMe = !channel.IsDeluge ? channel.Users.Keys.ToList() : channel.Users.Keys.Where(x => server.CanUserSee(x, Name)).ToList();
await server.TwoWaySyncUsers(Name, canSeeMe); // mutually sync user statuses
// send response with the list
await SendCommand(new JoinChannelResponse()
{
Success = true,
ChannelName = joinChannel.ChannelName,
Channel =
new ChannelHeader()
{
ChannelName = channel.Name,
Password = channel.Password,
Topic = channel.Topic,
Users = visibleUsers, // for zk use cansee test to not send all users
IsDeluge = channel.IsDeluge
}
});
// send missed messages
server.OfflineMessageHandler.SendMissedMessagesAsync(this, SayPlace.Channel, joinChannel.ChannelName, User.AccountID, channel.IsDeluge ? OfflineMessageHandler.DelugeMessageResendCount : OfflineMessageHandler.MessageResendCount);
// send self to other users who can see
if (added) await server.Broadcast(canSeeMe, new ChannelUserAdded { ChannelName = channel.Name, UserName = Name });
}
public async Task Process(LeaveChannel leaveChannel)
{
if (!IsLoggedIn) return;
Channel channel;
if (server.Channels.TryGetValue(leaveChannel.ChannelName, out channel))
{
User user;
var users = !channel.IsDeluge ? channel.Users.Keys.ToList() : channel.Users.Keys.Where(x => server.CanUserSee(x, Name)).ToList();
if (channel.Users.TryRemove(Name, out user))
{
await server.Broadcast(users, new ChannelUserRemoved() { ChannelName = channel.Name, UserName = Name });
}
}
}
public async Task Process(JoinFactionRequest joinFaction)
{
var db = new ZkDataContext();
var acc = db.Accounts.Find(User.AccountID);
var fac = db.Factions.First(x => !x.IsDeleted && x.Shortcut == joinFaction.Faction);
if (acc.FactionID == null)
{
if (acc.Clan != null && acc.Clan.FactionID == null) // if your clan is faction-less join the faciton too
{
acc.Clan.FactionID = fac.FactionID;
foreach (Account member in acc.Clan.Accounts) member.FactionID = fac.FactionID;
db.SaveChanges();
db.Events.InsertOnSubmit(server.PlanetWarsEventCreator.CreateEvent("Clan {0} moved to faction {1}", acc.Clan, fac));
}
acc.FactionID = fac.FactionID;
}
db.SaveChanges();
db.Events.InsertOnSubmit(server.PlanetWarsEventCreator.CreateEvent("{0} joins {1}", acc, fac));
db.SaveChanges();
await server.PublishAccountUpdate(acc);
await server.PublishUserProfileUpdate(acc);
}
public async Task Process(RequestConnectSpring connectSpring)
{
if (!IsLoggedIn) return;
ServerBattle battle;
if (server.Battles.TryGetValue(connectSpring.BattleID, out battle) && battle.IsInGame)
{
await battle.RequestConnectSpring(this, connectSpring.Password);
}
else await Respond("No such running battle found");
}
public async Task Process(Say say)
{
if (!IsLoggedIn) return;
if (User.BanMute) return; // block all say for muted
if (DateTime.UtcNow < chatWait) return; //block all say for spam
if (say.Text.Length > GlobalConst.LobbyMaxMessageSize) say.Text = say.Text.Substring(0, GlobalConst.LobbyMaxMessageSize);
if (DateTime.UtcNow.AddMilliseconds(-5 * GlobalConst.MinMillisecondsBetweenMessages) > chatWait) chatWait = DateTime.UtcNow.AddMilliseconds(-5 * GlobalConst.MinMillisecondsBetweenMessages);
chatWait = chatWait.AddMilliseconds(Math.Max(GlobalConst.MinMillisecondsBetweenMessages, GlobalConst.MillisecondsPerCharacter * say.Text.Length));
say.User = Name;
say.Time = DateTime.UtcNow;
if (say.Ring)
if (!User.IsAdmin)
if (((say.Place != SayPlace.Battle) && (say.Place != SayPlace.BattlePrivate)) || (MyBattle == null) ||
(MyBattle.FounderName != Name)) say.Ring = false;
// verify basic permissions to talk
switch (say.Place)
{
case SayPlace.Channel:
if (server.Channels.Get(say.Target)?.Users?.ContainsKey(Name) != true) return;
break;
case SayPlace.Battle:
if (MyBattle?.Users?.Keys.Contains(Name) != true) return;
break;
case SayPlace.BattlePrivate:
return;
break;
case SayPlace.MessageBox:
if (!User.IsAdmin) return;
break;
}
await server.GhostSay(say, MyBattle?.BattleID);
}
public async Task Process(OpenBattle openBattle)
{
if (!IsLoggedIn) return;
if (string.IsNullOrEmpty(openBattle.Header.Password) && User.BanVotes)
{
await Respond("Your rights have been restricted. You can only open passworded battles. Check your user page for details.");
return;
}
if (MyBattle != null)
{
await Respond("You are already in a battle");
return;
}
if (openBattle.Header.Mode != null
&& !Enum.IsDefined(typeof(AutohostMode), openBattle.Header.Mode))
{
await Respond("Incorrect battle type");
return;
}
openBattle.Header.Title = openBattle.Header.Title.Truncate(200);
var battle = new ServerBattle(server, Name);
battle.UpdateWith(openBattle.Header);
server.Battles[battle.BattleID] = battle;
await server.Broadcast(server.ConnectedUsers.Keys, new BattleAdded() { Header = battle.GetHeader() });
await Process(new JoinBattle() { BattleID = battle.BattleID, Password = openBattle.Header.Password, });
}
public async Task Process(JoinBattle join)
{
if (!IsLoggedIn) return;
ServerBattle battle;
if (server.Battles.TryGetValue(join.BattleID, out battle))
{
await battle.ProcessPlayerJoin(this, join.Password);
}
}
public async Task Process(BattleUpdate battleUpdate)
{
if (!IsLoggedIn) return;
var h = battleUpdate.Header;
h.Title = h.Title.Truncate(200);
if ((h.BattleID == null) && (MyBattle != null)) h.BattleID = MyBattle.BattleID;
ServerBattle bat;
if (!server.Battles.TryGetValue(h.BattleID.Value, out bat))
{
await Respond("No such battle exists");
return;
}
if ((bat.FounderName != Name) && !User.IsAdmin)
{
await Respond("You don't have permission to edit this battle");
return;
}
if (battleUpdate.Header.Mode != null
&& !Enum.IsDefined(typeof(AutohostMode), battleUpdate.Header.Mode))
{
await Respond("Incorrect battle type");
return;
}
bat.UpdateWith(h);
await server.Broadcast(server.ConnectedUsers.Keys, battleUpdate);
}
public async Task Process(UpdateUserBattleStatus status)
{
if (!IsLoggedIn) return;
var bat = MyBattle;
if (bat == null) return;
if ((Name == bat.FounderName) || (Name == status.Name))
{
// founder can set for all, others for self
UserBattleStatus ubs;
if (bat.Users.TryGetValue(status.Name, out ubs))
{
ubs.UpdateWith(status);
bat.ValidateBattleStatus(ubs);
await server.Broadcast(bat.Users.Keys, ubs.ToUpdateBattleStatus());
await bat.RecalcSpectators();
}
}
}
public async Task Process(LeaveBattle leave)
{
if (!IsLoggedIn) return;
if ((leave.BattleID == null) && (MyBattle != null)) leave.BattleID = MyBattle.BattleID;
ServerBattle battle;
if (server.Battles.TryGetValue(leave.BattleID.Value, out battle))
{
await LeaveBattle(battle);
await battle.RecalcSpectators();
}
}
public async Task Process(ChangeUserStatus userStatus)
{
if (!IsLoggedIn || userStatus == null) return;
var changed = false;
if ((userStatus.IsInGame != null) && (User.IsInGame != userStatus.IsInGame))
{
if (userStatus.IsInGame == true) User.InGameSince = DateTime.UtcNow;
else User.InGameSince = null;
changed = true;
}
if ((userStatus.IsAfk != null) && (User.IsAway != userStatus.IsAfk))
{
if (userStatus.IsAfk == true) User.AwaySince = DateTime.UtcNow;
else User.AwaySince = null;
changed = true;
}
if (changed)
{
Interlocked.Increment(ref User.SyncVersion);
await server.SyncUserToAll(this);
}
}
public async Task Process(UpdateBotStatus add)
{
if (!IsLoggedIn) return;
var battle = MyBattle;
if ((battle != null) && !battle.IsInGame)
{
if (battle.Mode != AutohostMode.None && battle.Mode != AutohostMode.GameChickens)
{
await Respond("Sorry, this room type does not support bots, please use cooperative or custom");
return;
}
BotBattleStatus ubs;
if (!battle.Bots.TryGetValue(add.Name, out ubs))
{
if (battle.Bots.Count < 50)
{
ubs = new BotBattleStatus(add.Name, Name, add.AiLib);
}
else
{
await Respond("Maximal number of bots reached");
return;
}
}
else if ((ubs.owner != Name) && !User.IsAdmin && (Name != battle.FounderName))
{
await Respond(string.Format("No permissions to edit bot {0}", add.Name));
return;
}
ubs.UpdateWith(add);
battle.Bots[ubs.Name] = ubs;
await server.Broadcast(battle.Users.Keys, ubs.ToUpdateBotStatus());
}
}
public async Task Process(RemoveBot rem)
{
if (!IsLoggedIn) return;
var battle = MyBattle;
if ((battle != null) && !battle.IsInGame)
{
var bot = battle.Bots[rem.Name];
if ((bot.owner != Name) && !User.IsAdmin && (Name != battle.FounderName))
{
await Respond(string.Format("No permissions to edit bot {0}", rem.Name));
return;
}
BotBattleStatus ubs;
if (battle.Bots.TryRemove(rem.Name, out ubs)) await server.Broadcast(battle.Users.Keys, rem);
}
}
public async Task Process(SetModOptions options)
{
if (!IsLoggedIn) return;
var bat = MyBattle;
if (bat != null)
{
if ((bat.FounderName != Name || bat.IsAutohost) && !User.IsAdmin)
{
await Respond("You don't have permissions to change mod options here");
return;
}
await bat.SetModOptions(options.Options);
}
}
public async Task Process(SetMapOptions options)
{
if (!IsLoggedIn) return;
var bat = MyBattle;
if (bat != null)
{
if ((bat.FounderName != Name || bat.IsAutohost) && !User.IsAdmin)
{
await Respond("You don't have permissions to change mod options here");
return;
}
await bat.SetMapOptions(options.Options);
}
}
public async Task Process(UserReport report)
{
if (!IsLoggedIn) return;
if (string.IsNullOrEmpty(report.Username) && string.IsNullOrEmpty(report.Text)) return;
using (var db = new ZkDataContext())
{
var reporter = db.Accounts.FirstOrDefault(x => x.AccountID == User.AccountID);
var reported = db.Accounts.FirstOrDefault(x => x.Name == report.Username);
if (reported == null || reporter == null) return;
await server.ReportUser(db, reporter, reported, report.Text);
}
}
public async Task Process(GetCustomGameMode modeRequest)
{
using (var db = new ZkDataContext())
{
var mode = db.GameModes.FirstOrDefault(x => x.ShortName == modeRequest.ShortName);
if (mode == null)
{
await SendCommand(new CustomGameModeResponse() { ShortName = modeRequest.ShortName });
}
else
{
await SendCommand(new CustomGameModeResponse()
{
ShortName = mode.ShortName,
DisplayName = mode.DisplayName,
GameModeJson = mode.GameModeJson
});
}
}
}
public async Task Process(SetAccountRelation rel)
{
if (!IsLoggedIn) return;
if (string.IsNullOrEmpty(rel.TargetName) && string.IsNullOrEmpty(rel.SteamID)) return;
using (var db = new ZkDataContext())
{
ulong steamId = 0;
var srcAccount = db.Accounts.Find(User.AccountID);
ulong.TryParse(rel.SteamID, out steamId);
var trgtAccount = Account.AccountByName(db, rel.TargetName) ?? db.Accounts.FirstOrDefault(x => x.SteamID == steamId);
if (trgtAccount == null)
{
if (!string.IsNullOrEmpty(rel.TargetName)) await Respond("No such account found"); // only warn if name is set and not just steam id
return;
}
var friendAdded = false;
var entry = srcAccount.RelalationsByOwner.FirstOrDefault(x => x.TargetAccountID == trgtAccount.AccountID);
if ((rel.Relation == Relation.None) && (entry != null)) db.AccountRelations.Remove(entry);
if (rel.Relation != Relation.None)
if (entry == null)
{
if (rel.Relation == Relation.Friend) friendAdded = true;
entry = new AccountRelation() { Owner = srcAccount, Target = trgtAccount, Relation = rel.Relation };
srcAccount.RelalationsByOwner.Add(entry);
}
else entry.Relation = rel.Relation;
db.SaveChanges();
ConnectedUser targetConnectedUser;
if (server.ConnectedUsers.TryGetValue(trgtAccount.Name, out targetConnectedUser))
{
targetConnectedUser.LoadFriendsIgnores(); // update partner's mutual lists
if (friendAdded) // friend added, sync new friend to me (user, battle and channels)
{
await server.TwoWaySyncUsers(Name, new List<string>() { targetConnectedUser.Name });
foreach (var chan in
server.Channels.Values.Where(
x => (x != null) && x.Users.ContainsKey(Name) && x.Users.ContainsKey(targetConnectedUser.Name))) await SendCommand(new ChannelUserAdded() { ChannelName = chan.Name, UserName = targetConnectedUser.Name });
}
}
LoadFriendsIgnores();
await SendCommand(new FriendList() { Friends = FriendEntries.ToList() });
await SendCommand(new IgnoreList() { Ignores = Ignores.ToList() });
}
}
public async Task RemoveConnection(ClientConnection con, string reason)
{
bool dummy;
if (Connections.TryRemove(con, out dummy) && (Connections.Count == 0))
{
// notify all channels where i am to all users that i left
foreach (var chan in server.Channels.Values.Where(x => x.Users.ContainsKey(Name)).ToList()) await Process(new LeaveChannel() { ChannelName = chan.Name });
foreach (var b in server.Battles.Values.Where(x => x.Users.ContainsKey(Name)))
{
await LeaveBattle(b);
await b.RecalcSpectators();
}
if (await server.MatchMaker.RemoveUser(Name, true))
{
await server.UserLogSay($"{Name} disconnected, removing from MM.");
}
await server.PartyManager.OnUserDisconnected(Name);
await server.PlanetWarsMatchMaker.OnUserDisconnected(Name);
server.ForumListManager.OnUserDisconnected(User.AccountID);
await server.Broadcast(server.ConnectedUsers.Values.Where(x => x != null && server.CanUserSee(x, this)), new UserDisconnected() { Name = Name, Reason = reason });
server.RemoveSessionsForAccountID(User.AccountID);
ConnectedUser connectedUser;
if (server.ConnectedUsers.TryRemove(Name, out connectedUser))
{
int accountID = User.AccountID;
connectedUser.ResetHasSeen();
connectedUser.User.AccountID = 0;
using (var db = new ZkDataContext())
{
var acc = await db.Accounts.FindAsync(accountID);
acc.LastLogout = DateTime.UtcNow;
acc.LastChatRead = DateTime.UtcNow;
await db.SaveChangesAsync();
}
}
}
}
public void RequestCloseAll()
{
foreach (var c in Connections.Keys) c.RequestClose();
}
public Task Respond(string message)
{
return SendCommand(new Say() { Place = SayPlace.MessageBox, Target = Name, User = Name, Text = message });
}
public async Task Process(AreYouReadyResponse response)
{
await server.MatchMaker.AreYouReadyResponse(this, response);
}
public async Task Process(InviteToParty invite)
{
await partyManager.ProcessInviteToParty(this, invite);
}
public async Task Process(PartyInviteResponse response)
{
await partyManager.ProcessPartyInviteResponse(this, response);
}
public async Task Process(LeaveParty message)
{
await partyManager.ProcessLeaveParty(this, message);
}
public override string ToString()
{
return string.Format("[{0}]", Name);
}
public void ResetHasSeen()
{
HasSeenUserVersion.Clear();
foreach (var conus in server.ConnectedUsers.Values.Where(x => x != null))
{
int seenVersion;
conus.HasSeenUserVersion.TryRemove(Name, out seenVersion);
}
}
private async Task LeaveBattle(ServerBattle battle)
{
if (battle.Users.ContainsKey(Name))
{
MyBattle = null;
UserBattleStatus oldVal;
if (battle.Users.TryRemove(Name, out oldVal))
{
await server.SyncUserToAll(this);
var bots = battle.Bots.Values.Where(x => x.owner == Name).ToList();
foreach (var b in bots)
{
BotBattleStatus obs;
if (battle.Bots.TryRemove(b.Name, out obs)) await server.Broadcast(battle.Users.Keys, new RemoveBot() { Name = b.Name });
}
}
await battle.CheckCloseBattle();
}
}
private DateTime lastThrottleReset = DateTime.UtcNow;
private int bytesSent;
public async Task Throttle(int lineLength)
{
bytesSent += lineLength;
if (bytesSent < GlobalConst.LobbyThrottleBytesPerSecond) return;
var now = DateTime.UtcNow;
var seconds = now.Subtract(lastThrottleReset).TotalSeconds;
if (bytesSent <= GlobalConst.LobbyThrottleBytesPerSecond * seconds)
{
bytesSent = 0;
lastThrottleReset = now;
}
else
{
var needForSleep = (double)bytesSent / GlobalConst.LobbyThrottleBytesPerSecond - seconds;
await Task.Delay((int)Math.Round(needForSleep * 1000.0));
}
}
}
}