浏览代码

An neue Datenbank angepasst

Kolja Strohm 7 年之前
父节点
当前提交
86dea1775d
共有 4 个文件被更改,包括 49 次插入181 次删除
  1. 34 155
      LoginServer/Datenbank.cpp
  2. 4 6
      LoginServer/Datenbank.h
  3. 1 9
      LoginServer/LoginServer.cpp
  4. 10 11
      LoginServer/main.cpp

+ 34 - 155
LoginServer/Datenbank.cpp

@@ -1,24 +1,32 @@
 #include "Datenbank.h"
+#include <iostream>
 
 // Inhalt der LSDatenbank Klasse aus Datenbank.h
 // Konstruktor
 LSDatenbank::LSDatenbank( InitDatei *zIni )
 {
-	if( !zIni->wertExistiert( "DBBenutzer" ) )
-		zIni->addWert( "DBBenutzer", "loginserveru" );
-	if( !zIni->wertExistiert( "DBPasswort" ) )
-		zIni->addWert( "DBPasswort", "LTLoginServerPW" );
-	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 << "LS: 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 LSDatenbank::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 LSDatenbank::adminHatRecht( int id, int recht )
 
 bool LSDatenbank::proveKlient( int num, int sNum )
 {
-	Text *befehl = new Text( "SELECT * FROM server_login_clients WHERE server_login_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 LSDatenbank::proveKlient( int num, int sNum )
 Text *LSDatenbank::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 *LSDatenbank::getKlientKey( int cId )
 
 void LSDatenbank::unregisterKlient( int num, int sNum )
 {
-	Text *befehl = new Text( "DELETE FROM server_login_clients WHERE client_id = " );
+	Text *befehl = new Text( "DELETE FROM server_client WHERE client_id = " );
 	befehl->append( num );
-	befehl->append( " AND server_login_id = " );
+	befehl->append( " AND server_id = " );
 	befehl->append( sNum );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -126,7 +134,7 @@ void LSDatenbank::unregisterKlient( int num, int sNum )
 	unlock();
 	if( za == 1 )
 	{
-		befehl->setText( "UPDATE server_login 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 LSDatenbank::unregisterKlient( int num, int sNum )
 	befehl->release();
 }
 
-bool LSDatenbank::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_login 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_login 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_login
-		Text *befehl = new Text( "INSERT INTO server_login( " );
-		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_login ändern
-		Text *befehl = new Text( "UPDATE server_login 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 LSDatenbank::setServerStatus( int id, int status )
 {
-	Text *befehl = new Text( "UPDATE server_login 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 LSDatenbank::setServerStatus( int id, int status )
 
 bool LSDatenbank::setMaxClients( int id, int maxC )
 {
-	Text *befehl = new Text( "UPDATE server_login 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 LSDatenbank::setMaxClients( int id, int maxC )
 	return ret;
 }
 
-int LSDatenbank::getAdminPort( int id )
-{
-	Text *befehl = new Text( "SELECT admin_port FROM server_login 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 LSDatenbank::serverIstNichtPausiert( int id )
 {
-	Text *befehl = new Text( "SELECT server_status_id FROM server_login WHERE id = " );
+	Text *befehl = new Text( "SELECT server_status_id FROM server WHERE id = " );
 	befehl->append( id );
 	lock();
 	if( !datenbank->befehl( befehl->getText() ) )
@@ -332,11 +211,11 @@ int LSDatenbank::getAccountId( char *name, char *pass )
 	Text n( name );
 	n.ersetzen( "'", "''" );
 	befehl->append( (char*)n );
-	befehl->append( "' AND passwort = '" );
+	befehl->append( "' AND passwort = md5( '" );
 	Text p( pass );
 	p.ersetzen( "'", "''" );
 	befehl->append( (char*)p );
-	befehl->append( "'" );
+	befehl->append( "' )" );
 	lock();
 	datenbank->befehl( befehl->getText() );
 	Result res = datenbank->getResult();
@@ -463,7 +342,7 @@ int LSDatenbank::logoutKlient( int klientId, Array< int > *ret )
 
 Text **LSDatenbank::getChatServerIpPort( int id )
 {
-	Text *befehl = new Text( "SELECT ip, port FROM server_chat WHERE id = " );
+	Text *befehl = new Text( "SELECT ip, port FROM server WHERE id = " );
 	befehl->append( id );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -483,7 +362,7 @@ Text **LSDatenbank::getChatServerIpPort( int id )
 
 bool LSDatenbank::getSpielServerPortIp( int spielErstelltId, unsigned short *port, Text *ip )
 {
-	Text *befehl = new Text( "SELECT a.port, a.ip FROM server_spiel a, spiel_erstellt b "
+	Text *befehl = new Text( "SELECT a.port, a.ip FROM server a, spiel_erstellt b "
 							 "WHERE a.id = b.server_spiel_id AND b.id = " );
 	befehl->append( spielErstelltId );
 	lock();
@@ -546,8 +425,8 @@ int LSDatenbank::kickSpielerAusGruppe( int gruppeId, int accountId )
 
 bool LSDatenbank::getChatServerPortIp( int accountId, unsigned short *port, Text *ip )
 {
-	Text *befehl = new Text( "SELECT a.port, a.ip FROM server_chat AS a, server_chat_clients AS b, account_clients AS c "
-							 "WHERE c.client_id = b.client_id AND a.id = b.server_chat_id AND c.account_id = " );
+	Text *befehl = new Text( "SELECT a.port, a.ip FROM server a, server_client b, account_client c "
+							 "WHERE a.server_typ_id = 'chat' AND c.client_id = b.client_id AND a.id = b.server_id AND c.account_id = " );
 	befehl->append( accountId );
 	lock();
 	if( !datenbank->befehl( befehl->getText() ) )

+ 4 - 6
LoginServer/Datenbank.h

@@ -10,10 +10,10 @@ using namespace sql;
 
 namespace Admin_Recht
 {
-	const int LSStarten = 0x0000000C;
-	const int LSBeenden = 0x0000000D;
-	const int LSPausieren = 0x0000000E;
-	const int LSMCChange = 0x0000000F;
+	const int LSStarten = 12;
+	const int LSBeenden = 13;
+	const int LSPausieren = 14;
+	const int LSMCChange = 15;
 }
 
 class LSDatenbank
@@ -36,10 +36,8 @@ public:
 	bool proveKlient( int num, int sNum );
 	Text *getKlientKey( int cId );
 	void unregisterKlient( int num, int sNum );
-	bool serverAnmelden( InitDatei *zIni );
 	bool setServerStatus( int id, int status );
 	bool setMaxClients( int id, int maxC );
-	int getAdminPort( int id );
 	bool serverIstNichtPausiert( int id );
 	int getAccountId( char *name, char *pass );
 	int loginKlient( int klientId, int accountId );

+ 1 - 9
LoginServer/LoginServer.cpp

@@ -17,19 +17,11 @@ LoginServer::LoginServer( InitDatei *zIni )
 	gesendet = 0;
 	fehler = new Text();
 	ini = zIni->getThis();
-	if( !db->serverAnmelden( zIni ) )
-	{
-		std::cout << "LS: Der Server konnte nicht in die Datenbank eingetragen werden:\n";
-		Text *txt = db->getLetzterFehler();
-		std::cout << txt->getText() << "\nDas Programm wird beendet.";
-		txt->release();
-		exit( 1 );
-	}
 	id = *zIni->zWert( "ServerId" );
 	server = new Server();
 	aServer = new Server();
 	std::cout << "LS: Starten des Admin Servers...\n";
-	if( !aServer->verbinde( (unsigned short)db->getAdminPort( id ), 10 ) )
+	if( !aServer->verbinde( (unsigned short)TextZuInt( ini->zWert( "AdminServerPort" )->getText(), 10 ), 10 ) )
 	{
 		std::cout << "LS: Der Admin Server konnte nicht gestartet werden. Das Programm wird beendet.\n";
 		exit( 1 );

+ 10 - 11
LoginServer/main.cpp

@@ -1,20 +1,12 @@
 #include "LoginServer.h"
-#ifdef WIN32
-#include <main.h>
-#endif
 #include <Zeit.h>
 #include <iostream>
 #include <fstream>
 #include <Globals.h>
 
-#ifdef WIN32
-int KSGStart Framework::Start( Startparam p )
-{
-#else
 int main()
 {
     Framework::initFramework();
-#endif
 	Zeit *z = getZeit();
 	Text *pfad = new Text( "../log/login/" );
 	pfad->append( z->getZeit( "y-m-d h-i-s.log" ) );
@@ -35,6 +27,16 @@ int main()
 		dat->release();
 		exit( 1 );
 	}
+    const char *wichtig[] = { "ServerId", "DBBenutzer", "DBPasswort", "DBName", "DBIP", "DBPort", "Aktiv" };
+    for( const char *w : wichtig )
+    {
+        if( !dat->wertExistiert( w ) )
+        {
+            std::cout << "LS: error: Der Wert '" << w << "' wurde nicht gefunden. Das Programm wird geschlossen.\n";
+            dat->release();
+            exit( 1 );
+        }
+    }
 
 	LoginServer *lServer = new LoginServer( dat );
 
@@ -43,13 +45,10 @@ int main()
 
 	lServer->ende();
 	lServer->release();
-	dat->speichern();
 	dat->release();
 	std::cout << "LS: Der Server ist heruntergefahren.\n";
 	file.close();
 	std::cout.rdbuf( sbuf );
-#ifndef WIN32
     Framework::releaseFramework();
-#endif
 	return 0;
 }