AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help with, I think, a math problem (https://forums.alliedmods.net/showthread.php?t=25510)

Willmaker 03-15-2006 10:56

Help with, I think, a math problem
 
I have the following code:

Code:
if(something == 10){     //code } if(something == 20){     //code } if(something == 30){     //code }

I want it so that it will last near infinity. I dont want to keep adding a snippet of code every 10 as I will have quite a lot of code.

I was wondering if there was a way so that it could check every 10 until near infinity, without using so much code.

Sandurr 03-15-2006 11:21

Code:
switch(something) {          case 10:                 // blabla          case 20:                 // bla          case 30:                 // -bla }

How ever this doesn't save much code neither :p

What are you trying to make? Might be easier for us to simplify our code

Willmaker 03-15-2006 11:25

Ive got a variable which goes up as time passes, and I wanted to execute some code whenever it reached certain intervals, ie every 10. Also, I wanted it to announce like, if it reached 20, it would say You have reached 20. I just didnt want to repeat the same basic code snippet 100's or 1000's of times.

Brad 03-15-2006 11:38

just divide the count by 10 and make sure there's no remainder.

if you want to announce a message every nth iteration, divide by that number.

MaximusBrood 03-15-2006 13:50

It's called modulus, the remainder of a fraction.
In this case it checks if there isn't a remainder, so you know it is a multiplication of 10

Code:
//Could be: if( !(something % 10) ) //But I dont know if small counts a 0 as false as C does if( (something % 10) == 0 ) {     client_print(id, print_chat, "You have reached %d", something) }

Willmaker 03-15-2006 20:19

Thanks Maximus, that looks like what I needed. Ill try it out later.

All I need to know now is, how do I check this value when it reaches 10, 20. I tried client_prethink, but it executed the code at least 4 times, when I only wanted it done once. I also tried a repeating set_task to check every 0.9 seconds, but for some reason that didnt work.
In the following code, the time thats printed is always 0. So what am I doing wrong?

Code:
new time[33] public client_authorized(id){       get_user_authid(id,authid,31)     res = dbi_query(mysql,"SELECT time FROM mytime WHERE steamid = '%s'",authid)     if (res == RESULT_FAILED){ //Problem with mysql         log_amx("SQL connection failed with authorized dbi_query")         return PLUGIN_HANDLED     }     if (res == RESULT_NONE){ //not in db, set to 0         time[id] = 0 //New player, create a database entry for them         log_amx("res = RESULT_NONE, time = 0")         new playtime = get_user_time (id)         resss = dbi_query(mysql,"INSERT INTO mytime (steamid, time,date) values ('%s',%i,NOW())",authid,playtime)         dbi_free_result(resss)     }     if (res == RESULT_OK){ //get totaltime from database         dbi_nextrow(res)         time[id] = dbi_result(res,"time")         log_amx(" time > 0")     }     return PLUGIN_CONTINUE } public check_time(id){     new name[18]     get_user_name(id, name, 17)     new playtime = get_user_time(id)     store_time = (playtime + time[id]) //Get total time     client_print(id, print_chat,"time = %i",time[id])     if(store_time == 10){         client_print(0, print_center,"Please welcome %s to the server. May your time here be enjoyable.",name)         client_cmd(0, "spk gargr/congrats.wav")     }     return PLUGIN_CONTINUE }

slmclarengt 03-15-2006 22:14

With the exception of my one computer science class that taught me some SQL, SQL is not my strongpoint. However,

The only thing I see that seems to be of issue is that I don't know if your table "time" has been initialized in the SQL database. It seems that would cause problems.

Willmaker 03-16-2006 19:36

Yes, I do initialize it. I just didnt include that code.

Code:
public sql_init() {     new host[64], username[32], password[32], dbname[32], error[128]     get_cvar_string("amx_sql_host",host,64)     get_cvar_string("amx_sql_user",username,32)     get_cvar_string("amx_sql_pass",password,32)     get_cvar_string("amx_sql_db",dbname,32)     mysql = dbi_connect(host,username,password,dbname,error,128)     if (mysql == SQL_FAILED){         log_amx("SQL connection failed")         server_print("MySQL error : could not connect : '%s'",error)     }else{         dbi_query(mysql,"CREATE TABLE IF NOT EXISTS `mytime` (`steamid` VARCHAR(32) NOT NULL,`time` INT NOT NULL,`date` TIMESTAMP, PRIMARY KEY(`steamid`))")     } }

I know its connecting, as the correct time is added to the database. No errors at all.

slurpycof 03-17-2006 16:04

Code:
public check_time(id){     new name[18]     get_user_name(id, name, 17)     new playtime = get_user_time(id)     store_time = (playtime + time[id]) //Get total time     client_print(id, print_chat,"time = %i",time[id])     if(store_time == 10){         client_print(0, print_center,"Please welcome %s to the server. May your time here be enjoyable.",name)         client_cmd(0, "spk gargr/congrats.wav")     }     return PLUGIN_CONTINUE }

Shouldn't you use new?
Code:
  new store_time = (playtime + time[id]) //Get total time

Don't you want to print their total time?
Code:
client_print(id, print_chat,"time = %i", store_time)

If you are planning on releasing this I would suggest using nvault instead. I have found there are very few people that have or understand mysql and most that do do not have it locally on their game server and it causes lag issues.



***edit***
Do you also know that the time you are storing is in seconds and that number can get huge and make no sense? Here is a sugegstion
Code:
    new store_time, days = 0, temp_hours = 0, hours = 0, temp_minutes = 0, minutes = 0     store_time = (playtime + time[id]) //Get total time     days = store_time/86400 //number of days     temp_hours = store_time%86400     hours = temp_hours/3600 //number of hours     temp_minutes = temp_hours%3600     minutes = temp_minutes/60 //number of minutes     client_print(id,print_chat,"Your time =  %d Days %d Hours and %d minutes",days,hours,minutes)

Using a peice of that code you could check by minutes/hours/days to see if it is divisable by 10 and do something if it is.

Willmaker 03-17-2006 19:13

Maybe I should have posted the whole code.

I have store_time as a global variable, so I dont need the 'new' part. Also with the client_print time, I just put that there for testing, to see if it was actually returning something other than 0. Im going to remove it when everything is working.

And yes, I know they are seconds. Thanks anyway, I just need some way of checking when they reach certain times, as for some reason, time[id] returns 0, even though I know the database has a time value in it for my steamid.


All times are GMT -4. The time now is 20:14.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.