Skip to content

Commit

Permalink
feat: add quest attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Feb 10, 2023
1 parent 3868615 commit e713b5c
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 37 deletions.
6 changes: 6 additions & 0 deletions MHFZ_Overlay/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,12 @@
<setting name="QuestAttemptsShown" serializeAs="String">
<value>False</value>
</setting>
<setting name="QuestAttemptsX" serializeAs="String">
<value>300</value>
</setting>
<setting name="QuestAttemptsY" serializeAs="String">
<value>90</value>
</setting>
</MHFZ_Overlay.Settings>
</userSettings>
</configuration>
15 changes: 13 additions & 2 deletions MHFZ_Overlay/ConfigWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,10 @@
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>

</Grid.RowDefinitions>
<TextBlock Margin="5,5,5,5" ToolTip="{Binding Source={StaticResource Settings}, Path=GameFolderPath}" Style="{StaticResource ConfigTextBox}" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" Grid.ColumnSpan="2" Text="Hover to view game folder path (automatically set by checking the folder location of the current mhf.exe at overlay startup)"/>
<TextBlock Margin="5,5,5,5" ToolTip="{Binding Source={StaticResource Settings}, Path=DatabaseFilePath}" Style="{StaticResource ConfigTextBox}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Text="Hover to view database file path"/>
Expand Down Expand Up @@ -1141,8 +1145,15 @@
<TextBox IsUndoEnabled="False" TextAlignment="Center" Background="#585b70" Style="{StaticResource ConfigTextField}" Grid.Row="10" Grid.Column="1" Text="{Binding Source={StaticResource Settings},Path=PersonalBestX}" PreviewTextInput="ValidateNumber"/>
<TextBlock Margin="5,5,5,5" Style="{StaticResource ConfigTextBox}" Grid.Row="11" Grid.Column="0" Text="Personal Best Y Coordinate"/>
<TextBox IsUndoEnabled="False" TextAlignment="Center" Background="#585b70" Style="{StaticResource ConfigTextField}" Grid.Row="11" Grid.Column="1" Text="{Binding Source={StaticResource Settings},Path=PersonalBestY}" PreviewTextInput="ValidateNumber"/>

<Grid Grid.Row="12" Grid.Column="0" Grid.ColumnSpan="2">

<TextBlock Margin="5,5,5,5" ToolTip="Show Quest Attempts divided by overlay mode and weapon type" Style="{StaticResource ConfigTextBox}" Grid.Row="12" Grid.Column="0" Text="Quest Attempts" />
<ToggleButton Grid.Row="12" Grid.Column="1" Style="{StaticResource MaterialDesignSwitchToggleButton}" IsChecked="{Binding Source={StaticResource Settings}, Path=QuestAttemptsShown}"/>
<TextBlock Margin="5,5,5,5" Style="{StaticResource ConfigTextBox}" Grid.Row="13" Grid.Column="0" Text="Quest Attempts X Coordinate"/>
<TextBox IsUndoEnabled="False" TextAlignment="Center" Background="#585b70" Style="{StaticResource ConfigTextField}" Grid.Row="13" Grid.Column="1" Text="{Binding Source={StaticResource Settings},Path=QuestAttemptsX}" PreviewTextInput="ValidateNumber"/>
<TextBlock Margin="5,5,5,5" Style="{StaticResource ConfigTextBox}" Grid.Row="14" Grid.Column="0" Text="Quest Attempts Y Coordinate"/>
<TextBox IsUndoEnabled="False" TextAlignment="Center" Background="#585b70" Style="{StaticResource ConfigTextField}" Grid.Row="14" Grid.Column="1" Text="{Binding Source={StaticResource Settings},Path=QuestAttemptsY}" PreviewTextInput="ValidateNumber"/>

<Grid Grid.Row="15" Grid.Column="0" Grid.ColumnSpan="2">
<StackPanel Orientation="Horizontal">
<Button BorderThickness="0" Style="{StaticResource ConfigBottomButtons}" Background="Transparent" Content="{Binding Converter={StaticResource XamlIconToViewBoxConverter },ConverterParameter='UI/Icons/save_FILL0_wght400_GRAD0_opsz48.xaml'}" Width="32" Height="32"/>
<TextBlock Text="If you see this save icon in the top left corner, do not close the game or the overlay. The database is saving your quest information while it is being shown." Margin="4,0,0,0" VerticalAlignment="Center" />
Expand Down
107 changes: 107 additions & 0 deletions MHFZ_Overlay/Core/Class/DataAccessLayer/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MHFZ_Overlay.UI.Class.Mapper;
using Newtonsoft.Json;
using NLog;
using Octokit;
using System;
using System.Collections.Generic;
using System.Data;
Expand Down Expand Up @@ -2033,6 +2034,7 @@ FOR EACH ROW
}
}

