00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <cstring>
00031
00032 #include <curl/curl.h>
00033 #include <curl/types.h>
00034 #include <curl/easy.h>
00035
00036
00037 #include "../global.h"
00038 #include "../misc.h"
00039 #include "../sgstream.h"
00040 #include "../loaders.h"
00041 #include "../gamemap.h"
00042 #include "../dialog.h"
00043 #include "pbem-server.h"
00044
00045 #include "pbem-server-interaction.h"
00046
00047 #include "../paradialog.h"
00048
00049 #include <pgpropertyeditor.h>
00050 #include <pgpropertyfield_integer.h>
00051 #include <pgpropertyfield_intdropdown.h>
00052 #include <pgpropertyfield_dropdown.h>
00053 #include <pgpropertyfield_checkbox.h>
00054 #include <pgpropertyfield_string.h>
00055
00056
00057
00058
00059
00060
00061 class AuthenticationDialog : public ASC_PG_Dialog {
00062 private:
00063 ASCString name;
00064 ASCString password;
00065
00066 ASC_PropertyEditor* propertyEditor;
00067
00068 bool ok()
00069 {
00070 if ( propertyEditor->Apply() )
00071 QuitModal();
00072 return true;
00073 }
00074
00075 public:
00076 AuthenticationDialog() : ASC_PG_Dialog(NULL, PG_Rect( -1, -1, 300, 150 ), "Logging in to server...")
00077 {
00078 propertyEditor = new ASC_PropertyEditor( this, PG_Rect( 10, GetTitlebarHeight() + 20, Width() - 20, Height() - GetTitlebarHeight() - 50 ), "PropertyEditor", 50 );
00079
00080 new PG_PropertyField_String<ASCString>( propertyEditor , "Username", &name );
00081 (new PG_PropertyField_String<ASCString>( propertyEditor , "Default Password", &password ))->SetPassHidden('*');
00082
00083 PG_Button* ok = new PG_Button( this, PG_Rect( Width() - 100, Height() - 40, 90, 30), "OK" );
00084 ok->sigClick.connect( SigC::slot( *this, &AuthenticationDialog::ok ));
00085 }
00086
00087 const ASCString& getName() { return name; };
00088 const ASCString& getPassword() { return password; };
00089 };
00090
00091
00092
00093
00094 PBEMServer :: PBEMServer()
00095 : filename("myFileName"), gameID(-1)
00096 {
00097
00098 }
00099
00100
00101 void PBEMServer::readChildData ( tnstream& stream )
00102 {
00103 stream.readInt();
00104 serverAddress = stream.readString();
00105 gameID = stream.readInt();
00106 }
00107
00108 void PBEMServer::writeChildData ( tnstream& stream ) const
00109 {
00110 stream.writeInt( 1 );
00111 stream.writeString(serverAddress );
00112 stream.writeInt ( gameID );
00113 }
00114
00115 void PBEMServer::setup()
00116 {
00117
00118 }
00119
00120
00121
00122 ASC_PBEM* PBEMServer::getSession()
00123 {
00124 static ASC_PBEM* session = NULL;
00125 if ( !session ) {
00126 session = new ASC_PBEM( serverAddress );
00127
00128 bool success;
00129 do {
00130 AuthenticationDialog ad;
00131 ad.Show();
00132 ad.RunModal();
00133 success = session->login( ad.getName(), ad.getPassword() );
00134 if( !success )
00135 warning("login failed!");
00136
00137 } while ( !success );
00138 }
00139 return session;
00140 }
00141
00142
00143 void PBEMServer::send( const GameMap* map, int lastPlayer, int lastturn )
00144 {
00145
00146
00147 if ( choice_dlg("Do you want to upload the game to the ASC server?", "~y~es","~n~o") == 1) {
00148
00149
00150 if ( gameID == -1 )
00151 gameID = editInt( "turn", gameID );
00152
00153
00154 tmemorystreambuf buffer;
00155
00156 {
00157 tmemorystream stream ( &buffer, tnstream::writing );
00158
00159 tnetworkloaders nwl;
00160 nwl.savenwgame( &stream, map );
00161 }
00162
00163
00164
00165
00166 ASC_PBEM* session = getSession();
00167 bool result = session->uploadFile( filename, buffer.getBuffer(), buffer.getSize(), gameID );
00168 if ( !result )
00169 warning( "upload failed");
00170
00171 } else {
00172
00173
00174 }
00175 }
00176
00177
00178 GameMap* PBEMServer::receive()
00179 {
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 return NULL;
00197 }
00198
00199 ASCString PBEMServer::getDefaultServerAddress()
00200 {
00201
00202
00203
00204 return "http://localhost:8080/ascServer";
00205 }
00206
00207
00208 void PBEMServer::setServerAddress( const ASCString& address )
00209 {
00210 serverAddress = address;
00211 }
00212
00213
00214 namespace {
00215 const bool r1 = networkTransferMechanismFactory::Instance().registerClass( PBEMServer::mechanismID(), ObjectCreator<GameTransferMechanism, PBEMServer> );
00216 }
00217