The online racing simulator
[Solved] Retrieve UserNames - storedvalue.dbs
As the title says,
Problem:
I need to distribute a large sum of money (which occurs on a regular interval) for all players (users) in my server whether they are online or offline. One of the problems is that all the users' and server data are stored in storedvalue.dbs (located in "./bin/default/Databases" ).

GetListOfPlayers() - only returns list of all players online ...
GetListOfUsersGroup - doesn't work for storedvalue.dbs ...

Is there a way to retrieve all usernames of players in storedvalue.dbs so I can #do something# with their stored data?

e.g.: (This is what I'm trying to do)
Sub < this sub >
$mList = [b]< Get List of All Players' "UserNames" from storedvalue.dbs >[/b] ;
FOREACH ( $maVar IN $mList )
IF ( <they are online, their nickname != "" > )
THEN
# Distribute moneys #
SetPlayerVar( $maVar["value"] , "<var>" , <change> );
ELSE
# Rest aren't online, so distribute moneys to remaining (offline) players too #
SetUserStoredValue( $maVar["value"] , "<var>" , <change> );
ENDIF
ENDFOREACH
EndSub

Note: No, I am not asking for a full script lol, I just need a function that can draw all usernames from the current storedvalue.dbs database.
The answer to this "Is there a way to retrieve all usernames of players in storedvalue.dbs so I can #do something# with their stored data?" is no, you cannot do such thing by default.

If you want to do that, you need a storedvalue that has a fixed name and is raised every time by a number like "money_wallet_1" , "money_wallet_2" etc, while you also have a stored_value that keeps track of the latest number that has been used.
Those two combined, gives you the opportunity to retrieve all the values and do something with it.
I used a similar construction when I build by Register system.
Problem 2: This is the code so far, but it keeps registering the same user (me) every !reload. The "REGISTER_COUNTER" also increases by 1 as a consequence too. And yes, some of it is from your reg_system.txt.

IF ( GetStoredValue( "REGISTER_COUNTER" ) == "" )
THEN
SetStoredValue( "REGISTER_COUNTER" , ToNum("2") );
ENDIF
$register_counter = ToNum( GetStoredValue( "REGISTER_COUNTER" ) );
IF ($register_counter == "")
THEN
$register_counter=2;
ENDIF

FOR ( $i = 1; $i < $register_counter; $i = $i + 1 ) ### Set $i as 1 unit under $register_counter. ###
IF ( ToString(GetStoredValue( "UserName_Number_" . $i )) == ToString(GetPlayerVar( $userName , "UserName" )) )
THEN
GlobalMsg( "User already registered on system" );
GlobalMsg( "^7User ID: ^6" . $i );
writeline( "User already registered on system" );
$i = $i + $register_counter;
ELSE
IF (( GetStoredValue( "UserName_Number_" . $i ) != "" ) && ( GetStoredValue( "UserName_Number_" . $i ) != "0" ))
THEN
GlobalMsg( "^7Stored User/ID " . GetStoredValue( "UserName_Number_" . $i ) . "/" . $i . "." );
ELSE
IF ( ( GetStoredValue( "UserName_Number_" . $i ) == "" ) || ( GetStoredValue( "UserName_Number_" . $i ) == "0" ) )
THEN
SetStoredValue( "UserName_Number_" . ToString( $register_counter ) , $userName );
SetStoredValue( "REGISTER_COUNTER" , ToNum( GetStoredValue( "REGISTER_COUNTER" ) + 1 ) );
GlobalMsg( "^7New User (^6" . $userName . "^7) has been registered." );
GlobalMsg( "^7New User ID: ^6" . ToNum( $register_counter ) );
writeline( "^7New User (^6" . $userName . "^7) has been registered." );
ENDIF
ENDIF
ENDIF
ENDFOR

First of all I don't understand why you would start a counter with value 2, but maybe you have a good reason for that.
Then you have this: ToString(GetPlayerVar( $userName , "UserName" ))

Where does $userName come from?
As I do not see that anywhere in this piece of script.
Would it be possible to put up the whole script here?
Otherwise feel free to send me a PB with the whole script or to ask for my email to send it there.
Sorry I forgot to mention it, but it actually starts with:

[b]CatchEvent OnConnect ( $userName )[/b]
....does some stuff....
IF ( GetStoredValue( "REGISTER_COUNTER" ) == "" )
... # see my previous post for whole section of relevant code # ....
ENDFOR
... some other stuff ...
EndCatchEvent

I haven't altered anything except added those lines of code (see previous reply) to my Event OnConnect. I think those pieces of code are enough, it's just that the algorithm seems to be wrong and I'm having trouble applying the right algorithm (just can't wrap my head around it Confused ) so it only registers unique usernames. As far as error logs go it doesn't say anything's wrong (from the ERR.log file in bin/default/logs).

Also $userName is already declared in the beginning, and should not be the problem as it returns my username when registered (it basically registers me as a new user every reload Shrug )

