6 Commits

Author SHA1 Message Date
2bd2e5ba0a Add Open Shock Serial 2024-01-20 21:41:47 +01:00
d59543297a Update README.md 2024-01-19 03:14:53 +01:00
92cc79f707 Improved Setup
Update Dependency
2024-01-19 03:08:03 +01:00
6778375f43 Fix Dependency 2024-01-19 02:22:17 +01:00
0dcee037fc Update Readme 2024-01-19 02:22:12 +01:00
ee0ca69475 Fix wrong order of parameters 2024-01-19 02:13:25 +01:00
4 changed files with 60 additions and 21 deletions

View File

@ -27,10 +27,11 @@ public struct Configuration
internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null) internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null)
{ {
string settingsFilePath = path ?? "config.json"; string settingsFilePath = path ?? "config.json";
Configuration c;
if (!File.Exists(settingsFilePath)) if (!File.Exists(settingsFilePath))
Setup.Run().SaveConfiguration(); c = Setup.Run().SaveConfiguration();
else
Configuration c = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter()); c = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter());
if (!c.ConfigurationValid()) if (!c.ConfigurationValid())
throw new Exception("Configuration validation failed."); throw new Exception("Configuration validation failed.");
foreach (Shocker cShocker in c.Shockers) foreach (Shocker cShocker in c.Shockers)
@ -38,10 +39,11 @@ public struct Configuration
return c; return c;
} }
internal void SaveConfiguration(string? path = null) internal Configuration SaveConfiguration(string? path = null)
{ {
string settingsFilePath = path ?? "config.json"; string settingsFilePath = path ?? "config.json";
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented)); File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
return this;
} }
private bool ConfigurationValid() private bool ConfigurationValid()

View File

