OWtrack/OWTrack/Tracker.cs

207 lines
6.2 KiB
C#
Raw Normal View History

2018-12-10 08:56:29 +01:00
/*Copyright(c) 2018 Hesham Systems LLC.
2018-09-22 11:54:00 +02:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
using System;
2018-07-13 03:57:29 +02:00
using System.Diagnostics;
using System.Linq;
2018-08-12 01:21:12 +02:00
using System.IO;
2018-09-03 21:11:05 +02:00
using System.Collections.Generic;
2018-07-13 03:57:29 +02:00
namespace OWTrack
{
class Tracker
{
2018-09-23 14:14:11 +02:00
public int wins, losses, startSR, newSR, totalMatches = 0;
2018-12-23 18:41:28 +01:00
2018-09-27 09:34:14 +02:00
public void addWin() => wins++;
public void addLoss() => losses++;
public void reduceWin() => wins--;
public void rediceLoss() => losses--;
2018-08-15 10:32:42 +02:00
public int GetWins() { return wins; }
public int GetLosses() { return losses; }
2018-12-21 21:23:59 +01:00
public void setNewSR(int SR) { newSR = SR; }
public int srDiff() { return newSR - startSR; }
public Settings settings = new Settings();
public List<Session> sessions = new List<Session>();
2018-12-21 19:57:56 +01:00
public int GetTotalMatches()
{
2018-12-21 15:29:17 +01:00
int number = 0;
foreach (var session in sessions)
{
number += session.TotalMatches;
}
2018-12-21 19:57:56 +01:00
return number;
2018-12-21 15:29:17 +01:00
}
public int GetCurrentSessionMatches()
{
return sessions.Last().TotalMatches;
}
2018-12-21 19:57:56 +01:00
2018-12-21 21:23:59 +01:00
public void reset()
{
wins = 0;
losses = 0;
startSR = 0;
newSR = 0;
settings.Reset();
sessions.Clear();
StartNewSeission();
}
2018-12-23 18:41:28 +01:00
2018-12-21 19:57:56 +01:00
public void StartNewSeission()
{
Session ses = new Session(startSR);
2018-12-21 16:44:48 +01:00
sessions.Add(ses);
}
2018-12-21 16:44:48 +01:00
public Session GetCurrentSession()
{
2018-12-21 16:44:48 +01:00
return sessions.Last();
}
2018-12-21 21:23:59 +01:00
2018-07-13 03:57:29 +02:00
public bool owRunning()
2018-07-15 21:22:02 +02:00
{
if (settings.TrackOW)
2018-07-15 21:22:02 +02:00
{
2018-09-27 10:06:24 +02:00
try
{
bool isRunning = Process.GetProcessesByName("Overwatch")
.FirstOrDefault(p => p.MainModule.FileName.StartsWith(settings.GamePath)) != default(Process);
2018-09-27 10:06:24 +02:00
return isRunning;
}
catch (Exception)
{
Exception ex = new Exception("Error");
throw ex;
}
2018-07-15 21:22:02 +02:00
}
2018-09-27 10:06:24 +02:00
else return false;
2018-09-02 13:24:55 +02:00
}
2018-12-21 19:57:56 +01:00
public bool LoacteOW()
2018-09-02 13:24:55 +02:00
{
2018-12-21 19:57:56 +01:00
try
2018-09-02 13:24:55 +02:00
{
2018-12-10 08:42:14 +01:00
DriveInfo[] driveInfo = DriveInfo.GetDrives();
2018-09-03 21:11:05 +02:00
List<string> paths = new List<string>();
2018-12-10 08:42:14 +01:00
//Searches all drives (too long)
2018-12-10 13:55:26 +01:00
//foreach (var drive in driveInfo)
//{
2018-12-21 19:57:56 +01:00
//paths.AddRange(GetFiles(drive.ToString(),"Overwatch.exe"));
2018-12-10 13:55:26 +01:00
//}
paths.AddRange(GetFiles(Paths.ProgramFiles.C, "Overwatch.exe"));
paths.AddRange(GetFiles(Paths.ProgramFiles.D, "Overwatch.exe"));
2018-09-03 21:11:05 +02:00
if (paths.Count > 1)
{
//TODO: ask about correct path
2018-12-10 09:06:32 +01:00
//New Form??
2018-09-03 21:11:05 +02:00
return true;
}
2018-12-10 13:55:26 +01:00
else if (paths.Count == 1
&& paths[0].Contains("Overwatch.exe"))
2018-09-03 21:11:05 +02:00
{
settings.GamePath = paths[0];
2018-09-03 21:11:05 +02:00
return true;
}
2018-12-21 19:57:56 +01:00
else return false;
2018-09-02 13:24:55 +02:00
}
catch (Exception e)
{
2018-12-23 18:39:46 +01:00
throw e;
2018-12-21 19:57:56 +01:00
}
}
2018-12-10 08:42:14 +01:00
public static IEnumerable<string> GetFiles(string root, string searchPattern)
{
Stack<string> pending = new Stack<string>();
pending.Push(root);
while (pending.Count != 0)
{
var path = pending.Pop();
string[] next = null;
try
{
next = Directory.GetFiles(path, searchPattern);
}
catch { }
if (next != null && next.Length != 0)
foreach (var file in next) yield return file;
try
{
next = Directory.GetDirectories(path);
foreach (var subdir in next) pending.Push(subdir);
}
catch { }
}
}
2018-07-13 03:57:29 +02:00
}
2018-12-08 06:41:46 +01:00
2018-12-21 13:34:11 +01:00
class Session
{
public int TotalMatches;
public int SkillChange;
public int StartSR;
2018-12-21 20:00:11 +01:00
public DateTime date;
2018-12-23 18:41:28 +01:00
public List<Match> Matches = new List<Match>();
2018-12-21 19:57:56 +01:00
/// <summary>
/// Start a new session with a starting Skill Rating
///</summary>
2018-12-21 13:34:11 +01:00
public Session(int StartSR)
{
this.StartSR = StartSR;
date = DateTime.Now.Date;
2018-12-21 15:29:17 +01:00
TotalMatches = 0;
2018-12-21 13:34:11 +01:00
}
2018-12-21 19:57:56 +01:00
public bool IsNewSession()
2018-12-21 13:34:11 +01:00
{
if (Matches.Count == 0) return true;
else return false;
}
public Match GetLastMatch()
{
return Matches.Last();
}
public void AddMatch(Match match)
{
2018-12-23 18:18:28 +01:00
match.LastMatchSR = this.Matches.Last().newSR;
2018-12-21 13:34:11 +01:00
this.Matches.Add(match);
2018-12-21 15:29:17 +01:00
this.TotalMatches = Matches.Count();
2018-12-21 13:34:11 +01:00
}
}
2018-12-21 19:57:56 +01:00
class Match
2018-12-08 06:41:46 +01:00
{
public Match() { }
public DateTime dateTime { get; set; }
public string DateTimeSting;
2018-12-21 21:23:59 +01:00
public int StartSR;
2018-12-22 08:04:45 +01:00
public int LastMatchSR;
public int newSR;
public int ChangeInSR;
2018-12-08 06:41:46 +01:00
}
2018-07-13 03:57:29 +02:00
}