Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Money event.. uhm?

Tadao Nordenskiold
Registered User
Join date: 9 May 2009
Posts: 37
10-20-2009 18:38
I'm not sure what to set as title on this thread..

I'm having a problem...

I got two scripts,

The first one checks agains an MySQL database for two values, "cost" and "credits".
Th script then calculates cost - credits = total cost and sends the rsult, among some other data to be processed in another scipt.

What I need to hapend here, is:

if ( total_cost == 0)
{
Extend_no_pay();
}
else
{
Pay_then_extend();
}

If it goes to Extend_no_pay(), it should then send an message to the next script, and not continue with this script. I first thought llDie(); could help me with that, but fortunately I checked it first and saw that it will delete my object if I do that. Glad I didn't. :)

How ever,
if it goes to Pay_then_extend, I need to trigger the money event so that the user can pay the machine.
When the machine has been payed, it will go to the next event, which is another llHTTPRequest(), and then send message to the third script (the same as above).

I have the script working now, except for the if function to cech if Total_Cost is 0 or not.

The full script right now looks like this:

CODE


key customer_key;

integer Correct_amount;

string DO_EXP_ADD;

string server = "http://server address/";
string auth = "Auth Key";

list extend_msg;
string msg;
string Owner;
integer Portbase;
integer Cost;
integer Credits;
string Expire;
integer Total_Cost;
string Update_Friend_Credit;
string Update_Own_Credit;
key UUID;
key DO_F_CRED_ADD;
key DO_O_CRED_ADD;

key Tadao = "05c9fe42-8d89-401d-a9a5-2d82af58e16f";

