Skip to content

Commit

Permalink
Minor changes, rewards system works now, added regions to code and re…
Browse files Browse the repository at this point in the history
…moved SaveFileHandler.
  • Loading branch information
HW12Dev committed Jan 19, 2025
1 parent 20a2bef commit c70a745
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 106 deletions.
5 changes: 0 additions & 5 deletions PayCheck3ServerApp/PayCheck3ServerApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,5 @@
<ProjectReference Include="..\PayCheckServerLib\PayCheckServerLib.csproj" />
</ItemGroup>


<ItemGroup>
<Content Include="..\Cert\**" LinkBase="Cert\" CopyToOutputDirectory="Always" />
</ItemGroup>


</Project>
21 changes: 9 additions & 12 deletions PayCheckServerLib/Helpers/CloudSaveDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static bool PathContainsIllegalCharacters(string path)
return true;
return false;
}

#region Global CloudSave Data And Static Data
public static object? GetGlobalCloudSaveData(string key)
{
return GetStaticData<object>(String.Format("GlobalStatItems/{0}.json", key));
Expand All @@ -41,7 +43,9 @@ public static bool PathContainsIllegalCharacters(string path)

return null;
}
#endregion

#region User CloudSave Data
private static string GetUserCloudSaveFolderForUserId(string userId)
{
return String.Format("./UserData/{0}/CloudSave", userId);
Expand All @@ -61,7 +65,7 @@ private static string GetUserCloudSavePathForUserIdAndKey(string userId, string

string path = GetUserCloudSavePathForUserIdAndKey(userId, key);

if (!File.Exists(path))
if (!FileReadWriteHelper.Exists(path))
return null;


Expand All @@ -76,18 +80,13 @@ public static void SetUserCloudSaveData(string userId, string key, object data)
return;


string directory = GetUserCloudSaveFolderForUserId(userId);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);

string path = GetUserCloudSavePathForUserIdAndKey(userId, key);
if (!File.Exists(path))
File.Create(path);

FileReadWriteHelper.WriteAllText(path, JsonConvert.SerializeObject(data));
}
#endregion


#region User StatItems
private static string GetStatItemsFolderForUserId(string userId)
{
return String.Format("./UserData/{0}/StatItems", userId);
Expand All @@ -108,7 +107,7 @@ private static string GetUserStatItemsPathForUserIdAndStatCode(string userId, st

string path = GetUserStatItemsPathForUserIdAndStatCode(userId, statCode);

if(!File.Exists(path))
if(!FileReadWriteHelper.Exists(path))
return null;

return JsonConvert.DeserializeObject<UserStatItemsData>(FileReadWriteHelper.ReadAllText(path));
Expand Down Expand Up @@ -168,9 +167,6 @@ public static void IncrementStatItemValueForUser(string namespace_, string userI
Value = inc,
};

if(!Directory.Exists(GetStatItemsFolderForUserId(userId)))
Directory.CreateDirectory(GetStatItemsFolderForUserId(userId));

FileReadWriteHelper.WriteAllText(path, JsonConvert.SerializeObject(updatedStatItemData));

return;
Expand All @@ -185,5 +181,6 @@ public static void IncrementStatItemValueForUser(string namespace_, string userI

return;
}
#endregion
}
}
13 changes: 13 additions & 0 deletions PayCheckServerLib/Helpers/FileReadWriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static string ReadAllText(string path)
return File.ReadAllText(path);
}

public static bool Exists(string path)
{
while (CachedFileContentsBeingModified) { }
if (CachedFileContents.ContainsKey(path))
return true;

return File.Exists(path);
}

public static void WriteAllText(string path, string contents)
{
CachedFileContentsBeingModified = true;
Expand All @@ -37,11 +46,15 @@ public static void SaveCachedFiles()
{
try
{
if(!Directory.Exists(Path.GetDirectoryName(file.Key)))
Directory.CreateDirectory(Path.GetDirectoryName(file.Key));
File.WriteAllText(file.Key, file.Value);
}
catch
{
System.Threading.Thread.Sleep(100);
if (!Directory.Exists(Path.GetDirectoryName(file.Key)))
Directory.CreateDirectory(Path.GetDirectoryName(file.Key));
File.WriteAllText(file.Key, file.Value);
}
}
Expand Down
55 changes: 0 additions & 55 deletions PayCheckServerLib/Helpers/SaveFileHandler.cs

This file was deleted.

8 changes: 4 additions & 4 deletions PayCheckServerLib/Helpers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static void SaveUser(PayCheck3UserData user)
if (!Directory.Exists(String.Format("./UserData/{0}", user.UserData.UserId)))
Directory.CreateDirectory(String.Format("./UserData/{0}", user.UserData.UserId));

File.WriteAllText(GetDataJsonPathForUserId(user.UserData.UserId), JsonConvert.SerializeObject(user));
FileReadWriteHelper.WriteAllText(GetDataJsonPathForUserId(user.UserData.UserId), JsonConvert.SerializeObject(user));
}

/// <summary>
Expand All @@ -202,9 +202,9 @@ public static void SaveUser(PayCheck3UserData user)
string path = GetDataJsonPathForUserId(UserId);


if (File.Exists(path))
if (FileReadWriteHelper.Exists(path))
{
return JsonConvert.DeserializeObject<PayCheck3UserData>(File.ReadAllText(path));
return JsonConvert.DeserializeObject<PayCheck3UserData>(FileReadWriteHelper.ReadAllText(path));
}
return null;
}
Expand All @@ -220,7 +220,7 @@ public static List<PayCheck3UserData> GetUsers()
foreach (var item in Directory.GetFiles("UserData"))
{

var userData = JsonConvert.DeserializeObject(File.ReadAllText(item));
var userData = JsonConvert.DeserializeObject(FileReadWriteHelper.ReadAllText(item));

if (userData != null) { // validation to ensure that any json file checked for user data is actual user data.
if (userData.GetType() == typeof(JObject)) {
Expand Down
Loading

0 comments on commit c70a745

Please sign in to comment.