[ Login | Register ]

The Shining Source

« previous next »
Pages: [1] 2 Print
AJAX - A Viable Option?   (Read 33378 times)
Old Post June 09, 2006, 06:14:28 am
#1
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
Hi there guys.

http://www.bad.net.nz/shining/first.php

This is where I'm playing with code for a 1v1 battle game.
NO FLASH.
NO JAVA.
JUST YOUR BROWSER.

Currently you might think.. what the fish is this?  Just a grid with one dude on it.  

The grid is x1y1,x2y1,x3y1 etc all the way to x10y8 currently.

You can type x10y8 to make the character move to the bottom right hand corner (or x1y1 to move to the to left) without refreshing the page.

Now open another browser window at the same time.  In one window, change the characters position - it changes in the other window too.

This simple demonstration shows that it would be possible.
It's just a combination of javascript and php.

No expensive flash multiplayer server software, or clients having to download java.

Let me know what you think.

~Elvenfyre


Old Post June 09, 2006, 01:34:41 pm
#2
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
OK earlier today I added a chat system so that anyone visiting the page can interact.  Kind of like a lobby - but this is purely for development purposes so I can chat to anyone who is helping me test the 2 player stuff.

secondly a big update, is that I've limited the movement range for each turn and changed text input into clicking on the square you want to move to.

Just as before of course, if you move the guy on your screen, he'll move around on mine.

next I intend to build four characters onto the same map, each of which has a turn moving so that I get to play with XML and database options to see what's the best.

after that I'll work on the whole two player thing, so that you can only control your guys and the opponent can only control their guys.

~Elvenfyre


Old Post June 12, 2006, 04:42:05 am
#3
Job
Shining Light *

Posts: 230

Logged
AJAX - A Viable Option?
Ha, that's sweet Elvenfyre. You know, as a matter of fact i've just finished creating an Ajax object that's cross browser. I just do:
Code:
jx = new Jax();
jx.onComplete(rv){
    alert("return value:"+rv);
}
jx.GETRequest(<url>);


