Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] [SOLVED] How Find New ID from enum for SQL with forward loop function ?

 
  • 0 Vote(s) - 0 Average
Pawn [SOLVED] How Find New ID from enum for SQL with forward loop function ?
PutuSuhartawan
Offline

Burgershot Member
Posts: 120
Threads: 52
Joined: Nov 2020
Reputation: 1
Location: Indonesia
#1
2021-05-18, 12:01 AM (This post was last modified: 2021-05-21, 12:55 PM by PutuSuhartawan.)
SOLUTION:
Need get cahce GetSVarInt("SlotTrunkUsed")
Code:
GetFreeTrunkID(v, s)
{
    for (new i = 1; i <= 100; i++)
    {
        if (!get_vehicle_trunk[v][s][i])
        {
            return i + GetSVarInt("SlotTrunkUsed");
        }
    }
    return -1; //Return -1 when there is no free trunk ID
}

return SQL:
Code:
              if(sscanf( inputtext, "iiiii", targetOC_ID, slot, item_type, amount, value)) return SCM(playerid, -1, "USAGE: [targetOC_ID]  [slot] [item_type] [amount] [value]");

              new playa,carid=GetRealIdCar(targetOC_ID);

              SetPVarInt(playerid, "SelectedSlot", slot);

              printf("DEBUG: Checking VID changer from vehicle ID:[%d]", carid);

             

              if(slot >= MAX_TRUNK_SLOTS || slot <= 0) return SCM(playerid, -1, "VEHICLE: Max trunk slot is 3 sir.",1);

              printf("DEBUG: Adding item trunk in slot:[%d]", slot);

              LoadOCTrunks(playerid, carid);

              if(get_vehicle_trunk[carid][slot][TRUNK_SLOT]==slot) return SCM(playerid, -1, "OC_TRUNK: That slot has been used in this vehicle sir. Please remove item from this slot first.",1);

             

              /*

                for(new s=1; s<=10; s++)

                    {

                        if(get_vehicle_trunk[carid][s][TRUNK_SQL_ID]>0)continue;

                        if(get_vehicle_trunk[carid][s][TRUNK_SQL_ID]==slot)

                        {



                        break;

                        }

                    }

              */

              if(!IsValidVehicle(carid))return SCM(playerid, -1, "VEHICLE: Thats vehicle has not exist sir.",1);

              printf("DEBUG: Slot filter hassben passed sir");

              new VID = targetOC_ID;

              new result = n_mysql_query(g_Sql, "SELECT * FROM `trunkdata`", true); //ORDER BY oc_id ASC, slot ASC

              new rows = cache_num_rows();

              new number=get_vehicle_trunk[carid][slot][TRUNK_SQL_ID];

              for(new v; v<=MAX_VEHICLES; v++)

              {

                    for(new slot; slot<=MAX_TRUNK_SLOTS; slot++)

                    {

                   

                      for(new idx; idx <= rows; idx ++)

                          {

                                cache_get_value_int(idx, "id", get_vehicle_trunk[v][slot][TRUNK_SQL_ID] );

                                /*

                                if(get_vehicle_trunk[v][slot][TRUNK_SQL_ID]>0)continue;

                                number++;

                                if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                number++;

                                printf("[TRUNK]: New id:[%d]", number );

                                if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                {

                                    number++;

                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                    number++;

                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                    NewSQL_ID = number;

                                    number++;

                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                    NewSQL_ID = number;

                                    number++;

                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])

                                    printf("[TRUNK]: New final SQL id:[%d]", number );



                                    //if(NewSQL_ID ==0 )

                                    break; // find emty slot vehicle ID. with find number primary car player

                              }*/

                          } 

                    }





              }

             

              cache_delete(result);

              NewSQL_ID = GetFreeTrunkID(carid, slot)+1; printf("[TRUNK ID]: New final SQL id:[%d]", NewSQL_ID );

              format(str, sizeof(str), "{FFBBBB}DEBUG: NewSQL_ID: [%i]", NewSQL_ID); SCM(playerid, 0xFFCCCCFF, str);



              if(NewSQL_ID==0)return SCM(playerid, -1, "{FFBBBB}SQL: {FFFFFF}System new ID SQL was failed sir");

              if(NewSQL_ID>=0)printf("{FFBBBB}DEBUG: New id SQL trunk has been found",VID,GetRPName(playerid));

             

              slot, get_vehicle_trunk[carid][slot][TRUNK_ITEM_TYPE] = item_type, get_vehicle_trunk[carid][slot][TRUNK_ITEM_AMOUNT] = amount, get_vehicle_trunk[carid][slot][TRUNK_ITEM_VALUE] = value;

              //get_vehicle_trunk[carid][slot][TRUNK_SQL_ID]=get_vehicle_trunk[carid][slot][TRUNK_SQL_ID]+1;

              if(get_vehicle_trunk[carid][slot][TRUNK_SQL_ID] == NewSQL_ID)

              {



   

                  format(str, sizeof(str), "UPDATE `trunkdata` SET `oc_id`='%d',`slot`='%d',`item_type`='%d',`amount`='%d',`value`='%d',`owner_id`='%d' WHERE `id`='%d'", VID, slot, item_type, amount, value, pInfo[playerid][ID],  NewSQL_ID);

                  n_mysql_query(g_Sql, str);

                  get_vehicle_trunk[carid][slot][TRUNK_SQL_ID] = NewSQL_ID;

              }

              else

              {

                  format(str, sizeof(str), "INSERT INTO `trunkdata` (`id`, `owner_id`, `oc_id`, `slot`, `item_type`, `amount`, `value`) VALUES ('%d', '%d', '%d', '%d', '%d', '%d', '%d')", NewSQL_ID, pInfo[playerid][ID], VID, slot, item_type, amount, value);

                  n_mysql_query(g_Sql, str);

                  SetSVarInt("SlotTrunkUsed", NewSQL_ID);

              }

               



              SCM(playerid,-1,"VEHICLE: You have adding item at this trunk vehicle",2);
              SendClientMessage(playerid,-1, str);
