/mycars needed :/

Started by Kewun, Nov 26, 2016, 01:38 PM

Previous topic - Next topic

Kewun

function onConsoleInput(cmd,text) {
 if ( cmd == "createdatabase" ) {
  try
  {
   print("Creating... please wait a few seconds")
   CreateVehicleDatabase()
   print("Success!")
  }
  catch (e)
  {
   print("Error: "+e)
  }
 }
}
function CreateVehicleDatabase() {
 for ( local i=0; i <=2600; i++) {
  local vehicles = FindVehicle(i)
  if ( vehicles ) {
   WriteIniInteger("vdb.ini","Vehicle"+vehicles.ID,"Price",rand()%100000)
   WriteIniString("vdb.ini","Vehicle"+vehicles.ID,"Owner","Liberty City")
   WriteIniString("vdb.ini","Vehicle"+vehicles.ID,"Model",GetVehicleName(vehicles))
  }
 }
}
function onPlayerCommand(player,cmd,text) {

 if ( cmd == "buycar" )
 {
  if ( !player.Vehicle ) {
   MessagePlayer("[#ff0000]You must be in a vehicle",player)
   return 0;
  }
  local model = ReadIniString("vdb.ini","Vehicle"+player.Vehicle.ID,"Model")
  local price = ReadIniInteger("vdb.ini","Vehicle"+player.Vehicle.ID,"Price")
  local owner = ReadIniString("vdb.ini","Vehicle"+player.Vehicle.ID,"Owner")
  if ( owner == player.Name ) {
   MessagePlayer("[#ff0000]This vehicle already is bought by you!",player)
   return false;
  }
  if ( owner == "Liberty City" ) {
   if ( price > player.Cash ) {
    MessagePlayer("[#ff0000]You dont have enought money to buy this vehicle",player)
    return false;
   }
   player.Cash -= price;
   WriteIniString("vdb.ini","Vehicle"+player.Vehicle.ID,"Owner",player.Name)
   MessagePlayer("[#00ff00]Congratulations, you bought this vehicle! you can now get it by using /getcar vehicleid",player)
  }
 }
 if ( cmd == "gimmecash" )
 {
  player.Cash += 10000
 }
 if ( cmd == "getcar" )
 {
  if ( player.Vehicle ) return false;
  if ( !text ) {
   MessagePlayer("[#ff0000]You dont have included vehicle id in /getcar",player)
   return false;
  }
  local targetcar = FindVehicle(text.tointeger())
  if ( targetcar ) {
   if ( ReadIniString("vdb.ini","Vehicle"+targetcar.ID, "Owner") != player.Name )  {
    MessagePlayer("[#ff0000]This vehicle does not belong to you",player)
    return false;
   }
   if ( ReadIniString("vdb.ini","Vehicle"+targetcar.ID,"Owner") == player.Name ) {
    targetcar.Pos = Vector(player.Pos.x+3,player.Pos.y,player.Pos.z)
    player.EnterVehicle(targetcar,DOOR_DRIVER)
   }
  }
 }
}
function onPlayerEnteredVehicle(player,vehicle,seat) {
 local model = ReadIniString("vdb.ini","Vehicle"+player.Vehicle.ID,"Model")
 local price = ReadIniInteger("vdb.ini","Vehicle"+player.Vehicle.ID,"Price")
 local owner = ReadIniString("vdb.ini","Vehicle"+player.Vehicle.ID,"Owner")
 MessagePlayer("[#ffffff]You entered a "+model+", Owner: "+owner+", Price: "+price+", ID: "+player.Vehicle.ID,player)
}


How can i do /mycars ? i dont know how to find from ini

I was forced to recreate the topic because people like @Ankris @Xmair spammed because they dont understand what is a request

And please post something that i will understand remember its a request

Kewun


Mötley

Are you trying to create the purchased vehicle

Example /spawncar [Model.ID] ?

Or are you trying to get a list of the purchased cars?

Let me know so I can script it.
Sorry really tired just got off of work, My brain is not functioning well

Cool

Or are you trying to get a list of the purchased cars? he want this

Kewun

yeah list of purchased cars , their ids
example
/mycars - "Your purchased vehicles are; id, id, etc"

KAKAN

This system is kinda slow though. Any way, here is an example:-
function MyCars( playerName ){
local carsID = [];
for( local veh, i = 0; i < 1000; i++ ){
local veh = FindVehicle(i);
if( !veh ) continue;
local owner = ReadIniString("vdb.ini","Vehicle"+veh.ID,"Owner")
if( owner == playerName ) carsID.push( veh.ID );
}
return carsID;
}
You can use it somewhat like this:-
if( cmd == "mycars" ){
local ownedCars = MyCars( player.Name ), aString = "You own: "
foreach( carID in ownedCars ) aString += "," + carID + " ";
MessagePlayer( aString, player );
}
WARNING: This is extremely inefficient and slow. Use an array to hold all vehicles data and too, use a SQL database instead.
oh no

