Sonmez's Vehicle System (Update v1.8)

Started by Sonmez, Nov 15, 2020, 10:24 PM

Previous topic - Next topic

Sonmez

Source Code
[noae]function onScriptLoad(){
  VehicleDB <- ConnectSQL( "VehicleDB.db" );
  QuerySQL(VehicleDB, "create table if not exists Vehicles (List NUMERIC DEFAULT 0, ID NUMERIC DEFAULT 0,"
                     +"Model NUMERIC DEFAULT 0, World NUMERIC DEFAULT 0,"
                     +"PosX TEXT,PosY TEXT, PosZ TEXT,"
                     +"Col1 TEXT, Col2 TEXT, Angle TEXT,"
                     +"Locked BOOLEAN DEFAULT true,Owner TEXT,Price NUMERIC DEFAULT 0)");
  LoadVehicles();
}
function ReloadVehicles(){
  for(local i = 0; i<500; i++){
    local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
    if (r){
      local veh = FindVehicle(q(r,1).tointeger()); if (veh){veh.Delete();} if(i==500){}
    }
  }LoadVehicles();
}
function LoadVehicles(){
  for(local i = 0; i<500; i++){local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
    if (r){
      local Model = q(r,2).tointeger(), PosX = q(r,4).tofloat(), PosY = q(r,5).tofloat(), PosZ = q(r,6).tofloat(), Col1 = q(r,7).tointeger(), Col2 = q(r,8).tointeger(), Angle = q(r,9).tofloat();
      CreateVehicle( Model, 1, Vector(PosX,PosY,PosZ), Angle, Col1 ,Col2 )
      local newID = GetVehicleCount();
      QuerySQL( VehicleDB, "UPDATE Vehicles SET ID='"+newID+"' WHERE List LIKE '" + i + "'" );
    }
  }
}function q(r,n){return GetSQLColumnData(r,n);}

