Skip to content

Commit

Permalink
Merge pull request #169 from Erag0n001/Marketplace_Book_Fix
Browse files Browse the repository at this point in the history
Marketplace fixes
  • Loading branch information
Byte-Nova authored Oct 5, 2024
2 parents 2deda34 + f1e3911 commit 2555fc1
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 32 deletions.
12 changes: 6 additions & 6 deletions Source/Client/Dialogs/RT_Dialog_1Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RimWorld;
using UnityEngine;
using Verse;
using static GameClient.DialogManagerHelper;

namespace GameClient
{
Expand Down Expand Up @@ -54,25 +55,24 @@ public override void DoWindowContents(Rect rect)
float horizontalLineDif = Text.CalcSize(title).y + StandardMargin / 2;

float inputOneLabelDif = Text.CalcSize(inputOneLabel).y + StandardMargin;
float inputOneDif = inputOneLabelDif + 30f;
float inputOneDif = inputOneLabelDif + 28f;

Text.Font = GameFont.Medium;
Widgets.Label(new Rect(centeredX - Text.CalcSize(title).x / 2, rect.y, Text.CalcSize(title).x, Text.CalcSize(title).y), title);
Widgets.DrawLineHorizontal(rect.x, horizontalLineDif, rect.width);

DrawInputOne(centeredX, inputOneLabelDif, inputOneDif);

if (Widgets.ButtonText(new Rect(new Vector2(rect.xMin, rect.yMax - buttonY), new Vector2(buttonX, buttonY)), "Confirm"))
if (Widgets.ButtonText(GetRectForLocation(rect, defaultButtonSize, RectLocation.BottomLeft), "Confirm"))
{
DialogManager.dialog1ResultOne = inputOneResult;

if (actionYes != null) actionYes.Invoke();
actionYes?.Invoke();
Close();
}

if (Widgets.ButtonText(new Rect(new Vector2(rect.xMax - buttonX, rect.yMax - buttonY), new Vector2(buttonX, buttonY)), "Cancel"))
if (Widgets.ButtonText(GetRectForLocation(rect, defaultButtonSize, RectLocation.BottomRight), "Cancel"))
{
if (actionNo != null) actionNo.Invoke();
actionNo?.Invoke();
Close();
}
}
Expand Down
124 changes: 114 additions & 10 deletions Source/Client/Dialogs/RT_Dialog_MarketListing.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using RimWorld;
using Shared;
using UnityEngine;
Expand All @@ -13,7 +15,7 @@ public class RT_Dialog_MarketListing : Window
{
//UI

public override Vector2 InitialSize => new Vector2(400f, 400f);
public override Vector2 InitialSize => new Vector2(600f, 400f);

private Vector2 scrollPosition = Vector2.zero;

Expand All @@ -35,7 +37,7 @@ public class RT_Dialog_MarketListing : Window

//Variables

private readonly ThingDataFile[] elements;
private readonly Thing[] elements;

private readonly Map settlementMap;

Expand All @@ -45,8 +47,17 @@ public RT_Dialog_MarketListing(ThingDataFile[] elements, Map settlementMap, Acti

title = "Global Market";
description = $"Silver available for trade: {RimworldManager.GetSpecificThingCountInMap(ThingDefOf.Silver, settlementMap)}";

this.elements = elements;
List<Thing> things = new List<Thing>();
foreach (var element in elements)
{
Thing thing = null;
try { thing = ThingScribeManager.StringToItem(element); } catch{ continue; }
if (thing != null)
{
things.Add(thing);
}
}
this.elements = things.ToArray();
this.actionClick = actionClick;
this.actionCancel = actionCancel;
this.settlementMap = settlementMap;
Expand Down Expand Up @@ -124,11 +135,10 @@ private void FillMainRect(Rect mainRect)

try
{
Thing thing = ThingScribeManager.StringToItem(elements[i]);
if (thing.MarketValue > 0) DrawCustomRow(rect, thing, num4);
if (elements[i].MarketValue > 0) DrawCustomRow(rect, elements[i], num4);
else continue;
}
catch { continue; }
catch {; continue; }
}

num += 30f;
Expand All @@ -143,8 +153,11 @@ private void DrawCustomRow(Rect rect, Thing toDisplay, int index)
Text.Font = GameFont.Small;
Rect fixedRect = new Rect(new Vector2(rect.x, rect.y + 5f), new Vector2(rect.width - 16f, rect.height - 5f));
if (index % 2 == 0) Widgets.DrawHighlight(fixedRect);

Widgets.Label(fixedRect, $"{toDisplay.Label.CapitalizeFirst()} > ${toDisplay.MarketValue}/u");

string[] names = GetDisplayNames(toDisplay);
string displayName = names[0];
string displaySimple = names[1];
Widgets.Label(fixedRect, $"{displayName.CapitalizeFirst()}");
if (Widgets.ButtonText(new Rect(new Vector2(rect.xMax - selectButtonX, rect.yMax - selectButtonY), new Vector2(selectButtonX, selectButtonY)), "Select"))
{
DialogManager.dialogMarketListingResult = index;
Expand Down Expand Up @@ -172,8 +185,99 @@ private void DrawCustomRow(Rect rect, Thing toDisplay, int index)
else DialogManager.PushNewDialog(new RT_Dialog_Error("You do not have enough silver!"));
}
};
DialogManager.PushNewDialog(new RT_Dialog_1Input("Quantity to request", "Type the quantity you want to request", toDo, null));