Kewun


Mötley

Okay Kewun, I did not reply as I really prefer not to get into this yet, I have been dodging this for over a year.

There are many reasons why..

I will list a very tiny helper,

In LU you have 'ReadIniIndex'

function onPlayerCommand( player, cmd, text )
{
if ( cmd == "readindex" )
{
if ( text )
{
local message = ReadIniIndex( text, player.Name, 0 );
MessagePlayer( message, player );
}
}
}

This is very nice, You should Save the Players Vehicle with 'GetVehicleName( player.Vehicle )', This should be the index,,

This will become efficient if a player has already purchased the same car, As well It's better in general.

Do you remember VBS? Vrocker's Base Script?

Hear is a quickie of the code.
// ---------- Area Script ----------
// ------ Created by VRocker -------

const AREAS_INI_NAME = "Areas.ini";

// We can define 250 custom areas
areasString <- array( 250 );
areaNames <- array( 250 );

areaCount <- 0;

g_areaTemp <- "";

g_areaXml <- XmlDocument( "Areas.xml" );

function LoadAreas()
{
print( "Loaded areas file from " + AREAS_INI_NAME );
local numItems = CountIniSection( AREAS_INI_NAME, "Areas" );
local i = 0;

for ( ; i < numItems; i++ )
{
local name = ReadIniIndex( AREAS_INI_NAME, "Areas", i );
local points = ReadIniString ( AREAS_INI_NAME, "Areas", name );
areasString[ i ] = points;

areaNames[ i ] = name;

i++;
areaCount++;
}
}

function GetAreaName( x, y )
{
for( local i = 0; i < areaCount; i++ )
{
if ( InPoly( x, y, areasString[ i ] ) ) return areaNames[ i ];
}

return GetDistrictName( x, y );
}

function ProcessAreaCommands( plr, level, cmd, args )
{
if ( cmd == "loc" )
{
if ( !args )
Message( plr + " is currently in " + GetAreaName( plr.Pos.x, plr.Pos.y ) );
else
{
// Checking somebody elses cash
local plrText = GetTok( args, " ", 1 );
local plr2 = null;

if ( IsNum( plrText ) ) plr2 = FindPlayer( plrText.tointeger() );
else plr2 = FindPlayer( plrText );

if ( plr2 )
{
Message( plr2 + " is currently in " + GetAreaName( plr2.Pos.x, plr2.Pos.y ) );
}
else MessagePlayer( "Invalid player", plr, COL_CMDERROR );
}

return true;
}
else if ( cmd == "p" )
{
if ( level >= GetCmdLevel( "addarea" ) )
{
if ( g_areaTemp != "" )
g_areaTemp <- g_areaTemp + ",";

g_areaTemp <- g_areaTemp + plr.Pos.x.tostring() + "," + plr.Pos.y.tostring();

MessagePlayer( "Point set at " + plr.Pos.x + ", " + plr.Pos.y, plr );
}
else MessagePlayer( "You must be a level " + GetCmdLevel( "addarea" ) + " admin to use this command", plr, COL_CMDERROR );

return true;
}

else if ( cmd == "del" )
{
if ( level >= GetCmdLevel( "addarea" ) )
{
if ( g_areaTemp != "" )
{
g_areaTemp <- "";

MessagePlayer( "Area cleared.", plr );
}
else MessagePlayer( "No points are currently set" );
}
else MessagePlayer( "You must be a level " + GetCmdLevel( "addarea" ) + " admin to use this command", plr, COL_CMDERROR );

return true;
}

else if ( cmd == "savearea" )
{
if ( level >= GetCmdLevel( "addarea" ) )
{
if ( args )
{
if ( g_areaTemp != "" )
{
WriteIniString ( AREAS_INI_NAME, "Areas", args, g_areaTemp );

// Update the internal array
areasString[ areaCount ] = g_areaTemp;

areaNames[ areaCount ] = args;

areaCount++;
g_areaTemp <- "";

MessagePlayer( "Area saved as " + args );
}
else MessagePlayer( "No points have been set", plr, COL_CMDERROR );
}
else MessagePlayer( "Invalid arguments (/savearea <name>)", plr, COL_CMDERROR );
}
else MessagePlayer( "You must be a level " + GetCmdLevel( "addarea" ) + " admin to use this command", plr, COL_CMDERROR );

return true;
}

return false;
}

I have a lot of strong concepts as to how I would like to go about this, I really do not know as of how I want to go about this. But there is a partial way of how I will. I suggest you take your time with that script