function onPlayerEnteringVehicle( player, vehicle, door ){
  local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'")
  if (r){local Locked = q(r,10).tostring();
    if (Locked == "true"){
      local Owner = q(r,11).tostring();
      local Price = q(r,12).tointeger();
      if(Owner != "null"){MessagePlayer("This car locked by "+Owner,player);}
      if(Owner == "null"){MessagePlayer("You haven't got keys. Use /buycar "+vehicle.ID,player);}
      return false;
    }else{return true;}
  }else{return true;}
}
function onPlayerEnterVehicle( player, vehicle, door ){
  local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'")
  if (r){ local Owner=q(r,11).tostring(), Price=q(r,12).tointeger();
    if (Owner != "null"){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$",player);}
    if (Owner == "null"){MessagePlayer("This car is for sale "+Price+"$",player);}
  }
}

function onPlayerCommand( player, cmd, text ){
  if (cmd == "buycar"){
    if (!text){MessagePlayer("/buycar <id>",player); return false;}
    if (!IsNum(text)){MessagePlayer("/buycar <id>",player); return false;}
    local vehicle = FindVehicle(text.tointeger())
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
      if (r){
        local Model =q(r,2).tointeger(),Owner = q(r,11).tostring(),Price = q(r,12).tointeger();
        if (Owner != "null"){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$", player); return false;}
        if (player.Cash < Price){MessagePlayer("Your money isn't enough for buy this car", player); return false;}
        QuerySQL( VehicleDB, "UPDATE Vehicles SET Owner='"+player.Name+"' WHERE ID LIKE '" + vehicle.ID + "'" );
        player.Cash -= Price;
        MessagePlayer("You bought "+GetVehicleNameFromModel( Model )+" with "+vehicle.ID+" ID", player);
      }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    }
  }
  else if (cmd == "sellcar"){
    if (!text){MessagePlayer("/buycar <id>",player); return false;}
    if (!IsNum(text)){MessagePlayer("/buycar <id>",player); return false;}
    local vehicle = FindVehicle(text.tointeger())
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
      if (r){
        local Owner = q(r,11).tostring(),Price = q(r,12).tointeger();
        if (Owner != "null" && Owner != player.Name){MessagePlayer("This car is owned by "+Owner+" for "+Price+"$", player); return false;}
        if (Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
        QuerySQL( VehicleDB, "UPDATE Vehicles SET Owner='"+"null"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
        MessagePlayer("You sold your vehicle for "+Price*0.5+"$", player);
        player.Cash += Price*0.5;
      }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    }
  }
  else if (cmd == "getcar"){
    if (!text){MessagePlayer("/getcar <id>",player); return false;}
    if (!IsNum(text)){MessagePlayer("/getcar <id>",player); return false;}
    local vehicle = FindVehicle(text.tointeger())
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
      if (r){
        local Owner = q(r,11).tostring();
        local Price = q(r,12).tointeger();
        if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
        vehicle.Pos = Vector(player.Pos.x+2, player.Pos.y, player.Pos.z);
        MessagePlayer("You teleported your vehicle to yourself", player);
      }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    }
  }
  else if (cmd == "lockcar"){
    if (!text){MessagePlayer("/lockcar <id>",player); return false;}
    if (!IsNum(text)){MessagePlayer("/lockcar <id>",player); return false;}
    local vehicle = FindVehicle(text.tointeger())
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
        if (r){local Owner = q(r,11).tostring(),Price = q(r,12).tointeger();
          if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
          QuerySQL( VehicleDB, "UPDATE Vehicles SET Locked='"+"true"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
          MessagePlayer("You locked your car", player);
        }else{
          MessagePlayer("This car isn't available try another vehicle", player);
          return false;
        }
    }
  }
  else if (cmd == "unlockcar"){
    if (!text){MessagePlayer("/unlockcar <id>",player); return false;}
    if (!IsNum(text)){MessagePlayer("/unlockcar <id>",player); return false;}
    local vehicle = FindVehicle(text.tointeger())
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
      if (r){
        local Owner = q(r,11).tostring(), Price = q(r,12).tointeger();
        if (Owner != "null" && Owner != player.Name||Owner == "null"){MessagePlayer("This car isn't yours", player); return false;}
        QuerySQL( VehicleDB, "UPDATE Vehicles SET Locked='"+"false"+"' WHERE ID LIKE '" + vehicle.ID + "'" );
        MessagePlayer("You unlocked your car", player);
      }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    }
  }
  else if (cmd == "mycars"){
    local cars = "";
    for(local i = 0; i<500; i++){local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
      if (r){
        local Owner = q(r,11).tostring(),ID = q(r,1).tointeger(),Model = q(r,2).tointeger();
        if (Owner == player.Name){
          if (cars !=""){
            cars = cars+", "+GetVehicleNameFromModel( Model )+"("+ID+")";
          }
          if (cars == ""){
            cars = ""+GetVehicleNameFromModel( Model )+"("+ID+")";
          }
        }
      }
    }
    local t = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE Owner = '" + player.Name + "'");
    if (t){MessagePlayer("Your cars: " +cars, player);}
    else{MessagePlayer("You haven't got any car", player);return false;}
  }
  else{
    if (stats[player.ID].Level==200){onPlayerACommand(player,cmd,text);}
  }
}
function onPlayerACommand( player, cmd, text ){
  if (cmd == "addcar"){
    if (!text){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}
    if (!GetTok( text, " ", 1 )||!GetTok( text, " ", 2 )||!GetTok( text, " ", 3 )||!GetTok( text, " ", 4 )||!GetTok( text, " ", 5 )||GetTok( text, " ", 6 )){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}
    if (!IsNum(GetTok( text, " ", 1 ))||!IsNum(GetTok( text, " ", 2 ))||!IsNum(GetTok( text, " ", 3 ))||!(IsNum(GetTok( text, " ", 4 )))||!IsNum(GetTok( text, " ", 5 ))){MessagePlayer("/addcar <world> <model> <col1> <col2> <price>",player); return false;}
    local lastlist = 0,world = GetTok( text, " ", 1 ).tointeger(),model = GetTok( text, " ", 2 ).tointeger(),col1 = GetTok( text, " ", 3 ).tointeger(),col2 = GetTok( text, " ", 4 ).tointeger(),price = GetTok( text, " ", 5 ).tointeger(),
    veh = CreateVehicle( model, world, Vector(player.Pos.x,player.Pos.y,player.Pos.z), player.Angle, col1 ,col2 ),
    vehID= GetVehicleCount();
    for(local i = 0; i<500; i++){local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
      if (r){lastlist++;}
    }
    QuerySQL(VehicleDB, "INSERT INTO Vehicles ( List, ID, Model , World, PosX, PosY, PosZ, Col1, Col2, Angle, Locked,Owner,Price) VALUES ( '" + lastlist + "','" + vehID + "','" + model + "','" + world + "','" + player.Pos.x + "','" + player.Pos.y  + "','" + player.Pos.z  + "','" + col1 + "','" + col2 + "','" + player.Angle + "','" + true + "','" + "null" + "','" + price + "' )");
  }
  if (cmd == "removecar"){
    if (!text){MessagePlayer("/removecar <vehicleID>",player); return false;}
    if (!GetTok( text, " ", 1 )||!IsNum(GetTok( text, " ", 1 )) || GetTok( text, " ", 2 )){MessagePlayer("/removecar <vehicleID>",player); return false;}
    local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger());
    if (!vehicle){MessagePlayer("This car isn't available try another vehicle", player); return false;}
    local lastlist = 0,List = 0,i = 0;
    for(i = 0; i<500; i++){local a = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
      if (a){lastlist++;}
    }
    local b = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
    if (b){List = GetSQLColumnData(b, 0).tointeger();
      QuerySQL( VehicleDB, "DELETE FROM Vehicles WHERE ID='"+vehicle.ID+"'" );
      vehicle.Delete();
    }
    for(i = List+1; i<lastlist; i++){local c = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE List = '" + i + "'");
      if (c){
        local newList = GetSQLColumnData(c, 0).tointeger()-1;
        QuerySQL( VehicleDB, "UPDATE Vehicles SET List='"+newList+"' WHERE List LIKE '" + i + "'" );
      }ReloadVehicles();
    }
  }
  if (cmd == "changecol")
  {
    if (!text){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
    if(!GetTok( text, " ", 1 )||!GetTok( text, " ", 2 )||!GetTok( text, " ", 3 )||GetTok(text, " ", 4)){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
    if(!IsNum(GetTok( text, " ", 1 ))||!IsNum(GetTok( text, " ", 2 ))||!IsNum(GetTok( text, " ", 3 ))){MessagePlayer("/changecol <vehicle id> <col1 id> <col2 id>",player); return false;}
    local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger())
    local col1 = GetTok( text, " ", 2 ).tointeger(),col2 = GetTok( text, " ", 3 ).tointeger();
    if (vehicle){
      local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
      if (r){
        QuerySQL( VehicleDB, "UPDATE Vehicles SET Col1='"+col1+"', Col2 = '"+col2+"' WHERE ID LIKE '" + vehicle.ID + "'" );
        MessagePlayer("Vehicle ID:"+vehicle.ID+"'s color has been changed with col1:"+col1+", col2: "+col2+"", player);
        vehicle.Colour1 = col1
        vehicle.Colour2 = col2
      }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    }
  }
  if (cmd == "changepos"){
    if (!text){MessagePlayer("/changepos <vehicle id>",player); return false;}
    if (!GetTok( text, " ", 1 )||!IsNum(GetTok(text, " ", 1))||GetTok( text, " ", 2 )){MessagePlayer("/changepos <vehicle id>",player); return false;}
    local vehicle = FindVehicle(GetTok( text, " ", 1 ).tointeger()),px = player.Pos.x.tostring(),py = player.Pos.y.tostring(),pz = player.Pos.z.tostring(),angle = asin(vehicle.Rotation.z)*2;
    if (!vehicle){return false;}
    local r = QuerySQL(VehicleDB, "SELECT * FROM Vehicles WHERE ID = '" + vehicle.ID + "'");
    if (r){QuerySQL( VehicleDB, "UPDATE Vehicles SET PosX='"+px+"', PosY = '"+py+"', PosZ = '"+pz+"', Angle = '"+angle+"' WHERE ID LIKE '" + vehicle.ID + "'" );
      MessagePlayer("Vehicle ID:"+vehicle.ID+"'s position has been changed with position:"+px+", "+py+", "+pz+"", player);
    }else{MessagePlayer("This car isn't available try another vehicle", player);return false;}
    ReloadVehicles();
  }
}

