Skip to content

Commit ff33f21

Browse files
author
Nifty255
committed
v2.4.1:
- Modified DraftKerbal method to invoke success using a generic Dictionary instead of a specialized DraftInfo object. - ScenarioDraftManager now destroys itself when returning to the main menu (and reloads when entering another save) to prevent potential load/save issues.
1 parent f4399f3 commit ff33f21

File tree

9 files changed

+76
-36
lines changed

9 files changed

+76
-36
lines changed
0 Bytes
Binary file not shown.

DraftTwitchViewers/README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Draft Twitch Viewers (DTV) uses web requests to connect to Twitch, and can pick
3030

3131
CHANGELOG:
3232

33+
v2.4.1:
34+
- Modified DraftKerbal method to invoke success using a generic Dictionary instead of a specialized DraftInfo object.
35+
- ScenarioDraftManager now destroys itself when returning to the main menu (and reloads when entering another save) to prevent potential load/save issues.
36+
3337
v2.4:
3438
- Drafts are now saved directly to each save's persistent file, allowing reverts to free drafted names.
3539
- An upgrade system was left in place which will migrate pre-existing drafts over to the new system.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Draft Twitch Viewers (DTV) uses web requests to connect to Twitch, and can pick
3030

3131
CHANGELOG:
3232

33+
v2.4.1:
34+
- Modified DraftKerbal method to invoke success using a generic Dictionary instead of a specialized DraftInfo object.
35+
- ScenarioDraftManager now destroys itself when returning to the main menu (and reloads when entering another save) to prevent potential load/save issues.
36+
3337
v2.4:
3438
- Drafts are now saved directly to each save's persistent file, allowing reverts to free drafted names.
3539
- An upgrade system was left in place which will migrate pre-existing drafts over to the new system.