PROBLEM: No icrement in new ID maker from enum.
Code:
if(sscanf( inputtext, "iiiii", targetOC_ID, slot, item_type, amount, value)) return SCM(playerid, -1, "USAGE: [targetOC_ID]  [slot] [item_type] [amount] [value]");
              new playa,carid=GetRealIdCar(targetOC_ID);
              SetPVarInt(playerid, "SelectedSlot", slot);
              printf("DEBUG: Checking VID changer from vehicle ID:[%d]", carid);
             
              if(slot >= MAX_TRUNK_SLOTS || slot <= 0) return SCM(playerid, -1, "VEHICLE: Max trunk slot is 3 sir.",1);
              printf("DEBUG: Adding item trunk in slot:[%d]", slot);
              LoadOCTrunks(playerid, carid);
              if(get_vehicle_trunk[carid][slot][TRUNK_SLOT]==slot) return SCM(playerid, -1, "OC_TRUNK: That slot has been used in this vehicle sir. Please remove item from this slot first.",1);
             
              /*
                for(new s=1; s<=10; s++)
                    {
                        if(get_vehicle_trunk[carid][s][TRUNK_SQL_ID]>0)continue;
                        if(get_vehicle_trunk[carid][s][TRUNK_SQL_ID]==slot)
                        {

                        break;
                        }
                    }
              */
              if(!IsValidVehicle(carid))return SCM(playerid, -1, "VEHICLE: Thats vehicle has not exist sir.",1);
              printf("DEBUG: Slot filter hassben passed sir");
              new VID = targetOC_ID;
              new result = n_mysql_query(g_Sql, "SELECT * FROM `trunkdata`", true); //ORDER BY oc_id ASC, slot ASC
              new rows = cache_num_rows();
              new number=get_vehicle_trunk[carid][slot][TRUNK_SQL_ID];
              for(new v; v<=MAX_VEHICLES; v++)
              {
                    for(new slot; slot<=MAX_TRUNK_SLOTS; slot++)
                    {
                   
                      for(new idx; idx <= rows; idx ++)
                          {
                                cache_get_value_int(idx, "id", get_vehicle_trunk[v][slot][TRUNK_SQL_ID] );
                               
                                if(get_vehicle_trunk[v][slot][TRUNK_SQL_ID]>0)continue;
                                number++;
                                if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                number++;
                                printf("[TRUNK]: New id:[%d]", number );
                                if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                {
                                    number++;
                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                    number++;
                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                    NewSQL_ID = number;
                                    number++;
                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                    NewSQL_ID = number;
                                    number++;
                                    if(number != get_vehicle_trunk[v][slot][TRUNK_SQL_ID])
                                    printf("[TRUNK]: New final SQL id:[%d]", number );

                                    //if(NewSQL_ID ==0 )
                                    break; // find emty slot vehicle ID. with find number primary car player
                          }    }
                     
                    }


              }


