|
@@ -1,24 +1,32 @@
|
|
|
#include "Datenbank.h"
|
|
|
+#include <iostream>
|
|
|
|
|
|
// Inhalt der CSDatenbank Klasse aus Datenbank.h
|
|
|
// Konstruktor
|
|
|
CSDatenbank::CSDatenbank( InitDatei *zIni )
|
|
|
{
|
|
|
- if( !zIni->wertExistiert( "DBBenutzer" ) )
|
|
|
- zIni->addWert( "DBBenutzer", "chatserveru" );
|
|
|
- if( !zIni->wertExistiert( "DBPasswort" ) )
|
|
|
- zIni->addWert( "DBPasswort", "LTChatServerPW" );
|
|
|
- if( !zIni->wertExistiert( "DBName" ) )
|
|
|
- zIni->addWert( "DBName", "lenck_tech_db" );
|
|
|
- if( !zIni->wertExistiert( "DBIP" ) )
|
|
|
- zIni->addWert( "DBIP", "127.0.0.1" );
|
|
|
- if( !zIni->wertExistiert( "DBPort" ) )
|
|
|
- zIni->addWert( "DBPort", "5432" );
|
|
|
datenbank = new Datenbank( zIni->zWert( "DBBenutzer" )->getText(), zIni->zWert( "DBPasswort" )->getText(),
|
|
|
zIni->zWert( "DBName" )->getText(), zIni->zWert( "DBIP" )->getText(),
|
|
|
(unsigned short)TextZuInt( zIni->zWert( "DBPort" )->getText(), 10 ) );
|
|
|
+ if( !datenbank->istOk() )
|
|
|
+ {
|
|
|
+ std::cout << "CS: Die Verbindung zur Datenbank konnte nicht hergestellt werden.\nDas Programm wird beendet.";
|
|
|
+ exit( 1 );
|
|
|
+ }
|
|
|
InitializeCriticalSection( &cs );
|
|
|
ref = 1;
|
|
|
+ Text befehl = "SELECT port, admin_port FROM server WHERE id = ";
|
|
|
+ befehl += zIni->zWert( "ServerId" )->getText();
|
|
|
+ lock();
|
|
|
+ datenbank->befehl( befehl );
|
|
|
+ Result res = datenbank->getResult();
|
|
|
+ unlock();
|
|
|
+ if( res.zeilenAnzahl == 1 )
|
|
|
+ {
|
|
|
+ zIni->addWert( "ServerPort", res.values[ 0 ] );
|
|
|
+ zIni->addWert( "AdminServerPort", res.values[ 1 ] );
|
|
|
+ }
|
|
|
+ res.destroy();
|
|
|
}
|
|
|
|
|
|
// Destruktor
|
|
@@ -45,11 +53,11 @@ int CSDatenbank::istAdministrator( const char *name, const char *passwort )
|
|
|
Text n( name );
|
|
|
n.ersetzen( "'", "''" );
|
|
|
befehl->append( (char*)n );
|
|
|
- befehl->append( "' AND passwort = '" );
|
|
|
+ befehl->append( "' AND passwort = md5('" );
|
|
|
Text p( passwort );
|
|
|
p.ersetzen( "'", "''" );
|
|
|
befehl->append( (char*)p );
|
|
|
- befehl->append( "'" );
|
|
|
+ befehl->append( "')" );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
|
Result res = datenbank->getResult();
|
|
@@ -78,7 +86,7 @@ bool CSDatenbank::adminHatRecht( int id, int recht )
|
|
|
|
|
|
bool CSDatenbank::proveKlient( int num, int sNum )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT * FROM server_chat_clients WHERE server_chat_id = " );
|
|
|
+ Text *befehl = new Text( "SELECT * FROM server_client WHERE server_id = " );
|
|
|
befehl->append( sNum );
|
|
|
befehl->append( " AND client_id = " );
|
|
|
befehl->append( num );
|
|
@@ -97,7 +105,7 @@ bool CSDatenbank::proveKlient( int num, int sNum )
|
|
|
Text *CSDatenbank::getKlientKey( int cId )
|
|
|
{
|
|
|
lock();
|
|
|
- if( !datenbank->befehl( Text( "SELECT schluessel FROM clients WHERE id = " ) += cId ) )
|
|
|
+ if( !datenbank->befehl( Text( "SELECT schluessel FROM client WHERE id = " ) += cId ) )
|
|
|
{
|
|
|
unlock();
|
|
|
return 0;
|
|
@@ -116,9 +124,9 @@ Text *CSDatenbank::getKlientKey( int cId )
|
|
|
|
|
|
void CSDatenbank::unregisterKlient( int num, int sNum )
|
|
|
{
|
|
|
- Text *befehl = new Text( "DELETE FROM server_chat_clients WHERE client_id = " );
|
|
|
+ Text *befehl = new Text( "DELETE FROM server_client WHERE client_id = " );
|
|
|
befehl->append( num );
|
|
|
- befehl->append( " AND server_chat_id = " );
|
|
|
+ befehl->append( " AND server_id = " );
|
|
|
befehl->append( sNum );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
@@ -126,7 +134,7 @@ void CSDatenbank::unregisterKlient( int num, int sNum )
|
|
|
unlock();
|
|
|
if( za == 1 )
|
|
|
{
|
|
|
- befehl->setText( "UPDATE server_chat SET clients = clients - 1 WHERE id = " );
|
|
|
+ befehl->setText( "UPDATE server SET tasks = tasks - 1 WHERE id = " );
|
|
|
befehl->append( sNum );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
@@ -135,114 +143,9 @@ void CSDatenbank::unregisterKlient( int num, int sNum )
|
|
|
befehl->release();
|
|
|
}
|
|
|
|
|
|
-bool CSDatenbank::serverAnmelden( InitDatei *zIni )
|
|
|
-{
|
|
|
- if( !zIni->wertExistiert( "ServerId" ) )
|
|
|
- zIni->addWert( "ServerId", "0" );
|
|
|
- if( !zIni->wertExistiert( "ServerName" ) )
|
|
|
- zIni->addWert( "ServerName", "Name" );
|
|
|
- if( !zIni->wertExistiert( "ServerPort" ) )
|
|
|
- zIni->addWert( "ServerPort", "49144" );
|
|
|
- if( !zIni->wertExistiert( "ServerIP" ) )
|
|
|
- zIni->addWert( "ServerIP", "127.0.0.1" );
|
|
|
- if( !zIni->wertExistiert( "AdminServerPort" ) )
|
|
|
- zIni->addWert( "AdminServerPort", "49143" );
|
|
|
- if( !zIni->wertExistiert( "Aktiv" ) )
|
|
|
- zIni->addWert( "Aktiv", "FALSE" );
|
|
|
- if( !zIni->wertExistiert( "MaxClients" ) )
|
|
|
- zIni->addWert( "MaxClients", "50" );
|
|
|
- bool insert = 0;
|
|
|
- int id = *zIni->zWert( "ServerId" );
|
|
|
- if( id )
|
|
|
- {
|
|
|
- lock();
|
|
|
- if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE id = " ) += id ) )
|
|
|
- {
|
|
|
- unlock();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- int anz = datenbank->getZeilenAnzahl();
|
|
|
- unlock();
|
|
|
- insert = anz == 0;
|
|
|
- if( !insert )
|
|
|
- {
|
|
|
- lock();
|
|
|
- if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE server_status_id = 1 AND id = " ) += id ) )
|
|
|
- {
|
|
|
- unlock();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- int anz = datenbank->getZeilenAnzahl();
|
|
|
- unlock();
|
|
|
- if( !anz ) // Server läuft bereits
|
|
|
- return 0;
|
|
|
- }
|
|
|
- }
|
|
|
- if( insert || !id )
|
|
|
- { // Neuer Eintrag in Tabelle server_chat
|
|
|
- Text *befehl = new Text( "INSERT INTO server_chat( " );
|
|
|
- if( id )
|
|
|
- *befehl += "id, ";
|
|
|
- *befehl += "name, ip, port, admin_port, server_status_id, max_clients ) VALUES( ";
|
|
|
- if( id )
|
|
|
- {
|
|
|
- *befehl += id;
|
|
|
- *befehl += ", ";
|
|
|
- }
|
|
|
- *befehl += "'";
|
|
|
- *befehl += zIni->zWert( "ServerName" )->getText();
|
|
|
- *befehl += "', '";
|
|
|
- *befehl += zIni->zWert( "ServerIP" )->getText();
|
|
|
- *befehl += "', ";
|
|
|
- *befehl += zIni->zWert( "ServerPort" )->getText();
|
|
|
- *befehl += ", ";
|
|
|
- *befehl += zIni->zWert( "AdminServerPort" )->getText();
|
|
|
- *befehl += ", 1, ";
|
|
|
- *befehl += zIni->zWert( "MaxClients" )->getText();
|
|
|
- *befehl += " ) RETURNING id";
|
|
|
- lock();
|
|
|
- if( !datenbank->befehl( *befehl ) )
|
|
|
- {
|
|
|
- unlock();
|
|
|
- befehl->release();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- Result res = datenbank->getResult();
|
|
|
- unlock();
|
|
|
- befehl->release();
|
|
|
- if( !res.zeilenAnzahl )
|
|
|
- {
|
|
|
- res.destroy();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- zIni->setWert( "ServerId", res.values[ 0 ] );
|
|
|
- return 1;
|
|
|
- }
|
|
|
- else
|
|
|
- { // Alten Eintrag aus Tabelle server_chat ändern
|
|
|
- Text *befehl = new Text( "UPDATE server_chat SET name = '" );
|
|
|
- *befehl += zIni->zWert( "ServerName" )->getText();
|
|
|
- *befehl += "', port = ";
|
|
|
- *befehl += zIni->zWert( "ServerPort" )->getText();
|
|
|
- *befehl += ", ip = '";
|
|
|
- *befehl += zIni->zWert( "ServerIP" )->getText();
|
|
|
- *befehl += "', max_clients = ";
|
|
|
- *befehl += zIni->zWert( "MaxClients" )->getText();
|
|
|
- *befehl += ", admin_port = ";
|
|
|
- *befehl += zIni->zWert( "AdminServerPort" )->getText();
|
|
|
- *befehl += " WHERE id = ";
|
|
|
- *befehl += id;
|
|
|
- lock();
|
|
|
- bool ret = datenbank->befehl( *befehl );
|
|
|
- unlock();
|
|
|
- befehl->release();
|
|
|
- return ret;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
bool CSDatenbank::setServerStatus( int id, int status )
|
|
|
{
|
|
|
- Text *befehl = new Text( "UPDATE server_chat SET server_status_id = " );
|
|
|
+ Text *befehl = new Text( "UPDATE server SET server_status_id = " );
|
|
|
*befehl += status;
|
|
|
*befehl += "WHERE id = ";
|
|
|
*befehl += id;
|
|
@@ -261,7 +164,7 @@ bool CSDatenbank::setServerStatus( int id, int status )
|
|
|
|
|
|
bool CSDatenbank::setMaxClients( int id, int maxC )
|
|
|
{
|
|
|
- Text *befehl = new Text( "UPDATE server_chat SET max_clients = " );
|
|
|
+ Text *befehl = new Text( "UPDATE server SET max_tasks = " );
|
|
|
befehl->append( maxC );
|
|
|
befehl->append( " WHERE id = " );
|
|
|
befehl->append( id );
|
|
@@ -278,33 +181,9 @@ bool CSDatenbank::setMaxClients( int id, int maxC )
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-int CSDatenbank::getAdminPort( int id )
|
|
|
-{
|
|
|
- Text *befehl = new Text( "SELECT admin_port FROM server_chat WHERE id = " );
|
|
|
- befehl->append( id );
|
|
|
- lock();
|
|
|
- if( !datenbank->befehl( befehl->getText() ) )
|
|
|
- {
|
|
|
- unlock();
|
|
|
- befehl->release();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- Result res = datenbank->getResult();
|
|
|
- unlock();
|
|
|
- befehl->release();
|
|
|
- if( !res.zeilenAnzahl )
|
|
|
- {
|
|
|
- res.destroy();
|
|
|
- return 0;
|
|
|
- }
|
|
|
- int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
|
|
|
- res.destroy();
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
bool CSDatenbank::serverIstNichtPausiert( int id )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT server_status_id FROM server_chat WHERE id = " );
|
|
|
+ Text *befehl = new Text( "SELECT server_status_id FROM server WHERE id = " );
|
|
|
befehl->append( id );
|
|
|
lock();
|
|
|
if( !datenbank->befehl( befehl->getText() ) )
|
|
@@ -328,7 +207,7 @@ bool CSDatenbank::serverIstNichtPausiert( int id )
|
|
|
|
|
|
int CSDatenbank::getKlientAccountId( int klientId )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT account_id FROM account_clients WHERE client_id = " );
|
|
|
+ Text *befehl = new Text( "SELECT account_id FROM account_client WHERE client_id = " );
|
|
|
befehl->append( klientId );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
@@ -365,10 +244,10 @@ int CSDatenbank::getAccountFreunde( int accountId, Array< int > *fAccountId )
|
|
|
|
|
|
int CSDatenbank::getAccountOnlineFreunde( int accountId, Array< int > *fAccountId )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT freund.account_id AS freundId FROM freund, account_clients WHERE freund.freund_account_id = " );
|
|
|
+ Text *befehl = new Text( "SELECT freund.account_id AS freundId FROM freund, account_client WHERE freund.freund_account_id = " );
|
|
|
befehl->append( accountId );
|
|
|
befehl->append( " AND freund.account_id = account_clients.account_id "
|
|
|
- "UNION SELECT freund.freund_account_id AS freundId FROM freund, account_clients WHERE freund.account_id = " );
|
|
|
+ "UNION SELECT freund.freund_account_id AS freundId FROM freund, account_client WHERE freund.account_id = " );
|
|
|
befehl->append( accountId );
|
|
|
befehl->append( " AND freund.freund_account_id = account_clients.account_id " );
|
|
|
lock();
|
|
@@ -550,7 +429,7 @@ Text *CSDatenbank::getAccountRufName( int accountId )
|
|
|
|
|
|
bool CSDatenbank::accountIstOnline( int accountId )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT * FROM account_clients WHERE account_id = " );
|
|
|
+ Text *befehl = new Text( "SELECT * FROM account_client WHERE account_id = " );
|
|
|
befehl->append( accountId );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
@@ -575,8 +454,8 @@ bool CSDatenbank::accountIstImSpiel( int accountId )
|
|
|
|
|
|
int CSDatenbank::getChatServerId( int accountId )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT server_chat_clients.server_chat_id FROM server_chat_clients, account_clients "
|
|
|
- "WHERE server_chat_clients.client_id = account_clients.client_id AND account_clients.account_id = " );
|
|
|
+ Text *befehl = new Text( "SELECT a.server_id FROM server_client a, account_client b, server c "
|
|
|
+ "WHERE a.client_id = b.client_id AND and a.server_id = c.id AND c.server_typ_name = 'chat' AND b.account_id = " );
|
|
|
befehl->append( accountId );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|
|
@@ -595,7 +474,7 @@ int CSDatenbank::getChatServerId( int accountId )
|
|
|
|
|
|
bool CSDatenbank::getChatServerIpPort( int serverId, unsigned short *port, char **ip )
|
|
|
{
|
|
|
- Text *befehl = new Text( "SELECT port, ip FROM server_chat WHERE id = " );
|
|
|
+ Text *befehl = new Text( "SELECT port, ip FROM server WHERE id = " );
|
|
|
befehl->append( serverId );
|
|
|
lock();
|
|
|
datenbank->befehl( befehl->getText() );
|