|
6 | 6 |
|
7 | 7 | namespace DraftTwitchViewers |
8 | 8 | { |
9 | | - struct DraftInfo |
10 | | - { |
11 | | - public string name; |
12 | | - public string job; |
13 | | - |
14 | | - public DraftInfo(string name, string job = "Any") |
15 | | - { |
16 | | - this.name = name; |
17 | | - this.job = job; |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - [KSPScenario(ScenarioCreationOptions.AddToAllGames, new GameScenes[] { GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT })] |
| 9 | + /// <summary> |
| 10 | + /// A KSP Scenario module which handles all draft functions, both internal and external. |
| 11 | + /// </summary> |
| 12 | + [KSPScenario(ScenarioCreationOptions.AddToAllGames, new GameScenes[] { GameScenes.SPACECENTER })] |
22 | 13 | class ScenarioDraftManager : ScenarioModule |
23 | 14 | { |
24 | 15 | #region Instance |
@@ -254,6 +245,9 @@ public override void OnAwake() |
254 | 245 |
|
255 | 246 | #endregion |
256 | 247 |
|
| 248 | + // Destroy this instance if the Main Menu is loaded. |
| 249 | + GameEvents.onGameSceneLoadRequested.Add(SceneRequested); |
| 250 | + |
257 | 251 | // Initialize the regex array. |
258 | 252 | InitRegexes(); |
259 | 253 | } |
@@ -281,10 +275,19 @@ void Update() |
281 | 275 | } |
282 | 276 | } |
283 | 277 |
|
| 278 | + void OnDestroy() |
| 279 | + { |
| 280 | + Instance = null; |
| 281 | + } |
| 282 | + |
284 | 283 | #endregion |
285 | 284 |
|
286 | 285 | #region KSP Functions |
287 | 286 |
|
| 287 | + /// <summary> |
| 288 | + /// Loads the local game settings for this scenario. |
| 289 | + /// </summary> |
| 290 | + /// <param name="node">The config node for this scenario.</param> |
288 | 291 | public override void OnLoad(ConfigNode node) |
289 | 292 | { |
290 | 293 | #region Local |
@@ -323,6 +326,10 @@ public override void OnLoad(ConfigNode node) |
323 | 326 | #endregion |
324 | 327 | } |
325 | 328 |
|
| 329 | + /// <summary> |
| 330 | + /// Saves the local game settings for this scenario. |
| 331 | + /// </summary> |
| 332 | + /// <param name="node">The config node for this scenario.</param> |
326 | 333 | public override void OnSave(ConfigNode node) |
327 | 334 | { |
328 | 335 | SaveGlobalSettings(); |
@@ -410,19 +417,32 @@ public void LegacyLoadLocalSettings() |
410 | 417 | } |
411 | 418 | } |
412 | 419 |
|
| 420 | + /// <summary> |
| 421 | + /// Destroys this instance if the requested scene is the Main Menu. |
| 422 | + /// </summary> |
| 423 | + /// <param name="requestedScene">The requested scene.</param> |
| 424 | + void SceneRequested(GameScenes requestedScene) |
| 425 | + { |
| 426 | + if (requestedScene == GameScenes.MAINMENU) |
| 427 | + { |
| 428 | + Destroy(gameObject); |
| 429 | + } |
| 430 | + } |
| 431 | + |
413 | 432 | #endregion |
414 | 433 |
|
415 | 434 | #region Draft function |
416 | 435 |
|
417 | 436 | /// <summary> |
418 | 437 | /// Drafts a Kerbal, invoking the suplied success Action if the draft succeeds, or the failure Action if the draft fails. |
419 | 438 | /// </summary> |
420 | | - /// <param name="success">The Action to invoke on draft success.</param> |
421 | | - /// <param name="failure">The Action to invoke on draft failure.</param> |
| 439 | + /// <param name="success">The Action to invoke on draft success. Provides a dictionary which will contain a "winner" entry for drawings, or a "name" and "job" entry for drafts.</param> |
| 440 | + /// <param name="failure">The Action to invoke on draft failure. Provides a string reason the draft failed.</param> |
422 | 441 | /// <param name="forDrawing">Whether the draft is for a drawing, or for an actual draft.</param> |
| 442 | + /// <param name="suppressSave">If true, the drafted user will not be saved.</param> |
423 | 443 | /// <param name="job">The job for the Kerbal. Optional and defaults to "Any" and is not needed if forDrawing is true.</param> |
424 | 444 | /// <returns>The IEnumerator (used for making the draft asynchronously).</returns> |
425 | | - public static IEnumerator DraftKerbal(Action<DraftInfo> success, Action<string> failure, bool forDrawing, bool suppressSave, string job = "Any") |
| 445 | + public static IEnumerator DraftKerbal(Action<Dictionary<string, string>> success, Action<string> failure, bool forDrawing, bool suppressSave, string job = "Any") |
426 | 446 | { |
427 | 447 | // If a channel hasn't been input yet, |
428 | 448 | if (string.IsNullOrEmpty(Instance.channel)) |
@@ -534,8 +554,11 @@ public static IEnumerator DraftKerbal(Action<DraftInfo> success, Action<string> |
534 | 554 | Instance.SaveDrawn(); |
535 | 555 | } |
536 | 556 |
|
| 557 | + Dictionary<string, string> winner = new Dictionary<string, string>(); |
| 558 | + winner.Add("winner", realUsername); |
| 559 | + |
537 | 560 | // Invoke the success Action, allowing the caller to continue. |
538 | | - success.Invoke(new DraftInfo(realUsername)); |
| 561 | + success.Invoke(winner); |
539 | 562 | } |
540 | 563 | // If there is an error, |
541 | 564 | else |
@@ -620,8 +643,13 @@ public static IEnumerator DraftKerbal(Action<DraftInfo> success, Action<string> |
620 | 643 | Instance.AlreadyDrafted.Add(oddUsername); |
621 | 644 | } |
622 | 645 |
|
| 646 | + Dictionary<string, string> drafted = new Dictionary<string, string>(); |
| 647 | + |
| 648 | + drafted.Add("name", realUsername + (Instance.addKerman ? " Kerman" : "")); |
| 649 | + drafted.Add("job", job); |
| 650 | + |
623 | 651 | // Invoke the success Action, allowing the caller to continue. |
624 | | - success.Invoke(new DraftInfo(realUsername + (Instance.addKerman ? " Kerman" : ""), job)); |
| 652 | + success.Invoke(drafted); |
625 | 653 | } |
626 | 654 | // Else, if we failed to find one, |
627 | 655 | else if (failedToFindOne) |
|
0 commit comments