@ -11,7 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CS2GSI" Version="1.0.7" /> <PackageReference Include="CS2GSI" Version="1.0.7" />
<PackageReference Include="CShocker" Version="1.2.2" /> <PackageReference Include="CShocker" Version="1.3.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -105,12 +105,12 @@ public static class Setup
{ {
Console.WriteLine("Select API:"); Console.WriteLine("Select API:");
Console.WriteLine("1) OpenShock (HTTP)"); Console.WriteLine("1) OpenShock (HTTP)");
Console.WriteLine("2) OpenShock (Serial) NotImplemented"); //TODO Console.WriteLine("2) OpenShock (Serial)");
Console.WriteLine("3) PiShock (HTTP)"); Console.WriteLine("3) PiShock (HTTP)");
Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO
string? selectedChar = Console.ReadLine(); string? selectedChar = Console.ReadLine();
int selected; int selected;
while (!int.TryParse(selectedChar, out selected) || (selected != 1 && selected != 3)) while (!int.TryParse(selectedChar, out selected) || selected < 1 || selected > 3)
selectedChar = Console.ReadLine(); selectedChar = Console.ReadLine();
string apiUri, apiKey; string apiUri, apiKey;
@ -125,9 +125,24 @@ public static class Setup
apiKey = QueryString("OpenShock API-Key:",""); apiKey = QueryString("OpenShock API-Key:","");
intensityRange = GetIntensityRange(); intensityRange = GetIntensityRange();
durationRange = GetDurationRange(); durationRange = GetDurationRange();
newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiUri, apiKey); newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiKey, apiUri);
newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers()); newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers());
break; break;
case 2: //OpenShock (Serial)
apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net");
apiKey = QueryString("OpenShock API-Key:","");
intensityRange = GetIntensityRange();
durationRange = GetDurationRange();
SerialShocker.SerialPortInfo serialPort = SelectSerialPort();
newShocker = new OpenShockSerial(new Dictionary<string, OpenShockSerial.ShockerModel>(), intensityRange,
durationRange, serialPort);
foreach (KeyValuePair<string, OpenShockSerial.ShockerModel> kv in ((OpenShockSerial)newShocker)
.GetShockers(apiUri, apiKey))
{
newShocker.ShockerIds.Add(kv.Key);
((OpenShockSerial)newShocker).Model.Add(kv.Key, kv.Value);
}
break;
case 3: //PiShock (HTTP) case 3: //PiShock (HTTP)
apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate"); apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate");
apiKey = QueryString("PiShock API-Key:",""); apiKey = QueryString("PiShock API-Key:","");
@ -141,7 +156,6 @@ public static class Setup
newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri); newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri);
break; break;
// ReSharper disable thrice RedundantCaseLabel // ReSharper disable thrice RedundantCaseLabel
case 2: //OpenShock (Serial)
case 4: //PiShock (Serial) case 4: //PiShock (Serial)
default: default:
throw new NotImplementedException(); throw new NotImplementedException();
@ -149,6 +163,25 @@ public static class Setup
c.Shockers.Add(newShocker); c.Shockers.Add(newShocker);
} }
private static SerialShocker.SerialPortInfo SelectSerialPort()
{
List<SerialShocker.SerialPortInfo> serialPorts = SerialShocker.GetSerialPorts();
for(int i = 0; i < serialPorts.Count; i++)
Console.WriteLine($"{i}) {serialPorts[i]}");
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
string? selectedPortStr = Console.ReadLine();
int selectedPort = -1;
while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || selectedPort > serialPorts.Count - 1)
{
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
selectedPortStr = Console.ReadLine();
}
return serialPorts[selectedPort];
}
private static void AddAction(ref Configuration c) private static void AddAction(ref Configuration c)
{ {
CS2Event triggerEvent = GetTrigger(); CS2Event triggerEvent = GetTrigger();
@ -218,23 +251,27 @@ public static class Setup
private static List<string> GetShockerIds(List<Shocker> shockers) private static List<string> GetShockerIds(List<Shocker> shockers)
{ {
List<string> allShockerIds = new();
foreach(Shocker shocker in shockers)
allShockerIds.AddRange(shocker.ShockerIds);
List<string> ids = new(); List<string> ids = new();
bool addAnother = true; bool addAnother = true;
while (ids.Count < 1 || addAnother) while (ids.Count < 1 || addAnother)
{ {
Console.WriteLine("Select Shocker API:");
for(int i = 0; i < shockers.Count; i++)
Console.WriteLine($"{i}) {shockers[i]}");
for (int i = 0; i < allShockerIds.Count; i++) int selectedShocker;
Console.WriteLine($"{i}) {allShockerIds[i]}"); while (!int.TryParse(Console.ReadLine(), out selectedShocker) || selectedShocker < 0 || selectedShocker >= shockers.Count)
Console.WriteLine("Select Shocker API:");
Console.WriteLine("Select Shocker:");
for (int i = 0; i < shockers[selectedShocker].ShockerIds.Count; i++)
Console.WriteLine($"{i}) {shockers[selectedShocker].ShockerIds[i]}");
int selectedIndex; int selectedIndex;
while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= allShockerIds.Count) while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= shockers[selectedShocker].ShockerIds.Count)
Console.WriteLine("Select ID:"); Console.WriteLine("Select Shocker:");
ids.Add(allShockerIds[selectedIndex]); ids.Add(shockers[selectedShocker].ShockerIds[selectedIndex]);
Console.WriteLine("Add another ID? (Y/N):"); Console.WriteLine("Add another ID? (Y/N):");
addAnother = Console.ReadKey().Key == ConsoleKey.Y; addAnother = Console.ReadKey().Key == ConsoleKey.Y;

View File

@ -2,7 +2,7 @@
![GitHub License](https://img.shields.io/github/license/c9glax/OpenCS2hock) ![GitHub License](https://img.shields.io/github/license/c9glax/OpenCS2hock)
![GitHub Release](https://img.shields.io/github/v/release/c9glax/OpenCS2hock) ![GitHub Release](https://img.shields.io/github/v/release/c9glax/OpenCS2hock)
Electrifying your Counter-Strike experience. With [OpenShock](https://openshock.org/)! Electrifying your Counter-Strike experience. With [OpenShock](https://openshock.org/) and [PiShock](https://pishock.com/#/)! (Not Associated)
## How to use ## How to use
@ -49,8 +49,8 @@ Example `config.json`. Place next to executable. Will also be generated on first
## Shockers ## Shockers
### ApiKey ### ApiKey
For OpenShock (HTTP) get token [here](https://shocklink.net/#/dashboard/tokens) - For OpenShock (HTTP) get token [here](https://shocklink.net/#/dashboard/tokens)
For PiShock (HTTP) get information [here](https://apidocs.pishock.com/#header-authenticating) - For PiShock (HTTP) get information [here](https://apidocs.pishock.com/#header-authenticating)
### ApiType ### ApiType
CShocker [![Github](https://img.shields.io/badge/Github-8A2BE2)](https://github.com/C9Glax/cshocker) [here](https://github.com/C9Glax/CShocker/blob/master/CShocker/Shockers/Abstract/ShockerApi.cs) CShocker [![Github](https://img.shields.io/badge/Github-8A2BE2)](https://github.com/C9Glax/cshocker) [here](https://github.com/C9Glax/CShocker/blob/master/CShocker/Shockers/Abstract/ShockerApi.cs)