-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactions.cs
More file actions
1989 lines (1941 loc) · 116 KB
/
Factions.cs
File metadata and controls
1989 lines (1941 loc) · 116 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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Data;
using System.ComponentModel;
using Terraria;
using Hooks;
using TShockAPI;
using TShockAPI.DB;
using Mono.Data.Sqlite;
using MySql.Data.MySqlClient;
using ChatAssistant;
using System.Threading;
namespace Factions
{
[APIVersion(1, 12)]
public class Factions : TerrariaPlugin
{
private static string savepath = Path.Combine(TShock.SavePath, "Factions/");
private static IDbConnection db;
public static void DoQuery(String query, params object[] args)
{
db.Query(query, args);
}
public static QueryResult GetQuery(String query, params object[] args)
{
return db.QueryReader(query, args);
}
public static List<Faction> FactionList = new List<Faction>();
public static List<Region> Regions = new List<Region>();
public static Player[] PlayerList = new Player[256];
private static Thread UpdatePowerThread = new Thread(UpdatePower);
private static void UpdatePower()
{
while (UpdatePowerThread.IsAlive)
{
if (Main.worldID > 0)
{
try
{
db.Query("UPDATE factions_Players SET OfflineCount = OfflineCount + 10 WHERE Online = 0 AND Power > -100 AND WorldID = @0", Main.worldID);
db.Query("UPDATE factions_Players SET Power = Power - 1, OfflineCount = 0 WHERE OfflineCount >= @1 AND Power > -100 AND WorldID = @0", Main.worldID, 120);
foreach (Faction fact in FactionList)
{
fact.RefreshPower();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
}
Thread.Sleep(600000);
}
}
public override string Name
{
get { return "Factions"; }
}
public override string Author
{
get { return "by InanZen"; }
}
public override string Description
{
get { return "factions"; }
}
public override Version Version
{
get { return new Version("0.65"); }
}
public Factions(Main game)
: base(game)
{
Order = 1;
}
public override void Initialize()
{
NetHooks.GetData += GetData;
NetHooks.SendData += SendData;
NetHooks.GreetPlayer += OnJoin;
ServerHooks.Leave += OnLeave;
ServerHooks.Chat += OnChat;
GameHooks.Update += OnUpdate;
Commands.ChatCommands.Add(new Command("factions.command", CommandMethod, "factions", "faction", "fact", "f"));
if (!Directory.Exists(savepath))
Directory.CreateDirectory(savepath);
SetupDb();
UpdatePowerThread.Start();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
NetHooks.GetData -= GetData;
NetHooks.SendData -= SendData;
NetHooks.GreetPlayer -= OnJoin;
ServerHooks.Leave -= OnLeave;
ServerHooks.Chat -= OnChat;
GameHooks.Update -= OnUpdate;
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
try
{
UpdatePowerThread.Abort();
db.Query("UPDATE factions_Players SET Online = 0 WHERE Online = 1 AND WorldID = @0", Main.worldID);
for (int i = 0; i < PlayerList.Length; i++)
{
if (PlayerList[i] != null && PlayerList[i].UserID != -1)
PlayerList[i].StopUpdating();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
}
base.Dispose(disposing);
}
public void OnChat(messageBuffer buffer, int who, string text, HandledEventArgs args)
{
var player = PlayerList[who];
if (player != null)
{
if (text.StartsWith("/login") && player.TSplayer.UserID != player.UserID)
{
// Console.WriteLine("player.userID: {0}, TSplayer.userID: {1}", player.UserID, player.TSplayer.UserID);
if (player.UserID != -1)
{
int factid = 0;
if (player.Faction != null)
factid = player.Faction.ID;
db.Query("UPDATE factions_Players SET Power = @0, Faction = @1 WHERE UserID = @2 AND WorldID = @3", player.Power, factid, player.UserID, Main.worldID);
player.StopUpdating();
}
using (QueryResult reader = db.QueryReader("SELECT * FROM factions_Players WHERE UserID = @0 AND WorldID = @1", player.TSplayer.UserID, Main.worldID))
{
if (reader.Read())
{
var faction = Faction.GetFactionByID(reader.Get<int>("Faction"));
PlayerList[who] = new Player(who, reader.Get<int>("Power"), faction);
reader.Dispose();
db.Query("UPDATE factions_Players SET Online = 1 WHERE UserID = @0 AND WorldID = @1", player.TSplayer.UserID, Main.worldID);
}
else
{
reader.Dispose();
db.Query("INSERT INTO factions_Players (UserID, Name, Power, Online, OfflineCount, Faction, WorldID) VALUES (@0, @1, 10, 1, 0, 0, @2)", player.TSplayer.UserID, player.TSplayer.UserAccountName, Main.worldID);
PlayerList[who] = new Player(who, 10, null);
}
}
PlayerList[who].StartUpdating();
}
}
}
/* private static void CheckPlayer(Player player)
{
if (text.StartsWith("/login") && player.TSplayer.UserID != player.UserID)
{
Console.WriteLine("player.userID: {0}, TSplayer.userID: {1}", player.UserID, player.TSplayer.UserID);
if (player.UserID != -1)
{
int factid = 0;
if (player.Faction != null)
factid = player.Faction.ID;
db.Query("UPDATE factions_Players SET Power = @0, Faction = @1 WHERE UserID = @2 AND WorldID = @3", player.Power, factid, player.UserID, Main.worldID);
}
using (QueryResult reader = db.QueryReader("SELECT * FROM factions_Players WHERE UserID = @0 AND WorldID = @1", player.TSplayer.UserID, Main.worldID))
{
if (reader.Read())
{
var faction = GetFactionByID(reader.Get<int>("Faction"));
PlayerList[who] = new Player(who, reader.Get<int>("Power"), faction);
db.Query("UPDATE factions_Players SET Online = 1 WHERE UserID = @0 AND WorldID = @1", player.TSplayer.UserID, Main.worldID);
}
else
{
db.Query("INSERT INTO factions_Players (UserID, Name, Power, Online, OfflineCount, Faction, WorldID) VALUES (@0, @1, 10, 1, 0, 0, @2)", player.TSplayer.UserID, player.TSplayer.UserAccountName, Main.worldID);
PlayerList[who] = new Player(who, 10, null);
}
}
Console.WriteLine("playerlist[who].userid: {0}", PlayerList[who].UserID);
PlayerList[who].StartUpdating();
}
}*/
public void OnJoin(int who, HandledEventArgs e)
{
var player = TShock.Players[who];
if (player != null)
{
if (player.UserID != -1)
{
try
{
QueryResult reader = db.QueryReader("SELECT * FROM factions_Players WHERE UserID = @0 AND WorldID = @1", player.UserID, Main.worldID);
if (reader.Read())
{
var faction = Faction.GetFactionByID(reader.Get<int>("Faction"));
PlayerList[who] = new Player(who, reader.Get<int>("Power"), faction);
if (faction != null)
player.SetTeam(faction.Team);
reader.Dispose();
db.Query("UPDATE factions_Players SET Online = 1 WHERE UserID = @0 AND WorldID = @1", player.UserID, Main.worldID);
}
else
{
reader.Dispose();
db.Query("INSERT INTO factions_Players (UserID, Name, Power, Online, OfflineCount, Faction, WorldID) VALUES (@0, @1, 10, 1, 0, 0, @2)", player.UserID, player.UserAccountName, Main.worldID);
PlayerList[who] = new Player(who, 10, null);
}
PlayerList[who].StartUpdating();
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
}
else
PlayerList[who] = new Player(who, 0, null);
}
}
public void OnLeave(int who)
{
var player = PlayerList[who];
if (player != null)
{
int factid = 0;
if (player.Faction != null)
factid = player.Faction.ID;
db.Query("UPDATE factions_Players SET Power = @0, Faction = @1, Online = 0 WHERE UserID = @2 AND WorldID = @3", player.Power, factid, player.UserID, Main.worldID);
player.StopUpdating();
}
PlayerList[who] = null;
}
private static void DisplayRegionInfo(Player player)
{
OutlineRegion(player);
var region = Region.GetRegionFromLocation(player.TSplayer.TileX, player.TSplayer.TileY);
List<MenuItem> menuData = new List<MenuItem>();
menuData.Add(new MenuItem(String.Format("Plot ({0}, {1}) - ({2}, {3})", region.X, region.Y, region.X + region.Width - 1, region.Y + region.Height - 1), 0, false, Color.Gray));
if (region.ID == -1)
{
menuData.Add(new MenuItem("Plot has no owner", 0, false, Color.LightGray));
int price = GetPlotPrice(player, new Point(player.TSplayer.TileX, player.TSplayer.TileY));
if (price == -1)
menuData.Add(new MenuItem("This land is unclaimable", 0, false, Color.Gray));
else if (player.Faction == null)
{
if (price == -2)
menuData.Add(new MenuItem("You cannot claim any more plots at the moment", 0, false, Color.Gray));
else
{
menuData.Add(new MenuItem(string.Format("Price for you: {0} power", price), 0, false, Color.LightGray));
menuData.Add(new MenuItem(String.Format("You have: {0} power", player.Power), 0, false, (player.Power - price >= 0) ? Color.DarkGreen : Color.DarkRed));
if (player.Power - price >= 0)
menuData.Add(new MenuItem("[ Claim it for yourself ]", 20, Color.BurlyWood));
}
}
else
{
if (price == -2)
menuData.Add(new MenuItem("Your faction cannot claim any more plots at the moment", 0, false, Color.Gray));
else
{
menuData.Add(new MenuItem(string.Format("Price for your faction: {0} power", price), 0, false, Color.LightGray));
menuData.Add(new MenuItem(String.Format("Your faction has: {0} power", player.Faction.Power), 0, false, (player.Faction.Power - price >= 0) ? Color.DarkGreen : Color.DarkRed));
if (player.Faction.Power - price >= 0 && player.Faction.IsAdmin(player.UserID))
menuData.Add(new MenuItem("[ Claim it for your faction ]", 20, Color.BurlyWood));
}
}
}
else
{
if (region.Owner != 0)
{
if (region.Owner == player.UserID)
{
menuData.Add(new MenuItem("This is your land", 0, false, Color.LightGray));
menuData.Add(new MenuItem("[ Unclaim ]", 22, Color.DarkRed));
}
else
{
using (QueryResult reader = db.QueryReader("SELECT * FROM factions_Players WHERE UserID = @0", region.Owner))
{
if (reader.Read())
menuData.Add(new MenuItem(String.Format("This land belongs to: {0}", reader.Get<String>("Name")), 0, false, Color.LightGray));
}
}
}
else if (region.Faction != 0)
{
var faction = Faction.GetFactionByID(region.Faction);
if (player.Faction != null && player.Faction.Equals(faction))
{
menuData.Add(new MenuItem("This land belongs to your Faction", 0, false, Color.LightGray));
if (faction.IsAdmin(player.UserID))
menuData.Add(new MenuItem("[ Unclaim ]", 22, Color.DarkRed));
}
else
{
var menuItem = new MenuItem("This land belongs to Faction: [ @0 ]", 310, Color.LightGray);
menuItem.Input = faction.Name;
menuData.Add(menuItem);
}
}
else
menuData.Add(new MenuItem("Abnormal", -1, false, Color.LightGray));
}
if (player.Menu == null)
player.Menu = Menu.CreateMenu(player.ID, "Plot manager", menuData, new CAMain.MenuAction(OnMenu));
else
{
player.Menu.title = "Plot manager";
player.Menu.index = 0;
menuData.Add(new MenuItem("[ <- Back ]", 1, Color.BurlyWood));
player.Menu.contents = menuData;
player.Menu.DisplayMenu();
}
}
private static void OutlineRegion(Player player)
{
int x = player.TSplayer.TileX;
int y = player.TSplayer.TileY;
int width = 20;
int height = 20;
int x1 = (int)(x / width) * width;
int y1 = (int)(y / height) * height;
for (int j = y1; j < y1 + height; j++)
{
for (int i = x1; i < x1 + width; i++)
{
if (i == x1 || i == x1 + width - 1 || j == y1 || j == y1 + height - 1)
{
byte tempType = Main.tile[i, j].type;
bool tempActive = Main.tile[i, j].active;
Main.tile[i, j].type = 70;
Main.tile[i, j].active = true;
player.TSplayer.SendTileSquare(i, j, 1);
Main.tile[i, j].type = tempType;
Main.tile[i, j].active = tempActive;
}
}
}
player.tempOutline = true;
}
public static void CommandMethod(CommandArgs args)
{
var player = PlayerList[args.Player.Index];
if (player == null)
return;
if (args.Parameters.Count > 0)
{
if (args.Parameters[0] == "invite")
{
if (player.Faction == null || !player.Faction.IsAdmin(player.UserID))
{
player.TSplayer.SendMessage("Only faction admins can invite players to the faction", Color.LightSalmon);
return;
}
if (args.Parameters.Count < 2)
{
player.TSplayer.SendMessage("Syntax: /faction invite PlayerName", Color.LightSalmon);
return;
}
var targetName = String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count-1));
var target = Player.GetPlayerByName(targetName);
if (target == null)
player.TSplayer.SendMessage(String.Format("Could not find player '{0}'", targetName), Color.LightSalmon);
else if (target.UserID == -1)
player.TSplayer.SendMessage(String.Format("Player '{0}' is not registered or logged in", targetName), Color.LightSalmon);
else if (player.Faction.Invites.Contains(target.UserID))
player.TSplayer.SendMessage(String.Format("Player '{0}' is already invited to join your faction", targetName), Color.LightSalmon);
else if (player.Faction.Members.Contains(target.UserID))
player.TSplayer.SendMessage(String.Format("Player '{0}' is already in your faction", targetName), Color.LightSalmon);
else if (player.Faction.InvitePlayer(target))
player.TSplayer.SendMessage(String.Format("Player '{0}' has been invited to join your faction", targetName), Color.DarkGreen);
else
player.TSplayer.SendMessage(String.Format("Could not invite '{0}' to join your faction", targetName), Color.LightSalmon);
}
}
else
{
List<MenuItem> menuData = new List<MenuItem>();
menuData.Add(new MenuItem("[ Plot information & management ]", 2, Color.LightGray));
menuData.Add(new MenuItem("[ Faction information & management ]", 3, Color.LightGray));
menuData.Add(new MenuItem("[ Player information & management ]", 4, Color.LightGray));
menuData.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
Menu.CreateMenu(player.ID, "Factions", menuData, new CAMain.MenuAction(OnMenu));
}
}
public static void OnMenu(Object menu, MenuEventArgs args)
{
var player = PlayerList[args.PlayerID];
if (player != null)
{
if (args.Status == MenuStatus.Exit)
{
player.ClearOutline();
player.Menu = null;
return;
}
else if (args.Status == MenuStatus.Select && args.Selected >= 0 && args.Selected < args.Data.Count)
{
player.Menu = (Menu)menu;
int value = args.Data[args.Selected].Value;
switch (value)
{
case -1:
{
player.CloseMenu();
break;
}
case 1: // Main Menu
{
player.Menu.title = "Factions";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("[ Plot information & management ]", 2, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Faction information & management ]", 3, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Player information & management ]", 4, Color.LightGray));
/* if (player.TSplayer.Group.Name == "superadmin" || player.TSplayer.Group.HasPermission("factions.admin"))
player.Menu.contents.Add(new MenuItem("Factions Administration", 9, Color.LightGray));*/
player.Menu.contents.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 2: // plot manager
{
DisplayRegionInfo(player);
args.Handled = true;
break;
}
case 20: // buying plot
{
int price = GetPlotPrice(player, new Point(player.TSplayer.TileX, player.TSplayer.TileY));
if (price < 0)
return;
player.Menu.contents.Clear();
OutlineRegion(player);
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem(String.Format("Buy this plot for {0} power?", price), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Yes ]", 21, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ No ]", 2, Color.DarkRed));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 21: // claim plot
{
int price = GetPlotPrice(player, new Point(player.TSplayer.TileX, player.TSplayer.TileY));
if (price < 0)
return;
if ((player.Faction == null && player.ChangePower(-1 * price)) || (player.Faction != null && player.Faction.IsAdmin(player.UserID) && player.Faction.RemovePower(price)))
{
AddRegion(player);
DisplayRegionInfo(player);
args.Handled = true;
}
break;
}
case 22: // unclaim plot
{
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("Are you sure you want to unclaim this land?", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Yes ]", 23, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ No ]", 2, Color.DarkRed));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 23: // unclaim plot confirmation
{
var region = Region.GetRegionFromLocation(player.TSplayer.TileX, player.TSplayer.TileY);
if (region.ID != -1)
{
if ((region.Owner != 0 && player.UserID == region.Owner) || (region.Faction != 0 && player.Faction != null && player.Faction.ID == region.Faction && player.Faction.IsAdmin(player.UserID)))
{
if (UnclaimRegion(region))
{
DisplayRegionInfo(player);
args.Handled = true;
}
else
player.TSplayer.SendMessage("Error: Could not unclaim region", Color.Red);
}
}
break;
}
case 3: // faction management
{
player.Menu.title = "Faction Manager";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("[ Faction List ]", 31, Color.LightGray));
if (player.Faction != null)
player.Menu.contents.Add(new MenuItem("[ My Faction ]", 33, Color.LightGray));
else
player.Menu.contents.Add(new MenuItem("[ New Faction ]", 32, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 1, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 4: //player management
{
player.changingMoney = false;
player.Menu.title = "Player information";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem(String.Format("Power: {0}", player.Power), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("[ Convert gold to Power ]", player.Power), 41, Color.LightGray));
if (player.Faction != null)
player.Menu.contents.Add(new MenuItem(String.Format("[ My faction: {0} ]", player.Faction.Name), 33, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 1, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 41: // convert money to power
{
player.changingMoney = true;
player.Menu.title = "Player information";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("Drop gold from your inventory", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("to exchange it for Power.", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("1 Gold = 2 Power", 0, false, Color.Goldenrod));
player.Menu.contents.Add(new MenuItem("Click [Back] when you're done", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 4, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
/*case 9: //admin
{
break;
*/
case 31: // List Factions
{
player.Menu.title = "Faction List";
var factname = player.Menu.GetItemByValue(3101);
player.Menu.contents.Clear();
player.Menu.index = 0;
foreach (Faction fact in Faction.GetTopFactionsByPower())
{
if (factname != null && factname.Text == fact.Name)
player.Menu.index = player.Menu.contents.Count;
var menuI = new MenuItem("- @0", 310, Color.Gray);
menuI.Input = fact.Name;
player.Menu.contents.Add(menuI);
player.Menu.contents.Add(new MenuItem(String.Format(" Power: {0}, Members: {1}", fact.Power, fact.Members.Count), 0, false, Color.LightGray));
}
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 3, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 310: // Faction Information
{
var faction = Faction.GetFactionByName(args.Data[args.Selected].Input);
if (faction != null)
{
player.Menu.title = "Faction information";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem(faction.Name, 3101, false, Color.YellowGreen));
player.Menu.contents.Add(new MenuItem(String.Format(" \"{0}\"", faction.Desc), 0, false, Color.Gray));
player.Menu.contents.Add(new MenuItem("", 0, false, Color.White));
player.Menu.contents.Add(new MenuItem(String.Format("Join status: {0}", faction.Private ? "Invite Only" : "Open to all"), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("PVP status: {0}", faction.Hostile ? "Hostile" : "Peaceful"), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Team color: {0}", faction.TeamColor), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Admins ({0}):", faction.Admins.Count), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Join(", ", faction.AdminNames()), 0, false, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem(String.Format("Members ({0}):", faction.Members.Except(faction.Admins).ToList().Count), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Join(", ", faction.MemberNames(true)), 0, false, Color.DarkBlue));
player.Menu.contents.Add(new MenuItem(String.Format("Power: {0}:", faction.Power), 0, false, Color.LightGray));
if (faction.Allies.Count > 0)
player.Menu.contents.Add(new MenuItem(String.Format("Allies: {0}:", faction.GetAlliesString()), 0, false, Color.LightGray));
if (faction.Enemies.Count > 0)
player.Menu.contents.Add(new MenuItem(String.Format("Enemies: {0}:", faction.GetEnemiesString()), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("", 0, false, Color.White));
if (player.Faction == null)
{
if (faction.Private)
{
if (faction.Invites.Contains(player.UserID))
player.Menu.contents.Add(new MenuItem("[ Join ] (You have been invited to join)", 3100, Color.DarkGreen));
else
player.Menu.contents.Add(new MenuItem("[ Join ] (To join request invitation from faction Admins)", 0, false, Color.Gray));
}
else
player.Menu.contents.Add(new MenuItem("[ Join ]", 3100, Color.DarkGreen));
}
else if (player.Faction.ID != faction.ID && player.Faction.IsAdmin(player.UserID))
{
if (player.Faction.Allies.Contains(faction.ID))
{
player.Menu.contents.Add(new MenuItem("[ Revoke Ally status ]", 3103, Color.DarkRed));
}
else if (faction.AllyInvites.Contains(player.Faction.ID))
{
player.Menu.contents.Add(new MenuItem("Avaiting Ally confirmation", 0, false, Color.LightGray));
}
else if (player.Faction.AllyInvites.Contains(faction.ID))
{
player.Menu.contents.Add(new MenuItem("[ Confirm Alliance ]", 3102, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ Decline Alliance ]", 3103, Color.DarkRed));
}
else if (player.Faction.Enemies.Contains(faction.ID))
{
player.Menu.contents.Add(new MenuItem("[ Revoke Enemy status ]", 3105, Color.DarkRed));
}
else
{
player.Menu.contents.Add(new MenuItem("[ Request Alliance ]", 3102, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ Declare War ]", 3104, Color.DarkRed));
}
}
if (player.Menu.GetItemByValue(1) != null)
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 1, Color.BurlyWood));
else
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 31, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
}
break;
}
case 3100: // Join Faction
{
var faction = Faction.GetFactionByName(player.Menu.GetItemByValue(3101).Text);
if (faction != null)
{
if ((faction.Private && faction.Invites.Contains(player.UserID)) || !faction.Private)
{
player.Menu.title = "Faction information";
player.Menu.contents.Clear();
player.Menu.index = 0;
if (faction.AddMember(player))
{
player.Menu.contents.Add(new MenuItem(String.Format("Success, you have joined the {0}", faction.Name), 0, false, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ My faction ]", 33, Color.BurlyWood));
}
else
player.Menu.contents.Add(new MenuItem(String.Format("Error, could not join faction: {0}", faction.Name), 0, false, Color.DarkRed));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 3, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
}
}
break;
}
case 3102: // request alliance
{
try
{
var faction = Faction.GetFactionByName(player.Menu.GetItemByValue(3101).Text);
if (faction != null)
{
player.Menu.contents.Clear();
player.Menu.index = 0;
if (player.Faction.AllyInvites.Contains(faction.ID))
{
Faction.AllyFactions(faction, player.Faction);
string allyString = String.Format("{0} and {1} are now Allies", player.Faction.Name, faction.Name);
player.Menu.contents.Add(new MenuItem(allyString, 0, false, Color.DarkGreen));
TShock.Utils.Broadcast(allyString, Color.DarkGreen);
}
else
{
faction.AllyInvites.Add(player.Faction.ID);
db.Query("UPDATE factions_Factions SET AllyInvites = @0 WHERE ID = @1", String.Join(",", faction.AllyInvites), faction.ID);
player.Menu.contents.Add(new MenuItem("Ally request sent", 0, false, Color.DarkGreen));
faction.MessageMembers(String.Format("Faction '{0}' requests an Alliance with you", player.Faction.Name));
}
var backbutton = new MenuItem("[ <- Back to @0 overview ]", 310, Color.BurlyWood);
backbutton.Input = faction.Name;
player.Menu.contents.Add(backbutton);
player.Menu.contents.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
args.Handled = true;
player.Menu.DisplayMenu();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
break;
}
case 3103: // revoke alliance
{
try
{
var faction = Faction.GetFactionByName(player.Menu.GetItemByValue(3101).Text);
if (faction != null)
{
player.Menu.contents.Clear();
player.Menu.index = 0;
Faction.BreakAlly(faction, player.Faction);
TShock.Utils.Broadcast(String.Format("Factions {0} and {1} have broken off the Alliance", faction.Name, player.Faction.Name), Color.LightSalmon);
player.Menu.contents.Add(new MenuItem(String.Format("You've broken off the alliance with {0}", faction.Name), 0, false, Color.LightGray));
var backbutton = new MenuItem("[ <- Back to @0 overview ]", 310, Color.BurlyWood);
backbutton.Input = faction.Name;
player.Menu.contents.Add(backbutton);
player.Menu.contents.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
args.Handled = true;
player.Menu.DisplayMenu();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
break;
}
case 3104: // declare war
{
try
{
var faction = Faction.GetFactionByName(player.Menu.GetItemByValue(3101).Text);
if (faction != null)
{
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Faction.Enemies.Add(faction.ID);
db.Query("UPDATE factions_Factions SET Enemies = @0 WHERE ID = @1", String.Join(",", player.Faction.Enemies), player.Faction.ID);
TShock.Utils.Broadcast(String.Format("Faction {0} has declared war on {1}", player.Faction.Name, faction.Name), Color.LightSalmon);
player.Menu.contents.Add(new MenuItem(String.Format("You are now at war with {0}", faction.Name), 0, false, Color.LightGray));
var backbutton = new MenuItem("[ <- Back to @0 overview ]", 310, Color.BurlyWood);
backbutton.Input = faction.Name;
player.Menu.contents.Add(backbutton);
player.Menu.contents.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
args.Handled = true;
player.Menu.DisplayMenu();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
break;
}
case 3105: // revoke enemy
{
try
{
var faction = Faction.GetFactionByName(player.Menu.GetItemByValue(3101).Text);
if (faction != null)
{
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Faction.Enemies.Remove(faction.ID);
db.Query("UPDATE factions_Factions SET Enemies = @0 WHERE ID = @1", String.Join(",", player.Faction.Enemies), player.Faction.ID);
TShock.Utils.Broadcast(String.Format("Faction {0} has declared neutral towards {1}", player.Faction.Name, faction.Name), Color.LightSalmon);
player.Menu.contents.Add(new MenuItem(String.Format("You are now at neutral towards {0}", faction.Name), 0, false, Color.LightGray));
var backbutton = new MenuItem("[ <- Back to @0 overview ]", 310, Color.BurlyWood);
backbutton.Input = faction.Name;
player.Menu.contents.Add(backbutton);
player.Menu.contents.Add(new MenuItem("[ Exit ]", -1, Color.Gray));
args.Handled = true;
player.Menu.DisplayMenu();
}
}
catch (Exception ex) { Log.ConsoleError(ex.ToString()); }
break;
}
case 32: // New Faction
{
player.Menu.title = "Faction Creator";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("Name: ", 321, false, true, Color.LightGray));
player.Menu.contents.Add(new MenuItem("Description: ", 322, false, true, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Create ]", 320, false, Color.BurlyWood));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 3, true, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 320: // Create faction
{
var nameitem = player.Menu.GetItemByValue(321);
var descitem = player.Menu.GetItemByValue(322);
if (descitem == null || nameitem == null)
break;
MenuItem newNameItem = new MenuItem(nameitem);
MenuItem newDescItem = new MenuItem(descitem);
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("Notice:", 0, false, Color.Red));
player.Menu.contents.Add(new MenuItem("+------------------------------------------------+", 0, false, Color.LightSalmon));
player.Menu.contents.Add(new MenuItem("| If you already own any regions, they will get deleted |", 0, false, Color.LightSalmon));
player.Menu.contents.Add(new MenuItem("| and be made available for anyone to aquire. |", 0, false, Color.LightSalmon));
player.Menu.contents.Add(new MenuItem("| Number of allowed regions for Factions = number of faction members |", 0, false, Color.LightSalmon));
player.Menu.contents.Add(new MenuItem("+------------------------------------------------+", 0, false, Color.LightSalmon));
player.Menu.contents.Add(new MenuItem("", 0, false, Color.LightGray));
newNameItem.Text = " Name: <@0> ";
newNameItem.Color = Color.LightGray;
newNameItem.Writable = false;
player.Menu.contents.Add(newNameItem);
newDescItem.Text = " Description: <@0> ";
newDescItem.Color = Color.LightGray;
newDescItem.Writable = false;
player.Menu.contents.Add(newDescItem);
player.Menu.contents.Add(new MenuItem("", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("Accept & Create a faction:", 0, false, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("Yes", 325, Color.BurlyWood));
player.Menu.contents.Add(new MenuItem("No", -1, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 325: // faction create confirmation
{
String newFactName = player.Menu.GetItemByValue(321).Input;
String newFactDesc = player.Menu.GetItemByValue(322).Input;
if (Faction.GetFactionByName(newFactName) == null)
{
db.Query("INSERT INTO factions_Factions (Name, Description, Members, Admins, Power, Flags, WorldID) VALUES (@0, @1, @2, @3, @4, @5, @6)", newFactName, newFactDesc, player.TSplayer.UserID, player.TSplayer.UserID, player.Power, 0, Main.worldID);
int newFactID = -1;
using (QueryResult reader = db.QueryReader("SELECT ID FROM factions_Factions WHERE Name = @0", newFactName))
{
if (reader.Read())
{
newFactID = reader.Get<int>("ID");
}
}
if (newFactID != -1)
{
Faction newFaction = new Faction(newFactID, newFactName, newFactDesc, player);
FactionList.Add(newFaction);
player.Faction = newFaction;
player.TSplayer.SendMessage("Faction created");
var playerregions = Region.GetRegionsByUserID(player.UserID);
foreach (Region region in playerregions)
{
UnclaimRegion(region);
}
newFaction.RefreshPVPStatus();
newFaction.RefreshTeamStatus();
}
}
break;
}
case 33: // Your Faction
{
if (player.Faction != null)
{
player.Menu.title = "Your Faction";
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem(player.Faction.Name, 0, false, false, Color.GreenYellow));
player.Menu.contents.Add(new MenuItem(String.Format(" \"{0}\"", player.Faction.Desc), 0, false, false, Color.Gray));
player.Menu.contents.Add(new MenuItem("", 0, false, false));
player.Menu.contents.Add(new MenuItem(String.Format("Join status: {0}", player.Faction.Private ? "Invite Only" : "Open to all"), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("PVP status: {0}", player.Faction.Hostile ? "Hostile" : "Peaceful"), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Team color: {0}", player.Faction.TeamColor), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Admins ({0}):", player.Faction.Admins.Count), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Join(", ", player.Faction.AdminNames()), 0, false, false, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem(String.Format("Members ({0}):", player.Faction.Members.Except(player.Faction.Admins).ToList().Count), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Join(", ", player.Faction.MemberNames(true)), 0, false, Color.DarkBlue));
player.Menu.contents.Add(new MenuItem(String.Format("Power: {0}", player.Faction.Power), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Territory: {0}/{1}", player.Faction.Regions.Count, player.Faction.Members.Count), 0, false, Color.LightGray));
if (player.Faction.Allies.Count > 0)
player.Menu.contents.Add(new MenuItem(String.Format("Allies: {0}:", player.Faction.GetAlliesString()), 0, false, Color.LightGray));
if (player.Faction.Enemies.Count > 0)
player.Menu.contents.Add(new MenuItem(String.Format("Enemies: {0}:", player.Faction.GetEnemiesString()), 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("", 0, false, Color.White));
if (player.Faction.IsAdmin(player.TSplayer.UserID))
player.Menu.contents.Add(new MenuItem("[ Faction Settings ]", 30, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Leave Faction ]", 35, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 3, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
}
break;
}
case 35: // leave faction
{
player.Menu.contents.Clear();
player.Menu.index = 0;
player.Menu.contents.Add(new MenuItem("Are you sure you want to leave the faction?", 0, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Yes ]", 36, Color.DarkGreen));
player.Menu.contents.Add(new MenuItem("[ No ]", 33, Color.DarkRed));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 36: // leave faction confirmation
{
if (player.Faction != null)
player.Faction.RemoveMember(player.UserID);
break;
}
case 30: // Faction Settings (Admins only)
{
if (!player.Faction.IsAdmin(player.TSplayer.UserID))
break;
player.Menu.title = "Faction Settings";
player.Menu.contents.Clear();
player.Menu.index = 0;
var nameitem = new MenuItem("Name: @0", 301, false, true, Color.LightGray);
nameitem.Input = player.Faction.Name;
var descitem = new MenuItem("Desctiption: @0", 302, false, true, Color.LightGray);
descitem.Input = player.Faction.Desc;
player.Menu.contents.Add(nameitem);
player.Menu.contents.Add(descitem);
player.Menu.contents.Add(new MenuItem(String.Format("Join status: [{0}]", player.Faction.Private ? "Invite Only" : "Open to all"), 306, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("PvP status: [{0}]", player.Faction.Hostile ? "Hostile" : "Peaceful"), 304, Color.LightGray));
player.Menu.contents.Add(new MenuItem(String.Format("Team color: [{0}]", player.Faction.TeamColor), 305, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ Manage members ]", 303, true, false, Color.LightGray));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 3, true, false, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 303: // Faction Member management
{
if (!player.Faction.IsAdmin(player.TSplayer.UserID))
break;
player.Menu.title = String.Format("{0} Members", player.Faction.Name);
player.Menu.contents.Clear();
player.Menu.index = 0;
foreach (int userid in player.Faction.Members)
{
using (QueryResult reader = db.QueryReader("SELECT Name, Power FROM factions_Players WHERE UserID = @0 ORDER BY Name ASC", userid))
{
if (reader.Read())
{
player.Menu.contents.Add(new MenuItem(String.Format("- (@0) {0} [Power: {1}] {2}", reader.Get<string>("Name"), reader.Get<int>("Power"), (player.Faction.IsAdmin(userid)) ? "<Admin>" : ""), 3030, (player.UserID == userid) ? false : true, Color.BurlyWood));
player.Menu.contents.Last().Input = userid.ToString();
}
}
}
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 30, true, false, Color.BurlyWood));
args.Handled = true;
player.Menu.DisplayMenu();
break;
}
case 3030:
{
if (!player.Faction.IsAdmin(player.TSplayer.UserID))
break;
player.Menu.title = String.Format("{0} Members", player.Faction.Name);
int userid = 0;
if (int.TryParse(args.Data[args.Selected].Input, out userid))
{
player.Menu.contents.Clear();
player.Menu.index = 0;
using (QueryResult reader = db.QueryReader("SELECT Name, Power FROM factions_Players WHERE UserID = @0", userid))
{
if (reader.Read())
{
player.Menu.contents.Add(new MenuItem(String.Format("Name: {0} (userID: @0)", reader.Get<string>("Name")), 3035, false, Color.GreenYellow));
player.Menu.contents.Last().Input = userid.ToString();
player.Menu.contents.Add(new MenuItem(String.Format("Power: {0}", reader.Get<int>("Power")), 0, false, Color.LightGray));
if (player.Faction.Admins.Contains(userid))
{
player.Menu.contents.Add(new MenuItem("Status: Admin", 0, false, Color.DarkOrange));
player.Menu.contents.Add(new MenuItem("[ Demote to default ]", 3032, Color.BurlyWood));
}
else
{
player.Menu.contents.Add(new MenuItem("Status: Default", 0, false, Color.Gray));
player.Menu.contents.Add(new MenuItem("[ Promote to admin ]", 3032, Color.BurlyWood));
}
player.Menu.contents.Add(new MenuItem("[ Kick from Faction ]", 3031, Color.BurlyWood));
player.Menu.contents.Add(new MenuItem("[ <- Back ]", 303, Color.BurlyWood));
}
}
args.Handled = true;
player.Menu.DisplayMenu();
}
break;
}
case 3031: //Kick from Faction
{
int userid = -1;
var useridItem = player.Menu.GetItemByValue(3035);
if (useridItem != null && player.Faction != null && player.Faction.IsAdmin(player.UserID) && int.TryParse(useridItem.Input, out userid) && player.UserID != userid && player.Faction.Members.Contains(userid))
{
player.Faction.RemoveMember(userid);
player.Menu.contents.Clear();
player.Menu.index = 0;