// TODO optimization
private void CreateDatabaseIndexes(SQLiteConnection conn)
{
List<string> createIndexSqlStatements = new List<string>
Expand Down Expand Up @@ -3557,6 +3559,22 @@ FOREIGN KEY(RoadDureSkill16ID) REFERENCES AllRoadDureSkills(RoadDureSkillID)

InsertDictionaryDataIntoTable(Dictionary.RoadDureSkills.RoadDureSkillIDs, "AllRoadDureSkills", "RoadDureSkillID", "RoadDureSkillName", conn);

sql = @"CREATE TABLE IF NOT EXISTS QuestAttempts(
QuestAttemptsID INTEGER PRIMARY KEY AUTOINCREMENT,
QuestID INTEGER NOT NULL,
WeaponTypeID INTEGER NOT NULL,
ActualOverlayMode TEXT NOT NULL,
Attempts INTEGER NOT NULL,
FOREIGN KEY(QuestID) REFERENCES QuestName(QuestNameID),
FOREIGN KEY(WeaponTypeID) REFERENCES WeaponType(WeaponTypeID),
UNIQUE (QuestID, WeaponTypeID, ActualOverlayMode)
)
";
using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
{
cmd.ExecuteNonQuery();
}

#region gacha
// a mh game but like a MUD. hunt in-game to get many kinds of points for this game. hunt and tame monsters. challenge other CPU players/monsters.

Expand Down Expand Up @@ -4126,6 +4144,95 @@ FinalTimeValue ASC
return personalBest;
}

public void UpsertQuestAttempts(long questID, int weaponTypeID, string category)
{
using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
try
{
// I'm not taking into account party size, should be fine
using (SQLiteCommand command = new SQLiteCommand(conn))
{
command.CommandText =
@"UPDATE
QuestAttempts
SET
Attempts = Attempts + 1
WHERE
QuestID = @QuestID
AND WeaponTypeID = @WeaponTypeID
AND ActualOverlayMode = @ActualOverlayMode;
INSERT INTO
QuestAttempts (QuestID, WeaponTypeID, ActualOverlayMode, Attempts)
SELECT
@QuestID, @WeaponTypeID, @ActualOverlayMode, 1
WHERE
(SELECT Changes() = 0);";

command.Parameters.AddWithValue("@QuestID", questID);
command.Parameters.AddWithValue("@WeaponTypeID", weaponTypeID);
command.Parameters.AddWithValue("@ActualOverlayMode", category);

command.ExecuteNonQuery();
}
transaction.Commit();
}
catch (Exception ex)
{
HandleError(transaction, ex);
}
}
}
}

