Skip to content

Commit

Permalink
tweak: misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
traditionalism committed Mar 13, 2023
1 parent dafd92c commit bbf4fab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist
build.zip
*/obj
Server/bin
Client/bin
Client/bin
.idea
23 changes: 12 additions & 11 deletions Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ClientMain()
string data = LoadResourceFile(GetCurrentResourceName(), "config.ini");
Configuration loaded = Configuration.LoadFromString(data);

if (!System.Boolean.TryParse(loaded["geneva-vending"]["UnlimitedSoda"].StringValue, out _unlimitedSoda))
if (!bool.TryParse(loaded["geneva-vending"]["UnlimitedSoda"].StringValue, out _unlimitedSoda))
{
_unlimitedSoda = false;
}
Expand All @@ -44,7 +44,7 @@ public ClientMain()
}
}

private async Task LoadAnimDict(string dict)
private static async Task LoadAnimDict(string dict)
{
RequestAnimDict(dict);
while (!HasAnimDictLoaded(dict))
Expand All @@ -53,7 +53,7 @@ private async Task LoadAnimDict(string dict)
}
}

private async Task LoadModel(uint model)
private static async Task LoadModel(uint model)
{
RequestModel(model);
while (!HasModelLoaded(model))
Expand All @@ -62,15 +62,15 @@ private async Task LoadModel(uint model)
}
}

private async Task LoadAmbientAudioBank(string bank)
private static async Task LoadAmbientAudioBank(string bank)
{
while (!RequestAmbientAudioBank(bank, false))
{
await Delay(0);
}
}