Regarding starting with 2:
I had trouble with it starting at $i = 0 so I just moved up 1. The problem isn't where it starts but how it executes. I think the main problem might be the parameters, or the order of the IF statements...

I also manually set the stored-values "REGISTER_COUNTER" and "UserName_Number_" . $i
if I find duplicates after connecting/reloading.

CONTEXT: (If it helps...)
I need a script that runs at 5pm, everyday, that distributes the player's dividend (gives them some money from the total profit) relative to their shareholding from the companies they have partial ownership of, even when they're offline. This is where listing all usernames from storedvalue.dbs comes in:
(All in a WHILE Loop with $i = 1 and $register_counter = "REGISTER_COUNTER")
IF ( GetStoredValue( "UserName_Number_" . $i ) != "" )
THEN
IF ( GetPlayerVar( GetStoredValue( "UserName_Number_" . $i ), "NickName" ) == "" ) # Offline
THEN
# distribute dividend by set/getuserstoredvalue. #
ELSE
IF ( GetPlayerVar( GetStoredValue( "UserName_Number_" . $i ), "NickName" ) != "" ) # Online
THEN # distribute dividend normally by set/getplayervar. #
ENDIF
ENDIF
ENDIF

If they're offline, I can send them a mail by a:

IF ( GetUserStoredValue("HAVEMAIL") == 1 ) THEN PrivMsg GetStoredValue("MAIL_" . $userName)

if they do receive dividends while offline. These investors have every right to know how much dividend they receive at 5pm, even if they come online at 6pm.
This is working like it should:


CatchEvent OnLapperStart()
OnLapperStart_Register_ID();
EndCatchEvent

CatchEvent OnConnect( $userName )
OnConnect_Register_ID();
EndCatchEvent

Sub OnLapperStart_Register_ID()
### Declare global variables ###
GlobalVar $register_counter;
### End ###

### Load stored values and give them a default setting if no stored value has been found ###
$register_counter = GetStoredValue( "REGISTER_COUNTER" );

IF ($register_counter == "")
THEN
$register_counter=0;
ELSE
$register_counter = ToNum( GetStoredValue( "REGISTER_COUNTER" ) );
ENDIF
### End ###
EndSub

Sub OnConnect_Register_ID()
### Retrieve the username ###
$userName = GetCurrentPlayerVar( "UserName");
### End ###

### Set $user_found to 'no' ###
$user_found = "no";
### End ###

### Check if $userName is NOT empty ###
IF ( $userName != "" )
THEN
### Loop through already registered user accounts, looking for the current username ###
FOR ( $i = 0; $i < $register_counter; $i = $i + 1 )
IF ( GetStoredValue( "UserName_Number_" . $i ) == $userName )
THEN
GlobalMsg( "User already registered on system" );
GlobalMsg( "^7User ID: ^6" . $i );
writeline( "User already registered on system" );
$user_found = "yes";
BREAK;
ENDIF
ENDFOR
### End ###

### If username hasn't been found in registered users, register this user, raise $register_counter by 1 and write new value for REGISTER_COUNTER to the database ###
IF ( $user_found == "no" )
THEN
SetStoredValue( "UserName_Number_" . ToString( $register_counter ) , $userName );
$register_counter = $register_counter+1;
SetStoredValue( "REGISTER_COUNTER" , $register_counter );

GlobalMsg( "^7New User (^6" . $userName . "^7) has been registered." );
GlobalMsg( "^7New User ID: ^6" . ToNum( $register_counter ) );
writeline( "^7New User (^6" . $userName . "^7) has been registered." );
ENDIF
### End ###
ENDIF
### End ###
EndSub

Thanks a lot Heart , it's working flawlessly! Big grin

Now time for me to write the dividend script cuz rip some people missed out on the money today Frown
Attached images
Yisc_Registration_ID_System.jpg
New Problem:
## This part of the code is within a FOR loop, algorithm works, SetUserStoredValue doesn't.
## $IDUser = GetStoredValue( "UserName_Number_" . $i ) ## from FOR loop ##
writeline( "attempting to give user " . $IDUser . " cash from " . GetUserStoredValue( $IDUser, "Cash" ) );
SetUserStoredValue( $IDUser , "Cash", Round(ToNum( GetUserStoredValue( $IDUser, "Cash" ) + $Dividend ),2) );
writeline( "attempted to give user " . $IDUser . " cash to " . GetUserStoredValue( $IDUser, "Cash" ) );

SetUserStoredValue cannot be used on an offline user...If you can GET why shouldn't you be able to SET? For the record GetUserStoredValue works...

I've tried it with other stored values other than "Cash" too, but it just can't Set stored values for any offline users.

Does this mean I have to resort to using SetStoredValue( "UserName_Cash_" . $IDUser ) instead?
** Best answer **
There is no reason why SetUserStoredValue should not work when a player is not online.
After all, the only thing Lapper does, is adjusting values in a database, and therefor the players aren't needed.
I quickly extended my script, and as far as I can tell (I looked live into my database) this is working as intended:


