Jump to content
Eternal Lands Official Forums
Nintenduh

#calc command

Recommended Posts

I think a calc command would kick @$$ :) Usually I run in window mode but when I'm in fullscreen this would be rather usefull.

 

Examples

 

#calc 100+25

The gods returned the number 125

 

#calc 100*25

The gods returned the number 2500

 

#calc 100/25

The gods returned the number 4

Share this post


Link to post
Share on other sites
linux has a calc funtion u can use... just pm him with #calc [math equation]

and that's it :D

The syntax is: /Linux calc equation.

 

e.g. /Linux calc 100/75

 

I'd wait for Vegar's patch link, its worth it ;)

Share this post


Link to post
Share on other sites

Does it still do this?

 

[PM to linux: calc 2+2+2]

[PM from linux: 4]

 

Try Minai's calc funtion if it's more complex, if she's online (not that often unfortunately).

Share this post


Link to post
Share on other sites

Linux calculates "simple expressions" it means she can do only one operation on max two

arguments at once, therefore all arguments after first two value and first operations will be ignored.

Seems quite sufficient for EL needs and no one was complaining, she gets daily like 400+ calc requests...

 

Regards.

Edited by Platyna

Share this post


Link to post
Share on other sites

O, come on plat.

res = arg
pop ()
while (plus_follows)
   pop ()
   res += arg
   pop ()
return res

Share this post


Link to post
Share on other sites

What? ;-) I am a noob. I am having problems with parse errors sometimes. :-D

Anyway thank you for code, I will see what I can do with that.

 

Regards.

Share this post


Link to post
Share on other sites
What? ;-) I am a noob. I am having problems with parse errors sometimes. :-D

Doesn't everybody? ;)

Anyway thank you for code, I will see what I can do with that.

It is of course a gross oversimplification (for instance you need compute multiplications and divisions first before you do the addition). But I think that with a simple loop in the current code, you can solve this problem.

Share this post


Link to post
Share on other sites

Well, I will take care of it when I will finish new items db for her.

 

Regards.

Share this post


Link to post
Share on other sites
It is of course a gross oversimplification (for instance you need compute multiplications and divisions first before you do the addition). But I think that with a simple loop in the current code, you can solve this problem.

Either you make it sound way simpler than it is, or I completely missed somthing when writing Minai's calc funtion, because it is pretty complicated.

 

Ok, the worst is the "in" operator, because it takes three arguments (two before the word "in" and one after)

Share this post


Link to post
Share on other sites

Could you not use python/perl to access bash and execute

expr <equation from pm>?

 

Ok, I dont know python, but I know bash.

 

(Obviously providing some formatting before execution, i.e. escaping asterisks etc)

Edited by Placid

Share this post


Link to post
Share on other sites
Could you not use python/perl to access bash and execute

expr <equation from pm>?

 

Ok, I dont know python, but I know bash.

 

(Obviously providing some formatting before execution, i.e. escaping asterisks etc)

That could be risky.

Let's say we get an expression like 2+2;rm -rf /

If you don't think about evil people when coding it you may end up with some missing files.

Share this post


Link to post
Share on other sites
Either you make it sound way simpler than it is, or I completely missed somthing when writing Minai's calc funtion, because it is pretty complicated.

 

Is that a challenge? ;)

 

I don't know what 'in' is supposed to do, but, for simple expressions (+,-,*./,^, and parentheses) the following will suffice:

OP_ADD   = '+'
OP_SUB   = '-'
OP_MUL   = '*'
OP_DIV   = '/'
OP_POW   = '^'
OP_OPEN  = '('
OP_CLOSE = ')'

def calc_prim (stack):
   if stack[0] == OP_OPEN:
       arg, stack = calc_expr (stack[1:])
       assert stack[0] == OP_CLOSE
   else:
       arg = stack[0]
   return arg, stack[1:]

def calc_factor (stack):
   arg1, stack = calc_prim (stack)
   if stack and stack[0] == OP_POW:
       arg2, stack = calc_factor (stack[1:])
       arg1 **= arg2
   return arg1, stack

def calc_term (stack):
   arg1, stack = calc_factor (stack)
   while stack and (stack[0] in [OP_MUL, OP_DIV]):
       op = stack[0]
       arg2, stack = calc_factor (stack[1:])
       if op == OP_MUL:
           arg1 *= arg2
       else:
           arg1 /= arg2
   return arg1, stack

def calc_expr (stack):
   arg1, stack = calc_term (stack)
   while stack and (stack[0] in [OP_ADD, OP_SUB]):
       op = stack[0]
       arg2, stack = calc_term (stack[1:])
       if op == OP_ADD:
           arg1 += arg2
       else:
           arg1 -= arg2
   return arg1, stack
   
stack = [2, '+', 15, '/', '(', 5, '*', 3, ')'];
print calc_expr (stack)

Yes, it's python because I just cobbled it together, and I have completely ignored building the stack, but it works, and it's simple.

 

EDIT: board messing up indentation

EDIT2: added quote so that we know what we're talking about

Edited by Grum

Share this post


Link to post
Share on other sites
Let's say we get an expression like 2+2;rm -rf /

Seriously good point. I obviously didnt think of that. But thats not to hard to code against.

 

Pseudo code:

method(String pm_expression){
   if( expression.indexOf( ";" ) >= 0 ){
       //DEPENDING ON IMPLEMENTATION/CHOICE:
       //EITHER:
           pm_expression = pm_expression.substring( 0,pm_expression.indexOf( ";" ) );
       //OR:
       return_pm_to_sender.( "Im not going to execute THAT!" );
   }
}

 

Obviously providing similar method to get rid of any other possible hacking.

 

Not so bad...?

Share this post


Link to post
Share on other sites

There are so many ways to execute code if you feed it a raw bash script, that it'd be impossible to code against it without reimplementing the whole shell. Perhaps a python implementation which only has access to the local namespace would be doable, bu I'm no expert.

Share this post


Link to post
Share on other sites
if( expression.indexOf( ";" ) >= 0 ){

2+2 | rm -rf /

2+$(rm -rf /)

2+`rm -rf /`

2+2 <(rm -rf /)

2+2 >(rm -rf /)

 

Just the bunch I could think of.

Share this post


Link to post
Share on other sites
if( expression.indexOf( ";" ) >= 0 ){

2+2 | rm -rf /

2+$(rm -rf /)

2+`rm -rf /`

2+2 <(rm -rf /)

2+2 >(rm -rf /)

 

Just the bunch I could think of.

Good points; I used semi-colon as an example.

 

Which is why I said

Obviously providing similar method to get rid of any other possible hacking

You've rightly pointed out that my suggested method would be too much work, when you could probably use local python methods/functions (dont flame me, I said im not a python master ;)). I was simply suggesting an alternative method to fixing the 2+2+2=4 problem.

Share this post


Link to post
Share on other sites
There are so many ways to execute code if you feed it a raw bash script, that it'd be impossible to code against it without reimplementing the whole shell. Perhaps a python implementation which only has access to the local namespace would be doable, bu I'm no expert.

Frak has made a python implementation, built into ELC.. could probably be extended quite easily.

Share this post


Link to post
Share on other sites

First off, yes, I know, this is serious thread necromancy.

I wanted to add something like #calc, and found this thread, and the patch on berlios that was classed 'rejected'.

I thought I remembered using #calc before, and was wondering where it went.

 

If a #calc is not wanted in the game, let me know, and I'll can the idea; otherwise, if it's an issue of implementation, I'd like to give this a go.

 

I would do this in C, no calling out to external programs (that may or may not be available) for portability, if that was an issue

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×