• Welcome to Vice City Multiplayer.
 

phPSA (rev1)

Started by stormeus, April 17, 2011, 07:14:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

stormeus

phPSA is a project I started due to the inactivity of PSA (the Pawn Supporting Application) and its incompatibility with Linux servers. For guaranteed cross-compatibility and to let you guys look at the source easily, phPSA is written in PHP. Since this is a new project, a few bugs are expected here and there and it may not be as efficient as you'd like it to be, so please, post constructive feedback.

PHP Pawn Supporting Application
phPSA is an external application, extending Pawn's functionality. It communicates with Pawn through text files, allowing PSA to perform a variety of actions normally unavaliable in Pawn. You can think of PSA as a plugin, only external.

Currently phPSA only supports SQL data storage. IRC echo support will be implemented at a later time.

Download
The current phPSA version is Revision 1. You can download it using any of the following links:

The archive includes:




readme.htmHelp and tutorial file
phpsa.phpphPSA script. This is an executable AND the source code.
phpsa.cfgphPSA Configuration

Installation

  • Put the appropriate functions in your script. These functions are written below.
    Quote






    new gColumns[24][128]...Variables used for reading rows from SELECT queries.
    PSAtimer()PSA timer function. Processes activity between phPSA and your gamemode.
    SetTimer("PSAtimer", 200, 1)Activates the phPSA timer function every 200ms repeatedly.
    PerformSQLquery(str[])Performs a regular SQL query.
    OnQueryResult(qid[], str[], index)Called whenever any query is done. Returns the number of rows affected OR 1 on a SELECT query.
    If the query is a SELECT query, the column names are stored in gColumns and the values in gValues.
  • Put phpsa.php and phpsa.cfg in your server's root dir (where vcmp-svr is).
  • Edit phpsa.cfg with your MySQL settings
  • Launch phpsa.php using the PHP runtime.

phPSA REQUIRES PHP to work.

Ubuntu/Debian users can use this command to install PHP:
sudo apt-get install php5

Function Code

PerformSQLquery(str[])
{
// Open a file pointer to SQL input
new File: filex;
filex = fopen("sql_input.psa");

// Perform the query
fwrite(filex, str);

// Close the buffer
fclose(filex);

// Return
return 1;
}

new gColumns[32][24][128],
gValues[32][24][128],
allColumns[24][256],
allValues[2][128];

OnQueryResult(str[], index)
{
// Try to parse it
// If there's an = sign, it's a DINI
if(strfind(str, "=") > 0)
{
   // Declare some variables
   new i, j;
   
   // Find the prefix
   new prefix[4];
format(prefix, sizeof(prefix), "%d=", index);
   
   // Get rid of the "index=" prefix
   strdel(str, 0, strlen(prefix));

// Split according to each column in a row
split(str, allColumns, ',');
   
   // Now read all columns
   for(i = 0; i < 24; i++)
   {
       // Not empty, keep going
       if(strlen(allColumns[i]))
       {
       // Now split the string according to equal signs
       split(allColumns[i], allValues, '=');
       
       // And read all the values
        for(j = 0; j < sizeof(allValues); j++)
       {
           // The column name has to be set
           gColumns[index - 1][i] = allValues[j];
           
            // Increment j again. We only read every two values.
           j++;
           
           // The column value has to be set
           gValues[index - 1][i]  = allValues[j];
       }
       }
// It's empty. There can't possibly be more.
else
{
   // Format a message string
   new szMsg[256];
   format(szMsg, sizeof(szMsg), "Column %s == %s", gColumns[0][0], gValues[0][0]);
   
   // Output the first row's value for the first column
   // Uncomment this if you ever want to debug it
   SendClientMessageToAll(COLOR_YELLOW, szMsg);

   // Break the for loop.
   break;
}
   }
   
   // We're done. Everything went better than expected.
   return 1;
}
// Number of rows affected
else
{
   // Return the number of affected rows as an integer
   return strval(str);
}
}

public PSAtimer() // PSA timer for reading things.
{
// Declare some variables
new cont[2048], str[256], File: filex, index;

// Check if we have SQL output
if(fexist("sql_output.psa"))
{
// Open a pointer to the SQL output
filex = fopen("sql_output.psa");

// The index is 1.
index = 1;

// Read the output
while(fread(filex,cont,sizeof(cont)))
{
   if(strlen(cont))
   {
// Keep reading until we hit the end of the file
while(strfind(cont,"\n")>0)
{
strmid(str,cont,0,strfind(cont,"\n"));
strdel(cont,0,strfind(cont,"\n"));
}

// Call the "OnQueryResult" event
OnQueryResult(str,index);

// Increment the index
index++;
}
}

// Close the pointer
fclose(filex);

// Delete the files for a fresh start next time
fremove("sql_input.psa");
fremove("sql_output.psa");
}
}
stock split(const strsrc[], strdest[][], delimiter)
{
new k, li;
new aNum;
new len;
while(k <= strlen(strsrc))
{
if(strsrc[k] == delimiter || k == strlen(strsrc))
{
len = strmid(strdest[aNum], strsrc, li, k, 128);
strdest[aNum][len] = 0;
li = k+1;
aNum++;
}
k++;
}
return 1;
}


To-Do

  • Implement IRC functions.
  • Convert to SQLite? (Discuss in thread)

Feedback
Suggestions, questions, and bug reports should be posted in this thread.

Version History
Revision 1

  • PSA rewritten in PHP for cross-compatibility
  • Pawn functions written for easy SELECT querying