123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185 |
- #include "Datenbank.h"
- #include <iostream>
- #include <Datei.h>
- #ifndef WIN32
- #include <spawn.h>
- #endif
- // Inhalt der AppSDatenbank Klasse aus Datenbank.h
- // Konstruktor
- AppSDatenbank::AppSDatenbank( InitDatei *zDat )
- {
- datenbank = new Datenbank( zDat->zWert( "DBBenutzer" )->getText(), zDat->zWert( "DBPasswort" )->getText(),
- zDat->zWert( "DBName" )->getText(), zDat->zWert( "DBIP" )->getText(),
- (unsigned short)TextZuInt( zDat->zWert( "DBPort" )->getText(), 10 ) );
- if( !datenbank->istOk() )
- {
- std::cout << "AppS: Es konnte keine Verbindung zur Datenbank hergestellt werden:\n";
- Text *txt = datenbank->getLetzterFehler();
- std::cout << txt->getText() << "\nDas Programm wird beendet.";
- txt->release();
- exit( 1 );
- }
- InitializeCriticalSection( &ths );
- ref = 1;
- Text befehl = "SELECT port, admin_port FROM server WHERE id = ";
- befehl += zDat->zWert( "ServerId" )->getText();
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( res.zeilenAnzahl == 1 )
- {
- zDat->addWert( "AppSPort", res.values[ 0 ] );
- zDat->addWert( "AppSAPort", res.values[ 1 ] );
- }
- res.destroy();
- }
- // Detruktor
- AppSDatenbank::~AppSDatenbank()
- {
- datenbank->release();
- DeleteCriticalSection( &ths );
- }
- // nicht constant
- void AppSDatenbank::lock()
- {
- EnterCriticalSection( &ths );
- }
- void AppSDatenbank::unlock()
- {
- LeaveCriticalSection( &ths );
- }
- int AppSDatenbank::istAdministrator( const char *name, const char *passwort )
- {
- Text *befehl = new Text( "SELECT id FROM benutzer WHERE name = '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "' AND passwort = md5('" );
- Text p( passwort );
- p.ersetzen( "'", "''" );
- befehl->append( (char*)p );
- befehl->append( "')" );
- lock();
- datenbank->befehl( befehl->getText() );
- Result res = datenbank->getResult();
- unlock();
- befehl->release();
- int ret = 0;
- if( res.zeilenAnzahl > 0 )
- ret = TextZuInt( res.values[ 0 ].getText(), 10 );
- res.destroy();
- return ret;
- }
- bool AppSDatenbank::adminHatRecht( int id, int recht )
- {
- Text *befehl = new Text( "SELECT * FROM benutzer_rechte WHERE benutzer_id = " );
- befehl->append( id );
- befehl->append( " AND rechte_id = " );
- befehl->append( recht );
- lock();
- datenbank->befehl( befehl->getText() );
- int ret = datenbank->getZeilenAnzahl();
- unlock();
- befehl->release();
- return ret != 0;
- }
- Text *AppSDatenbank::getKey()
- {
- lock();
- datenbank->befehl( "SELECT get_next_schluessel()" );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- Text *ret = new Text( res.values[ 0 ].getText() );
- res.destroy();
- return ret;
- }
- int AppSDatenbank::login( char *name, char *passwort )
- {
- Text befehl = "SELECT id FROM account WHERE name = '";
- befehl += name;
- befehl += "' AND passwort = '";
- befehl += passwort;
- befehl += "'";
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- bool AppSDatenbank::istNeu( int account )
- {
- Text befehl = "SELECT * from app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int zeilen = datenbank->getZeilenAnzahl();
- unlock();
- return zeilen == 0;
- }
- int AppSDatenbank::getWeltBloeckeScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.bloecke, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.bloecke DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getWeltMauerScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.mauer, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.mauer DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getWeltFarbenScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.farben, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.farben DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getWeltFangenScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.fangen, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.fangen DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getWeltRennenScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.rennen, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.rennen DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getWeltGesamtScore( int anz, Array< int > *score, RCArray< Text > *namen )
- {
- Text befehl = "SELECT a.gesamt, b.ruf_name FROM app_5_minigames_score a, account b WHERE b.id = a.account_id ORDER BY a.gesamt DESC LIMIT ";
- befehl += anz;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- score->set( res.values[ i * 2 ], i );
- namen->set( new Text( res.values[ i * 2 + 1 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getBloeckeScore( int account )
- {
- Text befehl = "SELECT bloecke FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getMauerScore( int account )
- {
- Text befehl = "SELECT mauer FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getMauerZeit( int account, int level )
- {
- Text befehl = "SELECT sekunden FROM app_5_minigames_mauer_score WHERE account_id = ";
- befehl += account;
- befehl += " AND level = ";
- befehl += level;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getFarbenScore( int account )
- {
- Text befehl = "SELECT farben FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getFangenScore( int account )
- {
- Text befehl = "SELECT fangen FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getRennenScore( int account )
- {
- Text befehl = "SELECT rennen FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getGesamtScore( int account )
- {
- Text befehl = "SELECT gesamt FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return 0;
- int ret = res.values[ 0 ];
- res.destroy();
- return ret;
- }
- void AppSDatenbank::setBloeckeScore( int account, int score )
- {
- if( score < getBloeckeScore( account ) )
- return;
- Text befehl = "UPDATE app_5_minigames_score SET bloecke = ";
- befehl += score;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_score( bloecke, account_id ) VALUES( ";
- befehl += score;
- befehl += ", ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkGesamtScore( account );
- }
- void AppSDatenbank::setFarbenScore( int account, int score )
- {
- if( score < getFarbenScore( account ) )
- return;
- Text befehl = "UPDATE app_5_minigames_score SET farben = ";
- befehl += score;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_score( farben, account_id ) VALUES( ";
- befehl += score;
- befehl += ", ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkGesamtScore( account );
- }
- void AppSDatenbank::setFangenScore( int account, int score )
- {
- if( score < getFangenScore( account ) )
- return;
- Text befehl = "UPDATE app_5_minigames_score SET fangen = ";
- befehl += score;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_score( fangen, account_id ) VALUES( ";
- befehl += score;
- befehl += ", ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkGesamtScore( account );
- }
- void AppSDatenbank::setRennenScore( int account, int score )
- {
- if( score < getRennenScore( account ) )
- return;
- Text befehl = "UPDATE app_5_minigames_score SET rennen = ";
- befehl += score;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_score( rennen, account_id ) VALUES( ";
- befehl += score;
- befehl += ", ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkGesamtScore( account );
- }
- void AppSDatenbank::setMauerZeit( int account, int level, int zeit )
- {
- if( zeit > getMauerZeit( account, level ) && getMauerZeit( account, level ) != 0 )
- return;
- if( zeit > 600 )
- zeit = 600;
- Text befehl = "UPDATE app_5_minigames_mauer_score SET sekunden = ";
- befehl += zeit;
- befehl += " WHERE account_id = ";
- befehl += account;
- befehl += " AND level = ";
- befehl += level;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_mauer_score( account_id, level, sekunden ) VALUES( ";
- befehl += account;
- befehl += ", ";
- befehl += level;
- befehl += ", ";
- befehl += zeit;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkMauerScore( account );
- }
- void AppSDatenbank::calkMauerScore( int account )
- {
- Text befehl = "SELECT count( sekunden ), sum( sekunden ) FROM app_5_minigames_mauer_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( (int)res.values[ 0 ] == 0 )
- return;
- int score = (int)res.values[ 0 ] * 600 - (int)res.values[ 1 ];
- res.destroy();
- befehl = "UPDATE app_5_minigames_score SET mauer = ";
- befehl += score;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- int z = datenbank->getZeilenAnzahl();
- unlock();
- if( !z )
- {
- befehl = "INSERT INTO app_5_minigames_score( mauer, account_id ) VALUES( ";
- befehl += score;
- befehl += ", ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- calkGesamtScore( account );
- }
- void AppSDatenbank::calkGesamtScore( int account )
- {
- Text befehl = "SELECT bloecke, mauer, farben, fangen, rennen FROM app_5_minigames_score WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- if( !res.zeilenAnzahl )
- return;
- int gesamt = ( (int)res.values[ 0 ] + (int)res.values[ 1 ] + (int)res.values[ 2 ] + (int)res.values[ 3 ] + (int)res.values[ 4 ] ) / 5;
- res.destroy();
- befehl = "UPDATE app_5_minigames_score SET gesamt = ";
- befehl += gesamt;
- befehl += " WHERE account_id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- int AppSDatenbank::pruefNeuAccount( const char *name, const char *eMail )
- {
- Text *befehl = new Text( "SELECT account_neu_alt_check( '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "', '" );
- Text m( eMail );
- m.ersetzen( "'", "''" );
- befehl->append( (char*)m );
- befehl->append( "' )" );
- lock();
- datenbank->befehl( befehl->getText() );
- Result res = datenbank->getResult();
- unlock();
- if( res.values[ 0 ].istGleich( "t" ) )
- {
- befehl->release();
- res.destroy();
- return 0;
- }
- else
- {
- res.destroy();
- befehl->setText( "SELECT account.id FROM account, account_neu WHERE account.name = '" );
- befehl->append( (char*)n );
- befehl->append( "' OR account_neu.name = '" );
- befehl->append( (char*)n );
- befehl->append( "'" );
- lock();
- datenbank->befehl( befehl->getText() );
- res = datenbank->getResult();
- unlock();
- if( res.zeilenAnzahl > 0 )
- {
- befehl->release();
- res.destroy();
- return 1;
- }
- else
- {
- res.destroy();
- befehl->release();
- return 2;
- }
- }
- }
- bool AppSDatenbank::neuAccount( const char *name, const char *pass, const char *geheim, const char *eMail, const char *gebDatum, InitDatei *zIni )
- {
- Text *befehl = new Text( "INSERT INTO account_neu( name, passwort, geheimnis, e_mail, geb_datum ) VALUES ( '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "', '" );
- Text p( pass );
- p.ersetzen( "'", "''" );
- befehl->append( (char*)p );
- befehl->append( "', '" );
- Text g( geheim );
- g.ersetzen( "'", "''" );
- befehl->append( (char*)g );
- befehl->append( "', '" );
- Text m( eMail );
- m.ersetzen( "'", "''" );
- befehl->append( (char*)m );
- befehl->append( "', '" );
- Text d( gebDatum );
- d.ersetzen( "'", "''" );
- befehl->append( (char*)d );
- befehl->append( "' )" );
- lock();
- if( datenbank->befehl( befehl->getText() ) )
- {
- unlock();
- befehl->setText( "SELECT schluessel FROM account_neu WHERE name = '" );
- befehl->append( (char*)n );
- befehl->append( "'" );
- lock();
- datenbank->befehl( befehl->getText() );
- Result res = datenbank->getResult();
- unlock();
- Datei md;
- if( res.zeilenAnzahl )
- {
- md.setDatei( res.values[ 0 ].getText() );
- if( md.open( Datei::Style::schreiben ) )
- {
- Text txt = eMail;
- txt += "\naccount activation\n Account Details:\n";
- txt += "\r\nname: ";
- txt += name;
- txt += "\r\npassword: ";
- txt += pass;
- txt += "\r\nsecret: ";
- txt += geheim;
- txt += "\r\ndate of birth: ";
- txt += gebDatum;
- txt += "\r\nactivation key: ";
- txt += res.values[ 0 ].getText();
- md.schreibe( txt, txt.getLength() );
- md.close();
- // e_mail senden
- #ifdef WIN32
- PROCESS_INFORMATION prozessinfo1;
- STARTUPINFO startinfo1;
- startinfo1.cb = sizeof( STARTUPINFOW );
- ZeroMemory( &startinfo1, sizeof( STARTUPINFOW ) );
- Text cmdl = zIni->zWert( "Java" )->getText();
- cmdl += " Mailer ";
- cmdl += res.values[ 0 ].getText();
- if( CreateProcess( 0, cmdl, 0, 0, 0, 0, 0, 0, &startinfo1, &prozessinfo1 ) == 1 )
- CloseHandle( prozessinfo1.hThread );
- #else
- pid_t pid;
- char *pargs[] = { zIni->zWert( "Java" )->getText(), ( char* )"Mailer", res.values[ 0 ].getText(), (char*)0 };
- posix_spawn( &pid, zIni->zWert( "Java" )->getText(), 0, 0, pargs, 0 );
- #endif
- }
- }
- res.destroy();
- befehl->release();
- return 1;
- }
- unlock();
- befehl->release();
- return 0;
- }
- void AppSDatenbank::neuAccountAbbruch( const char *name )
- {
- Text *befehl = new Text( "DELETE FROM account_neu WHERE name = '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "'" );
- lock();
- datenbank->befehl( befehl->getText() );
- unlock();
- befehl->release();
- }
- bool AppSDatenbank::aktiviereAccount( const char *name, const char *key )
- {
- Text *befehl = new Text( "SELECT account_aktivieren( '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "', '" );
- Text s( key );
- s.ersetzen( "'", "''" );
- befehl->append( (char*)s );
- befehl->append( "' )" );
- lock();
- if( datenbank->befehl( befehl->getText() ) )
- {
- befehl->release();
- Result res = datenbank->getResult();
- unlock();
- if( res.values[ 0 ].istGleich( "f" ) )
- {
- res.destroy();
- return 0;
- }
- return 1;
- }
- unlock();
- befehl->release();
- return 0;
- }
- char AppSDatenbank::suchBestaetigung( const char *name, const char *pass )
- {
- Text *befehl = new Text( "select such_account_bestaetigung( '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "', '" );
- Text p( pass );
- p.ersetzen( "'", "''" );
- befehl->append( (char*)p );
- befehl->append( "' )" );
- lock();
- datenbank->befehl( befehl->getText() );
- Result res = datenbank->getResult();
- unlock();
- befehl->release();
- char ret = (char)TextZuInt( res.values[ 0 ].getText(), 10 );
- res.destroy();
- return ret;
- }
- void AppSDatenbank::sendeErstellEMail( const char *name, InitDatei *zIni )
- {
- Text *befehl = new Text( "SELECT passwort, geheimnis, e_mail, geb_datum, schluessel FROM account_neu WHERE name = '" );
- Text n( name );
- n.ersetzen( "'", "''" );
- befehl->append( (char*)n );
- befehl->append( "'" );
- lock();
- datenbank->befehl( befehl->getText() );
- Result res = datenbank->getResult();
- unlock();
- befehl->release();
- Datei md;
- if( res.zeilenAnzahl )
- {
- md.setDatei( res.values[ 4 ].getText() );
- if( md.open( Datei::Style::schreiben ) )
- {
- Text txt = res.values[ 2 ].getText();
- txt += "\naccount activation\n Account Details:\n";
- txt += "\r\nname: ";
- txt += name;
- txt += "\r\npassword: ";
- txt += res.values[ 0 ].getText();
- txt += "\r\nsecret: ";
- txt += res.values[ 1 ].getText();
- txt += "\r\ndate of birth: ";
- txt += res.values[ 3 ].getText();
- txt += "\r\nactivation key: ";
- txt += res.values[ 4 ].getText();
- md.schreibe( txt, txt.getLength() );
- md.close();
- // e_mail senden
- #ifdef WIN32
- PROCESS_INFORMATION prozessinfo1;
- STARTUPINFO startinfo1;
- startinfo1.cb = sizeof( STARTUPINFOW );
- ZeroMemory( &startinfo1, sizeof( STARTUPINFOW ) );
- Text cmdl = zIni->zWert( "Java" )->getText();
- cmdl += " Mailer ";
- cmdl += res.values[ 4 ].getText();
- if( CreateProcess( 0, cmdl, 0, 0, 0, 0, 0, 0, &startinfo1, &prozessinfo1 ) == 1 )
- CloseHandle( prozessinfo1.hThread );
- #else
- pid_t pid;
- char *pargs[] = { zIni->zWert( "Java" )->getText(), ( char* )"Mailer", res.values[ 4 ].getText(), (char*)0 };
- posix_spawn( &pid, zIni->zWert( "Java" )->getText(), 0, 0, pargs, 0 );
- #endif
- }
- }
- res.destroy();
- }
- void AppSDatenbank::markballsFinish( int level, int sek, int account, char *device, int diff, int kupfer, int gId )
- {
- if( !device && kupfer > 0 )
- {
- Text befehl = "UPDATE account SET kupfer = kupfer + ";
- befehl += kupfer;
- befehl += " WHERE id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- if( gId > 0 )
- {
- Text befehl = "SELECT markballs_gegenstand_id FROM markballs_gegenstand_account WHERE markballs_gegenstand_id = ";
- befehl += gId;
- befehl += " AND diff = ";
- befehl += diff;
- if( !device )
- {
- befehl += " AND account_id = ";
- befehl += account;
- }
- else
- {
- befehl += " AND device = '";
- befehl += device;
- befehl += "'";
- }
- lock();
- datenbank->befehl( befehl );
- int rows = datenbank->getZeilenAnzahl();
- unlock();
- if( !rows )
- {
- befehl = "INSERT INTO markballs_gegenstand_account( account_id, device, markballs_gegenstand_id, diff ) VALUES( ";
- if( !device )
- {
- befehl += account;
- befehl += ", NULL, ";
- }
- else
- {
- befehl += "NULL, '";
- befehl += device;
- befehl += "', ";
- }
- befehl += gId;
- befehl += ", ";
- befehl += diff;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- }
- Text befehl = "SELECT * FROM markballs WHERE level = ";
- befehl += level;
- befehl += " AND diff = ";
- befehl += diff;
- lock();
- datenbank->befehl( befehl );
- int rows = datenbank->getZeilenAnzahl();
- unlock();
- if( !rows )
- {
- befehl = "INSERT INTO markballs( level, diff ) VALUES( ";
- befehl += level;
- befehl += ", ";
- befehl += diff;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- Text spalte = "verloren";
- if( sek > 0 && sek <= 10 )
- spalte = "s_1_10";
- else if( sek > 10 && sek <= 20 )
- spalte = "s_11_20";
- else if( sek > 20 && sek <= 30 )
- spalte = "s_21_30";
- else if( sek > 30 && sek <= 40 )
- spalte = "s_31_40";
- else if( sek > 40 && sek <= 50 )
- spalte = "s_41_50";
- else if( sek > 50 && sek <= 60 )
- spalte = "s_51_60";
- else if( sek > 60 )
- spalte = "more";
- befehl = "UPDATE markballs SET ";
- befehl += spalte.getText();
- befehl += " = ";
- befehl += spalte.getText();
- befehl += " + 1, account_id = ";
- if( account )
- befehl += account;
- else
- befehl += "null";
- befehl += ", device = ";
- if( !device )
- befehl += "null";
- else
- {
- befehl += "'";
- befehl += device;
- befehl += "'";
- }
- befehl += ", best = ";
- befehl += sek;
- befehl += " WHERE level = ";
- befehl += level;
- befehl += " AND diff = ";
- befehl += diff;
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- int AppSDatenbank::getMarkballsScore( Array< int > *level, Array< int > *score, RCArray< Text > *name, int diff )
- {
- Text befehl = "SELECT a.level, a.best, b.ruf_name from markballs a left join account b ON a.account_id = b.id WHERE a.diff = ";
- befehl += diff;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- level->set( res.values[ i * 3 ], i );
- score->set( res.values[ i * 3 + 1 ], i );
- name->set( new Text( res.values[ i * 3 + 2 ].getText() ), i );
- }
- res.destroy();
- return ret;
- }
- int AppSDatenbank::getMarkballsScore( Array< int > *level, Array< int > *score, int diff )
- {
- Text befehl = "SELECT level, best from markballs WHERE diff = ";
- befehl += diff;
- lock();
- datenbank->befehl( befehl );
- Result res = datenbank->getResult();
- unlock();
- int ret = res.zeilenAnzahl;
- for( int i = 0; i < ret; i++ )
- {
- level->set( res.values[ i * 2 ], i );
- score->set( res.values[ i * 2 + 1 ], i );
- }
- res.destroy();
- return ret;
- }
- void AppSDatenbank::deviceAccount( char *device, int account )
- {
- Text befehl = "UPDATE markballs SET account_id = ";
- befehl += account;
- befehl += ", device = null WHERE account_id IS NULL AND device = '";
- befehl += device;
- befehl += "'";
- lock();
- datenbank->befehl( befehl );
- unlock();
- befehl = "SELECT app_device_to_account( '";
- befehl += device;
- befehl += "', ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- befehl = "UPDATE markballs_fortschritt SET account_id = ";
- befehl += account;
- befehl += ", device = null WHERE account_id IS NULL AND device = '";
- befehl += device;
- befehl += "'";
- lock();
- datenbank->befehl( befehl );
- unlock();
- befehl = "UPDATE markballs_gegenstand_account SET account_id = ";
- befehl += account;
- befehl += ", device = null WHERE account_id IS NULL AND device = '";
- befehl += device;
- befehl += "'";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- int AppSDatenbank::getGegenstaende( int accountId, char *device, Array< int > *gId, Array< int > *diff )
- {
- Text befehl = "SELECT markballs_gegenstand_id, diff FROM markballs_gegenstand_account WHERE ";
- if( !device )
- {
- befehl += " account_id = ";
- befehl += accountId;
- }
- else
- {
- befehl += " device = '";
- befehl += device;
- befehl += "'";
- }
- lock();
- datenbank->befehl( befehl );
- Result r = datenbank->getResult();
- unlock();
- if( !r.zeilenAnzahl )
- {
- r.destroy();
- return 0;
- }
- for( int i = 0; i < r.zeilenAnzahl; i++ )
- {
- gId->add( (int)r.values[ i * 2 ] );
- diff->add( (int)r.values[ i * 2 + 1 ] );
- }
- int ret = r.zeilenAnzahl;
- r.destroy();
- return ret;
- }
- int AppSDatenbank::getKupfer( int accountId )
- {
- Text befehl = "SELECT kupfer FROM account WHERE id = ";
- befehl += accountId;
- lock();
- datenbank->befehl( befehl );
- Result r = datenbank->getResult();
- unlock();
- int ret = r.zeilenAnzahl ? (int)r.values[ 0 ] : 0;
- r.destroy();
- return ret;
- }
- int AppSDatenbank::getMarkballsFortschritt( int accountId, char *device, Array< int > *f )
- {
- Text befehl = "SELECT level, diff FROM markballs_fortschritt WHERE ";
- if( !device )
- {
- befehl += " account_id = ";
- befehl += accountId;
- }
- else
- {
- befehl += " device = '";
- befehl += device;
- befehl += "'";
- }
- befehl += " ORDER BY diff";
- lock();
- datenbank->befehl( befehl );
- Result r = datenbank->getResult();
- unlock();
- int anz = 0;
- int lastDiff = 0;
- for( int i = 0; i < r.zeilenAnzahl; i++, anz++ )
- {
- int l = (int)r.values[ i * 2 ];
- int d = (int)r.values[ i * 2 + 1 ];
- for( int j = lastDiff + 1; j < d; j++, anz++ )
- f->add( 0 );
- f->add( l );
- lastDiff = d;
- }
- r.destroy();
- return anz;
- }
- void AppSDatenbank::curvesnakeFinish( int score, int accId, char *device, int map, int kupfer )
- {
- Text befehl = "SELECT id FROM curvesnake WHERE map = ";
- befehl += map;
- lock();
- datenbank->befehl( befehl );
- int anz = datenbank->getZeilenAnzahl();
- unlock();
- if( !anz )
- {
- befehl = "INSERT INTO curvesnake( map, score ) VALUES( ";
- befehl += map;
- befehl += ", 0 )";
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- befehl = "UPDATE curvesnake SET score = ";
- befehl += score;
- if( device )
- {
- befehl += ", device = '";
- befehl += device;
- befehl += "'";
- }
- else
- {
- befehl += ", account_id = ";
- befehl += accId;
- }
- befehl += " WHERE map = ";
- befehl += map;
- lock();
- datenbank->befehl( befehl );
- unlock();
- if( !device && kupfer )
- {
- befehl = "UPDATE account SET kupfer = kupfer + ";
- befehl += kupfer;
- befehl += " WHERE id = ";
- befehl += accId;
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- }
- int AppSDatenbank::getCurvesnakeScore( Array< int > *map, Array< int > *score )
- {
- lock();
- datenbank->befehl( "SELECT map, score from curvesnake ORDER BY map" );
- Result r = datenbank->getResult();
- unlock();
- for( int i = 0; i < r.zeilenAnzahl; i++ )
- {
- map->add( r.values[ i * 2 ] );
- score->add( r.values[ i * 2 + 1 ] );
- }
- int ret = r.zeilenAnzahl;
- r.destroy();
- return ret;
- }
- int AppSDatenbank::getCurvesnakeMapList( Array< int > *map, int account )
- {
- Text befehl = "SELECT id from curvesnake_map WHERE preis = 0 ";
- if( account )
- {
- befehl += "UNION SELECT map_id FROM curvesnake_map_account WHERE account_id = ";
- befehl += account;
- }
- lock();
- datenbank->befehl( befehl );
- Result r = datenbank->getResult();
- unlock();
- for( int i = 0; i < r.zeilenAnzahl; i++ )
- map->add( r.values[ i ] );
- int ret = r.zeilenAnzahl;
- r.destroy();
- return ret;
- }
- void AppSDatenbank::addKupfer( int account, int kupfer )
- {
- Text befehl = "UPDATE account SET kupfer = kupfer + ";
- befehl += kupfer;
- befehl += " WHERE id = ";
- befehl += account;
- lock();
- datenbank->befehl( befehl );
- unlock();
- }
- bool AppSDatenbank::getFreeAds( int account )
- {
- Text befehl = "SELECT get_account_free_ad( ";
- befehl += account;
- befehl += " )";
- lock();
- datenbank->befehl( befehl );
- Result r = datenbank->getResult();
- unlock();
- if( r.zeilenAnzahl < 1 )
- {
- r.destroy();
- return 0;
- }
- bool ret = r.values[ 0 ].istGleich( "t" );
- r.destroy();
- return ret;
- }
- // Reference Counting
- AppSDatenbank *AppSDatenbank::getThis()
- {
- ref++;
- return this;
- }
- AppSDatenbank *AppSDatenbank::release()
- {
- ref--;
- if( !ref )
- delete this;
- return 0;
- }
|