I think AJAX is definitely a viable option, good job. The only drawback with using AJAX for a multiplayer game is that you'll need to check the server for an update every so often, like every 5 seconds or so. This will generate alot of unneeded traffic. With Flash's (or Java's) socket you can create a direct connection to the server and the client will automaticaly be notified of any updates, so you don't need to check every so often. Other than that, anything flash can do can be done with Javascript.

You can make HEAD requests to the server rather than GET or POST to check whether something has changed on ther server. HEAD requests are much simpler and faster because the HTTP body is empty for such requests.
An even better idea is to embed an invisible flash movie (flash 5 compatible) and use it's socket object with flash-javascript communication.


Old Post June 12, 2006, 05:36:51 am
#4
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
How do I do it with flash without having the MultiUser server?

~Elvenfyre


Old Post June 12, 2006, 06:47:44 am
#5
Job
Shining Light *

Posts: 230

Logged
AJAX - A Viable Option?
You use flash's XMLSocket object. Basically you write a server, that listens on port X. This server creates a socket and listens for connections. When somebody connects it sends the client a HELLO message and waits for a reply. This is called "handshaking" and it's meant to verify that the client is actually running your program rather than say, an ftp program. During the handshake you exchange information, such as user name/id for example. The server associates the socket connections with the respective user ID and keeps other global variables, such as whose turn is it, messages in the message box, etc.
Whenever the server receives a message from a client it determines who it is meant to and forwards a message through the correct connection to the desired individual.
I know this might sound daunting, but it's actually straightforward if you use a language like VB.NET, C#.NET or just JAVA. I've done it a few times and a simple server takes like 10 minutes to code, doesn't even need an interface. The main challenge is that the server will have to use multithreading, a thread per client, which is not difficult but it might be a new concept to grasp.
Basically after you have the server you go into flash, create an XMLSocket and connect to the server. You don't have to communicate with XML, but it's highly recommended.
This is what the flash communication server does in the background. With the FCS all you do is write a simple script for the server, main.as (in actionscript) which creates a SharedObject.
Flash clients then just connect to the shared object. Any change to the shared object is broadcast to all clients (or you can just broadcast to a select number of clients). It's alot easier, for sure.


Old Post July 21, 2006, 04:14:22 am
#6
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
I  haven't done any more work on this since my last post, however I plan to get back into it.
I will let you know of the progress since it will be the engine I intend on using for the 1v1 shining battle system I wanted to make.

My first task will be to decide on and create the information sharing system.

I will create the lobby and opponent finder which will give you the ability to send a URL to a friend so they can play against you specifically.

Then I will be attempting to make a turn based system for a character on each side which includes attack/defense/move/hp.

Once this is working I will add 6 characters a side.

Then finally start a discussion on mechanics and balance before implementing spells, randomisation or team choosing or anything.

~Elvenfyre


Old Post July 25, 2006, 03:57:21 am
#7
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
I have decided at this junction to use a MySQL database to hold the information on player stats(including position and current turn).  This is bulkier but safer.  
My system will transmit ALL data pertaining to the battle every second from the database to the webpage, and the webpage then rebuilds itself.  

This is costly, and I will need to look at streamlining it so only changes to the data are transmitted.  

HOWEVER the benefit is great for the time being.  
Security of the game.  It means that if someone was able to make a change to their game through a javascript trick, it would be gone the following second, this would hamper any efforts to cheat considerably.

In my mind unless there was heavy useage, the database could handle the load without trouble, and the data amount will be small for bandwidth concerns.

So while there are less than 10 games happening at one time I see no need to even worry about it, and once there are even 2 games happening a day, I will have the motivation to dive back into it.

The movement and turnbasing is being developed here:
http://www.bad.net.nz/shining/new.php

I have kind of ruined the order of my strategy since although my information sharing is complete, I must first build a turn based event system in order to test it properly.  So I will do half of step 3 before going back and completing step 2.

~Elvenfyre


Old Post July 25, 2006, 09:47:56 am
#8
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
OK for development purposes of the attack engine I've made a seethrough model that has reassured me of the feasibility of this solution!

Thinking about initial useage, this model is wondeful due to the "spectator" mode the original URL provides.  I'm going to leave this here as a backup while I make a real lobby.

It took me a while to work out the algorithms for movement and collision detection, and I haven't as yet completed a "stay" or "attack" option, it's just movement - but it's turn based multiplayer movement haha!

Unfortunately making a real lobby will require username and password.  It is just the way things are.  The current "full update" transmission I talked about in the last post does mean that if you get disconnected, refresh the page or press "back" accidently (as I sometimes do when I'm playing browser-based games), you will not effect the game if you open the URL again.

Of course eventually I would like to see a rank system that incorporates user wins, losses etc, but I don't think that it's either necessary or worthwhile until all characters have been finalised to a balanced state.
So I will not even consider such things and will proceed only with the most useful portions of a user system and lobby.

This will mean that when you are logged in you can see all games currently running, and spectate them.

When you are logged in you may invite anyone else online to battle you, and a new battle will be created.

I will continue with a see-through URL system for development reasons until the time comes where it seems unreasonable.

You can see the engine working at:
http://www.bad.net.nz/shining/new.php
with urls to player 1 and player 2 control listed on that page.

~Elvenfyre


Old Post July 25, 2006, 04:46:50 pm
#9
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
OK so I'm getting somewhere.

http://www.bad.net.nz/shining/

is the URL for the lobby at the moment, and it requires you to set up a username and password to get in.
The lobby is simply one of my chat modules at the moment, though now with a working user list (you are cut off the list once you've left the page for 30 seconds).
I will need to check it around the 31st of the month (note) .. but other than that it will do what I want.

note: I have not done the following yet, I am merely speculating out loud

I have to make a decision what's going to happen in regard to getting a game.  I am thinking that on the userlist anyone who is in the lobby or in "spec" (spectator) can be invited to play a battle by someone else.

They then accept or deny the game, and on acceptance are given a URL which checks the correct user is accessing it.  

What has just happened is the site has created the game in the database and set the players up inside of it.  

Once both players are there they must click ready, and once this happens the battle begins.

If someone times out(60 seconds) the person can choose to play on (eventually moves will have a timelimit) or to quit back to the lobby.
For now though, the battle won't notice if you time out or not, it'll play on regardless and the poor sod playing you won't be able to take advantage since there IS no timelimit to moves yet.  I suppose this is an important component I need to create soon.

once one player wins the game is moved into an archive, where all moves made and attack sequences are kept for an "instant replay" of the battle in case you want to watch your victory over and over again(or it will be good for bug finding and balancing).

What I suppose I really need to do is get the above done along with actual attack sequences so winning is possible with the existing units before adding any more thoughts.
This is going to be quite a long hard slog to get to this stage so despite not wanting to waste time bolloxing on here about it, I probably will just to keep my motivation up.

~Elvenfyre


Old Post July 26, 2006, 04:37:33 am
#10
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
Inviting(challenging), withdrawing, accepting and declining now work!

Games are now created in the database and you can open them to play them.
Turnbased movement is working perfectly.
attack/stay still to come(next).

AJAX:
One downside I found is that from within the refreshing parts of the page you cannot access the ajax from the main part of the page.  This means in order to accept/decline battles I've had to resort to a small popup that runs the necessary script - since the messages that give you the options are sent through ajax anyway.

secondly, those buttons for acceptance sometimes require a couple of clicks.  This is because the link itself is refreshing every second even though you can't see it.  If you happen to click on it within about 4ms either side of the refresh(I'm estimating from my tests through my 2mb dsl line), it doesn't register your click whatsoever.  
I wonder if this is also a problem with the game?  I haven't noticed it, but i guess we'll see.

~Elvenfyre


Old Post July 26, 2006, 02:29:00 pm
#11
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
5 vs 5 system working from start to finish!

1 axman, 1 swordsman, 1 assassin, 1 mage, 1 archer.

The mage fires just like the archer, in the 2 "steps" away fashion.

My interface is TERRIBLE.  I'm working on it though, and for the time being, it works to help debugging.

There is one other bug, for some reason(even though i've put stopgaps in place that should have cured this!) when you kill a guy whose turn is next up, he still gets a turn, but the game can't find him in the database anymore cos he's dead... so you the person who lost him has to click STAY.. and they lose their turn.  bung eh.  but that doesn't happen often.. but yeah i will fix it soon.

Anyway, you can quickly and easily make an account to log in.
You need a partner to play it with, or you can ask me(or just see if i'm in the lobby when you get there).

http://www.bad.net.nz/shining/

If anything seems to have a problem during your game, refresh the page, and you'll be back on track without losing any info - but I don't expect you to have to do that.

YAY!

~Elvenfyre


Old Post July 27, 2006, 03:52:54 am
#12
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
OK the passing turn to dead player bug was due to some REALLY stupid programming on my part...

What I did was at the end of each turn I set the CURRENT players NEXT player up, and switched the turn over to the OTHER player..

So when someone died, if the turn had already been passed TO them on that players previous turn, then obviously it was still going to try to access it.  Man I'm stupid.  After playing with the sleep() function for a while, I put tracking messages throughout the whole death sequence code and finally clicked on.

Putting in randomisation for the attacks now, so that you can do 1 more or less damage than you're supposed to.

My next steps include 2 pieces of interesting programming, 2 mundane things with images that could probably be done a lot better if I knew javascript better, and 1 ajax thing.

[1]
Attack range beginnings and ends.  
Since the archer can only attack things 2 "steps" away, and the mage should be able to attack things up close AND 2 steps away, I need to work out the process.

[2]
No moving "through" an enemy.
In shining force you can walk "through" your own force, but not the enemy, I want to do the same.  This might be quite tough for me.

[3]
Show image of who you can attack.
attack images need to show the picture of the person to attack so you can see what you're doing rather than just the attack symbol.  I'll simply make extra images for now.

[4]
Move / Attack / Stay buttons that grey out.
Once you've used them up you shouldn't be able to click them.
This will probably require mucking around with the different types of "status" throughout the waterfall.

[5]
Hovering over characters shows you stats.
being able to decide who to attack is difficult when first playing the game since you don't know who does what.  If I can make it so hovering over characters brings up stats, the game would really be playable.

Finally, I will have to do something about the chat/location of people from the lobby into the game.
I am thinking of a "tab" system that stores a session variable as to which chatroom you are in.  In game, this tab system means you can switch from game to lobby chats without leaving the game, and your status would be updated correctly.
The only problem is spectators, who need to be available for challenges, so I will have to make a seperate build for them.

This system would mean that in the distant future you could have private chatwindow tabs open for talking to your friends while you play games.

~Elvenfyre


Old Post July 28, 2006, 03:52:32 am
#13
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
Can anyone set me on the right path for [2] No moving "through" an enemy ??

I know if I spent a couple hours on it I would eventually come up with it, but I know many here have already figured out the process and I would learn a lot faster by example.

I have achieved #3 and #5!  
The hover seems to have trouble if you take focus off the browser window, but too bad, can't have everything.
I also included a menu which has a "character reference" guide.

I have put #1 aside due to game balance issues with the turnbase system that is in place.  I think to make the variety of characters I hope to have eventually, I will need to change it to a more shining force style of turn (speed based), or else give characters a minimum recovery time.  

I have put #4 aside and may just use some standard Attack / Stay graphic that does not dim, since it is of no real use in the present state.
If I included move and attack that dimmed out, my "auto stay" function that I worked so hard on will be rendered impotent, and the player will have to go back to clicking stay every turn.

Have yet to include randomising for the team that goes first or for damage amounts.  will do that asap.

So I'm going to work on making a new SEPERATE page that has the same user database, but a new unit and turn table.
It will run turns based on speed.
It will have attack range beginning and end for the mage.

I will use this new one to play with graphics for the site itself to make the game look and feel like it is part of something.

~Elvenfyre


Old Post July 29, 2006, 08:15:23 pm
#14
Administrator Shining Spammer *

Posts: 1,129

Logged
AJAX - A Viable Option?
Scrapping the new pages and database tables idea I decided to octopus what I've got.

Now when you challenge someone, they get the option of alternating turns or shining turns.  Shining turns is working well based on agility.  I don't use agility for anything else yet, but I suppose I can have a go at working out dodge from agility(though the current players will probably have to be changed in more ways than agility since it will give some of them too much advantage.)

Anyway, for now my focus is on the following:
the movement blocking by enemies procedure
showing who the current player is better.

{an auto attack system when within range
being able to go back to "move" after accidently clicking "attack"}
on writing these two I realise i could solve both issues by doing the following.
at any time while the turn is still active, you have not performed an action(like attacking, or staying (or later on, healing))... so perhaps I should build the attack options in automatically from the beginning of the turn.
The idea would be that the attack icon would appear over characters you can attack from your starting position, and if you clicked on them, your move turn would end, you would attack them, and your turn would be over.
If instead you clicked on a blinking square, your move turn would be over and it would be in attack mode, showing you who you can attack (or failing finding anyone in the zone, auto-stay).
I think I will do this because I can't see any real negatives, and it would help the games smoothness a great deal.


Regarding Ajax:
I have tested with 3 configurations personally:
Desktop: AMD 2500+, 1024MB, WinXP Pro, Firefox 1.0.3
Desktop: Duron 800, 384MB, WinXP Pro, IE 6.0
Laptop: 1.6GHz P4, 768MB, WinXP Pro, both IE 6.0 and Firefox 1.0.3

The slowest was the Duron, with a native 1% CPU useage showing 13% cpu usage playing the game, jumping to 26%(highest 32%) on hovers.

Hovering uses a 3rd ajax query to receive data and place it on the screen with javascript, so I'm guessing that is the cause.  Will have to watch this if it starts to creep up due to other addons.

Ty ran the game today quite slowly on a celeron 900 laptop, 512MB ram on Firefox (I'm didn't ask what version).
100% cpu usage at times!  Is this a firefox version problem with the XMLHttpRequest itself?  Or(slightly better news), the javascript?
Or my crap programming?
Or perhaps it's just Ty running too much.  I guess we'll see tomorrow when we try it out again.

~Elvenfyre


Old Post January 09, 2007, 03:29:33 am
#15
Job
Shining Light *

Posts: 230

Logged
Re: AJAX - A Viable Option?
Recently i've come across something called "Reverse Ajax" which makes Ajax more viable. The idea is, instead of querying the server every X seconds, you query the server once, the server holds the query without timing out and returns only when it has something to return.
I'm already starting on this.


Pages: [1] 2 Print 
« previous next »
Jump to:  

Powered by SMF 1.1.21 | SMF © 2013, Simple Machines