Source/DraftTwitchViewers/DraftManagerApp.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,11 @@ private void DoDraft(bool forDrawing, string job = "Any")
649649
/// Creates a new Kerbal based on the provided name.
650650
/// </summary>
651651
/// <param name="kerbalName">The name of the new Kerbal.</param>
652-
private void DraftSuccess(DraftInfo info)
652+
private void DraftSuccess(Dictionary<string, string> info)
653653
{
654654
ProtoCrewMember newKerbal = HighLogic.CurrentGame.CrewRoster.GetNewKerbal();
655-
newKerbal.name = info.name;
656-
KerbalRoster.SetExperienceTrait(newKerbal, info.job);
655+
newKerbal.name = info["name"];
656+
KerbalRoster.SetExperienceTrait(newKerbal, info["job"]);
657657

658658
// If the game is career, subtract the cost of hiring.
659659
if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
@@ -685,7 +685,7 @@ private void DraftSuccess(DraftInfo info)
685685
else
686686
{
687687
// Alert in-game.
688-
alertingMsg = draftMessage.Replace("&user", info.name).Replace("&skill", newKerbal.experienceTrait.Title);
688+
alertingMsg = draftMessage.Replace("&user", info["name"]).Replace("&skill", newKerbal.experienceTrait.Title);
689689
draftBusy = false;
690690
failedToDraft = false;
691691
alertShowing = true;
@@ -698,10 +698,10 @@ private void DraftSuccess(DraftInfo info)
698698
/// Displays the winner of the drawing.
699699
/// </summary>
700700
/// <param name="winner">The winner of the drawing.</param>
701-
private void DrawingSuccess(DraftInfo info)
701+
private void DrawingSuccess(Dictionary<string, string> info)
702702
{
703703
// Alert in-game.
704-
alertingMsg = drawMessage.Replace("&user", info.name);
704+
alertingMsg = drawMessage.Replace("&user", info["winner"]);
705705
draftBusy = false;
706706
failedToDraft = false;
707707
alertShowing = true;

Source/DraftTwitchViewers/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.4.0.0")]
36-
[assembly: AssemblyFileVersion("2.3.0.0")]
35+
[assembly: AssemblyVersion("2.4.1.0")]
36+
[assembly: AssemblyFileVersion("2.3.1.0")]

Source/DraftTwitchViewers/README.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Draft Twitch Viewers (DTV) uses web requests to connect to Twitch, and can pick
3030

3131
CHANGELOG:
3232

33+
v2.4.1:
34+
- Modified DraftKerbal method to invoke success using a generic Dictionary instead of a specialized DraftInfo object.
35+
- ScenarioDraftManager now destroys itself when returning to the main menu (and reloads when entering another save) to prevent potential load/save issues.
36+
3337
v2.4:
3438
- Drafts are now saved directly to each save's persistent file, allowing reverts to free drafted names.
3539
- An upgrade system was left in place which will migrate pre-existing drafts over to the new system.

Source/DraftTwitchViewers/RescueContractModifier.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void EnqueueContract(Contract toEnqueue)
136136
/// Called when a draft succeeds.
137137
/// </summary>
138138
/// <param name="kerbalName">The name of the drafted viewer.</param>
139-
void DraftSuccess(DraftInfo info)
139+
void DraftSuccess(Dictionary<string, string> info)
140140
{
141141
// Resets failures. The addon should only destroy after 5 consecutive failures.
142142
failures = 0;
@@ -152,7 +152,7 @@ void DraftSuccess(DraftInfo info)
152152
string oldName = replacement.GetValue("kerbalName");
153153

154154
// Replace the old name with the new.
155-
replacement.SetValue("kerbalName", info.name);
155+
replacement.SetValue("kerbalName", info["name"]);
156156

157157
// For each PARAM node in the CONTRACT node,
158158
foreach (ConfigNode node in replacement.nodes)
@@ -165,23 +165,23 @@ void DraftSuccess(DraftInfo info)
165165
{
166166
case "AcquireCrew":
167167
{
168-
node.SetValue("title", "Save " + info.name);
168+
node.SetValue("title", "Save " + info["name"]);
169169
break;
170170
}
171171
case "AcquirePart":
172172
{
173-
string firstName = info.name.Substring(0, info.name.IndexOf(' '));
173+
string firstName = info["name"].Substring(0, info["name"].IndexOf(' '));
174174
node.SetValue("title", "Obtain " + firstName + "'s Scrap");
175175
break;
176176
}
177177
case "RecoverKerbal":
178178
{
179-
node.SetValue("title", "Recover " + info.name + " on Kerbin");
179+
node.SetValue("title", "Recover " + info["name"] + " on Kerbin");
180180
break;
181181
}
182182
case "RecoverPart":
183183
{
184-
string firstName = info.name.Substring(0, info.name.IndexOf(' '));
184+
string firstName = info["name"].Substring(0, info["name"].IndexOf(' '));
185185
node.SetValue("title", "Recover " + firstName + "'s Scrap on Kerbin");
186186
break;
187187
}
@@ -206,10 +206,10 @@ void DraftSuccess(DraftInfo info)
206206

207207
// Get the old Kerbal and rename it.
208208
ProtoCrewMember toRename = HighLogic.CurrentGame.CrewRoster[oldName];
209-
toRename.name = info.name;
209+
toRename.name = info["name"];
210210

211211
// Logging.
212-
Logger.DebugLog("Draft Success (" + contractsToModify.Count.ToString() + " contracts waiting): " + info.name);
212+
Logger.DebugLog("Draft Success (" + contractsToModify.Count.ToString() + " contracts waiting): " + info["name"]);
213213

214214
// Refresh the contract list by firing the onContractListChanged event.
215215
GameEvents.Contract.onContractsListChanged.Fire();

Source/DraftTwitchViewers/ScenarioDraftManager.cs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@
66

77
namespace DraftTwitchViewers
88
{
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 })]
2213
class ScenarioDraftManager : ScenarioModule
2314
{
2415
#region Instance
@@ -254,6 +245,9 @@ public override void OnAwake()
254245

255246
#endregion
256247

248+
// Destroy this instance if the Main Menu is loaded.
249+
GameEvents.onGameSceneLoadRequested.Add(SceneRequested);
250+
257251
// Initialize the regex array.
258252
InitRegexes();
259253
}
@@ -281,10 +275,19 @@ void Update()
281275
}
282276
}
283277

278+
void OnDestroy()
279+
{
280+
Instance = null;
281+
}
282+
284283
#endregion
285284

286285
#region KSP Functions
287286

287+
/// <summary>
288+
/// Loads the local game settings for this scenario.
289+
/// </summary>
290+
/// <param name="node">The config node for this scenario.</param>
288291
public override void OnLoad(ConfigNode node)
289292
{
290293
#region Local
@@ -323,6 +326,10 @@ public override void OnLoad(ConfigNode node)
323326
#endregion
324327
}
325328

329+
/// <summary>
330+
/// Saves the local game settings for this scenario.
331+
/// </summary>
332+
/// <param name="node">The config node for this scenario.</param>
326333
public override void OnSave(ConfigNode node)
327334
{
328335
SaveGlobalSettings();
@@ -410,19 +417,32 @@ public void LegacyLoadLocalSettings()
410417
}
411418
}
412419

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+
413432
#endregion
414433