function GetTok(string, separator, n, ...){local m = vargv.len() > 0 ? vargv[0] : n,tokenized = split(string, separator),text = "";
if (n > tokenized.len() || n < 1){return null;}for (; n <= m; n++){text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];} return text;
}function NumTok( string, separator ){local tokenized = split(string, separator); return tokenized.len();}
[/noae]

    Update Notes:
    • Code has been reproduced
    • Bug fixes.
if (!perfect) createAgain();

Altay

Mustafa Kemal ATATURK, the man who changed the course of history. https://en.wikipedia.org/wiki/Mustafa_Kemal_Atat%C3%BCrk

Inferno

Viva la VU
VFS Developer
VCCNR Administrator

Altay

In addition, you should add /removecar code for removing wrong cars.
Mustafa Kemal ATATURK, the man who changed the course of history. https://en.wikipedia.org/wiki/Mustafa_Kemal_Atat%C3%BCrk

Sonmez

#4
Quote from: ivedik on Nov 16, 2020, 02:18 PMIn addition, you should add /removecar code for removing wrong cars.
Updated. Now removing the car is completely hassle free
if (!perfect) createAgain();

Inferno

I remember that /removecar in KrlOzz Vehicle System was bugged as it bugged vehicle db after restarting hence messing up car models ids and owners.

