| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683 |
- /*
- * Copyright (c) 2014, Oculus VR, Inc.
- * All rights reserved.
- *
- * This source code is licensed under the BSD-style license found in the
- * LICENSE file in the root directory of this source tree. An additional grant
- * of patent rights can be found in the PATENTS file in the same directory.
- *
- */
- #include "Lobby2Message_PGSQL.h"
- using namespace RakNet;
- STATIC_FACTORY_DEFINITIONS(Lobby2MessageFactory_PGSQL,Lobby2MessageFactory_PGSQL);
- unsigned int RakNet::GetUserRowFromHandle(RakNet::RakString& userName, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT userId_pk,handle from lobby2.users WHERE handleLower=lower(%s)", userName.C_String());
- if (result)
- {
- unsigned int primaryKey;
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned>0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&primaryKey, result, 0, "userId_pk");
- PostgreSQLInterface::PQGetValueFromBinary(&userName, result, 0, "handle");
- PQclear(result);
- return primaryKey;
- }
- PQclear(result);
- return 0;
- }
- return 0;
- }
- unsigned int RakNet::GetClanIdFromHandle(RakNet::RakString clanName, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT clanId_pk from lobby2.clans WHERE clanHandleLower=lower(%s)", clanName.C_String());
- if (result)
- {
- unsigned int primaryKey;
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned>0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&primaryKey, result, 0, "clanId_pk");
- PQclear(result);
- return primaryKey;
- }
- PQclear(result);
- return 0;
- }
- return 0;
- }
- bool RakNet::IsClanLeader(RakNet::RakString clanName, unsigned int userId, PostgreSQLInterface *pgsql)
- {
- unsigned int clanId = GetClanIdFromHandle(clanName, pgsql);
- if (clanId==0)
- return false;
- return IsClanLeader(clanId, userId, pgsql);
- }
- unsigned int RakNet::GetClanLeaderId(unsigned int clanId, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT leaderUserId_fk FROM lobby2.clans WHERE clanId_pk=%i", clanId);
- if (result==0)
- return 0;
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- return 0;
- }
- int leaderId;
- PostgreSQLInterface::PQGetValueFromBinary(&leaderId, result, 0, "leaderUserId_fk");
- PQclear(result);
- return leaderId;
- }
- bool RakNet::IsClanLeader(unsigned int clanId, unsigned int userId, PostgreSQLInterface *pgsql)
- {
- return userId!=0 && GetClanLeaderId(clanId, pgsql)==userId;
- }
- RakNet::ClanMemberState RakNet::GetClanMemberState(unsigned int clanId, unsigned int userId, bool *isSubleader, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT memberState_fk FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", userId, clanId);
- if (result==0)
- return CMD_UNDEFINED;
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- return CMD_UNDEFINED;
- }
- int memberState;
- PostgreSQLInterface::PQGetValueFromBinary(&memberState, result, 0, "memberState_fk");
- PostgreSQLInterface::PQGetValueFromBinary(isSubleader, result, 0, "isSubleader");
- PQclear(result);
- return (ClanMemberState) memberState;
- }
- void RakNet::GetClanMembers(unsigned int clanId, DataStructures::List<ClanMemberDescriptor> &clanMembers, PostgreSQLInterface *pgsql)
- {
- ClanMemberDescriptor cmd;
- PGresult *result = pgsql->QueryVariadic(
- "SELECT M.userId_fk, M.isSubleader, M.memberState_fk, M.banReason, U.handle "
- "FROM lobby2.clanMembers AS M, lobby2.users AS U WHERE M.clanId_fk=%i AND U.userId_pk=M.userId_fk", clanId);
- if (result==0)
- return;
- int numRowsReturned = PQntuples(result);
- int idx;
- for (idx=0; idx < numRowsReturned; idx++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&cmd.userId, result, idx, "userId_fk");
- PostgreSQLInterface::PQGetValueFromBinary(&cmd.isSubleader, result, idx, "isSubleader");
- int cms;
- PostgreSQLInterface::PQGetValueFromBinary(&cms, result, idx, "memberState_fk");
- cmd.memberState=(ClanMemberState)cms;
- PostgreSQLInterface::PQGetValueFromBinary(&cmd.banReason, result, idx, "banReason");
- PostgreSQLInterface::PQGetValueFromBinary(&cmd.name, result, idx, "handle");
- clanMembers.Insert(cmd, _FILE_AND_LINE_ );
- }
- PQclear(result);
- }
- bool RakNet::IsTitleInUse(RakNet::RakString titleName, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT titleName_pk FROM lobby2.titles where titleName_pk=%s", titleName.C_String());
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned==0)
- return false;
- return true;
- }
- bool RakNet::StringContainsProfanity(RakNet::RakString string, PostgreSQLInterface *pgsql)
- {
- RakNet::RakString strLower1 = " " + string;
- RakNet::RakString strLower2 = string + " ";
- RakNet::RakString strLower3 = " " + string + " ";
- RakNet::RakString strLower4 = string;
- strLower1.ToLower();
- strLower2.ToLower();
- strLower3.ToLower();
- strLower4.ToLower();
- PGresult *result = pgsql->QueryVariadic("SELECT wordLower FROM lobby2.profanity WHERE "
- "wordLower LIKE %s OR wordLower LIKE %s OR wordLower LIKE %s OR wordLower LIKE %s"
- , strLower1.C_String(), strLower2.C_String(), strLower3.C_String(), strLower4.C_String());
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned==0)
- return false;
- return true;
- }
- bool RakNet::IsValidCountry(RakNet::RakString string, bool *countryHasStates, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT country_name,country_has_states FROM lobby2.country where lower(country_name)=lower(%s)", string.C_String());
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- if (countryHasStates && numRowsReturned>0)
- PostgreSQLInterface::PQGetValueFromBinary(countryHasStates, result, 0, "country_has_states");
- PQclear(result);
- if (numRowsReturned==0)
- return false;
- return true;
- }
- bool RakNet::IsValidState(RakNet::RakString string, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT state_name FROM lobby2.state WHERE lower(state_name)=lower(%s)", string.C_String());
- if (result==0)
- return false;
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned==0)
- return false;
- return true;
- }
- bool RakNet::IsValidRace(RakNet::RakString string, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT race_text FROM lobby2.race WHERE lower(race_text)=lower(%s)", string.C_String());
- if (result==0)
- return false;
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned==0)
- return false;
- return true;
- }
- void RakNet::GetFriendIDs(unsigned int callerUserId, bool excludeIfIgnored, PostgreSQLInterface *pgsql, DataStructures::List<unsigned int> &output)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT userTwo_fk from lobby2.friends WHERE userOne_fk=%i AND "
- "actionId_fk=(SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends');", callerUserId);
- if (result==0)
- return;
- int numRowsReturned = PQntuples(result);
- int idx;
- unsigned int id;
- for (idx=0; idx < numRowsReturned; idx++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&id, result, idx, "userTwo_fk");
- if (excludeIfIgnored==false || IsIgnoredByTarget(callerUserId, id, pgsql)==false)
- output.Insert(id, _FILE_AND_LINE_ );
- }
- PQclear(result);
- }
- void RakNet::GetClanMateIDs(unsigned int callerUserId, bool excludeIfIgnored, PostgreSQLInterface *pgsql, DataStructures::List<unsigned int> &output)
- {
- PGresult *result = pgsql->QueryVariadic(
- "select userId_fk from lobby2.clanMembers where clanId_fk="
- "(select clanId_fk from lobby2.clanMembers where userId_fk=%i)"
- "AND userId_fk !=%i;"
- , callerUserId, callerUserId);
- if (result==0)
- return;
- int numRowsReturned = PQntuples(result);
- int idx;
- unsigned int id;
- for (idx=0; idx < numRowsReturned; idx++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&id, result, idx, "userId_fk");
- if (excludeIfIgnored==false || IsIgnoredByTarget(callerUserId, id, pgsql)==false)
- output.Insert(id, _FILE_AND_LINE_ );
- }
- PQclear(result);
- }
- bool RakNet::IsIgnoredByTarget(unsigned int callerUserId, unsigned int targetUserId, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic(
- "select userMe_fk from lobby2.ignore where userMe_fk=%i AND userOther_fk=%i"
- , callerUserId, targetUserId);
- if (result==0)
- return false;
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- return numRowsReturned>0;
- }
- void RakNet::OutputFriendsNotification(RakNet::Notification_Friends_StatusChange::Status notificationType, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
- {
- // Tell all friends about this new login
- DataStructures::List<unsigned int> output;
- GetFriendIDs(command->callerUserId, true, pgsql, output);
-
- unsigned int idx;
- for (idx=0; idx < output.Size(); idx++)
- {
- Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->op=notificationType;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, output[idx], "");
- }
- }
- void RakNet::GetFriendInfosByStatus(unsigned int callerUserId, RakNet::RakString status, PostgreSQLInterface *pgsql, DataStructures::List<FriendInfo> &output, bool callerIsUserOne)
- {
- RakNet::RakString query;
- /*
- if (callerIsUserOne)
- {
- query = "SELECT handle from lobby2.users WHERE userId_pk ="
- "(SELECT userTwo_fk from lobby2.friends WHERE userOne_fk=%i AND actionId_fk ="
- "(SELECT actionId_pk FROM lobby2.friendActions where description='";
- }
- else
- {
- query = "SELECT handle from lobby2.users WHERE userId_pk ="
- "(SELECT userOne_fk from lobby2.friends WHERE userTwo_fk=%i AND actionId_fk ="
- "(SELECT actionId_pk FROM lobby2.friendActions where description='";
- }
-
- query+=status;
- query+="'));";
- */
- if (callerIsUserOne)
- {
- query = "SELECT handle from lobby2.friends,lobby2.users WHERE userId_pk=userTwo_fk AND userOne_fk=%i";
- }
- else
- {
- query = "SELECT handle from lobby2.friends,lobby2.users WHERE userId_pk=userOne_fk AND userTwo_fk=%i";
- }
- query+=" AND actionId_fk=(SELECT actionId_pk FROM lobby2.friendActions where description='";
- query+=status;
- query+="');";
- PGresult *result = pgsql->QueryVariadic( query.C_String(), callerUserId );
- RakAssert(result);
- int numRowsReturned = PQntuples(result);
- int i;
- for (i=0; i < numRowsReturned; i++)
- {
- FriendInfo fi;
- PostgreSQLInterface::PQGetValueFromBinary(&fi.usernameAndStatus.handle, result, i, "handle");
- fi.usernameAndStatus.isOnline=false;
- output.Insert(fi, _FILE_AND_LINE_ );
- }
- PQclear(result);
- }
- void RakNet::SendEmail(DataStructures::List<RakNet::RakString> &recipientNames, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
- {
- DataStructures::List<unsigned int> targetUserIds;
- unsigned int targetUserId;
- for (unsigned int i=0; i < recipientNames.Size(); i++)
- {
- targetUserId = GetUserRowFromHandle(recipientNames[i], pgsql);
- if (targetUserId!=0)
- targetUserIds.Insert(targetUserId, _FILE_AND_LINE_ );
- }
- SendEmail(targetUserIds, senderUserId, senderUserName, server, subject, body, binaryData, status, triggerString, pgsql);
- }
- void RakNet::SendEmail(unsigned int targetUserId, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
- {
- DataStructures::List<unsigned int> targetUserIds;
- targetUserIds.Insert(targetUserId, _FILE_AND_LINE_ );
- SendEmail(targetUserIds, senderUserId, senderUserName, server, subject, body, binaryData, status, triggerString, pgsql);
- }
- void RakNet::SendEmail(DataStructures::List<unsigned int> &targetUserIds, unsigned int senderUserId, RakNet::RakString senderUserName, Lobby2Server *server, RakNet::RakString subject, RakNet::RakString body, RakNetSmartPtr<BinaryDataBlock>binaryData, int status, RakNet::RakString triggerString, PostgreSQLInterface *pgsql)
- {
- if (targetUserIds.Size()==0)
- return;
- PGresult *result=0;
- if (binaryData.IsNull()==false)
- {
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.emails (subject, body, binaryData, triggerId_fk) VALUES "
- "(%s, %s, %a, (SELECT triggerId_pk FROM lobby2.emailTriggers WHERE description=%s) ) RETURNING emailId_pk;"
- ,subject.C_String(), body.C_String(), binaryData->binaryData, binaryData->binaryDataLength, triggerString.C_String()
- );
- }
- else
- {
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.emails (subject, body, triggerId_fk) VALUES "
- "(%s, %s, %a, (SELECT triggerId_pk FROM lobby2.emailTriggers WHERE description=%s) ) RETURNING emailId_pk;"
- ,subject.C_String(), body.C_String(), triggerString.C_String()
- );
- }
-
- RakAssert(result);
- unsigned int emailId_pk;
- PostgreSQLInterface::PQGetValueFromBinary(&emailId_pk, result, 0, "emailId_pk");
- PQclear(result);
- unsigned int i;
- for (i=0; i < targetUserIds.Size(); i++)
- {
- // Once in my inbox
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.emailTargets (emailId_fk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail) VALUES "
- "(%i, %i, %i, %i, %b, %b);", emailId_pk, senderUserId, targetUserIds[i], status, true, true);
- RakAssert(result);
- PQclear(result);
- // Once in the destination inbox
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.emailTargets (emailId_fk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail) VALUES "
- "(%i, %i, %i, %i, %b, %b) RETURNING emailTarget_pk;", emailId_pk, targetUserIds[i], senderUserId, status, false, false);
- RakAssert(result);
- unsigned int emailTarget_pk;
- PostgreSQLInterface::PQGetValueFromBinary(&emailTarget_pk, result, 0, "emailTarget_pk");
- PQclear(result);
- // Notify recipient that they got an email
- Notification_Emails_Received *notification = (Notification_Emails_Received *) server->GetMessageFactory()->Alloc(L2MID_Notification_Emails_Received);
- notification->sender=senderUserName;
- notification->subject=subject;
- notification->emailId=emailTarget_pk;
- notification->resultCode=L2RC_SUCCESS;
- server->AddOutputFromThread(notification, targetUserIds[i], "");
- }
- }
- int RakNet::GetActiveClanCount(unsigned int userId, PostgreSQLInterface *pgsql)
- {
- PGresult *result = pgsql->QueryVariadic("SELECT clanMemberId_pk FROM lobby2.clanMembers WHERE userId_fk=%i AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active')", userId);
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- return numRowsReturned;
- }
- bool RakNet::CreateAccountParametersFailed( CreateAccountParameters &createAccountParameters, RakNet::Lobby2ResultCode &resultCode, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
- {
- bool hasStates=true;
- if (createAccountParameters.homeCountry.IsEmpty()==false)
- {
- if (IsValidCountry(createAccountParameters.homeCountry, &hasStates, pgsql)==false)
- {
- resultCode=L2RC_Client_RegisterAccount_INVALID_COUNTRY;
- return true;
- }
- }
- if (hasStates==true)
- {
- if (createAccountParameters.homeState.IsEmpty()==false && IsValidState(createAccountParameters.homeState, pgsql)==false)
- {
- resultCode=L2RC_Client_RegisterAccount_INVALID_STATE;
- return true;
- }
- }
- else
- createAccountParameters.homeState.Clear();
- if (createAccountParameters.billingCountry.IsEmpty()==false)
- {
- if (IsValidCountry(createAccountParameters.billingCountry, &hasStates, pgsql)==false)
- {
- resultCode=L2RC_Client_RegisterAccount_INVALID_COUNTRY;
- return true;
- }
- }
- if (hasStates==true)
- {
- if (createAccountParameters.billingState.IsEmpty()==false && IsValidState(createAccountParameters.billingState, pgsql)==false)
- {
- resultCode=L2RC_Client_RegisterAccount_INVALID_STATE;
- return true;
- }
- }
- else
- createAccountParameters.billingState.Clear();
- if (createAccountParameters.race.IsEmpty()==false &&
- IsValidRace(createAccountParameters.race, pgsql)==false)
- {
- resultCode=L2RC_Client_RegisterAccount_INVALID_RACE;
- return true;
- }
- unsigned int requiredAgeYears = command->server->GetConfigurationProperties()->accountRegistrationRequiredAgeYears;
- if (createAccountParameters.ageInDays < requiredAgeYears*365 )
- {
- resultCode=L2RC_Client_RegisterAccount_REQUIRED_AGE_NOT_MET;
- return true;
- }
- return false;
- }
- void RakNet::UpdateAccountFromMissingCreationParameters(CreateAccountParameters &createAccountParameters, unsigned int userPrimaryKey, Lobby2ServerCommand *command, PostgreSQLInterface *pgsql)
- {
- (void)command;
- PGresult *result=0;
- unsigned int key;
- if (createAccountParameters.homeState.IsEmpty()==false)
- {
- result = pgsql->QueryVariadic("SELECT state_id FROM lobby2.state where lower(state_name)=lower(%s)", createAccountParameters.homeState.C_String());
- if (result)
- {
- if (PQntuples(result))
- {
- PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "state_id");
- PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeStateId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- }
- PQclear(result);
- }
- }
- if (createAccountParameters.homeCountry.IsEmpty()==false)
- {
- result = pgsql->QueryVariadic("SELECT country_id FROM lobby2.country where lower(country_name)=lower(%s)", createAccountParameters.homeCountry.C_String());
- if (result)
- {
- if (PQntuples(result))
- {
- PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "country_id");
- PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- }
- PQclear(result);
- }
- }
- if (createAccountParameters.billingState.IsEmpty()==false)
- {
- result = pgsql->QueryVariadic("SELECT state_id FROM lobby2.state where lower(state_name)=lower(%s)", createAccountParameters.billingState.C_String());
- if (result)
- {
- if (PQntuples(result))
- {
- PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "state_id");
- PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET billingStateId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- }
- PQclear(result);
- }
- }
- if (createAccountParameters.billingCountry.IsEmpty()==false)
- {
- result = pgsql->QueryVariadic("SELECT country_id FROM lobby2.country where lower(country_name)=lower(%s)", createAccountParameters.billingCountry.C_String());
- if (result)
- {
- if (PQntuples(result))
- {
- PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "country_id");
- PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET billingCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- }
- PQclear(result);
- }
- }
- if (createAccountParameters.race.IsEmpty()==false)
- {
- result = pgsql->QueryVariadic("SELECT race_id FROM lobby2.race where lower(race_text)=lower(%s)", createAccountParameters.race.C_String());
- if (result)
- {
- if (PQntuples(result))
- {
- PostgreSQLInterface::PQGetValueFromBinary(&key, result, 0, "race_id");
- PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET raceId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- }
- PQclear(result);
- }
- }
- }
- bool RakNet::System_CreateDatabase_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- bool success;
- PGresult *result=0;
- pgsql->ExecuteBlockingCommand("DROP SCHEMA lobby2 CASCADE;", &result, false);
- PQclear(result);
- pgsql->ExecuteBlockingCommand("DROP LANGUAGE plpgsql;", &result, false);
- PQclear(result);
- FILE *fp = fopen("Lobby2Schema.txt", "rb");
- RakAssert(fp && "Can't find Lobby2Schema.txt");
- fseek( fp, 0, SEEK_END );
- unsigned int fileSize = ftell( fp );
- fseek( fp, 0, SEEK_SET );
- char *cmd = (char*) rakMalloc_Ex(fileSize+1, _FILE_AND_LINE_);
- fread(cmd, 1, fileSize, fp);
- fclose(fp);
- cmd[fileSize]=0;
- success = pgsql->ExecuteBlockingCommand(cmd, &result, false);
- PQclear(result);
- if (success)
- {
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- printf(cmd);
- printf(pgsql->GetLastError());
- }
- rakFree_Ex(cmd, _FILE_AND_LINE_ );
- return true;
- }
- bool RakNet::System_DestroyDatabase_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- bool success;
- PGresult *result=0;
- success=pgsql->ExecuteBlockingCommand("DROP SCHEMA lobby2 CASCADE;", &result, false);
- PQclear(result);
- if (success)
- resultCode=L2RC_SUCCESS;
- else
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- bool RakNet::System_CreateTitle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("INSERT INTO lobby2.titles (titleName_pk, titleSecretKey, requiredAge, binaryData) VALUES (%s,%s,%i,%a)",
- titleName.C_String(),
- titleSecretKey.C_String(),
- requiredAge,
- binaryData->binaryData,
- binaryData->binaryDataLength);
- if (result!=0)
- {
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_CreateTitle_TITLE_ALREADY_IN_USE;
- }
- return true;
- }
- bool RakNet::System_DestroyTitle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic("DELETE FROM lobby2.titles WHERE titlename_pk=%s", titleName.C_String());
- if (result!=0)
- {
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_DestroyTitle_TITLE_NOT_IN_USE;
- }
- return true;
- }
- bool RakNet::System_GetTitleRequiredAge_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT requiredAge FROM lobby2.titles where titlename_pk=%s", titleName.C_String());
- if (result!=0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&requiredAge, result, 0, "requiredAge");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_GetTitleRequiredAge_TITLE_NOT_IN_USE;
- }
- return true;
- }
- bool RakNet::System_GetTitleBinaryData_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT binaryData FROM lobby2.titles where titlename_pk=%s", titleName.C_String());
- if (result!=0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_GetTitleBinaryData_TITLE_NOT_IN_USE;
- }
- return true;
- }
- bool RakNet::System_RegisterProfanity_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- // pgsql->PrepareVariadic("INSERT INTO lobby2.profanity (word) VALUES (%s)");
- // for (unsigned int i=0; i < profanityWords.Size(); i++)
- // pgsql->PrepareVariadicArgs(0, profanityWords[i].C_String());
- // PGresult *result = pgsql->ExecutePreparedStatement();
- PGresult *result=0;
- resultCode=L2RC_SUCCESS;
- for (unsigned int i=0; i < profanityWords.Size(); i++)
- {
- result = pgsql->QueryVariadic("INSERT INTO lobby2.profanity (word) VALUES (%s)", profanityWords[i].C_String());
- if (result==0)
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- PQclear(result);
- if (resultCode==L2RC_DATABASE_CONSTRAINT_FAILURE)
- return true;
- }
- return true;
- }
- bool RakNet::System_BanUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic("INSERT INTO lobby2.bannedUsers (userId_fk, description, timeout) VALUES (%i, %s, now () + %i * interval '1 hours')", userRow, banReason.C_String(), durationHours);
- if (result!=0)
- {
- PQclear(result);
- result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, description, triggerId_fk) "
- "VALUES (%i, %s, (SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Banned'))", userRow, banReason.C_String());
- RakAssert(result);
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_BanUser_ALREADY_BANNED;
- }
-
- return true;
- }
- bool RakNet::System_UnbanUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic("DELETE FROM lobby2.bannedUsers WHERE userId_fk=%i", userRow);
- if (result!=0)
- {
- PQclear(result);
- result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, description, triggerId_fk) "
- "VALUES (%i, %s, (SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Unbanned'))", userRow, reason.C_String());
- RakAssert(result);
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- else
- {
- resultCode=L2RC_System_BanUser_ALREADY_BANNED;
- }
- return true;
- }
- bool RakNet::CDKey_Add_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_CDKey_Add_TITLE_NOT_IN_USE;
- return true;
- }
- unsigned int i;
- for (i=0; i < cdKeys.Size(); i++)
- {
- result = pgsql->QueryVariadic("INSERT INTO lobby2.cdkeys (cdKey, usable, stolen, titleName_fk) VALUES (%s, true, false, %s);", cdKeys[i].C_String(), titleName.C_String());
- RakAssert(result);
- PQclear(result);
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::CDKey_GetStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_CDKey_GetStatus_TITLE_NOT_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic(
- "SELECT lobby2.cdkeys.usable, lobby2.cdkeys.stolen, lobby2.cdkeys.activationDate, lobby2.users.handle "
- "FROM lobby2.cdkeys LEFT OUTER JOIN lobby2.users ON lobby2.users.userId_pk=lobby2.cdkeys.userId_fk "
- "WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s;"
- , cdKey.C_String(), titleName.C_String());
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_CDKey_GetStatus_UNKNOWN_CD_KEY;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "lobby2.cdkeys.usable");
- PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "lobby2.cdkeys.stolen");
- PostgreSQLInterface::PQGetValueFromBinary(&usedBy, result, 0, "lobby2.users.handle");
- PostgreSQLInterface::PQGetValueFromBinary(&activationDate, result, 0, "lobby2.cdkeys.activationDate");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::CDKey_Use_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_CDKey_Use_TITLE_NOT_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT lobby2.cdkeys.usable, lobby2.cdkeys.stolen, lobby2.cdkeys.userId_fk "
- "FROM lobby2.cdkeys, lobby2.users WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
- cdKey.C_String(), titleName.C_String());
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_CDKey_Use_UNKNOWN_CD_KEY;
- return true;
- }
- bool usable, wasStolen, alreadyUsed;
- unsigned int userId;
- PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "lobby2.cdkeys.usable");
- PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "lobby2.cdkeys.stolen");
- alreadyUsed=PostgreSQLInterface::PQGetValueFromBinary(&userId, result, 0, "lobby2.cdkeys.userId_fk");
- PQclear(result);
- if (wasStolen)
- resultCode=L2RC_CDKey_Use_CD_KEY_STOLEN;
- else if (alreadyUsed)
- resultCode=L2RC_CDKey_Use_CD_KEY_ALREADY_USED;
- else if (usable==false)
- resultCode=L2RC_CDKey_Use_NOT_USABLE;
- else
- {
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET activationDate=now(),"
- "userId_fk=%i WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", userRow, cdKey.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- }
- return true;
- }
- bool RakNet::CDKey_FlagStolen_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_CDKey_FlagStolen_TITLE_NOT_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT lobby2.cdkeys.cdKey, lobby2.cdkeys.userId_fk FROM lobby2.cdkeys WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
- cdKey.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_CDKey_Use_UNKNOWN_CD_KEY;
- return true;
- }
- unsigned int userId_fk;
- if (PostgreSQLInterface::PQGetValueFromBinary(&userId_fk, result, 0, "userId_fk"))
- {
- PQclear(result);
- result = pgsql->QueryVariadic("SELECT handle from lobby2.users WHERE userId_pk=%i", userId_fk);
- PostgreSQLInterface::PQGetValueFromBinary(&userUsingThisKey, result, 0, "handle");
- PQclear(result);
- }
- else
- PQclear(result);
- result = pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET stolen=%b WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", wasStolen, cdKey.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_Login_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_Client_Login_HANDLE_NOT_IN_USE_OR_BAD_SECRET_KEY;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT password FROM lobby2.users WHERE userId_pk=%i", userRow);
- RakNet::RakString password;
- PostgreSQLInterface::PQGetValueFromBinary(&password, result, 0, "password");
- PQclear(result);
- if (password!=userPassword)
- {
- resultCode=L2RC_Client_Login_HANDLE_NOT_IN_USE_OR_BAD_SECRET_KEY;
- return true;
- }
- if (command->server->GetConfigurationProperties()->requiresEmailAddressValidationToLogin)
- {
- result = pgsql->QueryVariadic("SELECT emailAddressValidated FROM lobby2.users WHERE userId_pk=%i", userRow);
- bool emailAddressValidated;
- PostgreSQLInterface::PQGetValueFromBinary(&emailAddressValidated, result, 0, "emailAddressValidated");
- PQclear(result);
- if (emailAddressValidated==false)
- {
- resultCode=L2RC_Client_Login_EMAIL_ADDRESS_NOT_VALIDATED;
- return true;
- }
- }
- if (command->server->GetConfigurationProperties()->requiresTitleToLogin)
- {
- RakNet::RakString titleDBSecretKey;
- result = pgsql->QueryVariadic("SELECT titleSecretKey FROM lobby2.titles where titleName_pk=%s", titleName.C_String());
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- resultCode=L2RC_Client_Login_BAD_TITLE_OR_TITLE_SECRET_KEY;
- PQclear(result);
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&titleDBSecretKey, result, 0, "titleSecretKey");
- PQclear(result);
- // title can have no secret key, in which case you just have to specify a valid title
- if (titleDBSecretKey.IsEmpty()==false && titleDBSecretKey!=titleSecretKey)
- {
- resultCode=L2RC_Client_Login_BAD_TITLE_OR_TITLE_SECRET_KEY;
- return true;
- }
- }
- // Does this user have any stolen CD keys?
- result = pgsql->QueryVariadic("SELECT stolen FROM lobby2.cdkeys WHERE userId_fk=%i AND stolen=TRUE", userRow);
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned!=0)
- {
- resultCode=L2RC_Client_Login_CDKEY_STOLEN;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT description, timeout, creationDate from lobby2.bannedUsers WHERE userId_fk=%i AND now() < timeout", userRow);
- numRowsReturned = PQntuples(result);
- if (numRowsReturned!=0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&bannedReason, result, 0, "description");
- PostgreSQLInterface::PQGetValueFromBinary(&bannedExpiration, result, 0, "timeout");
- PostgreSQLInterface::PQGetValueFromBinary(&whenBanned, result, 0, "creationDate");
- PQclear(result);
- resultCode=L2RC_Client_Login_BANNED;
- return true;
- }
- PQclear(result);
- result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, triggerId_fk) "
- "VALUES "
- "(%i,"
- "(SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Login'))", userRow);
- PQclear(result);
- command->callingUserName=userName;
- command->callerUserId=userRow;
- // Let the user do this if they want. Not all users may want ignore lists
- /*
- // Trigger GetIgnoreList for this user, so they download the client ignore list used for rooms when they logon
- Client_GetIgnoreList *ignoreListRequest = (Client_GetIgnoreList *) command->server->GetMessageFactory()->Alloc(L2MID_Client_GetIgnoreList);
- ignoreListRequest->ServerDBImpl(command, databaseInterface);
- command->server->AddOutputFromThread(ignoreListRequest, userRow, userName);
- */
- OutputFriendsNotification(Notification_Friends_StatusChange::FRIEND_LOGGED_IN, command, pgsql);
- // Have the server record in memory this new login
- command->server->OnLogin(command, true);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_Logoff_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- if (command->callerUserId==0)
- return false;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic("INSERT INTO lobby2.userHistory (userId_fk, triggerId_fk) "
- "VALUES "
- "(%i,"
- "(SELECT triggerId_pk FROM lobby2.userHistoryTriggers WHERE description='Logoff'));", command->callerUserId);
- PQclear(result);
- // Notification is done below
- command->server->OnLogoff(command, true);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_RegisterAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (StringContainsProfanity(userName, pgsql))
- {
- resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
- return true;
- }
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow!=0)
- {
- resultCode=L2RC_Client_RegisterAccount_HANDLE_ALREADY_IN_USE;
- return true;
- }
- if (CreateAccountParametersFailed(createAccountParameters, resultCode, command, pgsql))
- return true;
- if (command->server->GetConfigurationProperties()->accountRegistrationRequiresCDKey)
- {
- if (cdKey.IsEmpty())
- {
- resultCode=L2RC_Client_RegisterAccount_REQUIRES_CD_KEY;
- return true;
- }
- if (titleName.IsEmpty())
- {
- resultCode=L2RC_Client_RegisterAccount_REQUIRES_CD_KEY;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT usable, stolen "
- "FROM lobby2.cdkeys WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s",
- cdKey.C_String(), titleName.C_String());
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Client_RegisterAccount_CD_KEY_NOT_USABLE;
- return true;
- }
- bool usable;
- bool wasStolen;
- RakNet::RakString usedBy;
- PostgreSQLInterface::PQGetValueFromBinary(&usable, result, 0, "usable");
- PostgreSQLInterface::PQGetValueFromBinary(&wasStolen, result, 0, "stolen");
- PQclear(result);
- if (usable==false)
- {
- PQclear(result);
- resultCode=L2RC_Client_RegisterAccount_CD_KEY_NOT_USABLE;
- return true;
- }
- if (wasStolen)
- {
- PQclear(result);
- resultCode=L2RC_Client_RegisterAccount_CD_KEY_STOLEN;
- return true;
- }
- if (usedBy.IsEmpty()==false)
- {
- PQclear(result);
- resultCode=L2RC_Client_RegisterAccount_CD_KEY_ALREADY_USED;
- return true;
- }
- }
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.users ("
- "handle, firstname, middlename, lastname,"
- "sex_male, homeaddress1, homeaddress2, homecity, "
- "homezipcode, billingaddress1, billingaddress2, billingcity,"
- "billingzipcode, emailaddress, password, passwordrecoveryquestion,"
- "passwordrecoveryanswer, caption1, caption2, dateOfBirth, binaryData) "
- "VALUES ("
- "%s, %s, %s, %s,"
- "%b, %s, %s, %s,"
- "%s, %s, %s, %s,"
- "%s, %s, %s, %s,"
- "%s, %s, %s, (select now() - %i * interval '1 day'), %a) RETURNING userId_pk",
- userName.C_String(), createAccountParameters.firstName.C_String(), createAccountParameters.middleName.C_String(), createAccountParameters.lastName.C_String(),
- createAccountParameters.sex_male, createAccountParameters.homeAddress1.C_String(), createAccountParameters.homeAddress2.C_String(), createAccountParameters.homeCity.C_String(),
- createAccountParameters.homeZipCode.C_String(), createAccountParameters.billingAddress1.C_String(), createAccountParameters.billingAddress2.C_String(), createAccountParameters.billingCity.C_String(),
- createAccountParameters.billingZipCode.C_String(), createAccountParameters.emailAddress.C_String(), createAccountParameters.password.C_String(), createAccountParameters.passwordRecoveryQuestion.C_String(),
- createAccountParameters.passwordRecoveryAnswer.C_String(), createAccountParameters.caption1.C_String(),createAccountParameters.caption2.C_String(),
- createAccountParameters.ageInDays, createAccountParameters.binaryData->binaryData, createAccountParameters.binaryData->binaryDataLength);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- unsigned int userPrimaryKey;
- PostgreSQLInterface::PQGetValueFromBinary(&userPrimaryKey, result, 0, "userId_pk");
- PQclear(result);
- // Assign the cd key, already validated earlier
- if (command->server->GetConfigurationProperties()->accountRegistrationRequiresCDKey)
- {
- PQclear(pgsql->QueryVariadic("UPDATE lobby2.cdKeys SET activationDate=now(),"
- "userId_fk=%i WHERE lobby2.cdkeys.cdKey=%s AND lobby2.cdkeys.titleName_fk=%s", userPrimaryKey, cdKey.C_String(), titleName.C_String()));
- }
- UpdateAccountFromMissingCreationParameters(createAccountParameters, userPrimaryKey, command, pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::System_SetEmailAddressValidated_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic("UPDATE lobby2.users SET emailAddressValidated=%b WHERE userId_pk=%i", validated, userRow);
- PQclear(result);
- if (result!=0)
- resultCode=L2RC_SUCCESS;
- else
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- bool RakNet::Client_ValidateHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow!=0)
- {
- resultCode=L2RC_Client_ValidateHandle_HANDLE_ALREADY_IN_USE;
- return true;
- }
- if (StringContainsProfanity(userName, pgsql))
- {
- resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::System_DeleteAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "SELECT password from lobby2.users WHERE userId_pk = %i", userRow);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString passwordFromDB;
- PostgreSQLInterface::PQGetValueFromBinary(&passwordFromDB, result, 0, "password");
- PQclear(result);
- if (passwordFromDB!=password)
- {
- PQclear(result);
- resultCode=L2RC_System_DeleteAccount_INVALID_PASSWORD;
- return true;
- }
- // Notify friends of account deletion
- command->callingUserName=userName;
- command->callerUserId=userRow;
- OutputFriendsNotification(Notification_Friends_StatusChange::FRIEND_ACCOUNT_WAS_DELETED, command, pgsql);
- // Trigger logoff as well
- Client_Logoff *logoffRequest = (Client_Logoff *) command->server->GetMessageFactory()->Alloc(L2MID_Client_Logoff);
- logoffRequest->ServerDBImpl( command, databaseInterface );
- command->server->AddOutputFromThread(logoffRequest, userRow, userName);
- // Delete the account
- result = pgsql->QueryVariadic("DELETE FROM lobby2.users WHERE userId_pk=%i", userRow);
- PQclear(result);
- if (result!=0)
- resultCode=L2RC_SUCCESS;
- else
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- bool RakNet::System_PruneAccounts_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic(
- "SELECT handle from lobby2.users WHERE userId_pk ="
- "(SELECT userId_fk FROM lobby2.userHistory WHERE creationDate < now() - %i * interval '1 day' ORDER by creationDate DESC LIMIT 1 AND triggerid_fk="
- "(SELECT triggerId_pk from lobby2.userHistoryTriggers where description='Login')"
- ") ;", deleteAccountsNotLoggedInDays);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- // Delete all accounts where the user has not logged in deleteAccountsNotLoggedInDays
- System_DeleteAccount *deleteAccount = (System_DeleteAccount *) command->server->GetMessageFactory()->Alloc(L2MID_System_DeleteAccount);
- RakNet::RakString userName;
- for (int i=0; i < numRowsReturned; i++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&userName, result, i, "handle");
- deleteAccount->userName=userName;
- deleteAccount->ServerDBImpl( command, databaseInterface );
- }
- command->server->GetMessageFactory()->Dealloc(deleteAccount);
- PQclear(result);
- return true;
- }
- bool RakNet::Client_GetEmailAddress_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "SELECT emailaddress, emailAddressValidated from lobby2.users WHERE userId_pk = %i", userRow);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&emailAddress, result, 0, "emailAddress");
- PostgreSQLInterface::PQGetValueFromBinary(&emailAddressValidated, result, 0, "emailAddressValidated");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_GetPasswordRecoveryQuestionByHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "SELECT passwordRecoveryQuestion,emailAddress from lobby2.users WHERE userId_pk = %i", userRow);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&emailAddress, result, 0, "emailAddress");
- PostgreSQLInterface::PQGetValueFromBinary(&passwordRecoveryQuestion, result, 0, "passwordRecoveryQuestion");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_GetPasswordByPasswordRecoveryAnswer_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "SELECT password from lobby2.users WHERE lower(passwordrecoveryanswer) = lower(%s) AND userId_pk = %i", passwordRecoveryAnswer.C_String(), userRow);
- if (result==0)
- {
- resultCode=L2RC_Client_GetPasswordByPasswordRecoveryAnswer_BAD_ANSWER;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Client_GetPasswordByPasswordRecoveryAnswer_BAD_ANSWER;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&password, result, 0, "password");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_ChangeHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int userRow = GetUserRowFromHandle(userName, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- unsigned int userRow2 = GetUserRowFromHandle(newHandle, pgsql);
- if (userRow2!=0)
- {
- resultCode=L2RC_Client_ChangeHandle_NEW_HANDLE_ALREADY_IN_USE;
- return true;
- }
- if (StringContainsProfanity(newHandle, pgsql))
- {
- resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
- return true;
- }
- if (requiresPasswordToChangeHandle)
- {
- PGresult *result = pgsql->QueryVariadic(
- "SELECT password from lobby2.users WHERE userId_pk = %i", userRow);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString passwordFromDB;
- PostgreSQLInterface::PQGetValueFromBinary(&passwordFromDB, result, 0, "password");
- PQclear(result);
- if (passwordFromDB!=password)
- {
- PQclear(result);
- resultCode=L2RC_Client_ChangeHandle_INVALID_PASSWORD;
- return true;
- }
- }
- PGresult *result = pgsql->QueryVariadic(
- "UPDATE lobby2.users SET handle=%s WHERE userId_pk=%i", newHandle.C_String(), userRow);
- PQclear(result);
- // Tell all friends and clanMembers
- DataStructures::List<unsigned int> output;
- GetFriendIDs(command->callerUserId, false, pgsql, output);
- GetClanMateIDs(command->callerUserId, false, pgsql, output);
- unsigned int i;
- for (i=0; i < output.Size(); i++)
- {
- Notification_User_ChangedHandle *notification = (Notification_User_ChangedHandle *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_User_ChangedHandle);
- notification->oldHandle=userName;
- notification->newHandle=newHandle;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, output[i], "");
- }
- command->callingUserName=newHandle;
- command->server->OnChangeHandle(command, true);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_UpdateAccount_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (CreateAccountParametersFailed(createAccountParameters, resultCode, command, pgsql))
- return true;
-
- // PQclear( pgsql->QueryVariadic("UPDATE lobby2.users SET homeCountryId_fk=%i WHERE userId_pk=%i", key, userPrimaryKey ));
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.users SET "
- "firstname=%s, middlename=%s, lastname=%s,"
- "sex_male=%b, homeaddress1=%s, homeaddress2=%s, homecity=%s, "
- "homezipcode=%s, billingaddress1=%s, billingaddress2=%s, billingcity=%s,"
- "billingzipcode=%s, emailaddress=%s, password=%s, passwordrecoveryquestion=%s,"
- "passwordrecoveryanswer=%s, caption1=%s, caption2=%s, dateOfBirth=(select now() - %i * interval '1 day'), binaryData=%a "
- "WHERE userId_pk = %i",
- createAccountParameters.firstName.C_String(), createAccountParameters.middleName.C_String(), createAccountParameters.lastName.C_String(),
- createAccountParameters.sex_male, createAccountParameters.homeAddress1.C_String(), createAccountParameters.homeAddress2.C_String(), createAccountParameters.homeCity.C_String(),
- createAccountParameters.homeZipCode.C_String(), createAccountParameters.billingAddress1.C_String(), createAccountParameters.billingAddress2.C_String(), createAccountParameters.billingCity.C_String(),
- createAccountParameters.billingZipCode.C_String(), createAccountParameters.emailAddress.C_String(), createAccountParameters.password.C_String(), createAccountParameters.passwordRecoveryQuestion.C_String(),
- createAccountParameters.passwordRecoveryAnswer.C_String(), createAccountParameters.caption1.C_String(),createAccountParameters.caption2.C_String(),
- createAccountParameters.ageInDays, createAccountParameters.binaryData->binaryData, createAccountParameters.binaryData->binaryDataLength,
- command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- UpdateAccountFromMissingCreationParameters(createAccountParameters, command->callerUserId, command, pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_GetAccountDetails_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic(
- "SELECT firstname, middlename, lastname, (SELECT race_text FROM lobby2.race WHERE "
- "race_id=(SELECT raceid_fk FROM lobby2.users WHERE userId_pk = %i )) as race, sex_male, homeaddress1, "
- "homeaddress2, homecity,(SELECT state_name FROM lobby2.state WHERE state_id=(SELECT homestateid_fk FROM lobby2.users WHERE userId_pk = %i)) as homeState,(SELECT country_name FROM lobby2.country "
- "WHERE country_id=(SELECT homeCountryid_fk FROM lobby2.users WHERE userId_pk = %i)) as homeCountry,homezipcode, billingaddress1, billingaddress2, billingcity,"
- "(SELECT state_name FROM lobby2.state WHERE state_id=(SELECT billingstateid_fk FROM lobby2.users WHERE userId_pk = %i)) as billingState,(SELECT country_name FROM "
- "lobby2.country WHERE country_id=(SELECT billingCountryid_fk FROM lobby2.users WHERE userId_pk = %i)) as billingCountry,billingzipcode, emailaddress, password, passwordrecoveryquestion,"
- "passwordrecoveryanswer, caption1, caption2, (SELECT (EXTRACT(EPOCH FROM now()) - extract(epoch from dateofbirth))/(24*60*60) AS days_old "
- "FROM lobby2.users WHERE userId_pk = %i) as ageInDays, binaryData FROM lobby2.users WHERE userId_pk = %i"
- ,command->callerUserId, command->callerUserId,command->callerUserId, command->callerUserId,command->callerUserId
- ,command->callerUserId, command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.firstName, result, 0, "firstname");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.middleName, result, 0, "middleName");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.lastName, result, 0, "lastName");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.race, result, 0, "race");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.sex_male, result, 0, "sex_male");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeAddress1, result, 0, "homeAddress1");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeAddress2, result, 0, "homeAddress2");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeCity, result, 0, "homeCity");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeState, result, 0, "homeState");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeCountry, result, 0, "homeCountry");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.homeZipCode, result, 0, "homeZipCode");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingAddress1, result, 0, "billingAddress1");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingAddress2, result, 0, "billingAddress2");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingCity, result, 0, "billingCity");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingState, result, 0, "billingState");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingCountry, result, 0, "billingCountry");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.billingZipCode, result, 0, "billingZipCode");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.emailAddress, result, 0, "emailAddress");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.password, result, 0, "password");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.passwordRecoveryQuestion, result, 0, "passwordRecoveryQuestion");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.passwordRecoveryAnswer, result, 0, "passwordRecoveryAnswer");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.caption1, result, 0, "caption1");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.caption2, result, 0, "caption2");
- double d;
- PostgreSQLInterface::PQGetValueFromBinary(&d, result, 0, "ageInDays");
- createAccountParameters.ageInDays=(unsigned int) d;
- // PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.ageInDays, result, 0, "ageInDays");
- PostgreSQLInterface::PQGetValueFromBinary(&createAccountParameters.binaryData->binaryData, &createAccountParameters.binaryData->binaryDataLength, result, 0, "binaryData");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_StartIgnore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Client_StartIgnore_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Client_StartIgnore_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.ignore (userMe_fk, userOther_fk) VALUES (%i, %i)", command->callerUserId, targetUserId);
- if (result==0)
- {
- resultCode=L2RC_Client_StartIgnore_ALREADY_IGNORED;
- return true;
- }
- PQclear(result);
- Notification_Client_IgnoreStatus *notification = (Notification_Client_IgnoreStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Client_IgnoreStatus);
- notification->otherHandle=command->callingUserName;
- notification->nowIgnored=true;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_GetIgnoreList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT handle FROM lobby2.users WHERE userId_pk="
- "(SELECT userOther_fk FROM lobby2.ignore WHERE userMe_fk=%i);", command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString handle;
- ignoredHandles.Clear(false, _FILE_AND_LINE_);
- int numRowsReturned = PQntuples(result);
- int i;
- for (i=0; i < numRowsReturned; i++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&handle, result, i, "handle");
- ignoredHandles.Insert(handle, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Write( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PGresult *result=0;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g);",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- /*
- switch (addConditionForOperation)
- {
- case PTISC_EQUAL:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
- break;
- case PTISC_NOT_EQUAL:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue);
- break;
- case PTISC_GREATER_THAN:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value<%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
- break;
- case PTISC_GREATER_OR_EQUAL:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value<=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
- break;
- case PTISC_LESS_THAN:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value>%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
- break;
- case PTISC_LESS_OR_EQUAL:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,userId_fk,value) VALUES (%s,%i,%i,%g) WHERE value>=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, inputValue, conditionValue);
- break;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
- */
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Read( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PGresult *result=0;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- result = pgsql->QueryVariadic("SELECT value FROM lobby2.perTitlePerUserIntegerStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i", titleName.C_String(), slotIndex, command->callerUserId);
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Client_PerTitleIntegerStorage_ROW_EMPTY;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Delete( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PGresult *result=0;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- result = pgsql->QueryVariadic("DELETE FROM lobby2.perTitlePerUserIntegerStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i", titleName.C_String(), slotIndex, command->callerUserId);
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_PerTitleIntegerStorage_PGSQL::Add( Lobby2ServerCommand *command, void *databaseInterface )
- {
- // In MySQL I think you can do this:
- // INSERT INTO lobby2.perTitlePerUserIntegerStorage (titleName_fk,slotIndex,value) VALUES ('tn',1,2) ON DUPLICATE KEY UPDATE titleName_fk='tn2', slotIndex=2, value=3;
- // But not in PostgreSQL
- Read(command, databaseInterface);
- if (resultCode==L2RC_Client_PerTitleIntegerStorage_ROW_EMPTY)
- return Write(command, databaseInterface);
-
- outputValue+=inputValue;
- PGresult *result=0;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- switch (addConditionForOperation)
- {
- case PTISC_EQUAL:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
- break;
- case PTISC_NOT_EQUAL:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value!=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue);
- break;
- case PTISC_GREATER_THAN:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value<%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
- break;
- case PTISC_GREATER_OR_EQUAL:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value<=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
- break;
- case PTISC_LESS_THAN:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value>%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
- break;
- case PTISC_LESS_OR_EQUAL:
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.perTitlePerUserIntegerStorage SET value=%g WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i AND value>=%g RETURNING value;",
- titleName.C_String(), slotIndex, command->callerUserId, outputValue, conditionValue);
- break;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&outputValue, result, 0, "value");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_PerTitleIntegerStorage_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Client_PerTitleIntegerStorage_TITLE_NOT_IN_USE;
- return true;
- }
- switch (operationToPerform)
- {
- case PTISO_WRITE:
- return Write(command,databaseInterface);
- break;
- case PTISO_READ:
- return Read(command,databaseInterface);
- break;
- case PTISO_DELETE:
- return Delete(command,databaseInterface);
- break;
- case PTISO_ADD:
- return Add(command,databaseInterface);
- break;
- }
-
- return true;
- }
- bool RakNet::Client_PerTitleBinaryStorage_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Client_PerTitleBinaryStorage_TITLE_NOT_IN_USE;
- return true;
- }
- int numRowsReturned;
- switch (operationToPerform)
- {
- case PTISO_WRITE:
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.perTitlePerUserBinaryStorage (titleName_fk,slotIndex,userId_fk,binaryData) VALUES (%s,%i,%i,%a);",
- titleName.C_String(), slotIndex, command->callerUserId, binaryData->binaryData,
- binaryData->binaryDataLength);
- if (result==0)
- {
- resultCode=L2RC_GENERAL_ERROR;
- return true;
- }
- break;
- case PTISO_READ:
- result = pgsql->QueryVariadic(
- "SELECT binaryData FROM lobby2.perTitlePerUserBinaryStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i;",
- titleName.C_String(), slotIndex, command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_GENERAL_ERROR;
- return true;
- }
- numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- resultCode=L2RC_Client_PerTitleBinaryStorage_ROW_EMPTY;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
- break;
- case PTISO_DELETE:
- result = pgsql->QueryVariadic("DELETE FROM lobby2.perTitlePerUserBinaryStorage WHERE titleName_fk=%s AND slotIndex=%i AND userId_fk=%i;",
- titleName.C_String(), slotIndex, command->callerUserId);
- break;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_SetPresence_PGSQL::ServerPreDBMemoryImpl( Lobby2Server *server, RakString userHandle )
- {
- server->SetPresence( presence, userHandle );
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_GetPresence_PGSQL::ServerPreDBMemoryImpl( Lobby2Server *server, RakString userHandle )
- {
- server->GetPresence( presence, userHandle );
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Client_StopIgnore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Client_StopIgnore_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Client_StopIgnore_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- PGresult *result = pgsql->QueryVariadic(
- "DELETE FROM lobby2.ignore WHERE userMe_fk=%i AND userOther_fk=%i", command->callerUserId, targetUserId);
- PQclear(result);
- Notification_Client_IgnoreStatus *notification = (Notification_Client_IgnoreStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Client_IgnoreStatus);
- notification->otherHandle=command->callingUserName;
- notification->nowIgnored=false;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Friends_SendInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Friends_SendInvite_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Friends_SendInvite_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- // Don't do if already in friends table (already friends, or already has an invite)
- result = pgsql->QueryVariadic(
- "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
- "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
- , command->callerUserId, targetUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned!=0)
- {
- RakNet::RakString description;
- PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
- if (description=="sentInvite")
- resultCode=L2RC_Friends_SendInvite_ALREADY_SENT_INVITE;
- else if (description=="isFriends")
- resultCode=L2RC_Friends_SendInvite_ALREADY_FRIENDS;
- else
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- PQclear(result);
- return true;
- }
- PQclear(result);
- // Add friend invite
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.friends (userOne_fk, userTwo_fk, actionId_fk) VALUES "
- "(%i, %i, (SELECT actionId_pk FROM lobby2.friendActions WHERE description='sentInvite'));"
- , command->callerUserId, targetUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- // Notify by email
- SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_SendInvite", pgsql);
- // Tell the other system the invitation was sent
- Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->subject=subject;
- notification->body=body;
- notification->op=Notification_Friends_StatusChange::GOT_INVITATION_TO_BE_FRIENDS;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Friends_AcceptInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Friends_AcceptInvite_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Friends_AcceptInvite_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- // Make sure we have an invite from the other user
- result = pgsql->QueryVariadic(
- "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
- "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
- , targetUserId, command->callerUserId );
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned!=0)
- {
- RakNet::RakString description;
- PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
- PQclear(result);
- if (description!=RakNet::RakString("sentInvite"))
- {
- resultCode=L2RC_Friends_AcceptInvite_NO_INVITE;
- return true;
- }
- }
- else
- {
- PQclear(result);
- resultCode=L2RC_Friends_AcceptInvite_NO_INVITE;
- return true;
- }
- // Change from invited to friends, insert twice
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.friends SET actionId_fk=(SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends') WHERE userOne_fk=%i AND userTwo_fk=%i;"
- , targetUserId, command->callerUserId );
- RakAssert(result);
- PQclear(result);
- // Delete any existing invites, etc. if there are any
- result = pgsql->QueryVariadic(
- "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
- , command->callerUserId, targetUserId );
- PQclear(result);
- // Insert as a friend
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.friends (userOne_fk, userTwo_fk, actionId_fk) VALUES (%i, %i, (SELECT actionId_pk from lobby2.friendActions WHERE description='isFriends'));"
- ,command->callerUserId, targetUserId);
- RakAssert(result);
- PQclear(result);
- SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_AcceptInvite", (PostgreSQLInterface *) databaseInterface);
- // Tell the other system the invitation was accepted
- Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->subject=subject;
- notification->body=body;
- notification->op=Notification_Friends_StatusChange::THEY_ACCEPTED_OUR_INVITATION_TO_BE_FRIENDS;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- void RakNet::Friends_AcceptInvite_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
- {
- (void)userHandle;
- server->GetPresence(presence,targetHandle);
- }
- bool RakNet::Friends_RejectInvite_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Friends_RejectInvite_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Friends_RejectInvite_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- // Make sure we have an invite from the other user
- result = pgsql->QueryVariadic(
- "SELECT description FROM lobby2.friendActions WHERE actionId_pk="
- "(SELECT actionId_fk from lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i);"
- , targetUserId, command->callerUserId );
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned!=0)
- {
- RakNet::RakString description;
- PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
- PQclear(result);
- if (description!=RakNet::RakString("sentInvite"))
- {
- resultCode=L2RC_Friends_RejectInvite_NO_INVITE;
- return true;
- }
- }
- else
- {
- PQclear(result);
- resultCode=L2RC_Friends_RejectInvite_NO_INVITE;
- return true;
- }
- // Delete friend invite (both ways)
- result = pgsql->QueryVariadic(
- "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
- , targetUserId, command->callerUserId );
- PQclear(result);
- result = pgsql->QueryVariadic(
- "DELETE FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
- , command->callerUserId, targetUserId );
- PQclear(result);
- SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_RejectInvite", (PostgreSQLInterface *) databaseInterface);
- // Tell the other system the invitation was rejected
- Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->subject=subject;
- notification->body=body;
- notification->op=Notification_Friends_StatusChange::THEY_REJECTED_OUR_INVITATION_TO_BE_FRIENDS;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Friends_GetInvites_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- GetFriendInfosByStatus(command->callerUserId, "sentInvite", pgsql, invitesSent, true);
- GetFriendInfosByStatus(command->callerUserId, "sentInvite", pgsql, invitesReceived, false);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- void RakNet::Friends_GetInvites_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
- {
- (void)userHandle;
- for (unsigned int i=0; i < invitesSent.Size(); i++)
- server->GetUserOnlineStatus(invitesSent[i].usernameAndStatus);
- for (unsigned int i=0; i < invitesReceived.Size(); i++)
- server->GetUserOnlineStatus(invitesReceived[i].usernameAndStatus);
- }
- bool RakNet::Friends_GetFriends_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- GetFriendInfosByStatus(command->callerUserId, "isFriends", pgsql, myFriends, true);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- void RakNet::Friends_GetFriends_PGSQL::ServerPostDBMemoryImpl( Lobby2Server *server, RakString userHandle )
- {
- (void)userHandle;
- for (unsigned int i=0; i < myFriends.Size(); i++)
- server->GetUserOnlineStatus(myFriends[i].usernameAndStatus);
- }
- bool RakNet::Friends_Remove_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Friends_Remove_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Friends_Remove_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- result = pgsql->QueryVariadic(
- "SELECT userOne_fk FROM lobby2.friends WHERE userOne_fk=%i AND userTwo_fk=%i;"
- , command->callerUserId, targetUserId );
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- PQclear(result);
- if (numRowsReturned==0)
- {
- resultCode=L2RC_Friends_Remove_NOT_FRIENDS;
- return true;
- }
- // Bidirectional delete
- result = pgsql->QueryVariadic("DELETE FROM lobby2.friends WHERE (userOne_fk=%i AND userTwo_fk=%i) OR (userOne_fk=%i AND userTwo_fk=%i)",
- command->callerUserId, targetUserId, targetUserId, command->callerUserId);
- RakAssert(result);
- PQclear(result);
- SendEmail(targetUserId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Friends_Remove", (PostgreSQLInterface *) databaseInterface);
- Notification_Friends_StatusChange *notification = (Notification_Friends_StatusChange *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_StatusChange);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->subject=subject;
- notification->body=body;
- notification->op=Notification_Friends_StatusChange::YOU_WERE_REMOVED_AS_A_FRIEND;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetUserId, targetHandle);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::BookmarkedUsers_Add_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_BookmarkedUsers_Add_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_BookmarkedUsers_Add_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.bookmarkedUsers (userMe_fk, userOther_fk, type, description) VALUES (%i, %i, %i, %s)",
- command->callerUserId, targetUserId, type, description.C_String() );
- if (result==0)
- {
- resultCode=L2RC_BookmarkedUsers_Add_ALREADY_BOOKMARKED;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::BookmarkedUsers_Remove_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_BookmarkedUsers_Remove_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_BookmarkedUsers_Remove_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- result = pgsql->QueryVariadic(
- "DELETE FROM lobby2.bookmarkedUsers WHERE userOther_fk=%i AND type=%i",
- targetUserId, type);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::BookmarkedUsers_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic(
- "SELECT (SELECT handle FROM lobby2.users where userId_pk=userOther_fk) as handle, *"
- "FROM (SELECT userOther_fk, type, description, creationDate from lobby2.bookmarkedUsers WHERE userMe_fk=%i) as bm ORDER BY type, creationDate ASC;",
- command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- int i;
- for (i=0; i < numRowsReturned; i++)
- {
- BookmarkedUser bm;
- PostgreSQLInterface::PQGetValueFromBinary(&bm.targetHandle, result, i, "handle");
- PostgreSQLInterface::PQGetValueFromBinary(&bm.type, result, i, "type");
- PostgreSQLInterface::PQGetValueFromBinary(&bm.description, result, i, "description");
- PostgreSQLInterface::PQGetValueFromBinary(&bm.dateWhenAdded, result, i, "creationDate");
- bookmarkedUsers.Insert(bm, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Emails_Send_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- SendEmail(recipients, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, status, "Emails_Send", (PostgreSQLInterface *) databaseInterface);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Emails_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result;
-
- if (unreadEmailsOnly==true)
- {
- if (emailIdsOnly)
- {
- result = pgsql->QueryVariadic(
- "SELECT tbl2.emailid_fk from lobby2.users, ( "
- "SELECT tbl1.*, lobby2.emails.creationDate FROM "
- "(SELECT emailId_fk, userMe_fk, userOther_fk, isDeleted, wasRead FROM lobby2.emailTargets) as tbl1, lobby2.emails "
- "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE AND tbl1.wasRead=FALSE "
- ") as tbl2 "
- "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
- , command->callerUserId);
- }
- else
- {
- result = pgsql->QueryVariadic(
- "SELECT handle, tbl2.* from lobby2.users, ("
- "SELECT tbl1.*, lobby2.emails.subject, lobby2.emails.body, lobby2.emails.binaryData, lobby2.emails.creationDate FROM"
- "(SELECT emailId_fk, emailTarget_pk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
- "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE AND tbl1.wasRead=FALSE"
- ") as tbl2 "
- "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
- , command->callerUserId);
- }
- }
- else
- {
- if (emailIdsOnly)
- {
- result = pgsql->QueryVariadic(
- "SELECT tbl2.emailid_fk from lobby2.users, ( "
- "SELECT tbl1.*, lobby2.emails.creationDate FROM "
- "(SELECT emailId_fk, userMe_fk, userOther_fk, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
- "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE "
- ") as tbl2 "
- "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
- , command->callerUserId);
- }
- else
- {
- result = pgsql->QueryVariadic(
- "SELECT handle, tbl2.* from lobby2.users, ("
- "SELECT tbl1.*, lobby2.emails.subject, lobby2.emails.body, lobby2.emails.binaryData, lobby2.emails.creationDate FROM"
- "(SELECT emailId_fk, emailTarget_pk, userMe_fk, userOther_fk, status, wasRead, ISentThisEmail, isDeleted FROM lobby2.emailTargets) as tbl1, lobby2.emails "
- "WHERE tbl1.emailId_fk=lobby2.emails.emailId_pk AND tbl1.userMe_fk=%i AND tbl1.isDeleted=FALSE"
- ") as tbl2 "
- "WHERE userId_pk=tbl2.userother_fk ORDER BY creationDate ASC;"
- , command->callerUserId);
- }
- }
-
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- int i;
- for (i=0; i < numRowsReturned; i++)
- {
- EmailResult emailResult;
- RakNet::RakString otherHandle;
- RakNet::RakString myHandle = command->callingUserName;
- // 4/6/2011 emailTarget_pk is correct, this is used by Emails_Delete and Emails_SetStatus
- // 11/4/2010 - I think this was a copy/paste error
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.emailID, result, i, "emailTarget_pk");
- // PostgreSQLInterface::PQGetValueFromBinary(&emailResult.emailID, result, i, "emailId_fk");
- bool getThisEmail;
- if (emailsToRetrieve.Size()>0)
- {
- getThisEmail=false;
- for (unsigned int i=0; i < emailsToRetrieve.Size(); i++)
- {
- if (emailsToRetrieve[i]==emailResult.emailID)
- {
- getThisEmail=true;
- break;
- }
- }
- }
- else
- getThisEmail=true;
- if (getThisEmail)
- {
- if (emailIdsOnly==false)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&otherHandle, result, i, "handle");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.status, result, i, "status");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.wasReadByMe, result, i, "wasRead");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.wasSendByMe, result, i, "ISentThisEmail");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.subject, result, i, "subject");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.body, result, i, "body");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.binaryData->binaryData, &emailResult.binaryData->binaryDataLength, result, i, "binaryData");
- PostgreSQLInterface::PQGetValueFromBinary(&emailResult.creationDate, result, i, "creationDate");
- if (emailResult.wasSendByMe)
- {
- emailResult.sender=myHandle;
- emailResult.recipient=otherHandle;
- }
- else
- {
- emailResult.sender=otherHandle;
- emailResult.recipient=myHandle;
- }
- }
- emailResults.Insert(emailResult, __FILE__, __LINE__ );
- }
- emailResults.Insert(emailResult, _FILE_AND_LINE_ );
- }
- resultCode=L2RC_SUCCESS;
- PQclear(result);
- return true;
- }
- bool RakNet::Emails_Delete_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT isDeleted FROM lobby2.emailTargets WHERE emailTarget_pk = %i", emailId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Emails_Delete_UNKNOWN_EMAIL_ID;
- return true;
- }
- bool isDeleted;
- PostgreSQLInterface::PQGetValueFromBinary(&isDeleted, result, 0, "isDeleted");
- PQclear(result);
- if (isDeleted)
- {
- resultCode=L2RC_Emails_Delete_ALREADY_DELETED;
- return true;
- }
- // Don't actually delete, just flag as deleted. This is so the admin can investigate reports of abuse.
- result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET isDeleted=TRUE WHERE emailTarget_pk = %i", emailId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Emails_SetStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT isDeleted FROM lobby2.emailTargets WHERE emailTarget_pk = %i", emailId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Emails_SetStatus_UNKNOWN_EMAIL_ID;
- return true;
- }
- bool isDeleted;
- PostgreSQLInterface::PQGetValueFromBinary(&isDeleted, result, 0, "isDeleted");
- PQclear(result);
- if (isDeleted)
- {
- resultCode=L2RC_Emails_SetStatus_WAS_DELETED;
- return true;
- }
- if (updateStatusFlag)
- {
- result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET status=%i WHERE emailTarget_pk = %i", newStatusFlag, emailId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- }
- if (updateMarkedRead)
- {
- result = pgsql->QueryVariadic("UPDATE lobby2.emailTargets SET wasRead=%b WHERE emailTarget_pk = %i", isNowMarkedRead, emailId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_SubmitMatch_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_SubmitMatch_TITLE_NOT_IN_USE;
- return true;
- }
- // Insert
- result = pgsql->QueryVariadic("INSERT INTO lobby2.matches (gameTypeName, titleName_fk, matchNote, binaryData) VALUES "
- "(%s, %s, %s, %a) RETURNING matchId_pk;",
- gameType.C_String(), titleName.C_String(), submittedMatch.matchNote.C_String(), submittedMatch.binaryData->binaryData, submittedMatch.binaryData->binaryDataLength );
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result, 0, "matchId_pk");
- PQclear(result);
- // For each match participant, add to lobby2.matchParticipants
- unsigned int i;
- for (i=0; i < submittedMatch.matchParticipants.Size(); i++)
- {
- result = pgsql->QueryVariadic("INSERT INTO lobby2.matchParticipants (matchId_fk, userId_fk, score) VALUES "
- "(%i, (SELECT userId_pk FROM lobby2.users WHERE handleLower=lower(%s)), %f);",
- submittedMatch.matchID, submittedMatch.matchParticipants[i].handle.C_String(), submittedMatch.matchParticipants[i].score);
- // May fail if a user is deleted at the same time this is running
- if (result)
- PQclear(result);
- }
- result = pgsql->QueryVariadic("SELECT EXTRACT(EPOCH FROM now()) as whenSubmittedDate;");
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.whenSubmittedDate, result, 0, "whenSubmittedDate");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_GetMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result1, *result2;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_SubmitMatch_TITLE_NOT_IN_USE;
- return true;
- }
- result1 = pgsql->QueryVariadic("SELECT matchId_pk, matchNote, binaryData, EXTRACT(EPOCH FROM creationDate) as creationDate from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
- if (result1==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numMatchesReturned = PQntuples(result1);
- int i;
- for (i=0; i < numMatchesReturned; i++)
- {
- SubmittedMatch submittedMatch;
- MatchParticipant matchParticipant;
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result1, i, "matchId_pk");
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchNote, result1, i, "matchNote");
- // PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.binaryData->binaryData, &submittedMatch.binaryData->binaryDataLength, result1, i, "binaryData");
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.whenSubmittedDate, result1, i, "creationDate");
- PostgreSQLInterface::PQGetValueFromBinary(&submittedMatch.matchID, result1, i, "matchId_pk");
- result2=pgsql->QueryVariadic("SELECT (SELECT handle FROM lobby2.users where userId_pk=userId_fk) as handle, score from lobby2.matchParticipants where matchId_fk=%i;", submittedMatch.matchID);
- int numParticipants = PQntuples(result2);
- for (int j=0; j < numParticipants; j++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&matchParticipant.handle, result2, j, "handle");
- PostgreSQLInterface::PQGetValueFromBinary(&matchParticipant.score, result2, j, "score");
- submittedMatch.matchParticipants.Insert(matchParticipant, _FILE_AND_LINE_ );
- }
- PQclear(result2);
- submittedMatches.Insert(submittedMatch, _FILE_AND_LINE_ );
- }
- PQclear(result1);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_GetMatchBinaryData_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic("SELECT binaryData from lobby2.matches WHERE matchId_pk=%i", matchID);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Ranking_GetMatchBinaryData_INVALID_MATCH_ID;
- return false;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_GetTotalScore_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_GetTotalScore_TITLE_NOT_IN_USE;
- return true;
- }
- unsigned int userRow = GetUserRowFromHandle(targetHandle, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT COUNT(score) as count, sum(score) as sum from lobby2.matchParticipants WHERE userId_fk=%i AND matchId_fk IN"
- "(SELECT matchId_pk from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s);", userRow, gameType.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int64_t count;
- PostgreSQLInterface::PQGetValueFromBinary(&count, result, 0, "count");
- numScoresSubmitted = (unsigned int) count;
- if (numScoresSubmitted>0)
- PostgreSQLInterface::PQGetValueFromBinary(&scoreSum, result, 0, "sum");
- else
- scoreSum=0.0f;
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_WipeScoresForPlayer_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_WipeScoresForPlayer_TITLE_NOT_IN_USE;
- return true;
- }
- unsigned int userRow = GetUserRowFromHandle(targetHandle, pgsql);
- if (userRow==0)
- {
- resultCode=L2RC_UNKNOWN_USER;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.matchParticipants WHERE userId_fk=%i AND matchId_fk IN"
- "(SELECT matchId_pk from lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s);", userRow, gameType.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_WipeMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_WipeMatches_TITLE_NOT_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.matches WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_PruneMatches_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- result = pgsql->QueryVariadic("DELETE FROM lobby2.matches WHERE creationDate < (select now() - %i * interval '1 day')", pruneTimeDays);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_UpdateRating_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_UpdateRating_TITLE_NOT_IN_USE;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Ranking_UpdateRating_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- result = pgsql->QueryVariadic("INSERT INTO lobby2.ratings (userId_fk, gameTypeName, titleName_fk, userRating) VALUES (%i, %s, %s, %f)", targetUserId, gameType.C_String(), titleName.C_String(), targetRating);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_WipeRatings_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- // Verify title name
- if (IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_WipeRatings_TITLE_NOT_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.ratings WHERE gameTypeName=%s AND titleName_fk=%s;", gameType.C_String(), titleName.C_String());
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Ranking_GetRating_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (RakNet::IsTitleInUse(titleName, pgsql)==false)
- {
- resultCode=L2RC_Ranking_GetRating_TITLE_NOT_IN_USE;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Ranking_GetRating_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT userRating, creationDate FROM lobby2.ratings WHERE gameTypeName=%s AND titleName_fk=%s and userId_fk=%i ORDER by creationDate LIMIT 1;", gameType.C_String(), titleName.C_String(), targetUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_Ranking_GetRating_NO_RATING;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(¤tRating, result, 0, "userRating");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_Create_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- if (StringContainsProfanity(clanHandle, pgsql))
- {
- resultCode=L2RC_PROFANITY_FILTER_CHECK_FAILED;
- return true;
- }
- if (GetClanIdFromHandle(clanHandle, pgsql))
- {
- resultCode=L2RC_Clans_Create_CLAN_HANDLE_IN_USE;
- return true;
- }
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clans (leaderUserId_fk, clanHandle, requiresInvitationsToJoin, description, binaryData) VALUES "
- "(%i, %s, %b, %s, %a) RETURNING clanId_pk;", command->callerUserId, clanHandle.C_String(), requiresInvitationsToJoin, description.C_String(), binaryData->binaryData, binaryData->binaryDataLength );
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- unsigned int clanId_pk;
- PostgreSQLInterface::PQGetValueFromBinary(&clanId_pk, result, 0, "clanId_pk");
- PQclear(result);
- if (failIfAlreadyInClan)
- {
- // Checking after rather than before in case this user creates a clan in another thread at the same time
- result = pgsql->QueryVariadic("SELECT COUNT(*) as count from lobby2.clans WHERE leaderUserId_fk=%i", command->callerUserId);
- long long count;
- PostgreSQLInterface::PQGetValueFromBinary(&count, result, 0, "count");
- PQclear(result);
- if (count>1)
- {
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE (clanId_pk=%i);", clanId_pk);
- PQclear(result);
- resultCode=L2RC_Clans_Create_ALREADY_IN_A_CLAN;
- return true;
- }
- }
- // Add yourself as a clan member
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
- "(%i, %i, false, (SELECT stateId_Pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active'));", command->callerUserId, clanId_pk );
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- // Tell all friends about this new clan
- DataStructures::List<unsigned int> output;
- GetFriendIDs(command->callerUserId, true, pgsql, output);
- unsigned int idx;
- for (idx=0; idx < output.Size(); idx++)
- {
- Notification_Friends_CreatedClan *notification = (Notification_Friends_CreatedClan *)command->server->GetMessageFactory()->Alloc(L2MID_Notification_Friends_CreatedClan);
- RakAssert(command->callingUserName.IsEmpty()==false);
- notification->otherHandle=command->callingUserName;
- notification->clanName=clanHandle;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, output[idx], "");
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_SetProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SetProperties_UNKNOWN_CLAN;
- return true;
- }
- if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
- {
- resultCode=L2RC_Clans_SetProperties_MUST_BE_LEADER;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clans SET description=%s, binaryData=%a WHERE clanId_pk=%i", description.C_String(), binaryData->binaryData, binaryData->binaryDataLength, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_GetProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_GetProperties_UNKNOWN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT description, binaryData FROM lobby2.clans WHERE clanId_pk=%i", clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
- PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_SetMyMemberProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SetMyMemberProperties_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_SetMyMemberProperties_NOT_IN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET description=%s, binaryData=%a WHERE userId_fk=%i AND clanId_fk=%i", description.C_String(), binaryData->binaryData, binaryData->binaryDataLength, command->callerUserId, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_Get_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- // Gets all clans that I am in, as active
- PGresult *result = pgsql->QueryVariadic(
- "SELECT U.handle, C.clanHandle, C.description, C.binaryData, C.clanId_fk, C.leaderUserId_fk "
- "FROM lobby2.users AS U, (SELECT clanHandle, description, binaryData, leaderUserId_fk, myClans.* FROM lobby2.clans, "
- "(SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i "
- "AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active')) AS myClans "
- "WHERE clanId_pk=myClans.clanId_fk) AS C WHERE U.userId_pk=C.leaderUserId_fk;", command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- for (int i=0; i < numRowsReturned; i++)
- {
- ClanInfo ci;
- unsigned int clanId;
- int leaderId;
- PostgreSQLInterface::PQGetValueFromBinary(&ci.clanName, result, i, "clanHandle");
- PostgreSQLInterface::PQGetValueFromBinary(&ci.description, result, i, "description");
- PostgreSQLInterface::PQGetValueFromBinary(&ci.clanLeader, result, i, "handle");
- PostgreSQLInterface::PQGetValueFromBinary(&ci.binaryData->binaryData, &ci.binaryData->binaryDataLength, result, i, "binaryData");
- PostgreSQLInterface::PQGetValueFromBinary(&clanId, result, i, "clanId_fk");
- PostgreSQLInterface::PQGetValueFromBinary(&leaderId, result, i, "leaderUserId_fk");
- // Get the names of all other active members in this clan
- PGresult *result2 = pgsql->QueryVariadic(
- "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
- "WHERE M.clanId_fk=%i AND M.userId_fk!=%i AND U.userId_pk=M.userId_fk "
- "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active');",
- clanId, leaderId);
- int numRowsReturned2 = PQntuples(result2);
- RakNet::RakString memberHandle;
- for (int j=0; j < numRowsReturned2; j++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&memberHandle, result2, j, "handle");
- ci.clanMembersOtherThanLeader.Insert(memberHandle, _FILE_AND_LINE_ );
- }
- PQclear(result2);
- clans.Insert(ci, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- };
- bool RakNet::Clans_GrantLeader_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_GrantLeader_UNKNOWN_CLAN;
- return true;
- }
- if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
- {
- resultCode=L2RC_Clans_GrantLeader_MUST_BE_LEADER;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Clans_GrantLeader_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Clans_GrantLeader_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_GrantLeader_TARGET_NOT_IN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clans SET leaderUserId_fk=%i WHERE clanId_pk=%i;",targetUserId, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- Notification_Clans_GrantLeader *notification;
- // Tell all clan members of the new leader
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- // Tell all clan members, except the command originator, about the new clan leader
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_GrantLeader *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_GrantLeader);
- notification->clanHandle=clanHandle;
- notification->oldLeader=command->callingUserName;
- notification->newLeader=targetHandle;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- return true;
- }
- bool RakNet::Clans_SetSubleaderStatus_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SetSubleaderStatus_UNKNOWN_CLAN;
- return true;
- }
- if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
- {
- resultCode=L2RC_Clans_SetProperties_MUST_BE_LEADER;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Clans_SetSubleaderStatus_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Clans_SetSubleaderStatus_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_SetSubleaderStatus_TARGET_NOT_IN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET isSubleader=%b WHERE clanId_fk=%i AND userId_fk=%i;",setToSubleader,clanId,targetUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- Notification_Clans_SetSubleaderStatus *notification;
- // Tell all clan members of the new leader
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- // Tell all clan members, except the command originator, about the new clan leader
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_SetSubleaderStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_SetSubleaderStatus);
- notification->clanHandle=clanHandle;
- notification->leaderHandle=command->callingUserName;
- notification->targetHandle=targetHandle;
- notification->setToSubleader=setToSubleader;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_SetMemberRank_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SetMemberRank_UNKNOWN_CLAN;
- return true;
- }
- if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
- {
- resultCode=L2RC_Clans_SetMemberRank_MUST_BE_LEADER;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Clans_SetMemberRank_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetUserId==command->callerUserId)
- {
- resultCode=L2RC_Clans_SetMemberRank_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_SetMemberRank_TARGET_NOT_IN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET rank=%i WHERE clanId_fk=%i AND userId_fk=%i;",newRank,clanId,targetUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- Notification_Clans_SetMemberRank *notification;
- // Tell all clan members of the new leader
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- // Tell all clan members, except the command originator, about the new clan leader
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_SetMemberRank *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_SetMemberRank);
- notification->clanHandle=clanHandle;
- notification->leaderHandle=command->callingUserName;
- notification->targetHandle=targetHandle;
- notification->newRank=newRank;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_GetMemberProperties_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PGresult *result=0;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_GetMemberProperties_UNKNOWN_CLAN;
- return true;
- }
- unsigned int targetUserId = GetUserRowFromHandle(targetHandle, pgsql);
- if (targetUserId==0)
- {
- resultCode=L2RC_Clans_GetMemberProperties_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetUserId, &isSubleader, pgsql);
- if (clanMemberState==CMD_UNDEFINED)
- {
- resultCode=L2RC_Clans_GetMemberProperties_TARGET_NOT_IN_CLAN;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT description, binaryData, isSubleader, rank, memberState_fk, banReason FROM lobby2.clanMembers where userId_fk=%i AND clanId_fk=%i",
- targetUserId, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PostgreSQLInterface::PQGetValueFromBinary(&description, result, 0, "description");
- PostgreSQLInterface::PQGetValueFromBinary(&binaryData->binaryData, &binaryData->binaryDataLength, result, 0, "binaryData");
- PostgreSQLInterface::PQGetValueFromBinary(&isSubleader, result, 0, "isSubleader");
- PostgreSQLInterface::PQGetValueFromBinary(&rank, result, 0, "rank");
- int cms;
- PostgreSQLInterface::PQGetValueFromBinary(&cms, result, 0, "memberState_fk");
- clanMemberState=(ClanMemberState)cms;
- PostgreSQLInterface::PQGetValueFromBinary(&banReason, result, 0, "banReason");
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_ChangeHandle_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(oldClanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_ChangeHandle_UNKNOWN_CLAN;
- return true;
- }
- if (IsClanLeader(clanId, command->callerUserId, pgsql)==false)
- {
- resultCode=L2RC_Clans_ChangeHandle_MUST_BE_LEADER;
- return true;
- }
- result = pgsql->QueryVariadic("UPDATE lobby2.clans SET clanHandle=%s WHERE clanId_pk=%i;", newClanHandle.C_String(), clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- Notification_Clans_ChangeHandle *notification;
- // Tell all clan members of the new leader
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- // Tell all clan members, except the command originator, about the new clan name
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_ChangeHandle *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_ChangeHandle);
- notification->oldClanHandle=oldClanHandle;
- notification->leaderHandle=command->callingUserName;
- notification->newClanHandle=newClanHandle;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_Leave_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_Leave_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- ClanMemberState state = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (state!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_Leave_NOT_IN_CLAN;
- return true;
- }
- bool isClanLeader = IsClanLeader(clanId, command->callerUserId, pgsql);
- // Remove from the clanMembers table
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- // Tell all clan members of the new leader
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- if (subject.IsEmpty()==false || body.IsEmpty()==false)
- {
- // Send this email to the members
- DataStructures::List<unsigned int> targetUserIds;
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState==CMD_ACTIVE)
- targetUserIds.Insert(clanMembers[i].userId, _FILE_AND_LINE_ );
- }
- SendEmail(targetUserIds, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_Leave", pgsql);
- }
- unsigned int validUserCount=0;
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- validUserCount++;
- }
- wasDissolved = false;
- if (isClanLeader)
- {
- if (dissolveIfClanLeader || validUserCount==0)
- {
- wasDissolved = true;
- // Send notification to clan members that the clan was destroyed, then destroy the clan
- Notification_Clans_Destroyed *notification;
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- notification = (Notification_Clans_Destroyed *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Destroyed);
- notification->clanHandle=clanHandle;
- notification->oldClanLeader=command->callingUserName;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- // Tell the former leader the clan was destroyed too
- notification = (Notification_Clans_Destroyed *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Destroyed);
- notification->clanHandle=clanHandle;
- notification->oldClanLeader=command->callingUserName;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, command->callerUserId, command->callingUserName);
- // Destroy the clan
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE clanId_pk=%i", clanId);
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- else
- {
- // Choose the oldest subleader to lead, or if no subleaders, the oldest member
- result = pgsql->QueryVariadic("SELECT handle, userId_pk FROM lobby2.users WHERE userId_pk=(SELECT userId_fk FROM lobby2.clanMembers WHERE clanId_fk=%i ORDER BY isSubleader, creationDate DESC LIMIT 1)",
- clanId);
- int numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- // Destroy the clan if no possible leader (due to asynch)
- wasDissolved = true;
- PQclear(result);
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clans WHERE clanId_pk=%i", clanId);
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- unsigned int newLeaderId;
- PostgreSQLInterface::PQGetValueFromBinary(&newClanLeader, result, 0, "handle");
- PostgreSQLInterface::PQGetValueFromBinary(&newLeaderId, result, 0, "userId_pk");
- PQclear(result);
- // Promote this member to the leader
- result = pgsql->QueryVariadic("UPDATE lobby2.clans SET leaderUserId_fk=%i WHERE clanId_pk = %i", newLeaderId, clanId);
- PQclear(result);
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET isSubleader=FALSE WHERE userId_fk = %i AND clanId_fk = %i", newLeaderId, clanId);
- PQclear(result);
- // Notify users of new leader
- Notification_Clans_GrantLeader *notification;
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_GrantLeader *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_GrantLeader);
- notification->clanHandle=clanHandle;
- notification->newLeader=newClanLeader;
- notification->oldLeader=command->callingUserName;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- }
- }
- // Send notification to clan members that the member has left the clan
- Notification_Clans_Leave *notification;
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- notification = (Notification_Clans_Leave *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Leave);
- notification->clanHandle=clanHandle;
- notification->targetHandle=command->callingUserName;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_SendJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_NOT_IN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- // Does target already have an entry?
- unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetId==command->callerUserId)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isTargetSubleader;
- RakNet::ClanMemberState targetClanMemberState = GetClanMemberState(clanId, targetId, &isTargetSubleader, pgsql);
- if (targetClanMemberState==CMD_ACTIVE)
- {
- // active member
- resultCode=L2RC_Clans_SendJoinInvitation_TARGET_ALREADY_IN_CLAN;
- return true;
- }
- if (targetClanMemberState==CMD_BANNED)
- {
- // banned
- resultCode=L2RC_Clans_SendJoinInvitation_TARGET_IS_BANNED;
- return true;
- }
- if (targetClanMemberState==CMD_JOIN_INVITED)
- {
- // already invited
- resultCode=L2RC_Clans_SendJoinInvitation_REQUEST_ALREADY_PENDING;
- return true;
- }
- if (targetClanMemberState==CMD_JOIN_REQUESTED)
- {
- resultCode=L2RC_Clans_SendJoinInvitation_TARGET_ALREADY_REQUESTED;
- // already requested
- return true;
- }
- // Add row to lobby2.clanMembers
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
- "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') );"
- ,targetId, clanId);
- PQclear(result);
- // Send email to targetman
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_SendJoinInvitation", pgsql);
- // Send notification to target, leader, subleaders about this invite
- Notification_Clans_PendingJoinStatus *notification;
- notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
- command->server->AddOutputFromThread(notification, targetId, targetHandle); // target
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_WithdrawJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_WithdrawJoinInvitation_UNKNOWN_CLAN;
- return true;
- }
- // Does target already have an entry?
- unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_WithdrawJoinInvitation_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetId==command->callerUserId)
- {
- resultCode=L2RC_Clans_WithdrawJoinInvitation_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_JOIN_INVITED)
- {
- resultCode=L2RC_Clans_WithdrawJoinInvitation_NO_SUCH_INVITATION_EXISTS;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_WithdrawJoinInvitation_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
- PQclear(result);
- // Send email to target
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_WithdrawJoinInvitation", pgsql);
- // Send notification to target, leader, subleaders
- Notification_Clans_PendingJoinStatus *notification;
- notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
- command->server->AddOutputFromThread(notification, targetId, targetHandle); // target
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- if (command->callerUserId==clanMembers[i].userId)
- continue;
- notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_AcceptJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_AcceptJoinInvitation_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_JOIN_INVITED)
- {
- resultCode=L2RC_Clans_AcceptJoinInvitation_NO_SUCH_INVITATION_EXISTS;
- return true;
- }
- // Change status from invited to clan member
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
- PQclear(result);
- // Do AFTER the update in case another thread also added to a clan
- if (failIfAlreadyInClan)
- {
- int count = GetActiveClanCount(command->callerUserId, pgsql);
- if (count>1)
- {
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i;", command->callerUserId, clanId);
- PQclear(result);
- resultCode=L2RC_Clans_AcceptJoinInvitation_ALREADY_IN_DIFFERENT_CLAN;
- return true;
- }
- }
- // Notify all members about this new member
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId )
- continue;
- Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
- notification->clanHandle=clanHandle;
- notification->targetHandle=command->callingUserName;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- // Send email to leader
- SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_AcceptJoinInvitation", pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_RejectJoinInvitation_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_RejectJoinInvitation_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_JOIN_INVITED)
- {
- resultCode=L2RC_Clans_RejectJoinInvitation_NO_SUCH_INVITATION_EXISTS;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
- PQclear(result);
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- // Send email to leader
- SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_RejectJoinInvitation", pgsql);
- // Subleader and leader notification
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_INVITATION;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_DownloadInvitationList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic(
- " SELECT clanHandle FROM lobby2.clans INNER JOIN "
- " (SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i AND memberState_fk = "
- " (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') ) as tbl1 "
- " ON tbl1.clanId_fk = lobby2.clans.clanId_pk;", command->callerUserId);
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- int numRowsReturned = PQntuples(result);
- int i;
- for (i=0; i < numRowsReturned; i++)
- {
- OpenInvite oi;
- PostgreSQLInterface::PQGetValueFromBinary(&oi.clanHandle, result, i, "clanHandle");
- invitationsSentToMe.Insert(oi, _FILE_AND_LINE_ );
- }
- PQclear(result);
- // Gets all users in clans that I am in, as active or leader, where an invitation has been sent
- result = pgsql->QueryVariadic(
- "SELECT creationDate, handle, clanHandle FROM "
- "( "
- "SELECT userId_fk, creationDate, myClans.clanId_fk FROM lobby2.clanMembers INNER JOIN (SELECT clanId_fk FROM lobby2.clanMembers WHERE userId_fk=%i) as myClans ON "
- "myClans.clanId_fk=lobby2.clanMembers.clanId_fk AND memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinInvited') "
- ") as t1 "
- "INNER JOIN lobby2.users as t2 on (t1.userId_fk=t2.userId_pk) "
- "INNER JOIN lobby2.clans as t3 on (t1.clanId_fk=t3.clanId_pk) ", command->callerUserId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
-
- numRowsReturned = PQntuples(result);
- if (numRowsReturned==0)
- {
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- for (i=0; i < PQntuples(result); i++)
- {
- ClanJoinInvite cjr;
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.sourceClan, result, i, "clanHandle");
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.joinRequestTarget, result, i, "joinRequestTarget");
- usersThatHaveAnInvitationFromClansThatIAmAMemberOf.Insert(cjr, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_SendJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- clanJoined=false;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_SendJoinRequest_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState==CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_SendJoinRequest_ALREADY_IN_CLAN;
- return true;
- }
- if (clanMemberState==CMD_BANNED)
- {
- resultCode=L2RC_Clans_SendJoinRequest_BANNED;
- return true;
- }
- if (clanMemberState==CMD_JOIN_REQUESTED)
- {
- resultCode=L2RC_Clans_SendJoinRequest_REQUEST_ALREADY_PENDING;
- return true;
- }
- if (clanMemberState==CMD_JOIN_INVITED)
- {
- resultCode=L2RC_Clans_SendJoinRequest_ALREADY_INVITED;
- return true;
- }
- result = pgsql->QueryVariadic("SELECT requiresInvitationsToJoin FROM lobby2.clans WHERE clanId_pk=%i",clanId);
- bool requiresInvitationsToJoin;
- PostgreSQLInterface::PQGetValueFromBinary(&requiresInvitationsToJoin, result, 0, "requiresInvitationsToJoin");
- PQclear(result);
- if (requiresInvitationsToJoin==false)
- {
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
- "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') );"
- ,command->callerUserId, clanId);
- PQclear(result);
- // Send notification all members about the new member
- // Notify all members about this new member
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId)
- continue;
- Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
- notification->clanHandle=clanHandle;
- notification->targetHandle=command->callingUserName;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- // Send email to leader
- SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_SendJoinRequest", pgsql);
- clanJoined=true;
- }
- else
- {
- // Add row to lobby2.clanMembers
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk) VALUES "
- "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested') );"
- ,command->callerUserId, clanId);
- PQclear(result);
- // Send notification to leader, subleaders about this invite
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=clanMembers[i].name;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_SENT;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_WithdrawJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_WithdrawJoinRequest_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState==CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_WithdrawJoinRequest_ALREADY_IN_CLAN;
- return true;
- }
- if (clanMemberState!=CMD_JOIN_REQUESTED)
- {
- resultCode=L2RC_Clans_WithdrawJoinRequest_NO_SUCH_INVITATION_EXISTS;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", command->callerUserId, clanId);
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- PQclear(result);
- // Send email to leader
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- SendEmail(clanLeaderId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_WithdrawJoinRequest", pgsql);
-
- // Send notification to leader, subleaders
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->targetHandle=clanMembers[i].name;
- notification->sourceHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_WITHDRAWN;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_AcceptJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql);
- if (clanMemberState!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_NOT_IN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- // Does target already have an entry?
- unsigned int targetId = RakNet::GetUserRowFromHandle(requestingUserHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetId==command->callerUserId)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- bool isTargetSubleader;
- RakNet::ClanMemberState targetClanMemberState = GetClanMemberState(clanId, targetId, &isTargetSubleader, pgsql);
- if (targetClanMemberState==CMD_ACTIVE)
- {
- // active member
- resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_ALREADY_IN_CLAN;
- return true;
- }
- if (targetClanMemberState==CMD_BANNED)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_IS_BANNED;
- return true;
- }
- if (targetClanMemberState!=CMD_JOIN_REQUESTED)
- {
- resultCode=L2RC_Clans_AcceptJoinRequest_REQUEST_NOT_PENDING;
- return true;
- }
- // Change status to clan member
- result = pgsql->QueryVariadic("UPDATE lobby2.clanMembers SET memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active') WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
- PQclear(result);
- // Do AFTER the update in case another thread also added to a clan
- if (failIfAlreadyInClan)
- {
- int count = GetActiveClanCount(targetId, pgsql);
- if (count>1)
- {
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i;", targetId, clanId);
- PQclear(result);
- resultCode=L2RC_Clans_AcceptJoinRequest_TARGET_ALREADY_IN_DIFFERENT_CLAN;
- return true;
- }
- }
- // Notify all members about this new member
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].userId==command->callerUserId )
- continue;
- Notification_Clans_NewClanMember *notification = (Notification_Clans_NewClanMember *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_NewClanMember);
- notification->clanHandle=clanHandle;
- notification->targetHandle=requestingUserHandle;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name); // subleader
- }
- // Send email to member
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_AcceptJoinRequest", pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_RejectJoinRequest_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_RejectJoinRequest_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_RejectJoinRequest_NOT_IN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_RejectJoinRequest_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- unsigned int targetId = RakNet::GetUserRowFromHandle(requestingUserHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_RejectJoinRequest_REQUESTING_USER_HANDLE_UNKNOWN;
- return true;
- }
- if (GetClanMemberState(clanId, targetId, &isSubleader, pgsql)!=CMD_JOIN_REQUESTED)
- {
- resultCode=L2RC_Clans_RejectJoinRequest_NO_SUCH_INVITATION_EXISTS;
- return true;
- }
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
- PQclear(result);
- // Subleader and leader notification
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- // requestingUserHandle will be notified too, insert a fake record
- ClanMemberDescriptor cmd;
- cmd.memberState=CMD_ACTIVE;
- cmd.isSubleader=true;
- cmd.userId=targetId;
- clanMembers.Insert(cmd, _FILE_AND_LINE_ );
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- Notification_Clans_PendingJoinStatus *notification = (Notification_Clans_PendingJoinStatus *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_PendingJoinStatus);
- notification->clanHandle=clanHandle;
- notification->sourceHandle=command->callingUserName;
- notification->targetHandle=requestingUserHandle;
- notification->clanMemberHandle=command->callingUserName;
- notification->majorOp=Notification_Clans_PendingJoinStatus::JOIN_CLAN_REQUEST;
- notification->minorOp=Notification_Clans_PendingJoinStatus::JOIN_REJECTED;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- // Send email to member
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_RejectJoinRequest", pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- };
- bool RakNet::Clans_DownloadRequestList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- int i;
- // Get all clanMembers that are in state requested of all clans where I am the leader or subleader
- result = pgsql->QueryVariadic(
- "SELECT C.clanHandle, creationDate, U.handle FROM lobby2.clanMembers INNER JOIN "
- "(SELECT clanId_pk as clanId FROM lobby2.clans WHERE leaderUserId_fk=%i "
- "UNION ALL "
- "SELECT clanId_fk as clanId FROM lobby2.clanMembers WHERE isSubleader=TRUE AND userId_fk=%i) AS clansWhereIAmLeaderOrSubleader "
- "ON clansWhereIAmLeaderOrSubleader.clanId=lobby2.clanMembers.clanId_fk "
- "INNER JOIN lobby2.users U ON U.userId_pk = userId_fk "
- "INNER JOIN lobby2.clans C ON C.clanId_pk = clanId "
- "WHERE lobby2.clanMembers.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested');",
- command->callerUserId, command->callerUserId);
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- for (i=0; i < PQntuples(result); i++)
- {
- ClanJoinRequest cjr;
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.targetClan, result, i, "clanHandle");
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.joinRequestSender, result, i, "handle");
- joinRequestsToMyClan.Insert(cjr, _FILE_AND_LINE_ );
- }
- PQclear(result);
- // Get all clanMembers where I am in state requested
- result = pgsql->QueryVariadic(
- "SELECT C.clanHandle, M.creationDate FROM lobby2.clanMembers AS M, lobby2.clans AS C WHERE "
- "M.userId_fk=%i AND C.clanId_pk=M.clanId_fk AND M.memberState_fk = "
- "(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_JoinRequested');",
- command->callerUserId);
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- for (i=0; i < PQntuples(result); i++)
- {
- ClanJoinRequest cjr;
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.targetClan, result, i, "clanHandle");
- PostgreSQLInterface::PQGetValueFromBinary(&cjr.dateSent, result, i, "creationDate");
- joinRequestsFromMe.Insert(cjr, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_KickAndBlacklistUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- (void)databaseInterface;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_NOT_IN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (targetId==command->callerUserId)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_CANNOT_PERFORM_ON_SELF;
- return true;
- }
- if (targetId==clanLeaderId)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_CANNOT_PERFORM_ON_LEADER;
- return true;
- }
- RakNet::ClanMemberState clanMemberState = GetClanMemberState(clanId, targetId, &isSubleader, pgsql);
- if (clanMemberState==CMD_BANNED)
- {
- resultCode=L2RC_Clans_KickAndBlacklistUser_ALREADY_BLACKLISTED;
- return true;
- }
- if (blacklist)
- {
- if (clanMemberState!=CMD_UNDEFINED)
- {
- // Change status to banned
- result = pgsql->QueryVariadic(
- "UPDATE lobby2.clanMembers "
- "SET banReason=%s, memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned') "
- "WHERE userId_fk=%i AND clanId_fk=%i;", reason.C_String(), targetId, clanId);
- }
- else
- {
- // Add row as banned
- result = pgsql->QueryVariadic(
- "INSERT INTO lobby2.clanMembers (userId_fk, clanId_fk, isSubleader, memberState_fk, banReason) VALUES "
- "(%i, %i, false, (SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned'), %s );"
- , targetId, clanId, reason.C_String());
- }
- PQclear(result);
- }
- else if (kick)
- {
- // Remove from the clanMembers table
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
- PQclear(result);
- }
- // Subleader and leader notification
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- if (clanMembers[i].userId==targetId)
- continue;
- Notification_Clans_KickAndBlacklistUser* notification = (Notification_Clans_KickAndBlacklistUser*) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_KickAndBlacklistUser);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->blacklistingUserHandle=command->callingUserName;
- notification->targetHandleWasKicked=(clanMemberState!=CMD_UNDEFINED);
- notification->reason=reason;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- // If user was in the clan, send Notification_Clans_Leave to them.
- if (clanMemberState!=CMD_UNDEFINED)
- {
- Notification_Clans_Leave *notification = (Notification_Clans_Leave *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_Leave);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, targetId, targetHandle);
- }
- // Send email to member
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_KickAndBlacklistUser", pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_UnblacklistUser_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_UnblacklistUser_UNKNOWN_CLAN;
- return true;
- }
- bool isSubleader;
- if (GetClanMemberState(clanId, command->callerUserId, &isSubleader, pgsql)!=CMD_ACTIVE)
- {
- resultCode=L2RC_Clans_UnblacklistUser_NOT_IN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- if (isSubleader==false && clanLeaderId!=command->callerUserId)
- {
- resultCode=L2RC_Clans_UnblacklistUser_MUST_BE_LEADER_OR_SUBLEADER;
- return true;
- }
- unsigned int targetId = RakNet::GetUserRowFromHandle(targetHandle, pgsql);
- if (targetId==0)
- {
- resultCode=L2RC_Clans_UnblacklistUser_UNKNOWN_TARGET_HANDLE;
- return true;
- }
- if (GetClanMemberState(clanId, targetId, &isSubleader, pgsql)!=CMD_BANNED)
- {
- resultCode=L2RC_Clans_UnblacklistUser_NOT_BLACKLISTED;
- return true;
- }
- // Remove from the clanMembers table
- result = pgsql->QueryVariadic("DELETE FROM lobby2.clanMembers WHERE userId_fk=%i AND clanId_fk=%i", targetId, clanId);
- PQclear(result);
- // Subleader and leader notification
- DataStructures::List<ClanMemberDescriptor> clanMembers;
- GetClanMembers(clanId, clanMembers, pgsql);
- for (unsigned int i=0; i < clanMembers.Size(); i++)
- {
- if (clanMembers[i].memberState!=CMD_ACTIVE)
- continue;
- if (clanMembers[i].isSubleader==false && clanMembers[i].userId!=clanLeaderId)
- continue;
- Notification_Clans_UnblacklistUser *notification = (Notification_Clans_UnblacklistUser *) command->server->GetMessageFactory()->Alloc(L2MID_Notification_Clans_UnblacklistUser);
- notification->clanHandle=clanHandle;
- notification->targetHandle=targetHandle;
- notification->unblacklistingUserHandle=command->callingUserName;
- notification->resultCode=L2RC_SUCCESS;
- command->server->AddOutputFromThread(notification, clanMembers[i].userId, clanMembers[i].name);
- }
- // Send email to member
- SendEmail(targetId, command->callerUserId, command->callingUserName, command->server, subject, body, binaryData, emailStatus, "Clans_UnblacklistUser", pgsql);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_GetBlacklist_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_GetBlacklist_UNKNOWN_CLAN;
- return true;
- }
- // Get the names of all banned members in this clan
- result = pgsql->QueryVariadic(
- "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
- "WHERE M.clanId_fk=%i AND M.userId_fk=U.userId_pk "
- "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Banned');",
- clanId );
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString memberName;
- for (int i=0; i < PQntuples(result); i++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&memberName, result, i, "handle");
- blacklistedUsers.Insert(memberName, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_GetMembers_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result=0;
- unsigned int clanId = GetClanIdFromHandle(clanHandle, pgsql);
- if (clanId==0)
- {
- resultCode=L2RC_Clans_GetMembers_UNKNOWN_CLAN;
- return true;
- }
- unsigned int clanLeaderId = GetClanLeaderId(clanId, pgsql);
- result = pgsql->QueryVariadic("SELECT handle from lobby2.users WHERE userId_pk=%i", clanLeaderId);
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- if (PQntuples(result)!=0)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&clanLeader, result, 0, "handle");
- }
- PQclear(result);
- // Get the names of all other active members in this clan
- result = pgsql->QueryVariadic(
- "SELECT U.handle FROM lobby2.clanMembers AS M, lobby2.users AS U "
- "WHERE M.clanId_fk=%i AND M.userId_fk!=%i AND M.userId_fk=U.userId_pk "
- "AND M.memberState_fk=(SELECT stateId_pk FROM lobby2.clanMemberStates WHERE description='ClanMember_Active');",
- clanId, clanLeaderId);
- if (result==0)
- {
- PQclear(result);
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString memberName;
- for (int i=0; i < PQntuples(result); i++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&memberName, result, i, "handle");
- clanMembersOtherThanLeader.Insert(memberName, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
- bool RakNet::Clans_GetList_PGSQL::ServerDBImpl( Lobby2ServerCommand *command, void *databaseInterface )
- {
- (void)command;
- PostgreSQLInterface *pgsql = (PostgreSQLInterface *)databaseInterface;
- PGresult *result = pgsql->QueryVariadic("SELECT clanhandle from lobby2.clans");
- if (result==0)
- {
- resultCode=L2RC_DATABASE_CONSTRAINT_FAILURE;
- return true;
- }
- RakNet::RakString clanName;
- for (int i=0; i < PQntuples(result); i++)
- {
- PostgreSQLInterface::PQGetValueFromBinary(&clanName, result, i, "clanhandle");
- clanNames.Insert(clanName, _FILE_AND_LINE_ );
- }
- PQclear(result);
- resultCode=L2RC_SUCCESS;
- return true;
- }
|