Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-13-2011, 10:32 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Grattpd: the world's best (GScript) web server

1. Description
I present to you Grattpd, the world's best GScript-powered webserver. Sporting cookies, POST/GET data, includes, and more, Grattpd is the only way to go if you're looking for a web server that does it all!

Grattpd enables you to run a web site from your Graal server. Combined with my FTP server, you've got your own web host!

The benefit of using Grattpd for your server's web site is that you can easily use data from the server itself, such as the current playercount or server news. It also makes it easy to integrate your web site with other functions of the server, such as a support center or message board.

Attached is a zip file with all of the scripts used, as well as a demo file (which shows how to use dynamic web pages) and the error page (which you can, of course, change). The instructions below will guide you through installation.

2. Installation
  1. Download the attached ZIP file and extract it.
  2. Create a class for each file in the "classes" folder and copy the script in there. Alternatively, you could upload all of the TXT files into the "scripts" folder of your server and restart the NPC-server.
  3. Create a new database NPC with type "CONTROL". Place it in any level (preferably a level that won't be ever deleted). Name the NPC "WebServer", and copy and paste the code from "npcs/WebServer.txt", which was in the ZIP you extracted.
  4. Give yourself and (npcserver) the following rights:
    Quote:
    rw webserver/public/*
    rw webserver/public/*/*
    rw webserver/public/*/*/*
    rw webserver/priv/*
    rw webserver/priv/*/*
    rw webserver/priv/*/*/*
    (feel free to expand these if you require more subdirectories)
  5. In the ZIP file, upload "webserver/public/index.arc" to the corresponding folder on the server. Do the same for "webserver/priv/error.arc" and "webserver/priv/bad_browser.html".

    Note: bad_browser.html is shown if the user is attempting to access the site via Internet Explorer 6. The site I was making for Era explodes under IE 6, so I decided simply to disable access, which is fairly common practice. You can remove this code (it's in http_clientsocket) if you'd prefer.
  6. Try accessing the site. It will be at the server and port which echoed to RC earlier.

    For example, if you saw
    Quote:
    HTTP: Bound to 50.23.136.183:44989, waiting for connections...
    you want to go to http://50.23.136.183:44989/

    If it's not working, you probably did something wrong. If you can't figure it out, feel free to ask a question in this thread.

Your web server should now be up and running. The section below will explain how to use the various features of the web server.

3. Usage
3.1 Static vs Dynamic
There are two types of content you will serve: static and dynamic. Static content does not change. Dynamic content does.

An example of static content on a typical web page would be images, spreadsheets, and JavaScript. An example of dynamic content could be the page itself, which may change based on a certain condition (for example, it might display the playercount or show the name of the logged-in user).

For static content, simply upload the file to webserver/public/ and it will be served as-is.

For dynamic content, I've chosen to use handler classes. The benefit of using classes for different pages is that you have total control over the page. My preferred method of using dynamic content is to have HTML in a file, and use a handler class for replacing keywords like <&LOGGED_IN_USER&> with the proper text. This is the only method which I will explain, although it is very easy to figure out how to do it another way by looking at the class http_handler_replace.

3.2 Dynamic Content
For must usage cases, each page will have its own handler class. If you open the example index.arc file I included, you will see the following:
HTML Code:
### http_handler_page_demo
<!doctype html>
<html>
	<head>
		<title>It works!</title>
	</head>
	
	<body>
		<h1>Hoorah!</h1>
		<p>Grattpd is working! There are currently <&PLAYERS&> players online!</p>
	</body>
</html>
The first line defines the handler that the page will use. I've arbitrarily named it a "shebang" for simplicity.

The shebang consists of three pounds, a space, and the name of the handler class. The code in this specific handler class looks like this:

Graal Script Code:
function onCreated() {
  
this.join("http_handler_replace");
}

function 
onReady() {
  
this.socket.sendStatus(200"OK");
  
this.socket.sendHeader("Content-Type: text/html");
  
  
temp.replace = {
                   {
"PLAYERS"allplayers.size()}
                 };
  
  
this.replaceInfo(replace);
  
this.sendData();

You can use this template for your own pages. It should be fairly obvious how to add additional parameters to be replaced.

Files ending in .arc are considered dynamic. Files with any other extension will be served as-is.

3.3 Restricted File Types
Unfortunately, the NPC-server can only interact with certain file types. This is why I am using "ARC" for dynamic content instead of something more semantic.

The following file types are known to work with the NPC-server (this is a very small list, I don't have the full one):
  • html
  • txt
  • arc
  • png
  • jpg
  • gif
  • gani
  • nw
  • db
  • css

The "js" extension, which is used for JavaScript source files, is not currently allowed. As a workaround, files whose names end with "_js.txt" will be sent with an application/javascript Content-type header. You could also embed JavaScript directly into your web pages.

3.4 Cookies
Cookies are small pieces of data which are stored by the web browser. They are used for logins, among other things. Grattpd supports cookies.

To send a cookie for the browser to store, use this command from the handler class:
Graal Script Code:
this.socket.sendCookie("username""chris"); // var name, value 
To access a cookie which you have previously set, use this command from the handler class:
Graal Script Code:
echo(this.socket.cookie.username); 
Remember that cookies can be faked by the user, so always validate them. Don't trust a cookie like "logged_in" or "username" without verifying it with some token or hash you've also stored as a cookie.

It is important to keep in mind that cookies are user-submitted data, so if you're using them in conjunction with SQLite, you need to remember to escape them.

3.5 GET/POST data
GET data is data attached to the URL of a page. For example, the URL might look like http://example.com/?threadid=123&postsperpage=1.

POST data is data which you can't see in the URL. An example of it would be when you make a post on these forums. The content of your post is sent via POST.

To access this data, use the following from the handler class:
Graal Script Code:
echo(this.socket.data.get.value);
echo(
this.socket.data.post.value); 
You can find an explanation of GET and POST data here.

3.6 Includes
Includes allow you to include another file inside the current file. Grattpd supports this very well. You can include both static and dynamic files.

An example of a file which uses includes:
HTML Code:
### http_handler_page_home
<&INCLUDE priv/templates/main/header.arc &>
<h1>Welcome to Era!</h1>
<p>Enter Era and find yourself immersed in modern city life. Fight using assault rifles, shotguns, and more! This 2D adventure pits you against the ruthless gangs that run the streets of Graal.</p>

<&INCLUDE priv/templates/main/footer.arc &>
The value of includes should be immediately obvious. You can use includes by placing the following in any dynamic file:
HTML Code:
<&INCLUDE path/to/file &>
The path is relative to the web root. In most cases you will want to store your templates outside of the public web, so you will want to do something like "priv/templates/header.arc". This would load a file in "webserver/priv/templates/header.arc".

For includes to work, your handler class must call this.socket.addIncludes();. This is an example of a handler class which uses includes:
Graal Script Code:
function onCreated() {
  
this.join("http_handler_replace");
}

function 
onReady() {
  
this.socket.sendStatus(200"OK");
  
this.socket.sendHeader("Content-Type: text/html; charset=UTF-8");
  
this.socket.selectedSection "home";
  
this.socket.pageTitle "Era: Graal's most popular modern playerworld.";
  
this.socket.pageTitleNoSuffix true;
  
  
this.socket.addIncludes();
  
  
temp.replace = {
    {
"test""test2"}
  };
  
  
this.replaceInfo(replace);
  
this.sendData();

Post continued below

Edit by Skyld: Links approved.
Attached Files
File Type: zip Grattpd.1.01.zip (9.9 KB, 190 views)
__________________

Last edited by cbk1994; 03-14-2011 at 05:46 AM..
Reply With Quote
  #2  
Old 03-13-2011, 10:32 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
3.7 Using a PHP gateway to access your site
The problem with using this setup is that your website's port will change periodically. To combat this problem, I've written a PHP script for Era's website which allows you to visit http://graalcenter.org/ and be redirected to the current site. It also redirects requests for specific pages, so that you can link to http://graalcenter.org/some/random/page and know that it will always work.

The PHP code I used for this PHP gateway is:

index.php
Graal Script Code:
<?php
$fh 
fopen("port.txt""r");
$port fread($fhfilesize("port.txt"));
fclose($fh);

header("Location: http://server.era.graalcenter.org:" $port "/" $_GET['go']);
?>
update.php
Graal Script Code:
<?php
$pass 
"change_me";
$ip "50.23.136.183"// get this from statistics.graal.us

if ($_SERVER['REMOTE_ADDR'] != $ip || $_GET['pass'] != $pass) {
    die(
"**** off");
}

$fh fopen("port.txt""w") or die("aa");
fwrite($fh$_GET['port']) or die("bb");
fclose($fh);
?>
.htaccess
Quote:
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?go=$1 [QSA,L]
Be sure to change the password and the redirect URL. You can then enable it in http_listensocket.

4. Conclusion
I think I've covered everything. The script is attached as a ZIP file to the post above. If you have any questions, feel free to ask. Comments are also welcome.

You can see an example of Grattpd in action at http://graalcenter.org/ (note that the site is unfinished).
__________________

Last edited by cbk1994; 03-13-2011 at 10:46 PM.. Reason: fixing stray line break
Reply With Quote
  #3  
Old 03-13-2011, 10:35 PM
Crono Crono is offline
:pluffy:
Join Date: Feb 2002
Location: Sweden
Posts: 19,993
Crono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond reputeCrono has a reputation beyond repute
Send a message via AIM to Crono
when you grow up you're going to be rich, just saying
__________________
Reply With Quote
  #4  
Old 03-13-2011, 10:53 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Fantastic.
Reply With Quote
  #5  
Old 03-13-2011, 10:54 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Era
You are currently logged in as cbk1994. Logout »
Reply With Quote
  #6  
Old 03-13-2011, 11:02 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by DustyPorViva View Post
Era
You are currently logged in as cbk1994. Logout »
It's not finished yet—soon that will show a login box .
__________________
Reply With Quote
  #7  
Old 03-13-2011, 11:05 PM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
wow
__________________
Reply With Quote
  #8  
Old 03-14-2011, 02:46 AM
Seich Seich is offline
Noctorious' NeoHunter
Seich's Avatar
Join Date: Jun 2008
Location: Honduras
Posts: 193
Seich will become famous soon enough
Send a message via MSN to Seich Send a message via Yahoo to Seich Send a message via Skype™ to Seich
That's awesome.
Reply With Quote
  #9  
Old 03-14-2011, 04:06 AM
joel34 joel34 is offline
(⌒▽⌒)
joel34's Avatar
Join Date: Aug 2009
Location: Sveden.
Posts: 554
joel34 is on a distinguished road
****ing incredible.
__________________
Reply With Quote
  #10  
Old 03-14-2011, 04:16 AM
Devenio Devenio is offline
Scripter
Devenio's Avatar
Join Date: Dec 2006
Posts: 28
Devenio is on a distinguished road
I think I just jizzed my pants, awesome work!
__________________

"Two things are infinite: the universe and human stupidity, and I'm not sure about the the universe.." -Einstein


Reply With Quote
  #11  
Old 03-14-2011, 04:31 AM
MattKan MattKan is offline
the KattMan
Join Date: Aug 2010
Location: United States
Posts: 1,325
MattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to behold
Send a message via AIM to MattKan Send a message via Skype™ to MattKan
This is absolutely amazing. Seriously. Fantastic!

I'm going to try to understand it.

Question: I don't really know how to explain what I'm asking, but I know that memory and stuff runs on the server? Like I could do a free webhoster with limited memory storage and traffic per month, but it would no longer be limited because I have the server to back it up?
__________________
Quote:
Originally Posted by Satoru Iwata
On the other hand, free-to-play games, if unbalanced, could result in some consumers paying extremely large amounts of money, and we can certainly not expect to build a good relationship with our consumers in this fashion. In order to have a favorable long-term relationship, we would like to offer free-to-play games that are balanced and reasonable.
Quote:
Originally Posted by Unximad
Eurocenter Games remains attached to the values of indies game developer and to the service our playerbase community.

Last edited by MattKan; 03-14-2011 at 04:56 AM..
Reply With Quote
  #12  
Old 03-14-2011, 04:58 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004 Send a message via Skype™ to Tolnaftate2004
bad
Graal Script Code:
Grattpdgrep -rn tamp .
./
npcs/WebServer.txt:43:  tamp.map.json "application/json"
good
Graal Script Code:
Grattpdgrep -rn -A 4 "\bIE 6" .
./
classes/http_clientsocket.txt:56:  // don't allow IE 6 -- you can remove this if you really
./classes/http_clientsocket.txt-57-  // want to
./classes/http_clientsocket.txt-58-  if (this.userAgent.pos("MSIE 6.0") > (- 1)) {
./
classes/http_clientsocket.txt-59-    this.throwOldBrowserError();
./
classes/http_clientsocket.txt-60-  } 
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #13  
Old 03-14-2011, 05:28 AM
Sky Sky is offline
GK Manager
Sky's Avatar
Join Date: Sep 2002
Location: PA, USA
Posts: 622
Sky is a glorious beacon of lightSky is a glorious beacon of lightSky is a glorious beacon of light
Send a message via AIM to Sky Send a message via Skype™ to Sky
Awesome... =o
__________________
Sky
Reply With Quote
  #14  
Old 03-14-2011, 05:46 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Tolnaftate2004 View Post
bad
Graal Script Code:
Grattpdgrep -rn tamp .
./
npcs/WebServer.txt:43:  tamp.map.json "application/json"
Whoops-- fixed, I've changed the attachment in the original post.
__________________
Reply With Quote
  #15  
Old 03-14-2011, 06:57 AM
MattKan MattKan is offline
the KattMan
Join Date: Aug 2010
Location: United States
Posts: 1,325
MattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to beholdMattKan is a splendid one to behold
Send a message via AIM to MattKan Send a message via Skype™ to MattKan
Your name is Alexander Roshal? 0.o
__________________
Quote:
Originally Posted by Satoru Iwata
On the other hand, free-to-play games, if unbalanced, could result in some consumers paying extremely large amounts of money, and we can certainly not expect to build a good relationship with our consumers in this fashion. In order to have a favorable long-term relationship, we would like to offer free-to-play games that are balanced and reasonable.
Quote:
Originally Posted by Unximad
Eurocenter Games remains attached to the values of indies game developer and to the service our playerbase community.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:14 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright (C) 1998-2008 Linux cyberjoueurs All Rights Reserved.