default
{
state_entry()
{
llRequestPermissions(llGetOwner(), PERMISSION_DEBIT);
}

link_message(integer sender_num, integer num, string extend_msg_str, key id)
{
extend_msg = llParseString2List(extend_msg_str, ["|"], []);
msg = llList2String(extend_msg,0);
if ( msg == "extend_a_friend" )
{
Owner = llList2String(extend_msg,1);
Cost = llList2Integer(extend_msg,2);
Credits = llList2Integer(extend_msg,3);
UUID = llList2Key(extend_msg,4);
Expire = llList2String(extend_msg,5);
Pbase = llList2Integer(extend_msg,6);
customer_key = llList2Key(extend_msg,7);
Total_Cost = Cost - Credits;

llInstantMessage(customer_key, "The monthly cost for " +Owner +"'s "
+"services is: $L" +(string)Cost
+" /30 days.\n"
+"The account is already credited with: " +(string)Credits
+" Lindens.\n"
+"The services expires at: " +Expire +"\n"
+"In order to update the services with another 30 days, please pay: $L" +(string)Total_Cost);
Correct_amount = Total_Cost;

Update_Friend_Credit = "?auth=" +auth
+"&action=creditupdate"
+"&uuid=" +(string)UUID
+"&credit="+(string)Correct_amount;
}
else if ( msg == "extend_your_own" )
{
Owner = llList2String(extend_msg,1);
Cost = llList2Integer(extend_msg,2);
Credits = llList2Integer(extend_msg,3);
Pbase = llList2Integer(extend_msg,4);
Expire = llList2String(extend_msg,5);
UUID = llList2Key(extend_msg,6);
Total_Cost = Cost - Credits;

llInstantMessage(UUID, "The monthly cost for " +Owner +"'s "
+"services is: $L" +(string)Cost
+" /30 days.\n"
+"The account is already credited with: " +(string)Credits
+" Lindens.\n"
+"The services expires at: " +Expire +"\n"
+"In order to update the services with another 30 days, please pay: $L" +(string)Total_Cost);
Correct_amount = Total_Cost;

Update_Own_Credit = "?auth=" +auth
+"&action=creditupdate"
+"&uuid=" +(string)UUID
+"&credit="+(string)Correct_amount;
}
}

money(key id, integer amount)
{
customer_key = id;
// If user pays correct amount, Add the money to users account
if (amount == Correct_amount)
{
if ( msg == "extend_a_friend" )
{
DO_F_CRED_ADD = llHTTPRequest(server + "api.php" + Update_Friend_Credit, [ HTTP_METHOD, "GET"], "");
}
else if ( msg == "extend_your_own" )
{
DO_O_CRED_ADD = llHTTPRequest(server +"api.php" +Update_Own_Credit, [ HTTP_METHOD, "GET"], "");
}
}
// If user pays less than total amount, pay back, and tell him/her to re-do the process.
else if ( amount < Correct_amount )
{
llInstantMessage(customer_key, "Sorry, you didn't pay the right amount. You need to pay exactly L$" +(string)Total_Cost
+"in order to update the expiry date."
+"\nYour money will be paid back shortly!");
llGiveMoney(id, amount);
}
// And in case they pay too much!
else
{
integer refund = amount - Correct_amount;
llInstantMessage(customer_key, "You paid too much. You will be refunded with L$ "
+(string)refund +"shortly.");
llGiveMoney(id, refund);
if ( msg == "extend_a_friend" )
{
DO_F_CRED_ADD = llHTTPRequest(server + "api.php" + Update_Friend_Credit, [ HTTP_METHOD, "GET"], "");
}
else if ( msg == "extend_your_own" )
{
DO_O_CRED_ADD = llHTTPRequest(server +"api.php" +Update_Own_Credit, [ HTTP_METHOD, "GET"], "");
}
}
}

http_response(key request_id, integer status, list metadata, string body)
{
if ( request_id == DO_F_CRED_ADD )
{
list HTTPReply_Upd_Cred = llParseString2List(body, ["|"], []);
string status = llList2String(HTTPReply_Upd_Cred,0);
string err_msg = llList2String(HTTPReply_Upd_Cred,1);

if ( status == "status=s" )
{
string Send_Upd_Exp_String = "?auth=" +auth
+"&action=updateexpiry"
+"&Pbase=" +(string)Pbase
+"&expiry_add=1month";

llMessageLinked(LINK_ROOT, 0, "DO_UPDATE_F|" +(string)UUID +"|" +(string)customer_key +"|" +Send_Upd_Exp_String, NULL_KEY);

}
else if ( status == "status=f" )
{
llInstantMessage(customer_key, "Something went wrong, error message given was: " +err_msg +"\n"
+"Please contact Tadao Nordenskiold ( secondlife:///app/agent/" +(string)Tadao +"/about ) for help!\n"
+" Your money will be returned shortly!");
llInstantMessage(Tadao, "Something went wrong when user " +llKey2Name(customer_key)
+" tried to update his/her or his/her friends services using method " +msg +".\n"
+"Please contact them using this link! ( secondlife:///app/agent/" +(string)customer_key +"/about ).\n"
+"The error message given was: " +err_msg);
}
}
if ( request_id == DO_O_CRED_ADD )
{
list HTTPReply_Upd_Cred = llParseString2List(body, ["|"], []);
string status = llList2String(HTTPReply_Upd_Cred,0);
string err_msg = llList2String(HTTPReply_Upd_Cred,1);

if ( status == "status=s" )
{
string Send_Upd_Exp_String = "?auth=" +auth
+"&action=updateexpiry"
+"&Pbase=" +(string)Pbase
+"&expiry_add=1month";

llMessageLinked(LINK_ROOT, 0, "DO_UPDATE_O|" +(string)UUID +"|SCRAP|" +Send_Upd_Exp_String, NULL_KEY);
}
else if ( status == "status=f" )
{
llInstantMessage(UUID, "Something went wrong, error message given was: " +err_msg +"\n"
+"Please contact Tadao Nordenskiold ( secondlife:///app/agent/" +(string)Tadao +"/about ) for help!\n"
+" Your money will be returned shortly!");
llInstantMessage(Tadao, "Something went wrong when user " +llKey2Name(customer_key)
+" tried to update his or her friends services using method " +msg +".\n"
+"Please contact them using this link! ( secondlife:///app/agent/" +(string)customer_key +"/about ).\n"
+"The error message given was: " +err_msg);
}
}
}
}

