-
-
Save DrewKestell/c089848a47d8a2bdfe878cb7ac55baa1 to your computer and use it in GitHub Desktop.
MainViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BloogBot.AI; | |
using BloogBot.Game; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Windows.Input; | |
namespace BloogBot.UI | |
{ | |
public class MainViewModel : INotifyPropertyChanged | |
{ | |
readonly BotLoader botLoader = new BotLoader(); | |
BotSettings botSettings; | |
public ObservableCollection<string> ConsoleOutput { get; } = new ObservableCollection<string>(); | |
public ObservableCollection<IBot> Bots { get; private set; } | |
public ObservableCollection<Hotspot> Hotspots { get; private set; } | |
public MainViewModel() | |
{ | |
ReloadBots(); | |
LoadBotSettings(); | |
Repository.Initialize(botSettings.DatabaseConnectionString); | |
InitializeHotspots(); | |
} | |
#region Commands | |
// Start command | |
ICommand startCommand; | |
void Start() => currentBot.Start(Food); | |
public ICommand StartCommand => | |
startCommand ?? (startCommand = new CommandHandler(Start, true)); | |
// Stop command | |
ICommand stopCommand; | |
void Stop() => currentBot.Stop(); | |
public ICommand StopCommand => | |
stopCommand ?? (stopCommand = new CommandHandler(Stop, true)); | |
// ReloadBot command | |
ICommand reloadBotCommand; | |
void ReloadBots() | |
{ | |
Bots = new ObservableCollection<IBot>(botLoader.ReloadBots()); | |
currentBot = Bots.First(); | |
Log("Bots successfully loaded!"); | |
} | |
public ICommand ReloadBotCommand => | |
reloadBotCommand ?? (reloadBotCommand = new CommandHandler(ReloadBots, true)); | |
// SaveSettings command | |
ICommand saveSettingsCommand; | |
void SaveSettings() | |
{ | |
var botSettings = new BotSettings() | |
{ | |
Food = food, | |
DatabaseConnectionString = this.botSettings.DatabaseConnectionString, | |
GrindingHotspotId = GrindingHotspot?.Id | |
}; | |
var currentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
var botSettingsFilePath = Path.Combine(currentFolder, "botSettings.json"); | |
var json = JsonConvert.SerializeObject(botSettings, Formatting.Indented); | |
File.WriteAllText(botSettingsFilePath, json); | |
} | |
public ICommand SaveSettingsCommand => | |
saveSettingsCommand ?? (saveSettingsCommand = new CommandHandler(SaveSettings, true)); | |
ICommand startRecordingHotspotCommand; | |
void StartRecordingHotspot() | |
{ | |
try | |
{ | |
if (ObjectManager.IsLoggedIn) | |
{ | |
HotspotGenerator.Record(); | |
OnPropertyChanged(nameof(StartRecordingHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(AddHotspotWaypointCommandEnabled)); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(CancelHotspotCommandEnabled)); | |
Log("Recording new hotspot..."); | |
} | |
else | |
Log("Recording failed. Not logged in."); | |
} | |
catch (Exception e) | |
{ | |
Log(e.ToString()); | |
} | |
} | |
public ICommand StartRecordingHotspotCommand => | |
startRecordingHotspotCommand ?? (startRecordingHotspotCommand = new CommandHandler(StartRecordingHotspot, true)); | |
// AddHotspotWaypoint | |
ICommand addHotspotWaypointCommand; | |
void AddHotspotWaypoint() | |
{ | |
try | |
{ | |
if (ObjectManager.IsLoggedIn) | |
{ | |
HotspotGenerator.AddWaypoint(ObjectManager.Player.Position); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
Log("Waypoint successfully added!"); | |
} | |
else | |
Log("Failed to add waypoint. Not logged in."); | |
} | |
catch (Exception e) | |
{ | |
Log(e.ToString()); | |
} | |
} | |
public ICommand AddHotspotWaypointCommand => | |
addHotspotWaypointCommand ?? (addHotspotWaypointCommand = new CommandHandler(AddHotspotWaypoint, true)); | |
// SaveHotspot | |
ICommand saveHotspotCommand; | |
void SaveHotspot() | |
{ | |
try | |
{ | |
string faction; | |
if (newHotspotHorde && newHotspotAlliance) | |
faction = "Alliance / Horde"; | |
else if (newHotspotHorde) | |
faction = "Horde"; | |
else | |
faction = "Alliance"; | |
var waypoints = HotspotGenerator.Save(); | |
var hotspot = Repository.AddHotspot( | |
waypoints, | |
ObjectManager.Zone, | |
faction, | |
newHotspotMinLevel, | |
newHotspotDescription); | |
Hotspots.Add(hotspot); | |
Hotspots = new ObservableCollection<Hotspot>(Hotspots.OrderBy(h => h?.MinimumLevel).ThenBy(h => h?.Zone).ThenBy(h => h?.Description)); | |
NewHotspotHorde = false; | |
NewHotspotAlliance = false; | |
NewHotspotMinLevel = 0; | |
NewHotspotDescription = string.Empty; | |
OnPropertyChanged(nameof(StartRecordingHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(AddHotspotWaypointCommandEnabled)); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(CancelHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(Hotspots)); | |
Log("New hotspot succesfully saved!"); | |
} | |
catch (Exception e) | |
{ | |
Log(e.ToString()); | |
} | |
} | |
public ICommand SaveHotspotCommand => | |
saveHotspotCommand ?? (saveHotspotCommand = new CommandHandler(SaveHotspot, true)); | |
// CancelHotspot | |
ICommand cancelHotspotCommand; | |
void CancelHotspot() | |
{ | |
try | |
{ | |
HotspotGenerator.Cancel(); | |
OnPropertyChanged(nameof(StartRecordingHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(AddHotspotWaypointCommandEnabled)); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
OnPropertyChanged(nameof(CancelHotspotCommandEnabled)); | |
Log("Canceling new travel path..."); | |
} | |
catch (Exception e) | |
{ | |
Log(e.ToString()); | |
} | |
} | |
public ICommand CancelHotspotCommand => | |
cancelHotspotCommand ?? (cancelHotspotCommand = new CommandHandler(CancelHotspot, true)); | |
#endregion | |
#region Observables | |
public bool StartRecordingHotspotCommandEnabled => !HotspotGenerator.Recording; | |
public bool AddHotspotWaypointCommandEnabled => HotspotGenerator.Recording; | |
public bool SaveHotspotCommandEnabled => | |
HotspotGenerator.Recording && | |
HotspotGenerator.PositionCount > 0 && | |
!string.IsNullOrWhiteSpace(newHotspotDescription) && | |
newHotspotMinLevel > 0 && | |
(newHotspotHorde || newHotspotAlliance); | |
public bool CancelHotspotCommandEnabled => HotspotGenerator.Recording; | |
string food; | |
public string Food | |
{ | |
get => food; | |
set | |
{ | |
food = value; | |
OnPropertyChanged(nameof(Food)); | |
} | |
} | |
public Hotspot grindingHotspot; | |
public Hotspot GrindingHotspot | |
{ | |
get => grindingHotspot; | |
set | |
{ | |
grindingHotspot = value; | |
OnPropertyChanged(nameof(GrindingHotspot)); | |
} | |
} | |
string newHotspotDescription; | |
public string NewHotspotDescription | |
{ | |
get => newHotspotDescription; | |
set | |
{ | |
newHotspotDescription = value; | |
OnPropertyChanged(nameof(NewHotspotDescription)); | |
} | |
} | |
int newHotspotMinLevel; | |
public int NewHotspotMinLevel | |
{ | |
get => newHotspotMinLevel; | |
set | |
{ | |
newHotspotMinLevel = value; | |
OnPropertyChanged(nameof(NewHotspotMinLevel)); | |
} | |
} | |
bool newHotspotHorde; | |
public bool NewHotspotHorde | |
{ | |
get => newHotspotHorde; | |
set | |
{ | |
newHotspotHorde = value; | |
OnPropertyChanged(nameof(NewHotspotHorde)); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
} | |
} | |
bool newHotspotAlliance; | |
public bool NewHotspotAlliance | |
{ | |
get => newHotspotAlliance; | |
set | |
{ | |
newHotspotAlliance = value; | |
OnPropertyChanged(nameof(NewHotspotAlliance)); | |
OnPropertyChanged(nameof(SaveHotspotCommandEnabled)); | |
} | |
} | |
IBot currentBot; | |
public IBot CurrentBot | |
{ | |
get => currentBot; | |
set | |
{ | |
currentBot = value; | |
OnPropertyChanged(nameof(CurrentBot)); | |
} | |
} | |
#endregion | |
public event PropertyChangedEventHandler PropertyChanged; | |
void OnPropertyChanged(string name) => | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); | |
void Log(string message) | |
{ | |
ConsoleOutput.Add($"({DateTime.Now.ToShortTimeString()}) {message}"); | |
OnPropertyChanged(nameof(ConsoleOutput)); | |
} | |
void LoadBotSettings() | |
{ | |
var currentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
var botSettingsFilePath = Path.Combine(currentFolder, "botSettings.json"); | |
botSettings = JsonConvert.DeserializeObject<BotSettings>(File.ReadAllText(botSettingsFilePath)); | |
Food = botSettings.Food; | |
} | |
void InitializeHotspots() | |
{ | |
var hotspots = Repository.ListHotspots() | |
.OrderBy(h => h.MinimumLevel) | |
.ThenBy(h => h.Zone) | |
.ThenBy(h => h.Description); | |
Hotspots = new ObservableCollection<Hotspot>(hotspots); | |
Hotspots.Insert(0, null); | |
GrindingHotspot = Hotspots.FirstOrDefault(h => h?.Id == botSettings.GrindingHotspotId); | |
OnPropertyChanged(nameof(Hotspots)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment