I'm trying to build a script for a Grand Theft Auto V modification. It's essentially a drug dealer job for online role play servers.
I have the following interface:
namespace DrugDealer
{
public interface IDrugDealerJobRepository
{
DrugDealerJob GetJob(int jobId);
int CountJobs();
}
}
I have the following class:
namespace DrugDealer
{
public class DrugDealerJob
{
public int JobId { get; set; }
public string Drug { get; set; }
public int UnitBuyPrice { get; set; }
public int UnitSalePrice { get; set; }
public string Location { get; set; }
public string Vehicle { get; set; }
public string VehicleHash { get; set; }
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}
}
I have the following class which acts as main:
using System;
using System.Collections.Generic;
using GTANetworkServer;
using Insight.Database;
using Insight.Database.Providers.MySql;
using MySql.Data.MySqlClient;
using Database;
using System.Threading;
namespace DrugDealer
{
public class Main : Script
{
private static MySqlConnectionStringBuilder _database;
private IUserRepository _userRepository;
private IDrugDealerJobRepository _drugDealerJobRepository;
public Main()
{
API.onResourceStart += API_onResourceStart;
}
private void API_onResourceStart()
{
API.consoleOutput("Starting DrugDealer!");
MySqlInsightDbProvider.RegisterProvider();
_database = new MySqlConnectionStringBuilder("");
_userRepository = _database.Connection().As<IUserRepository>();
_drugDealerJobRepository = _database.Connection().As<IDrugDealerJobRepository>();
ThreadStart drugDealerJob = new ThreadStart(StartDrugDealerJob);
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Creating the drug dealer job thread");
Thread dealerJob = new Thread(drugDealerJob);
dealerJob.Start();
}
public void StartDrugDealerJob()
{
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Dealer Job Starts thread");
// keep checking until players are on the server
PlayersOnServer();
int sleepfor = 5000; // 3600000 in one hour which we be using later
int i = 10;
while (i != 0)
{
List<Client> players = API.getAllPlayers();
foreach(Client player in players)
{
int numberOfJobs = _drugDealerJobRepository.CountJobs();
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Number of Jobs: {0}", numberOfJobs);
DrugDealerJob currentJob = _drugDealerJobRepository.GetJob(new Random().Next(1, numberOfJobs));
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] A Drug Shipment of {0} has arrived at {1}. Look out for a {2}", currentJob.Drug, currentJob.Location, currentJob.Vehicle);
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] {0} has a job of {1}", player.name, _userRepository.GetAccount(player.name).Job);
if(_userRepository.GetAccount(player.name).Job != "Cop")
{
//API.sendChatMessageToPlayer(player, "A new shipment of {0} has arrived at the following location: {1}", );
}
}
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Dealer Job thread Paused for {0} seconds", sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Dealer Job thread resumes");
i--;
}
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Dealer Job Ends thread");
}
public void PlayersOnServer()
{
// the thread is paused for 5000 milliseconds
int sleepfor = 5000;
List<Client> players;
// make thread loop endlessly until a user connects
do
{
players = API.getAllPlayers();
Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] Checking Player Count: {0}", players.Count);
if (players.Count == 0)
Thread.Sleep(sleepfor);
} while (players.Count == 0);
}
}
}
When I run the server with my resource added I get an error and the server crashes when the following line of code is reached:
int numberOfJobs = _drugDealerJobRepository.CountJobs();
I have removed the sensitive data from the connection string but it is working correctly because if I remove the line thats casuing the error then the following line works as expected returning the job data:
DrugDealerJob currentJob = _drugDealerJobRepository.GetJob(new Random().Next(1, numberOfJobs));
I can't seem to work out how to use visual studio to debug my scripts. If someone has a clue how to do this then I would appreciate the help on that.
The application connects to a mysql database via the interface which references the procedures that I have created in the mysql database.
Procedure CountJobs (No Parameters):
SELECT COUNT(*)
FROM drugdealerjobs
Procedure GetJob (Parameter jobid):
SELECT
t1.jobid
,t1.drug
,t1.unitbuyprice
,t1.unitsaleprice
,t1.location
,t1.vehicle
,t1.vehiclehash
,t2.x
,t2.y
,t2.z
FROM drugdealerjobs AS t1
INNER JOIN jobcoordinates AS t2
ON t1.jobid = t2.jobid
WHERE t1.jobid = jobid
Any ideas what I could be doing wrong? I think its something to do with the interface definition but could use some help.
Many thanks Comic Coder
In my opinion, problem is CountJobs
returns int
. Returning System.Int64
from CountJobs
can solve this problem.
1) Either CountJobs
should return System.Int64
.
2) Or Cast the return value of ExecuteScalar
to int
.
count = Convert.ToInt32(cmd.ExecuteScalar());
If this is not solving the problem, can you please share the implementation of both the methods.
DrugDealerJob GetJob(int jobId);
int CountJobs();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments