Optimization of code based on dates
#1

Hi! I've recently made a /ban command with the parameters "time" and "period". Times is an integer, and period is either "minutes", "hours", "days", "months", "years".
If you do the ban command with the time 4 and the period months, the ban-function will get the time and date, then advance it by 4 months (leap years are calculated, and days in each month is also correctly working), then save that date to a ban-file (using y_ini) with the player's name as a tag.

Now, onto my question and purpose of this thread:

Is there any way to optimize/improve my "conversion"-code?

I feel like I do unnecessary lots of calculations and that my code is super inefficient, but I'm not a hundred percent sure.
Please take a look if you think you can help: http://pastebin.com/ayW73Fka

Edit: There are some places in my script where tabs are written as spaces - by accident - so the code seems unindented, while in reality it is actually correctly indented. Just wanted you to know, hah.

Edit 2: Here's the command and the ban-check on connect too - just in case they also can be improved.

Command: http://pastebin.com/4eFwAa70

OnPlayerConnect: http://pastebin.com/BJNRUuEk
Reply
#2

I would suggest to use sqlite or mysql for own ban system
Reply
#3

Quote:
Originally Posted by Jefff
Посмотреть сообщение
I would suggest to use sqlite or mysql for own ban system
I made it just for fun - I have no desire nor intention of using this system at all. I'm asking for help to optimize the calculation, not the system.
Reply
#4

So time is basically made of minutes, correct? And you use a loop to decrease minutes?
edit:
minutes/hours etc.
Reply
#5

Quote:
Originally Posted by maramizo
Посмотреть сообщение
So time is basically made of minutes, correct? And you use a loop to decrease minutes?
edit:
minutes/hours etc.
Time is basically made out of days, actually. The only thing alternating is days. 60 minutes are always an hour, 24 hours are always a day, 12 months are always a year, but a month can be both 28, 29, 30 and 31 days.

I loop the value I add to - if I ban someone for 20 days, I'll run a loop on the current day + 20. If I ban someone for 20 months, I'll run a loop on the current month + 20. All this to adjust the higher values.

For minutes, I have to adjust hours, days, months and years.
For hours, I have to adjust days, months and years.
For days, I have to adjust months and years.
And lastly for months, I have to adjust years and sometimes days (because of the alternating days in a month + leap years).
Reply
#6

Well yes, you're being extremely accurate, thus your code is perfectly correct and clean.
An idea like:
pawn Код:
if(!strcmp(period, "second",true))
        {
            second = time;
        }
        if(!strcmp(period, "minute", true))
        {
            second = time/60;
        }
        if(!strcmp(period, "hour", true))
        {
            second = time/60;
            second = second/60;
        }
        if(!strcmp(period, "day", true))
        {
            second = time/60;
            second = second/60;
            second = second/24;
        }
        if(!strcmp(period, "week", true))
        {
            second = time/60;
            second = second/60;
            second = second/24;
            second = second/7;
        }
        if(!strcmp(period, "month", true))
        {
            second = time/60;
            second = second/60;
            second = second/24;
            second = second/7;
            second = second/30; //Here is the inaccuracy that you lose, the few days difference in the 30 to 31 month range.
        }
        if(!strcmp(period, "year", true))
        {
            second = time/60;
            second = second/60;
            second = second/24;
            second = second/30;
            second = second/12;
        }
Is inaccurate, but I'm sure you've already thought of that.
Reply
#7

Quote:
Originally Posted by maramizo
Посмотреть сообщение
your code is perfectly correct and clean.
I highly doubt it, but I guess I'll take that as a compliment?

Quote:
Originally Posted by maramizo
Посмотреть сообщение
An idea like:
pawn Код:
//code
Is inaccurate, but I'm sure you've already thought of that.
HIGHLY inaccurate. Leap years and days/month would indeed ruin that calculation completely.
Reply
#8

Quote:
Originally Posted by LarzI
Посмотреть сообщение
HIGHLY inaccurate. Leap years and days/month would indeed ruin that calculation completely.
The GetDate stock returns the days from the start of the year, if you could get a stock similar that could return the days from a specific date, your problem would be fixed.
Reply
#9

Quote:
Originally Posted by maramizo
Посмотреть сообщение
The GetDate stock returns the days from the start of the year, if you could get a stock similar that could return the days from a specific date, your problem would be fixed.
I'm not sure what you meant by this.

---

Anyway, I hope a "professional" scripter (e.g. ******) will review my calculations and rest of the code and help me optimize/improve it.
Reply
#10

When you use:
pawn Код:
new variable = GetDate();
It returns the days since the start of the current year in the date.
If you could get the GetDate stock, you could edit it to return days since a certain date that you choose in a parameter i.e GetDateEx(10,12,1941), multiply the days by 24, then 60 to get the minutes, then compare those on player login to the time in the ban parameter, and see if they're larger or smaller, if larger than player is unbanned, if smaller then player stays banned.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)