Tadao Nordenskiold
Registered User
Join date: 9 May 2009
Posts: 37
10-20-2009 18:54
As an update, I can say, I tried to put the money and HTTPRequest events inside the Pay_then_extend(), but I get errors when doing so... :/
Ron Khondji
Entirely unlike.
Join date: 6 Jan 2007
Posts: 224
States
10-20-2009 22:20
This is where the use of states is needed.

Like this:

state default
get debit perms
if perms goto 'state active'

state active
check the database and do calculations
if total_cost = 0 goto 'state pay'
else goto 'state nopay'

etc.

Only 'state' pay has a money event. The user cannot accidentaly pay the object this way.
Just stay in this state until the money is paid.
Then go back to 'state active'.

Or something like that.
Tadao Nordenskiold
Registered User
Join date: 9 May 2009
Posts: 37
10-21-2009 18:31
THank you. I didn't know how to work it out with selfmade states...
byt... let me ask this.

I know that one script can only handle one HTTPRequest properly, unless they are within if statements.
But hos is it with different states?
Can one state handle one HTTPRequest each? Or do I still have to send messages between scripts to have several HTTPRequests?

Edit:

Removed question under edit, since I managed to find and fix the answer myself. Now it's just the above question I'm looking for an answer to. Can't find any good answers anywhere. :/
Ron Khondji
Entirely unlike.
Join date: 6 Jan 2007
Posts: 224
10-22-2009 09:35
Every state can have it's own llHttpRequest() and http_responce.
You just have to make sure the key in the responce is the same as the key in the request.

If you change states in the responce event, then the responce from the server should not be from a request in another state. It's best to check the keys anyway though.
Tadao Nordenskiold
Registered User
Join date: 9 May 2009
Posts: 37
10-22-2009 11:12
I'm not really sure I understand how you mean..

Could I just ask you for an example?
Ron Khondji
Entirely unlike.
Join date: 6 Jan 2007
Posts: 224
10-23-2009 03:17
I was thinking something like this:

CODE

key request_1;
key request_2;
key request_3;


default
{
state_entry()
{
llSetClickAction( CLICK_ACTION_NONE );
llSetTimerEvent( 10.0 );
}

timer()
{
// Get some data
request_1 = llHTTPRequest( URL, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], DataRequest );
}

http_response( key request_id, integer status, list metadata, string body )
{
if ( request_id == request_1 ) {
if( status != 200 ) llOwnerSay("An unknown error occured.");
else if( body == "" ) llOwnerSay("Empty body! Check PHP script.");

else if ( body == "OK" ) state OK; // script will only enter state OK after it gets "OK" back from the server.
}
}
}

state OK
{
state_entry()
{
llSetTimerEvent( 0.0 ); // timer survives state changes

// Get some MORE data
request_2 = llHTTPRequest( URL, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], MoreDataRequest );
}

http_response( key request_id, integer status, list metadata, string body )
{
if ( request_id == request_2 ) {
if ( status != 200 ) llOwnerSay("An unknown error occured.");
else if ( body == "" ) llOwnerSay("Empty body! Check PHP script.");

else if ( body == "FUNDS" ) state FUNDS;
else if ( body == "NO_FUNDS" ) state NO_FUNDS;
}
}
}

state FUNDS
{
state_entry()
{
// Get even more data
request_3 = llHTTPRequest( URL, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], EvenMoreDataRequest );
}

http_response( key request_id, integer status, list metadata, string body )
{
if ( request_id == request_3 ) {
if( status != 200 ) llOwnerSay("An unknown error occured.");
else if( body == "" ) llOwnerSay("Empty body! Check PHP script.");

else {
integer cash = (integer)body;
llSetTimerEvent( 60.0 );
}
}
}

timer()
{
cash -= 1;
if ( cash == 0 ) state default;
}
}

state NO_FUNDS
{
state_entry()
{
llSetClickAction( CLICK_ACTION_PAY );
}

money(key giver, integer amount)
{
if ( amount == enough ) state default;
}
}


This maybe a bit too much but I think it demonstrates what I was trying to say.