Code:
GetFreeTrunkID(v, s) {
    new i=1;
    for(i; i <= 100; i++)
    {
            //i++;
            if(get_vehicle_trunk[v][s][i] == 0)
         
                if(i != 0)
                {
                  i++;
                  if(get_vehicle_trunk[v][s][i] == 0)
                  i+s;
                  return i+GetSVarInt("SlotTrunkUsed");
                }
             
         
       

    }
   

    return i+GetSVarInt("SlotTrunkUsed");
    printf("[TRUNK_ID]: Debug new ID SQL:[%d]", v);
}
Radical
Offline

Burgershot Member
Posts: 148
Threads: 21
Joined: Dec 2020
Reputation: 16
#2
2021-05-18, 12:52 AM (This post was last modified: 2021-05-18, 12:54 AM by Radical.)
An example how to get free id from a variable enum:
Code:
GetFreeSlot() {
    for (new i; i < size; i++) {
        if (!Var[i][InUse]) {
            return i;
        }
    }
    return -1;
}
PutuSuhartawan
Offline

Burgershot Member
Posts: 120
Threads: 52
Joined: Nov 2020
Reputation: 1
Location: Indonesia
#3
2021-05-18, 01:02 AM (This post was last modified: 2021-05-18, 01:37 AM by PutuSuhartawan.)
(2021-05-18, 12:52 AM)Radical Wrote: An example how to get free id from a variable enum:
Code:
GetFreeSlot() {
    for (new i; i < size; i++) {
        if (!Var[i][InUse]) {
            return i;
        }
    }
    return -1;
}

Sir but what about using 3 arrays as I used? is need twice forward function?

PHP Code:
GetFreeTrunkID() {
    for(new v; v <= MAX_VEHICLES; v++)
    {

          for (new s; s <= MAX_TRUNK_SLOTS; s++) {
            if (!get_vehicle_trunk[v][s][TRUNK_SQL_ID]) {
                return v;
            }
          }

    }
        
    
return -1;
} 

 return i; is not work on me sir. the return steal on 0. Nothink different from increment every adding new query from pawn.
Kwarde
Offline

Burgershot Member
Posts: 99
Threads: 2
Joined: Sep 2020
Reputation: 8
Location: The Netherlands
#4
2021-05-21, 08:04 AM (This post was last modified: 2021-05-21, 08:08 AM by Kwarde.)
Didn't look at all the code, but what are you doing here?

Code:
GetFreeTrunkID(v, s) {
    new i=1;
    for(i; i <= 100; i++)
    {
            //i++;
            if(get_vehicle_trunk[v][s][i] == 0)
          
                if(i != 0)
                {
                  i++;
                  if(get_vehicle_trunk[v][s][i] == 0)
                  i+s;

1-
"for(i; i <= 100; i++)"
...
"if (i != 0) i++"
i gets incremented by 1 every loop (as scripted in the last part in your for statement). You may be skipping indexes this way
EDIT: Nvm, I see you are checking two indexes per loop.

2-
"new i = 1"
...
"if (i != 0)"
You are never setting i to 0 in this piece of code (you're only incrementing it), so this statement is pretty useless.

3-
"i+s;"
If you meant to increase i with the value of s, the proper way is "i += s" or "i = i + s";


This would do the same thing:
Code:
GetFreeTrunkID(v, s)
{
    for (new i = 1; i <= 100; i++)
    {
        if (!get_vehicle_trunk[v][s][i])
        {
            return i + GetSVarInt("SlotTrunkUsed");
        }
    }
    return -1; //Return -1 when there is no free trunk ID
}

Also, (point #4): GetSVarInt() is global. This seems to be a per-vehicle script so it will most likely (I don't know the rest of your script, if the global dynamic server variable has been reset after calling GetFreeTrunkID() (or before) it might not. Depends on your script.
PutuSuhartawan
Offline

Burgershot Member
Posts: 120
Threads: 52
Joined: Nov 2020
Reputation: 1
Location: Indonesia
#5
2021-05-21, 12:49 PM
Quote:If you meant to increase i with the value of s, the proper way is "i += s" or "i = i + s";

Thank you sir.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode