-
Notifications
You must be signed in to change notification settings - Fork 0
/
KitchenPlugin.cs
477 lines (422 loc) · 18.6 KB
/
KitchenPlugin.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
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
using TShockAPI;
using Terraria;
using TerrariaApi.Server;
using Terraria.ID;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using System.Text;
using Terraria.Localization;
using System;
using Terraria.DataStructures;
using TShockAPI.DB;
namespace TerrariaKitchen
{
[ApiVersion(2, 1)]
public class KitchenPlugin : TerrariaPlugin
{
public override string Author => "bountygiver";
public override string Description => "Connect to some sort of chat so chatter can buy mob spawns!";
public override string Name => "Terraria Kitchen";
public override Version Version => new Version(1, 0, 0, 0);
private TwitchConnection _connection;
private string? kitchenTargetID;
private KitchenCreditsStore Store;
private KitchenOverlay Overlay;
private static readonly string KitchenConfigPath = Path.Combine(TShock.SavePath, "kitchen.json");
public KitchenConfig Config { get; private set; }
private static readonly string ApiPath = Path.Combine(TShock.SavePath, "kitchenapi.txt");
public static Random randomSeed = new Random();
public KitchenPlugin(Main game) : base(game)
{
if (File.Exists(KitchenConfigPath))
{
try
{
Config = JsonConvert.DeserializeObject<KitchenConfig>(File.ReadAllText(KitchenConfigPath));
Console.WriteLine($"(Terraria Kitchen) Config found! Loaded menu with {Config.Entries?.Count ?? 0} items!");
}
catch (Exception ex)
{
// No config, use default.
Config = new KitchenConfig();
Console.WriteLine($"(Terraria Kitchen) Error: Unable to load config properly. Default values used.");
if (ex is JsonReaderException je)
{
Console.WriteLine($"(Terraria Kitchen) Json Error: {je.Message}");
}
}
}
else
{
Config = new KitchenConfig();
}
Store = new KitchenCreditsStore(Config);
Overlay = new KitchenOverlay(Config);
_connection = new TwitchConnection(Store, Config, Overlay);
_connection.SpawnUnit = SpawnFunc;
if (File.Exists(ApiPath))
{
var apiText = File.ReadAllText(ApiPath);
if (apiText.Length > 0)
{
_connection.SetAPIKey(apiText);
}
}
if (!_connection.HasAPIKey())
{
Console.WriteLine("(Terraria Kitchen) Twitch API key not set. You will need to run /setkitchenkey <API key> before you can start kitchen. You can place the apikey in a tshock\\kitchenapi.txt to have it set automatically.");
Console.WriteLine("(Terraria Kitchen) Visit https://dev.twitch.tv/docs/irc/authenticate-bot/ for more information about getting an API key.");
}
}
public override void Initialize()
{
Commands.ChatCommands.Add(new Command(Permissions.spawn, StartKitchen, "startkitchen") { HelpText = "Start up the kitchen server." });
Commands.ChatCommands.Add(new Command(Permissions.spawn, SetupKitchen, "setkitchenkey") { HelpText = "Set Twitch API key for kitchen." });
Commands.ChatCommands.Add(new Command(Permissions.spawn, (args) =>
{
Store.ResetChatterBalance();
Overlay.SendPacket(new { @event = "reset" });
}, "resetkitchen") { HelpText = "Reset all chatter balance in the current world." });
Commands.ChatCommands.Add(new Command(Permissions.spawn, GiveMoney, "kitchengive") { HelpText = "Give a chatter an amount of credits." });
Commands.ChatCommands.Add(new Command(Permissions.spawn, (args) =>
{
try
{
var newConfig = JsonConvert.DeserializeObject<KitchenConfig>(File.ReadAllText(KitchenConfigPath));
Console.WriteLine($"(Terraria Kitchen) Config found! Loaded menu with {newConfig.Entries?.Count ?? 0} items!");
if (newConfig.Entries?.Count >= 0)
{
Config.Entries = newConfig.Entries;
}
if (newConfig.Events?.Count >= 0)
{
Config.Events = newConfig.Events;
}
Config.Income = newConfig.Income;
Config.MaxBalance = newConfig.MaxBalance;
Config.StartingMoney = newConfig.StartingMoney;
Config.SubscriberPriceMultiplier = newConfig.SubscriberPriceMultiplier;
Config.ModAbuse = newConfig.ModAbuse;
Config.XRange = newConfig.XRange;
Config.YRange = newConfig.YRange;
if (Config.IncomeInterval != newConfig.IncomeInterval)
{
Config.IncomeInterval = newConfig.IncomeInterval;
_connection?.UpdateTime();
}
}
catch (Exception ex)
{
Console.WriteLine($"(Terraria Kitchen) Error reading config.");
if (ex is JsonReaderException je)
{
Console.WriteLine($"(Terraria Kitchen) Json Error: {je.Message}");
}
}
}, "kitchenmenuupdate") { HelpText = "Re-read kitchen.json and update settings." });
ServerApi.Hooks.WorldSave.Register(this, (e) =>
{
Store.UpdatePlayersToDb();
});
ServerApi.Hooks.NpcSpawn.Register(this, RaffleTownNPC);
ServerApi.Hooks.NetGetData.Register(this, OnGetData);
Overlay.StartServer(Config.OverlayPort, Config.OverlayWsPort);
}
private void OnGetData(GetDataEventArgs args)
{
if (!args.Handled && args.MsgID == PacketTypes.PlayerDeathV2)
{
using (BinaryReader reader = new BinaryReader(new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)))
{
int? mobType = null;
byte playerid = reader.ReadByte();
byte deathReason = reader.ReadByte();
short killerId;
short killerNPCIdx;
short projectileIdx;
byte deathType;
short projectileType;
short itemType;
byte itemPrefix;
string deathMsg;
if ((deathReason & 0b1) != 0)
{
killerId = reader.ReadByte();
}
if ((deathReason & 0b10) != 0)
{
killerNPCIdx = reader.ReadInt16();
if (Main.npc.Length > killerNPCIdx)
{
var npc = Main.npc[killerNPCIdx];
if (npc != null)
{
mobType = npc.type;
}
}
}
if ((deathReason & 0b100) != 0)
{
projectileIdx = reader.ReadInt16();
var proj = Main.projectile[projectileIdx];
if (proj.owner >= 0)
{
if (Main.npc.Length > proj.owner)
{
var npc = Main.npc[proj.owner];
if (npc != null)
{
mobType = npc.type;
}
}
}
}
if ((deathReason & 0b1000) != 0)
{
deathType = reader.ReadByte();
}
if ((deathReason & 0b10000) != 0)
{
projectileType = reader.ReadInt16();
}
if ((deathReason & 0b100000) != 0)
{
itemType = reader.ReadInt16();
}
if ((deathReason & 0b1000000) != 0)
{
itemPrefix = reader.ReadByte();
}
if ((deathReason & 0b10000000) != 0)
{
deathMsg = reader.ReadString();
}
short damage = reader.ReadInt16();
byte hitDirection = reader.ReadByte();
byte flags = reader.ReadByte();
var player = Main.player[playerid];
var distributeAmt = Config.PlayerDeathIncome;
if (mobType != null && Config.Entries.FirstOrDefault(e => e.InternalName == mobType) is KitchenConfig.KitchenEntry entry)
{
distributeAmt += (int) (entry.Price * Config.PlayerDeathCreditRefund);
}
if (distributeAmt > 0)
{
Store.DistributeIncome(distributeAmt);
_connection?.Announce($"{player.name} died. Distributed {distributeAmt} credits to all chatters.");
}
}
}
}
private void RaffleTownNPC(NpcSpawnEventArgs args)
{
var npc = Main.npc[args.NpcId];
if (npc != null && npc.townNPC)
{
var existingNames = new HashSet<string>();
for (int i = 0; i < Main.npc.Length; i++)
{
if (Main.npc[i].active && Main.npc[i].netID != npc.netID && Main.npc[i].townNPC)
{
existingNames.Add(Main.npc[i].GivenName.ToLower());
}
}
var raffleNames = Store.CurrentChatters.Select(c => c.ToLower()).Except(existingNames);
if (raffleNames.Any())
{
var raffled = raffleNames.ElementAt(randomSeed.Next(raffleNames.Count()));
TSPlayer.All.SendSuccessMessage($"{raffled} has been raffled as {npc.TypeName}!");
npc.GivenName = raffled;
NetMessage.SendData(56, -1, -1, NetworkText.FromLiteral(npc.GivenName), args.NpcId);
}
}
}
private void GiveMoney(CommandArgs args)
{
if (args.Parameters.Count < 1 || args.Parameters.Count > 2)
{
args.Player.SendErrorMessage("Invalid syntax, use /kitchengive <playername> <amount>");
return;
}
if (int.TryParse(args.Parameters[1], out var amount))
{
Store.ModBalance(args.Parameters[0], amount);
args.Player.SendSuccessMessage($"Given {args.Parameters[0]} {amount} credits!");
return;
}
args.Player.SendErrorMessage("Invalid amount, please use only number.");
}
private void SetupKitchen(CommandArgs args)
{
if (args.Parameters.Count != 1)
{
args.Player.SendErrorMessage("Invalid syntax, use /setkitchenkey <twitch oauth key>");
return;
}
_connection.SetAPIKey(args.Parameters[0]);
}
private void StartKitchen(CommandArgs args)
{
if (args.Parameters.Count < 1 || args.Parameters.Count > 2)
{
args.Player.SendErrorMessage("Invalid syntax, use /startkitchen <channelname> <optional playername>");
return;
}
if (!_connection.HasAPIKey())
{
args.Player.SendErrorMessage("No Twitch API key set, use /setkitchenkey <twitch oauth key>");
}
var targetPlayer = args.Player;
if (args.Parameters.Count == 2)
{
List<TSPlayer> list = TSPlayer.FindByNameOrID(args.Parameters[1]);
targetPlayer = list.FirstOrDefault(p => p.RealPlayer);
if (targetPlayer == null)
{
args.Player.SendErrorMessage($"Can't find player {args.Parameters[1]}.");
}
}
if (kitchenTargetID != null && kitchenTargetID != targetPlayer?.UUID)
{
args.Player.SendInfoMessage("A previous player was already being served by the kitchen, changing customer...");
}
kitchenTargetID = targetPlayer?.UUID;
if (kitchenTargetID == null)
{
args.Player.SendInfoMessage("No players specified, all players are now eligible customers...");
}
_connection.Initialize(args.Parameters[0], () =>
{
args.Player.SendInfoMessage("(Terraria Kitchen) Connected to twitch chat.");
}, () =>
{
args.Player.SendInfoMessage("(Terraria Kitchen) Disconnected from twitch chat.");
});
Overlay?.SendPacket(new { @event = "initialize" });
}
private void SpawnNPC(int type, string name, int amount, int startTileX, int startTileY, int tileXRange, int tileYRange)
{
Region? zone = null;
if (Config.SafeSpawnZoneName != null)
{
zone = TShock.Regions.GetRegionByName(Config.SafeSpawnZoneName);
}
var inSafeZone = zone?.InArea(startTileX, startTileY) == true;
for (int i = 0; i < amount; i++)
{
int tileX, tileY;
if (inSafeZone)
{
if (randomSeed.Next(2) == 0)
{
TShock.Utils.GetRandomClearTileWithInRange(zone.Area.Left - tileXRange, startTileY, tileXRange, tileYRange, out tileX, out tileY);
}
else
{
TShock.Utils.GetRandomClearTileWithInRange(zone.Area.Right + tileXRange, startTileY, tileXRange, tileYRange, out tileX, out tileY);
}
}
else
{
TShock.Utils.GetRandomClearTileWithInRange(startTileX, startTileY, tileXRange, tileYRange, out tileX, out tileY);
int tries = 10;
while (zone?.InArea(tileX, tileY) == true && tries > 0)
{
if (startTileX < tileX)
{
startTileX -= tileXRange;
}
else
{
startTileX += tileXRange;
}
TShock.Utils.GetRandomClearTileWithInRange(startTileX, startTileY, tileXRange, tileYRange, out tileX, out tileY);
tries--;
}
}
NPC.NewNPC(new EntitySource_DebugCommand(), tileX * 16, tileY * 16, type);
}
}
private bool SpawnFunc(int mobId, int count, string? sender = null, string? targetPlayer = null, bool silent = false)
{
var result = Math.Min(count, 200);
if (result <= 0)
{
return false;
}
var players = TShock.Players.Where(p => p?.RealPlayer == true);
if (!players.Any())
{
return false;
}
if (!silent && Config.Entries.FirstOrDefault(e => e.InternalName == mobId) is KitchenConfig.KitchenEntry entry)
{
if (entry.NoDay && Main.dayTime)
{
return false;
}
if (entry.NoNight && !Main.dayTime)
{
return false;
}
}
if (mobId == 113)
{
// WOF Do not use target player, and will always attempt to spawn on whoever that is eligible.
if (Main.wofNPCIndex != -1)
{
return false;
}
float wofTargetY = Main.maxTilesY - 205;
var wofTarget = players.FirstOrDefault(p => p.Y / 16f >= wofTargetY);
if (wofTarget != null)
{
NPC.SpawnWOF(new Vector2(wofTarget.X, wofTarget.Y));
TSPlayer.All.SendSuccessMessage("ONE WALL OF FLESH, SERVING RIGHT UP");
return true;
}
return false;
}
var targetedPlayer = false;
TSPlayer? spawnPlayer = players.FirstOrDefault(p => p.UUID == kitchenTargetID);
if (spawnPlayer == null)
{
// Use a random active player if kitchen has no target.
spawnPlayer = players.ElementAt(randomSeed.Next(players.Count()));
}
if (targetPlayer != null && players.FirstOrDefault(p => p.Name.ToLower() == targetPlayer.ToLower()) is TSPlayer target)
{
// If target is specified, override
targetedPlayer = true;
spawnPlayer = target;
}
NPC nPC = TShock.Utils.GetNPCById(mobId);
if (nPC.type >= -65 && nPC.type < NPCID.Count && nPC.type != 113)
{
var successMsg = new StringBuilder($"{sender ?? "Someone"} ordered {result} {nPC.FullName}{(result > 1 ? "s" : "")}");
if (targetedPlayer)
{
successMsg.Append($" for {targetPlayer}");
}
successMsg.Append("!");
SpawnNPC(nPC.netID, nPC.FullName, result, spawnPlayer.TileX, spawnPlayer.TileY, Config.XRange ?? 50, Config.YRange ?? 20);
if (!silent)
{
TSPlayer.All.SendSuccessMessage(successMsg.ToString());
}
return true;
}
return false;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Deregister hooks here
_connection.Dispose();
Overlay?.Dispose();
}
base.Dispose(disposing);
}
}
}