Forum Moderators: open

Message Too Old, No Replies

Help with maybe a simple program code

Kinda a newbie with this.

         

Leveecius

3:27 am on Nov 3, 2007 (gmt 0)

10+ Year Member



Hey guys, I need some help. I'm working on programming a text-based Online RPG game and have most all of it figured out, except one part. I have my mysql database all figured out and whatnot, but I can't seem to figure out how to program a certain link.

What I'm trying to do is figure out how to make a button/link to produce random results within a certain set values. For example, if you go to attack someone in the RPG, you are a level 10 and the guy you're attacking is a level 9, so you have x number of things that can happen and it will randomly choose the results based on the information from the database, and the higher up you get the better the results. Do you follow what I'm trying to accomplish? I don't even know what that type of code is called. I'm hoping you guys can help me out on this. If you could, I'd really appreciate it. Thanks for your help and time in advance.

Edit: I forgot to mention, I was told by a friend that this is a Java script, that's why it's in here, if it's not, I'm sorry for posting it here.

[edited by: Leveecius at 3:33 am (utc) on Nov. 3, 2007]

phranque

6:58 am on Nov 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, Leveecius!

if it's really javascript you are using you could look at the random function of the Math object.

are you sure it isn't a java applet?

mehh

10:23 am on Nov 3, 2007 (gmt 0)

10+ Year Member



if it isnt a java applet would something like this work:
//Function to get the random number...
function randomNumber(a,b){
return Math.ceil(Math.random()*(b-a+1))+(a-1);
}
//calling the function
var lowest =10;
var highest=20;
alert(randomNumber(lowest,highest);

Leveecius

3:13 pm on Nov 3, 2007 (gmt 0)

10+ Year Member



Thanks for the welcome and the information. To answer your question, I'm not sure if it's even Java. I believe it is only because my friend told me it was. I've only experimented with Java a few times, so that's a whole new world to me. So it may just be a Java applet, but like I said before, I'm not sure what it is.

Now as for the non-Java applet script written there, that seems like it would work, I will try it a little later today. Only problem I forsee with it is I don't see how that would tie itself into information with my mysql database. But I appreciate the scripting, at least it gives me a place to start. Thanks for the help again guys, I really appreciate it. If it is a Java Applet, would anyone be able to expound on that a little?

Dabrowski

6:17 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you need a back-end script such as Perl. It depends if you want a seamless link.

You have 2 options:

1 User presses "Attack", page refreshes with results page. This would post the data to a server side script which would produce the result.
2 User presses "Attack", JavaScript uses AJAX to call aforementioned server side script.
3 User presses "Attack". JavaScript calculates result, sends result to server side script via AJAX.
4 User presses "Attack". JavaScript calculates result, and uses DOM to change page on the fly.

Sorry, got carried away.

1 is most browser-compatible.
2 is probably better for modern browsers and presentation.
3 kinda defeats the object calculating it on the front end and then having a back end anyway.
4 difficult to code, and probably quite slow.

I guess you have a back-end anyway for your SQL?

mehh

8:13 pm on Nov 5, 2007 (gmt 0)

10+ Year Member



5 Javascript calculates the result, updates using dom and uses AJAX as an "autosave" feature

My code above was ment to tie in with the rest of the script if it was writen as JavaScript if not it wont work unless you re-think your design or want to open the doors to cheaters.

ps Dabrowski surely 4 would be the fastest as it dosn't have the overheads of waiting for the server to respond.
5 has this speed as well as backing up all info

Dabrowski

9:17 pm on Nov 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dabrowski surely 4 would be the fastest

It really depends on how much processing the JS is doing. Making major calculations (such as calculating a simple AI) and modifying the DOM takes a lot of CPU time in JS. Compared to a decent server, maybe 2 secs for an AJAX refresh would seem preferable.

Both would work, that's just my oppinion, if I was doing it I'd do it that way.

And also as you pointed out, if you run the game in JS people could cheat, keeping everything on the back end mean they have to play by the rules!

mehh

5:21 pm on Nov 6, 2007 (gmt 0)

10+ Year Member



such as calculating a simple AI

It all depends on how simple the AI is but I would still use 2 falling back on 1 to stop cheaters. Some part of me is saying that you could have speed and stop cheaters but I'm not quite sure how to go about this.