private async Task BuySoda(Prop vendingMachine)
private async Task BuySoda(Entity vendingMachine)
{
ClearHelp(true);
Ped plyPed = Game.PlayerPed;
Expand Down Expand Up @@ -150,12 +150,13 @@ private async Task BuySoda(Prop vendingMachine)
plyPed.IsInvincible = false;
plyPed.CanBeTargetted = true;
plyPed.CanRagdoll = true;

if (!owner)
{
if (!_unlimitedSoda)
{
int sodaLeft = vendingMachine.State.Get("sodaLeft");
TriggerServerEvent("geneva-vending:setAsUnused", vendingMachine.NetworkId, sodaLeft -= 1);
TriggerServerEvent("geneva-vending:setAsUnused", vendingMachine.NetworkId, sodaLeft - 1);
}
else
{
Expand All @@ -168,7 +169,7 @@ private async Task BuySoda(Prop vendingMachine)
if (!_unlimitedSoda)
{
int sodaLeft = vendingMachine.State.Get("sodaLeft");
vendingMachine.State.Set("sodaLeft", sodaLeft -= 1, true);
vendingMachine.State.Set("sodaLeft", sodaLeft - 1, true);
}
}
}
Expand All @@ -188,21 +189,21 @@ private async Task FindVendingMachineTick()
.OrderBy(p => Vector3.DistanceSquared(p.Position, plyPos))
.FirstOrDefault();

if (prop == null)
if (prop is null)
{
await Delay(4000);
return;
}

if (!NetworkGetEntityIsNetworked(prop.Handle)) NetworkRegisterEntityAsNetworked(prop.Handle);

if (prop.State.Get("beingUsed") == null)
if (prop.State.Get("beingUsed") is null)
{
TriggerServerEvent("geneva-vending:initVendingMachine", prop.NetworkId);
await Delay(1000);
}

if (prop.State.Get("beingUsed") == null || prop.State.Get("beingUsed"))
if (prop.State.Get("beingUsed") is null || prop.State.Get("beingUsed"))
{
await Delay(3000);
return;
Expand Down Expand Up @@ -230,7 +231,7 @@ private async Task FindVendingMachineTick()
}
else
{
if (!_unlimitedSoda && prop.State.Get("markedForReset") == null || !prop.State.Get("markedForReset"))
if (!_unlimitedSoda && prop.State.Get("markedForReset") is null || !prop.State.Get("markedForReset"))
{
TriggerServerEvent("geneva-vending:markVendingMachineForReset", prop.NetworkId);
await Delay(500);
Expand Down
78 changes: 33 additions & 45 deletions Server/ServerMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public ServerMain()
string data = LoadResourceFile(GetCurrentResourceName(), "config.ini");
Configuration loaded = Configuration.LoadFromString(data);

if (!Boolean.TryParse(loaded["geneva-vending"]["UnlimitedSoda"].StringValue, out _unlimitedSoda))
if (!bool.TryParse(loaded["geneva-vending"]["UnlimitedSoda"].StringValue, out _unlimitedSoda))
{
_unlimitedSoda = false;
}
if (!Int32.TryParse(loaded["geneva-vending"]["SodaCanCount"].StringValue, out _sodaCanCount))
if (!int.TryParse(loaded["geneva-vending"]["SodaCanCount"].StringValue, out _sodaCanCount))
{
_sodaCanCount = 10;
}
if (!Int32.TryParse(loaded["geneva-vending"]["MinutesToReset"].StringValue, out _minutesToReset))
if (!int.TryParse(loaded["geneva-vending"]["MinutesToReset"].StringValue, out _minutesToReset))
{
_minutesToReset = 3;
}
Expand All @@ -46,56 +46,46 @@ public ServerMain()
[EventHandler("geneva-vending:initVendingMachine")]
private void OnInitVendingMachine([FromSource] Player player, int netId)
{
Entity vendingMachine = Entity.FromHandle(NetworkGetEntityFromNetworkId(netId));
if (vendingMachine != null)
{
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Initializing statebags for vending machine (^3NetID: {netId.ToString()}^0)");
vendingMachine.State.Set("beingUsed", false, true);
if (!_unlimitedSoda)
{
vendingMachine.State.Set("sodaLeft", _sodaCanCount, true);
vendingMachine.State.Set("markedForReset", false, true);
}
}
Entity vendingMachine = Entity.FromNetworkId(netId);
if (vendingMachine is null) return;
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Initializing statebags for vending machine (^3NetID: {netId}, Player: {player.Name} ({player.Handle})^0)");
vendingMachine.State.Set("beingUsed", false, true);
if (_unlimitedSoda) return;
vendingMachine.State.Set("sodaLeft", _sodaCanCount, true);
vendingMachine.State.Set("markedForReset", false, true);
}

[EventHandler("geneva-vending:markVendingMachineForReset")]
private void OnMarkVendingMachineForReset([FromSource] Player player, int netId)
{
Entity vendingMachine = Entity.FromHandle(NetworkGetEntityFromNetworkId(netId));
if (vendingMachine != null)
{
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Marking vending machine for reset (^3NetID: {netId}^0)");
DateTime resetTime = UtcNow.AddMinutes(_minutesToReset);
_resetTimes[netId] = resetTime;
vendingMachine.State.Set("markedForReset", true, true);
}
Entity vendingMachine = Entity.FromNetworkId(netId);
if (vendingMachine is null) return;
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Marking vending machine for reset (^3NetID: {netId}, Player: {player.Name} ({player.Handle})^0)");
DateTime resetTime = UtcNow.AddMinutes(_minutesToReset);
_resetTimes[netId] = resetTime;
vendingMachine.State.Set("markedForReset", true, true);
}

[EventHandler("geneva-vending:setAsUsed")]
private void OnSetAsUsed([FromSource] Player player, int netId)
{
Entity vendingMachine = Entity.FromHandle(NetworkGetEntityFromNetworkId(netId));
if (vendingMachine != null)
{
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Setting vending machine as used by non-owner {player.Name} ({player.Handle}) (^3NetID: {netId}^0)");
vendingMachine.State.Set("beingUsed", true, true);
}
Entity vendingMachine = Entity.FromNetworkId(netId);
if (vendingMachine is null) return;
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Setting vending machine as used by non-owner {player.Name} ({player.Handle}) (^3NetID: {netId}, Owner: {vendingMachine.Owner.Name} ({vendingMachine.Owner.Handle})^0)");
vendingMachine.State.Set("beingUsed", true, true);
}

[EventHandler("geneva-vending:setAsUnused")]
private void OnSetAsUnused([FromSource] Player player, int netId, int sodaLeft)
{
Entity vendingMachine = Entity.FromHandle(NetworkGetEntityFromNetworkId(netId));
if (vendingMachine != null)
{
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Setting vending machine as unused by non-owner {player.Name} ({player.Handle}) (^3NetID: {netId}^0)");
vendingMachine.State.Set("beingUsed", false, true);
Entity vendingMachine = Entity.FromNetworkId(netId);
if (vendingMachine is null) return;
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Setting vending machine as unused by non-owner {player.Name} ({player.Handle}) (^3NetID: {netId}, Owner: {vendingMachine.Owner.Name} ({vendingMachine.Owner.Handle})^0)");
vendingMachine.State.Set("beingUsed", false, true);

if (!_unlimitedSoda)
{
vendingMachine.State.Set("sodaLeft", sodaLeft, true);
}
if (!_unlimitedSoda)
{
vendingMachine.State.Set("sodaLeft", sodaLeft, true);
}
}

Expand All @@ -109,14 +99,12 @@ private async Task ResetTick()
if (!_resetTimes.TryGetValue(netId, out DateTime resetTime)) continue;
if (UtcNow < resetTime) continue;

Entity vendingMachine = Entity.FromHandle(NetworkGetEntityFromNetworkId(netId));
if (vendingMachine != null)
{
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Resetting vending machine (^3NetID: {netId}^0)");
vendingMachine.State.Set("sodaLeft", _sodaCanCount, true);
vendingMachine.State.Set("beingUsed", false, true);
vendingMachine.State.Set("markedForReset", false, true);
}
Entity vendingMachine = Entity.FromNetworkId(netId);
if (vendingMachine is null) continue;
Debug.WriteLine($"[^3{Now.ToString("G", CultureInfo.InvariantCulture)}^0] Resetting vending machine (^3NetID: {netId}^0)");
vendingMachine.State.Set("sodaLeft", _sodaCanCount, true);
vendingMachine.State.Set("beingUsed", false, true);
vendingMachine.State.Set("markedForReset", false, true);

_resetTimes.TryRemove(netId, out resetTime);
}
Expand Down

0 comments on commit bbf4fab

Please sign in to comment.