415434
#region Draft function
416435

417436
/// <summary>
418437
/// Drafts a Kerbal, invoking the suplied success Action if the draft succeeds, or the failure Action if the draft fails.
419438
/// </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>
422441
/// <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>
423443
/// <param name="job">The job for the Kerbal. Optional and defaults to "Any" and is not needed if forDrawing is true.</param>
424444
/// <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")
426446
{
427447
// If a channel hasn't been input yet,
428448
if (string.IsNullOrEmpty(Instance.channel))
@@ -534,8 +554,11 @@ public static IEnumerator DraftKerbal(Action<DraftInfo> success, Action<string>
534554
Instance.SaveDrawn();
535555
}
536556

557+
Dictionary<string, string> winner = new Dictionary<string, string>();
558+
winner.Add("winner", realUsername);
559+
537560
// Invoke the success Action, allowing the caller to continue.
538-
success.Invoke(new DraftInfo(realUsername));
561+
success.Invoke(winner);
539562
}
540563
// If there is an error,
541564
else
@@ -620,8 +643,13 @@ public static IEnumerator DraftKerbal(Action<DraftInfo> success, Action<string>
620643
Instance.AlreadyDrafted.Add(oddUsername);
621644
}
622645

646+
Dictionary<string, string> drafted = new Dictionary<string, string>();
647+
648+
drafted.Add("name", realUsername + (Instance.addKerman ? " Kerman" : ""));
649+
drafted.Add("job", job);
650+
623651
// Invoke the success Action, allowing the caller to continue.
624-
success.Invoke(new DraftInfo(realUsername + (Instance.addKerman ? " Kerman" : ""), job));
652+
success.Invoke(drafted);
625653
}
626654
// Else, if we failed to find one,
627655
else if (failedToFindOne)

Source/DraftTwitchViewers/TourismContractModifier.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ void EnqueueContract(Contract toEnqueue)
142142
/// Called when a draft succeeds.
143143
/// </summary>
144144
/// <param name="kerbalName">The name of the drafted viewer.</param>
145-
void DraftSuccess(DraftInfo info)
145+
void DraftSuccess(Dictionary<string, string> info)
146146
{
147147
// Enqueue the name first thing, since it needs to be in the queue whether it has enough with it or not.
148-
draftNames.Enqueue(info.name);
148+
draftNames.Enqueue(info["name"]);
149149

150150
// Resets failures. The addon should only destroy after 5 consecutive failures.
151151
failures = 0;

0 commit comments

Comments
 (0)