public long GetQuestAttempts(long questID, int weaponTypeID, string category)
{
long attempts = 0;

using (SQLiteConnection conn = new SQLiteConnection(dataSource))
{
conn.Open();
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
try
{
// I'm not taking into account party size, should be fine
using (SQLiteCommand cmd = new SQLiteCommand(
@"SELECT
Attempts
FROM
QuestAttempts
WHERE
QuestID = @questID
AND WeaponTypeID = @weaponTypeID
AND ActualOverlayMode = @category", conn))
{
cmd.Parameters.AddWithValue("@questID", questID);
cmd.Parameters.AddWithValue("@weaponTypeID", weaponTypeID);
cmd.Parameters.AddWithValue("@category", category);

object result = cmd.ExecuteScalar();
if (result != null)
{
attempts = Convert.ToInt64(result);
}
}
transaction.Commit();
}
catch (Exception ex)
{
HandleError(transaction, ex);
}
}
}

return attempts;
}

public AmmoPouch GetAmmoPouch(long runID)
{
AmmoPouch ammoPouch = new AmmoPouch();
Expand Down
14 changes: 14 additions & 0 deletions MHFZ_Overlay/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,20 @@
<local:OutlinedTextBlock Visibility="{Binding ElementName=Window, Path=DataLoader.model.ShowOverlayStatText, Converter={StaticResource BoolToVis}}" HorizontalAlignment="Left" Text="Quest ID" Grid.Column="0" Grid.Row="0" FontFamily="MS Gothic" FontSize="21" FontWeight="Bold" StrokeThickness="4" Stroke="#1e1e2e" Fill="{Binding Source={StaticResource Settings},Path=TextColor}"/>
<local:OutlinedTextBlock HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" FontFamily="{StaticResource Source Code Pro Bold}" FontSize="16" StrokeThickness="4" Stroke="#1e1e2e" Fill="{Binding Source={StaticResource Settings},Path=TextColor}" Text="{Binding QuestIDBind}" Margin="4,0,0,0"/>
</Grid>
<Grid x:Name="QuestAttemptsInfo" Canvas.Left="{Binding Source={StaticResource Settings},Path=QuestAttemptsX}" Canvas.Top="{Binding Source={StaticResource Settings},Path=QuestAttemptsY}" Width="360" Height="30" MouseLeftButtonDown="ElementMouseLeftButtonDown" Background="{Binding Source={StaticResource Settings},Path=TextBackGroundColor}" Visibility="{Binding ElementName=Window, Path=DataLoader.model.ShowQuestAttemptsInfo, Converter={StaticResource BoolToVis}}">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Image Visibility="{Binding ElementName=Window, Path=DataLoader.model.ShowOverlayStatIcon, Converter={StaticResource BoolToVis}}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Column="0" Source="UI/Icons/png/ticket.png"/>

<!--<TextBlock Foreground="#cdd6f4" FontFamily="Source Code Pro" IsHitTestVisible="False" Style="{StaticResource MainTextBox}" Grid.Column="0" Grid.Row="0" Text="Hit Count"/>-->
<local:OutlinedTextBlock Visibility="{Binding ElementName=Window, Path=DataLoader.model.ShowOverlayStatText, Converter={StaticResource BoolToVis}}" HorizontalAlignment="Left" Text="Attempts" Grid.Column="0" Grid.Row="0" FontFamily="MS Gothic" FontSize="21" FontWeight="Bold" StrokeThickness="4" Stroke="#1e1e2e" Fill="{Binding Source={StaticResource Settings},Path=TextColor}" />
<local:OutlinedTextBlock HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" FontFamily="{StaticResource Source Code Pro Bold}" FontSize="16" StrokeThickness="4" Stroke="#1e1e2e" Fill="{Binding Source={StaticResource Settings},Path=TextColor}" x:Name="questAttemptsTextBlock" Text="0" Margin="4,0,0,0"/>
</Grid>
<Grid x:Name="SaveIconGrid" Canvas.Left="0" Canvas.Top="0" Width="64" Height="64" MouseLeftButtonDown="ElementMouseLeftButtonDown" Background="{Binding Source={StaticResource Settings},Path=TextBackGroundColor}" Visibility="{Binding ElementName=Window, Path=DataLoader.model.ShowSaveIcon, Converter={StaticResource BoolToVis}}">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="1*" />
Expand Down
21 changes: 19 additions & 2 deletions MHFZ_Overlay/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ private void WriteCrashLog(Exception ex)
}

System.Windows.MessageBox.Show("Fatal error, closing overlay. See the crash log in the overlay folder for more information.", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
logger.Fatal("Program crashed");
logger.Fatal(ex, "Program crashed");

//https://stackoverflow.com/a/9050477/18859245
Cleanup();
Expand Down Expand Up @@ -1082,6 +1082,7 @@ private void HidePlayerInfoWhenNotInQuest()
DataLoader.model.ShowQuestID = v && s.QuestIDShown;

DataLoader.model.ShowPersonalBestInfo = v && s.PersonalBestShown;
DataLoader.model.ShowQuestAttemptsInfo = v && s.QuestAttemptsShown;
}

#endregion
Expand Down Expand Up @@ -1946,7 +1947,10 @@ private void UpdateDiscordRPC()
return;
}