string title = $"{displaySimple} request";
string description = $"Max ammount to request: {toDisplay.stackCount} | Price per unit: {toDisplay.MarketValue}";
DialogManager.PushNewDialog(new RT_Dialog_1Input(title, description, toDo, null));
}
}

private string[] GetDisplayNames(Thing thing)
{
string text = thing.LabelCapNoCount.CapitalizeFirst() + $" x{thing.stackCount} ";
string textSimple = thing.LabelCapNoCount.CapitalizeFirst();

Type type;
FieldInfo fieldInfo;
ReadingOutcomeDoerGainResearch research;
QualityCategory qc;

//Exceptions of things that must be handled differently

switch (thing.def.defName)
{
case "TextBook":
Book book = (Book)thing;
text = book.def.defName + ": ";
book.BookComp.TryGetDoer<BookOutcomeDoerGainSkillExp>(out BookOutcomeDoerGainSkillExp xp);
if (xp != null)
{
foreach (KeyValuePair<SkillDef, float> pair in xp.Values) text += $"{pair.Key.defName}, ";
}
thing.TryGetQuality(out qc);

text += QualityUtility.GetLabelShort(qc);
textSimple = thing.def.defName + " ";
break;

case "Schematic":
book = (Book)thing;
text = book.def.defName + ": ";
book.BookComp.TryGetDoer<ReadingOutcomeDoerGainResearch>(out research);
if (research != null)
{
type = research.GetType();
fieldInfo = type.GetField("values", BindingFlags.NonPublic | BindingFlags.Instance);
Dictionary<ResearchProjectDef, float> researchDict = (Dictionary<ResearchProjectDef, float>)fieldInfo.GetValue(research);
foreach (ResearchProjectDef key in researchDict.Keys) text += $"{key.defName}, ";
}
thing.TryGetQuality(out qc);

text += QualityUtility.GetLabelShort(qc);
textSimple = thing.def.defName + " ";
break;

case "Novel":
book = (Book)thing;
text = book.def.defName + ": ";
type = book.GetType();
fieldInfo = type.GetField("joyFactor", BindingFlags.NonPublic | BindingFlags.Instance);
text += (float)fieldInfo.GetValue(book) * 100 + "% recreation, ";
thing.TryGetQuality(out qc);

text += QualityUtility.GetLabelShort(qc);
textSimple = thing.def.defName + " ";
break;

case "Tome":
book = (Book)thing;
text = book.def.defName + ": ";
book.BookComp.TryGetDoer<ReadingOutcomeDoerGainResearch>(out research);
if (research != null)
{
type = research.GetType();
fieldInfo = type.GetField("values", BindingFlags.NonPublic | BindingFlags.Instance);
Dictionary<ResearchProjectDef, float> researchDict = (Dictionary<ResearchProjectDef, float>)fieldInfo.GetValue(research);
foreach (ResearchProjectDef key in researchDict.Keys) text += $"{key.defName}, ";

type = book.GetType();
fieldInfo = type.GetField("mentalBreakChancePerHour", BindingFlags.NonPublic | BindingFlags.Instance);
text += "mental break:"+ ((float)fieldInfo.GetValue(book) * 100).ToStringDecimalIfSmall() +"% ";
thing.TryGetQuality(out qc);

text += QualityUtility.GetLabel(qc);
textSimple = thing.def.defName + " ";
}
break;

case "Genepack":
Genepack pack = (Genepack)thing;
text = pack.def.defName + ": ";
foreach (GeneDef gene in pack.GeneSet.GenesListForReading) text += $"{gene.label}, ";
break;
}

return new string[] {text, textSimple};
}
}
}
12 changes: 7 additions & 5 deletions Source/Client/Managers/DeepScribeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -983,8 +983,8 @@ public static Thing[] GetItemsFromString(TransferData transferData)
public static ThingDataFile ItemToString(Thing thing, int thingCount)
{
ThingDataFile thingData = new ThingDataFile();

Thing toUse = null;

if (GetItemMinified(thing, thingData)) toUse = thing.GetInnerIfMinified();
else toUse = thing;

Expand All @@ -1010,6 +1010,7 @@ public static ThingDataFile ItemToString(Thing thing, int thingCount)

public static Thing StringToItem(ThingDataFile thingData)
{

Thing thing = SetItem(thingData);

SetItemQuantity(thing, thingData);
Expand Down Expand Up @@ -1137,6 +1138,7 @@ private static void GetBookDetails(Thing thing, ThingDataFile thingData)
}

thingData.BookData = bookData;
Logger.Warning(bookData.title);
}
catch (Exception e) { Logger.Warning(e.ToString()); }
}
Expand Down Expand Up @@ -1769,21 +1771,21 @@ public static bool CheckIfThingIsMinified(Thing thing)