Did you test this one if it doesn't bug the db like that?
Viva la VU
VFS Developer
VCCNR Administrator

Sonmez

Quote from: Inferno on Nov 16, 2020, 05:31 PMI remember that /removecar in KrlOzz Vehicle System was bugged as it bugged vehicle db after restarting hence messing up car models ids and owners.

Did you test this one if it doesn't bug the db like that?

There is no such error in my system. I solved all problems...
if (!perfect) createAgain();

Inferno

Viva la VU
VFS Developer
VCCNR Administrator

Altay

/unlockcar has a bug, i can unlock cars even though I don't own,
Mustafa Kemal ATATURK, the man who changed the course of history. https://en.wikipedia.org/wiki/Mustafa_Kemal_Atat%C3%BCrk

Sonmez

Quote from: ivedik on Nov 17, 2020, 12:20 PM/unlockcar has a bug, i can unlock cars even though I don't own,

Thank you for feedback. I solved the problem.
if (!perfect) createAgain();

Sonmez

#10
v1.1 update released.
    Now system has:
    • /addcar
    • /removecar
    • /changecol
    • /changepos
    • /mycars
    • /buycar
    • /sellcar
    • /getcar
    • /lockcar
    • /unlockcar
if (!perfect) createAgain();

Inferno

You have written /removecar twice.


Suggestion : Add /lights <on/off> . It will be good.
Viva la VU
VFS Developer
VCCNR Administrator

Sonmez

if (!perfect) createAgain();

husnainaligee


UncleRus

I LoperkinDead.My brothers scripters TimyrSem,VladSem