<%
# -*- encoding: utf-8 -*-
%>
<html>
<head><title>tHc Private Server Booking</title></head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" media="screen" type="text/css" href="css/design.css"  />
<body>
<%
import cgi, os, MySQLdb
from reservation import tryReservation, SERVERS
from datetime import datetime, timedelta
from reservation_session import getSession

session = getSession(req)
user = session.getUser()
if not user: #loggé ?
    util.redirect(req, "login.py?returnURL=form.py")

#Création de la liste de joueurs
f = file("/srv/tremulous/private_server/base/admin.dat", 'r')
data = f.read()
f.close()

l = data.split("\n\n")

sti, sto= os.popen2("sed -e 's#\^.##g'")
for i in l:
    d = i.split("\n")
    if d[0].strip() == "[admin]" and d[3].split("=")[1].strip()=="1":
        sti.write(d[1].split("=",1)[1].strip()+"\n")

sti.close()
r = sto.readlines()
sto.close()

#Ecriture de la liste de joueurs en HTML
chaine = []
for i in r:
    name = cgi.escape(i.strip())
    chaine.append("""<option value="%s">%s</option>"""%(name,name))
chaine.sort()
chaine = "\n".join(chaine)


#codes erreur renvoyés par tryReservation:
TRY_RESULT = {0:'Reservation booked!', 1:"Illegal date or hour.", 2:"Error while calculating starting time", 3:"Illegal duration", 4:"Error while calculating duration. Aborting", 5:"Illegal duration:must be more than 10 minutes", 6:"Error while calculating ending date. Aborting.", 7:"Reservation failed: a reservation is probably already booked on this interval.", 8:"Server doesn't exists"}


strParams = ['server', 'serverPwd','hour','min','day','month','year','duration','comment']
listParams = ['lvl3']
#Création des dicos de paramètres de réservation
dStrParams = {}
for i in strParams:
    try:
        dStrParams[i]= MySQLdb.escape_string(form.getfirst(i))
    except:
        dStrParams[i] = ''
dListParams = {}
for i in listParams:
    try:
        dListParams[i] = map(lambda x:MySQLdb.escape_string(x),form.getlist(i))
    except:
        dListParams[i] = ''
dStrParams['login']=user.username
#Si un des champs à valeur scalaire est vide, on affiche le formulaire
if '' in dStrParams.values() or None in dStrParams.values():
    #Ecriture du formulaire HTML


    #Ecriture de la liste des serveurs
    servers = []
    for i in SERVERS:
        selected = ""
        if dStrParams['server'] == i:
            selected = " selected"
        servers.append("""<option value="%s"%s>%s</option>"""%(i, selected, i))
    servers.sort()
    servers = "\n".join(servers)
%>
<div id="entete"></div>
<div id="menu">
<div class="element_menu">
  <ul>
    <li><a href="http://www.thcteam.org">Accueil</a></li>
    <li><a href="http://forum.thcteam.org">Forum</a></li>
    <li><a href="http://wiki.thcteam.org">Wiki</a></li>
    <li><a href="http://www.thcteam.org/map.php">Download maps</a></li>
    <li><a href="http://thcteam.org/reservation/overview.py">Réservations</a></li>
  </ul>
</div>
</div>

<div id="liens">
  <ul>
   <li><a href="login.py">Login</a></li>
   <li><a href="form.py">Form</a></li>
   <li><a href="overview.py">Overview</a></li>
  </ul>
</div>

<div id="formulaire">
<form method="post" action="">
<h2>Book the Server</h2>
<p>Server to Book:<select name="server">
<%= servers %>
</select><br /></p>
<p>Server Password: <input type="text" name="serverPwd" /> <br/></p>
<p>Date: <input type="text" name="day" size="2" />/<input type="text" name="month" size="2" />/<input type="text" name="year" size="4" /> (dd/mm/yyyy) <br />
Hour: <input type="text" name="hour" size="2"/>:<input type="text" name="min" size="2" /> (hh:mm) <br/>
Warning: the server is GMT+2<br />
Duration: <input type="text" name="duration" size="3" />In minutes.If you want to book the server for 2 hours, type 120<br /></p>
<p><table><tr><td valign="top">Players to !setlevel 3:<br />
You can select multiple players.<br />
/!\ You __must__ select yourself in order to set game options(like TL, SD...) ingame.
</td>
<td><select multiple name="lvl3">
<%= chaine %>
</select></td></tr></table></p>
<p>Leave a comment (CW, against who?, training ?.....)<input type="text" name="comment" size="30"/><br />
All fields must be filled, except, maybe, the list of players
<input type="submit" value="Submit" /></p>

</form>
</div>

<div id="pied">
   <p>Copyleft "tHcteam" 2007, aucun droits réservés. Thx to Whinze from wwF for the design</p>

<!-- phpmyvisites -->
<a href="http://www.phpmyvisites.net/" title="phpMyVisites | Open source web analytics" onclick="window.open(this.href);return(false);"><script type="text/javascript">
<!--
var a_vars = Array();
var pagename='';

var phpmyvisitesSite = 1;
var phpmyvisitesURL = "http://www.thcteam.org/phpmv2/phpmyvisites.php";
//-->
</script>
<script language="javascript" src="Site%20officiel%20des%20tHc_fichiers/phpmyvisites.js" type="text/javascript"></script>
<object><noscript><p>phpMyVisites | Open source web analytics
<img src="http://www.thcteam.org/phpmv2/phpmyvisites.php" alt="Statistics" style="border:0" />
</p></noscript></object></a>
<!-- /phpmyvisites -->
</div>
</body></html>
<%
#Formulaire écrit.

else: #Sinon, on affiche le résultat de la reservation
    resultatRes = tryReservation(dStrParams,dListParams, alter=False)
%>
<p class="info"><%= TRY_RESULT[resultatRes] %></p>
</body></html>