CatchEvent OnLapperStart()
OnLapperStart_Register_ID();
EndCatchEvent

CatchEvent OnMSO( $userName, $text ) # Player event
$idxOfFirtsSpace = indexOf( $text, " ");

$userName = GetCurrentPlayerVar("UserName");

UserGroupFromFile( "admin", "./../AB-Configs/admin.txt" );

IF( $idxOfFirtsSpace == -1 ) THEN
$command = $text;
$argv = "";
ELSE
$command = subStr( $text,0,$idxOfFirtsSpace );
$argv = trim( subStr( $text,$idxOfFirtsSpace ) );
ENDIF

SWITCH( $command )
CASE "!pay_divident":
CASE "!pd":

IF( UserInGroup( "admin", $userName ) == 1 )
THEN
Pay_divident( $KeyFlags );
ELSE
PrivMsg( "^1You are not allowed to do that");
ENDIF
BREAK;
ENDSWITCH
EndCatchEvent

CatchEvent OnConnect( $userName )
OnConnect_Register_ID();
EndCatchEvent

Sub OnLapperStart_Register_ID()
### Declare global variables ###
GlobalVar $register_counter;
### End ###

### Load stored values and give them a default setting if no stored value has been found ###
$register_counter = GetStoredValue( "REGISTER_COUNTER" );

IF ($register_counter == "")
THEN
$register_counter=0;
ELSE
$register_counter = ToNum( GetStoredValue( "REGISTER_COUNTER" ) );
ENDIF
### End ###
EndSub

Sub OnConnect_Register_ID()
### Retrieve the username ###
$userName = GetCurrentPlayerVar( "UserName");
### End ###

### Set $user_found to 'no' ###
$user_found = "no";
### End ###

### Check if $userName is NOT empty ###
IF ( $userName != "" )
THEN
### Loop through already registered user accounts, looking for the current username ###
FOR ( $i = 0; $i < $register_counter; $i = $i + 1 )
IF ( GetStoredValue( "UserName_Number_" . $i ) == $userName )
THEN
GlobalMsg( "User already registered on system" );
GlobalMsg( "^7User ID: ^6" . $i );
writeline( "User already registered on system" );
$user_found = "yes";
BREAK;
ENDIF
ENDFOR
### End ###

### If username hasn't been found in registered users, register this user, raise $register_counter by 1 and write new value for REGISTER_COUNTER to the database ###
IF ( $user_found == "no" )
THEN
SetStoredValue( "UserName_Number_" . ToString( $register_counter ) , $userName );
SetUserStoredValue( $userName , "Cash" , 0 );

$register_counter = $register_counter+1;
SetStoredValue( "REGISTER_COUNTER" , $register_counter );

GlobalMsg( "^7New User (^6" . $userName . "^7) has been registered." );
GlobalMsg( "^7New User ID: ^6" . ToNum( $register_counter ) );
writeline( "^7New User (^6" . $userName . "^7) has been registered." );
ENDIF
### End ###
ENDIF
### End ###
EndSub

Sub Pay_divident( $KeyFlags )
$Divident = 50;

### Loop through already registered user accounts, looking for the current username ###
FOR ( $i = 0; $i < $register_counter; $i = $i + 1 )
$IDUser = GetStoredValue( "UserName_Number_" . $i );
$cash = GetUserStoredValue( $IDUser, "Cash" );
$new_cash = ToNum ( Round( $cash + $Divident,2 ) );

writeline( "attempting to give user " . $IDUser . " cash from " . GetUserStoredValue( $IDUser, "Cash" ) );

SetUserStoredValue( $IDUser , "Cash", $new_cash );

writeline( "attempted to give user " . $IDUser . " cash to " . GetUserStoredValue( $IDUser, "Cash" ) );
ENDFOR
### End ###
EndSub

Hey, sorry for the late reply I just got back to this post. I'm using global Set/GetStoredValue( "UserName_Cash_" . $userName ) as a 'reserve' so when player's connect it goes into their wallet. On the plus side I can also use it to store mail for when they come online.

Nevertheless thank you very much for the script, I will be sure to use it when I need to change UserStoredValue(s) again, and I'm sure it'll be very helpful for other users to implement into their server, as it has done wonders for mine.

I know I can't always depend on global stored values as the table has to make a new row every time I name a new variable. I'm planning on scripting player contracts, memberships and insurance soon and I can't imagine how many more variables I have to name.
Let me give you a very value tip then (well, I think it is), you can use one variable and store a lot of information into it, as long as you have a separator to split the information.
See screenshot below, as an example what I did with all Pitboard settings.
Attached images
Capture.PNG
This thread is closed

[Solved] Retrieve UserNames - storedvalue.dbs
(11 posts, closed, started )
FGED GREDG RDFGDR GSFDG