public static bool CheckIfThingIsBook(Thing thing)
{
if (!ModsConfig.AnomalyActive) return false;

if (thing.def.defName == ThingDefOf.TextBook.defName) return true;
else if (thing.def.defName == ThingDefOf.Schematic.defName) return true;
else if (thing.def.defName == ThingDefOf.Tome.defName) return true;
else if (thing.def.defName == ThingDefOf.Novel.defName) return true;
else return false;
if (!ModsConfig.AnomalyActive) return false;
if (thing.def.defName == ThingDefOf.Tome.defName) return true;
return false;
}

public static bool CheckIfThingIsGenepack(Thing thing)
{
if (!ModsConfig.BiotechActive) return false;

if (thing.def.defName == ThingDefOf.Genepack.defName) return true;
else return false;
return false;
}

public static bool CheckIfThingIsXenoGerm(Thing thing)
Expand Down
38 changes: 27 additions & 11 deletions Source/Server/Managers/MarketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static void AddToMarket(ServerClient client, MarketData marketData)
NetworkHelper.SendPacketToAllClients(packet, client);
}

private static void RemoveFromMarket(ServerClient client, MarketData marketData)
private static void RemoveFromMarket(ServerClient client, MarketData marketData)
{
if (marketData._quantityToManage == 0)
{
Expand Down Expand Up @@ -99,22 +99,38 @@ private static void SendMarketStock(ServerClient client, MarketData marketData)

private static void TryCombineStackIfAvailable(ServerClient client, ThingDataFile thingData)
{
if (thingData.Quantity <= 0)
if (thingData.Quantity <= 0) ResponseShortcutManager.SendIllegalPacket(client, "Tried to sell illegal quantity at market");
else
{
ResponseShortcutManager.SendIllegalPacket(client, "Tried to sell illegal quantity at market");
return;
}
//Check if thing is book to handle differently
if (thingData.BookData.title != "null") Master.marketValues.MarketStock.Add(thingData);

foreach (ThingDataFile stockedItem in Master.marketValues.MarketStock.ToArray())
{
if (stockedItem.DefName == thingData.DefName && stockedItem.MaterialDefName == thingData.MaterialDefName)
//Check if thing is genepack to handle differently
else if (thingData.GenepackData.genepackDefs.Count != 0) Master.marketValues.MarketStock.Add(thingData);

//Act as if normal thing
else
{
stockedItem.Quantity += thingData.Quantity;
return;
foreach (ThingDataFile stockedItem in MarketManagerHelper.GetMarketStockSafe())
{
if (stockedItem.DefName == thingData.DefName && stockedItem.MaterialDefName == thingData.MaterialDefName)
{
stockedItem.Quantity += thingData.Quantity;
return;
}
}

Master.marketValues.MarketStock.Add(thingData);
}
}
}
}

Master.marketValues.MarketStock.Add(thingData);
public static class MarketManagerHelper
{
public static ThingDataFile[] GetMarketStockSafe()
{
return Master.marketValues.MarketStock.ToArray();
}
}
}

0 comments on commit 2555fc1

Please sign in to comment.