presenceTemplate.Details = string.Format("{0}{1}{2}{3}{4}{5}", GetPartySize(), GetQuestState(), GetCaravanScore(), DataLoader.model.GetOverlayModeForRPC(), DataLoader.model.GetAreaName(DataLoader.model.AreaID()), GetGameMode(DataLoader.isHighGradeEdition));
if (string.Format("{0}{1}{2}{3}{4}{5}", GetPartySize(), GetQuestState(), GetCaravanScore(), DataLoader.model.GetOverlayModeForRPC(), DataLoader.model.GetAreaName(DataLoader.model.AreaID()), GetGameMode(DataLoader.isHighGradeEdition)).Length >= 128)
presenceTemplate.Details = string.Format("{0}{1}{2}{3}", GetQuestState(), DataLoader.model.GetOverlayModeForRPC(), DataLoader.model.GetAreaName(DataLoader.model.AreaID()), GetGameMode(DataLoader.isHighGradeEdition));
else
presenceTemplate.Details = string.Format("{0}{1}{2}{3}{4}{5}", GetPartySize(), GetQuestState(), GetCaravanScore(), DataLoader.model.GetOverlayModeForRPC(), DataLoader.model.GetAreaName(DataLoader.model.AreaID()), GetGameMode(DataLoader.isHighGradeEdition));

if (IsInHubAreaID() && DataLoader.model.QuestID() == 0)
DataLoader.model.PreviousHubAreaID = DataLoader.model.AreaID();
Expand Down Expand Up @@ -2361,6 +2365,10 @@ private void MainGrid_DragOver(object sender, DragEventArgs e)
s.PersonalBestX = (double)(pos.X - XOffset);
s.PersonalBestY = (double)(pos.Y - YOffset);
break;
case "QuestAttemptsInfo":
s.QuestAttemptsX = (double)(pos.X - XOffset);
s.QuestAttemptsY = (double)(pos.Y - YOffset);
break;
case "HitCountInfo":
s.HitCountX = (double)(pos.X - XOffset);
s.HitCountY = (double)(pos.Y - YOffset);
Expand Down Expand Up @@ -2657,6 +2665,7 @@ private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
#region database

private bool calculatedPersonalBest = false;
private bool calculatedQuestAttempts = false;

//TODO
private void CheckQuestStateForDatabaseLogging()
Expand All @@ -2679,6 +2688,13 @@ private void CheckQuestStateForDatabaseLogging()
calculatedPersonalBest = true;
personalBestTextBlock.Text = databaseManager.GetPersonalBest(DataLoader.model.QuestID(), DataLoader.model.WeaponType(), OverlayModeWatermarkTextBlock.Text, DataLoader.model.QuestTimeMode, DataLoader);
}

if (!calculatedQuestAttempts && DataLoader.model.TimeDefInt() > DataLoader.model.TimeInt() && int.Parse(DataLoader.model.ATK) > 0)
{
calculatedQuestAttempts = true;
databaseManager.UpsertQuestAttempts(DataLoader.model.QuestID(), DataLoader.model.WeaponType(), OverlayModeWatermarkTextBlock.Text);
questAttemptsTextBlock.Text = databaseManager.GetQuestAttempts(DataLoader.model.QuestID(), DataLoader.model.WeaponType(), OverlayModeWatermarkTextBlock.Text).ToString();
}
}

if ((DataLoader.model.QuestState() == 0 && DataLoader.model.QuestID() == 0))
Expand All @@ -2689,6 +2705,7 @@ private void CheckQuestStateForDatabaseLogging()
DataLoader.model.resetQuestInfoVariables();
personalBestTextBlock.Text = "--:--.--";
calculatedPersonalBest = false;
calculatedQuestAttempts = false;
return;
}
else if (!DataLoader.model.loadedItemsAtQuestStart && DataLoader.model.QuestState() == 0 && DataLoader.model.QuestID() != 0)
Expand Down
24 changes: 24 additions & 0 deletions MHFZ_Overlay/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions MHFZ_Overlay/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -692,5 +692,11 @@
<Setting Name="QuestAttemptsShown" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="QuestAttemptsX" Type="System.Double" Scope="User">
<Value Profile="(Default)">300</Value>
</Setting>
<Setting Name="QuestAttemptsY" Type="System.Double" Scope="User">
<Value Profile="(Default)">90</Value>
</Setting>
</Settings>
</SettingsFile>
Loading

0 comments on commit e713b5c

Please sign in to comment.