S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CIMOpenAPIExamplemodel Class Reference

#include <IMOpenAPIExampleModel.h>

List of all members.

Public Member Functions

void HandleLoginL (const TInt aErrorCode)
void HandleCancelLoginL (const TInt aErrorCode)
void HandleLogoutL (const TInt aErrorCode)
void HandleMessageSentL (const TInt aOpCode, const TInt aErrorCode)
void HandleSendErrorL (const TInt aOpCode, const TInt aErrorCode, MImClientDetailedError *aDetailedError)
void HandleNewPToPMessageL (const TInt aErrorCode, const TContactItemId aContactId, const TDesC &aUserId, const TDesC &aMessageType, const TDesC8 &aContent)
void HandleNewPToPMessageL (const TInt aErrorCode, const TContactItemId aContactId, const TDesC &aUserId, const TDesC &aMessageType, const TDesC16 &aContent)
void LoginL (const TDesC &aServerAddress, const TDesC &aUserId, const TDesC &aPassword, const TInt aAccessPoint)
void LogoutL ()
void SendChallenge ()
void SendChallengeAccepted ()
void SendChallengeRejected ()
void SendAlreadyInGame ()
void SendStartGame ()
void SendExitGame ()
void SendPlayedIndex (TInt aIndex)
void SendEndChallenge ()
void SetOppID (const TDesC16 &aOppID)
 ~CIMOpenAPIExamplemodel ()
TGameState GetGameState () const
void SetGameState (TGameState const aState)
TGameSign GetOppSign () const
TBool IsIndexPositionEmpty (TInt aIndex)
void PrepareToExit ()

Static Public Member Functions

static CIMOpenAPIExamplemodel * NewLC (CIMOpenAPIExampleDocument *aDocument)
static CIMOpenAPIExamplemodel * NewL (CIMOpenAPIExampleDocument *aDocument)

Private Member Functions

 CIMOpenAPIExamplemodel ()
void ConstructL (CIMOpenAPIExampleDocument *aDocument)
TBool CheckForSeries (TGameSign aValue, TInt aStartIndex, TInt aIndexIncrement)
TBool MatchOppID (TDesC16 &aOppID)
TInt SendMsg (TDesC16 &aContent)
void ResetGameData ()
TBool IsGameFinnished (TBool &aIsDraw)
TBool IsGameDraw ()

Private Attributes

CImConnection * iIMConnection
MImClient * iIMClient
CIMOpenAPIExampleDocument * iDocument
TFixedArray< TGameSign, 9 > iGameData
TGameState iState
TInt iMsgID
CDesC16Array * iArrayOfIDs
HBufC * iOppID
TBool iSending
TMsg iLastMsg
TGameSign iMySign
TGameSign iOppSign
TBuf< KMaxProtocolStrLength > iMessageToSend
TInt64 iSeed
TBool iNotSupported


Detailed Description

This class is like an engine to the application. It holds the connection and communication to the IM server. It also holds most of the game logic and game data. The protocol allows only one outstanding opponent.

Definition at line 44 of file IMOpenAPIExampleModel.h.


Constructor & Destructor Documentation

CIMOpenAPIExamplemodel::~CIMOpenAPIExamplemodel (   ) 
 

~CIMOpenAPIExamplemodel. Destructor

Definition at line 101 of file IMOpenAPIExampleModel.cpp.

References iIMClient.

00102         {
00103         if( iIMClient )
00104                 {
00105         iIMClient->UnregisterObserver();
00106         delete iIMClient;
00107         iIMClient = NULL;
00108         }
00109     
00110     if (iIMConnection)
00111         {
00112         iIMConnection->UnregisterObserver();
00113         delete iIMConnection;
00114         iIMConnection = NULL;
00115         }
00116     
00117     if(iOppID)                    
00118         delete iOppID;
00119     iOppID = NULL;
00120     
00121     if (iArrayOfIDs)
00122         {
00123         iArrayOfIDs->Reset();
00124         delete iArrayOfIDs;
00125         }
00126         }

CIMOpenAPIExamplemodel::CIMOpenAPIExamplemodel (   )  [private]
 

CIMOpenAPIExamplemodel. C++ constructor

Definition at line 134 of file IMOpenAPIExampleModel.cpp.

00135         {
00136         }


Member Function Documentation

void CIMOpenAPIExamplemodel::HandleLoginL (  const TInt  aErrorCode  ) 
 

HandleLoginL. From MImConnectionObserver

Definition at line 144 of file IMOpenAPIExampleModel.cpp.

References CIMOpenAPIExampleAppUi::HandleMessageIMConnectionFromModel(), iDocument, SetGameState(), and CIMOpenAPIExampleDocument::UI().

00145     {
00146         iDocument->UI()->HandleMessageIMConnectionFromModel(Connected, aErrorCode);
00147         if (aErrorCode == KErrNone)
00148                 {
00149                 SetGameState(EStateLoginIdle);
00150                 }
00151     }

void CIMOpenAPIExamplemodel::HandleCancelLoginL (  const TInt  aErrorCode  ) 
 

HandleCancelLoginL. From MImConnectionObserver

Definition at line 169 of file IMOpenAPIExampleModel.cpp.

00170         {
00171         
00172         
00173         }

void CIMOpenAPIExamplemodel::HandleLogoutL (  const TInt  aErrorCode  ) 
 

HandleLogoutL. From MImConnectionObserver

Definition at line 158 of file IMOpenAPIExampleModel.cpp.

References CIMOpenAPIExampleAppUi::HandleMessageIMConnectionFromModel(), iDocument, SetGameState(), and CIMOpenAPIExampleDocument::UI().

00159     {
00160         iDocument->UI()->HandleMessageIMConnectionFromModel(DisConnected, aErrorCode);
00161         SetGameState(EStateNotLogin);                                                                                   
00162     }

void CIMOpenAPIExamplemodel::HandleMessageSentL (  const TInt  aOpCode,
const TInt  aErrorCode
) 
 

HandleMessageSentL. From MImObserver

Definition at line 180 of file IMOpenAPIExampleModel.cpp.

References GetGameState(), CIMOpenAPIExampleAppUi::HandleMessageFromModel(), iDocument, iLastMsg, iSending, SetGameState(), and CIMOpenAPIExampleDocument::UI().

00181     {
00182     iSending = EFalse;
00183     switch (iLastMsg)
00184         {
00185         case MsgChallenge:
00186             SetGameState(EStateChallenged);
00187             iDocument->UI()->HandleMessageFromModel(ChallengeSent, aErrorCode, KNullDesC);
00188             break;
00189         
00190         case MsgChallengeAccepted:
00191             SetGameState(EStateReadytoStart);
00192             break;
00193 
00194         case MsgChallengeRejected:
00195             SetGameState(EStateLoginIdle);
00196             break;
00197 
00198         case MsgStartGameYourTurn:
00199             if (GetGameState() == EStateReadytoStart)
00200                 SetGameState(EStateWaitingForOppMove);
00201             break;
00202 
00203         case MsgStartGameOppTurn:
00204             if (GetGameState() == EStateReadytoStart)
00205                 SetGameState(EStateWaitingForOwnMove);
00206             break;
00207 
00208         case MsgExitGame:
00209             iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, KNullDesC);
00210             SetGameState(EStateReadytoStart);
00211             break;
00212 
00213         case MsgPlayedIndex:
00214             break;
00215 
00216         case MsgEndChallenge:
00217             iDocument->UI()->HandleMessageFromModel(EndChallengeRec, aErrorCode, KNullDesC);
00218             SetGameState(EStateLoginIdle);
00219             break;
00220 
00221         case MsgAlreadyInGame:
00222             break;
00223 
00224         default:
00225             break;
00226         }
00227         }

void CIMOpenAPIExamplemodel::HandleSendErrorL (  const TInt  aOpCode,
const TInt  aErrorCode,
MImClientDetailedError *  aDetailedError
) 
 

HandleSendErrorL. From MImObserver

Definition at line 234 of file IMOpenAPIExampleModel.cpp.

References CIMOpenAPIExampleAppUi::HandleMessageErrorFromModel(), iDocument, iSending, SetGameState(), and CIMOpenAPIExampleDocument::UI().

00237     {
00238     iSending = EFalse;
00239 
00240 
00241     switch (aErrorCode)
00242         {
00243         case KImApiErrNotLogged:
00244             SetGameState(EStateNotLogin);
00245             iDocument->UI()->HandleMessageErrorFromModel(aErrorCode,KNullDesC );
00246             break;
00247 
00248         default:
00249             iDocument->UI()->HandleMessageErrorFromModel(aErrorCode,KNullDesC );
00250             break;
00251         }
00252     }

void CIMOpenAPIExamplemodel::HandleNewPToPMessageL (  const TInt  aErrorCode,
const TContactItemId  aContactId,
const TDesC &  aUserId,
const TDesC &  aMessageType,
const TDesC8 &  aContent
) 
 

HandleNewPToPMessageL. From MImObserver

Definition at line 405 of file IMOpenAPIExampleModel.cpp.

00411     {
00412     }

void CIMOpenAPIExamplemodel::HandleNewPToPMessageL (  const TInt  aErrorCode,
const TContactItemId  aContactId,
const TDesC &  aUserId,
const TDesC &  aMessageType,
const TDesC16 &  aContent
) 
 

HandleNewPToPMessageL. From MImObserver

Definition at line 260 of file IMOpenAPIExampleModel.cpp.

References CIMOpenAPIExampleAppUi::HandleChallengeRec(), CIMOpenAPIExampleAppUi::HandleMessageFromModel(), iDocument, iOppID, iState, SetGameState(), SetOppID(), and CIMOpenAPIExampleDocument::UI().

00266     {
00267     TInt integerInfo = 0;
00268     TLex16 receivedMsgLex;
00269     TPtrC16 receivedMsg;
00270     TBool isDraw = EFalse;
00271 
00272 
00273     // checking for played index, which is different from other messages
00274     if (aContent.Find(KPlayedIndex) != KErrNotFound)
00275         {
00276         receivedMsgLex.Assign(aContent.Right(1));
00277         receivedMsgLex.Val(integerInfo);
00278         receivedMsg.Set( aContent.Left(11) );
00279         }
00280     else
00281         receivedMsg.Set(aContent);
00282 
00283     switch (iState)
00284         {
00285         // In login idle state we only expect a Challenge from any other user
00286         case EStateLoginIdle:
00287             if ( receivedMsg.Compare(KChallenge) == 0)
00288                 {
00289                 SetOppID(aUserId);
00290                 iDocument->UI()->HandleChallengeRec(aUserId);
00291                 }
00292             break;
00293 
00294         // In Challenged state we only expects challenge accepted or rejected from our opp
00295         case EStateChallenged:
00296             if ( aUserId.Compare(*iOppID) != 0)
00297                 break;
00298             else if ( receivedMsg.Compare(KChallengeAccepted) == 0)
00299                 {
00300                 SetGameState(EStateReadytoStart);
00301                 iDocument->UI()->HandleMessageFromModel(ChallengeAcceptedRec, aErrorCode, aContent);
00302                 }
00303             else if ( receivedMsg.Compare(KChallengeRejected) == 0)
00304                 {
00305                 SetGameState(EStateLoginIdle);
00306                 iDocument->UI()->HandleMessageFromModel(ChallengeRejectedRec, aErrorCode, aContent);
00307                 }
00308             else if ( receivedMsg.Compare(KAlreadyInGame) == 0)
00309                 {
00310                 SetGameState(EStateLoginIdle);
00311                 iDocument->UI()->HandleMessageFromModel(AlreadyInGameRec, aErrorCode, aContent);
00312                 }
00313             // if opp also sent the challenge at the same time, we will reply already in game
00314             else if ( receivedMsg.Compare(KChallenge) == 0)
00315                 {
00316                 SetGameState(EStateLoginIdle);
00317                 iDocument->UI()->HandleMessageFromModel(AlreadyInGameRec, aErrorCode, aContent);
00318                 }
00319             // in all othe requests we will not take any action
00320             break;
00321 
00322         
00323         case EStateReadytoStart:
00324             if ( aUserId.Compare(*iOppID) != 0)
00325                 break;
00326             else if ( receivedMsg.Compare(KStartGameYourTurn) == 0)
00327                 {
00328                 SetGameState(EStateWaitingForOwnMove);
00329                 iDocument->UI()->HandleMessageFromModel(StartGameRec, aErrorCode, aContent);
00330                 }
00331             else if ( receivedMsg.Compare(KStartGameOppTurn) == 0)
00332                 {
00333                 SetGameState(EStateWaitingForOppMove);
00334                 iDocument->UI()->HandleMessageFromModel(StartGameRec, aErrorCode, aContent);
00335                 }
00336             else if ( receivedMsg.Compare(KExitGame) == 0)
00337                 {
00338                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00339                 SetGameState(EStateReadytoStart);
00340                 }
00341             else if ( receivedMsg.Compare(KEndChallenge) == 0)
00342                 {
00343                 iDocument->UI()->HandleMessageFromModel(EndChallengeRec, aErrorCode, KNullDesC);
00344                 SetGameState(EStateLoginIdle);
00345                 }
00346             else
00347                 SendEndChallenge();
00348             break;
00349 
00350 
00351         case EStateWaitingForOppMove:
00352             if ( aUserId.Compare(*iOppID) != 0)
00353                 break;
00354             else if ( receivedMsg.Compare(KPlayedIndex) == 0)
00355                 {
00356                 iDocument->UI()->HandleMessageFromModel(PlayedIndexRec, integerInfo, aContent);
00357                 iGameData.At(integerInfo) = iOppSign;
00358                 if ( IsGameFinnished(isDraw) )
00359                     {
00360                     if (isDraw)
00361                         iDocument->UI()->HandleMessageFromModel(GameDraw, aErrorCode, aContent);
00362                     else
00363                         iDocument->UI()->HandleMessageFromModel(YouLost, aErrorCode, aContent);
00364                     ResetGameData();
00365                     SetGameState(EStateReadytoStart);
00366                     break;
00367                     }
00368                 SetGameState(EStateWaitingForOwnMove);
00369                 }
00370             else if ( receivedMsg.Compare(KExitGame) == 0)
00371                 {
00372                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00373                 SetGameState(EStateReadytoStart);
00374                 }
00375             else
00376                 SendEndChallenge();
00377             break;
00378 
00379 
00380         case EStateWaitingForOwnMove:
00381             if ( aUserId.Compare(*iOppID) != 0)
00382                 break;
00383             if ( receivedMsg.Compare(KExitGame) == 0)
00384                 {
00385                 iDocument->UI()->HandleMessageFromModel(EndGameRec, aErrorCode, aContent);
00386                 SetGameState(EStateReadytoStart);
00387                 }
00388             else
00389                 SendEndChallenge();
00390             break;
00391 
00392 
00393         default:
00394             break;
00395 
00396         }
00397 
00398         }

void CIMOpenAPIExamplemodel::LoginL (  const TDesC &  aServerAddress,
const TDesC &  aUserId,
const TDesC &  aPassword,
const TInt  aAccessPoint
) 
 

LoginL. Login request to IM server. This method is Asynchronous. called from UI

Parameters:
aServerAddress IM server address aUserId user id aPassword password aAccessPoint access point number as integer

Definition at line 419 of file IMOpenAPIExampleModel.cpp.

References iIMConnection, and iNotSupported.

00421     {
00422         // We are using TRAPD at the UI level, because it has GUI to display
00423         if (iNotSupported)
00424             User::Leave(KErrNotSupported);      
00425     iIMConnection->LoginL(aServerAddress, aUserId, aPassword, aAccessPoint);
00426     }

void CIMOpenAPIExamplemodel::LogoutL (   ) 
 

LogoutL. Logout request from IM server. Called from UI

Definition at line 433 of file IMOpenAPIExampleModel.cpp.

References iIMConnection.

00434         {
00435         // We are using TRAPD at the UI level, because it has GUI to display
00436         iIMConnection->LogoutL();
00437         }

void CIMOpenAPIExamplemodel::SendChallenge (   ) 
 

SendChallenge. Send the challenge to the oponent. Called from UI

Definition at line 455 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, iMySign, iOppSign, and SendMsg().

00456         {
00457         iMessageToSend.Copy(KChallenge);
00458         iMsgID = SendMsg(iMessageToSend);
00459     iLastMsg = MsgChallenge;
00460     iMySign = ECrossSign;
00461     iOppSign = EZeroSign;
00462         }

void CIMOpenAPIExamplemodel::SendChallengeAccepted (   ) 
 

SendChallengeAccepted. Send that the challenge has been accepted by the UI. Called from UI

Definition at line 468 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, iMySign, iOppSign, and SendMsg().

00469         {
00470         iMessageToSend.Copy(KChallengeAccepted);
00471     iMsgID = SendMsg(iMessageToSend);
00472     iLastMsg = MsgChallengeAccepted;
00473     iMySign = EZeroSign;
00474     iOppSign = ECrossSign;
00475         }

void CIMOpenAPIExamplemodel::SendChallengeRejected (   ) 
 

SendChallengeRejected. Send that the challenge has been rejected by the UI. Called from UI

Definition at line 481 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, SendMsg(), and SetOppID().

00482         {
00483     iMessageToSend.Copy(KChallengeRejected);    
00484     iMsgID = SendMsg(iMessageToSend);
00485     iLastMsg = MsgChallengeRejected;
00486     SetOppID(KNullDesC);
00487         }

void CIMOpenAPIExamplemodel::SendAlreadyInGame (   ) 
 

SendAlreadyInGame. Send that user is already in a game so he/she cant accept messages from users other than opponent.

Definition at line 493 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, and SendMsg().

00494         {
00495     iMessageToSend.Copy(KAlreadyInGame);        
00496     iMsgID = SendMsg(iMessageToSend);
00497     iLastMsg = MsgAlreadyInGame;
00498         }

void CIMOpenAPIExamplemodel::SendStartGame (   ) 
 

SendStartGame. Send to the opponent that user has started the game.

Definition at line 504 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, iSeed, and SendMsg().

00505         {
00506     TBool turn;
00507     
00508     // Find a randomize turn
00509     turn = ( Math::Rand(iSeed) ) % 2;
00510 
00511     if (turn)
00512         {
00513         iMessageToSend.Copy(KStartGameOppTurn);
00514             iMsgID = SendMsg(iMessageToSend);
00515         iLastMsg = MsgStartGameOppTurn;
00516         }
00517     else
00518         {
00519         iMessageToSend.Copy(KStartGameYourTurn);
00520             iMsgID = SendMsg(iMessageToSend);
00521         iLastMsg = MsgStartGameYourTurn;
00522         }
00523         }

void CIMOpenAPIExamplemodel::SendExitGame (   ) 
 

SendExitGame. Send to the opponent that user has exited the game.

Definition at line 529 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, and SendMsg().

00530         {
00531     iMessageToSend.Copy(KExitGame);
00532         iMsgID = SendMsg(iMessageToSend);
00533     iLastMsg = MsgExitGame;
00534         }

void CIMOpenAPIExamplemodel::SendPlayedIndex (  TInt  aIndex  ) 
 

SendPlayedIndex. Send to the opponent that user has played on this index in the game matrix, so that opponent mark that position

Parameters:
aIndex the location where user mark. 0 to 8

Definition at line 540 of file IMOpenAPIExampleModel.cpp.

References CIMOpenAPIExampleAppUi::HandleMessageFromModel(), iDocument, iGameData, iLastMsg, iMessageToSend, iMsgID, iMySign, IsGameFinnished(), ResetGameData(), SendMsg(), SetGameState(), and CIMOpenAPIExampleDocument::UI().

00541         {
00542     TBool isDraw = EFalse;
00543 
00544     iMessageToSend.Copy(KPlayedIndex);
00545         iMessageToSend.AppendNumUC(aIndex,EDecimal);
00546         iMsgID = SendMsg(iMessageToSend);
00547     iLastMsg = MsgPlayedIndex;
00548 
00549     iGameData.At(aIndex) = iMySign;
00550     if ( IsGameFinnished(isDraw) )
00551         {
00552         if (isDraw)
00553             iDocument->UI()->HandleMessageFromModel(GameDraw, KErrNone, KNullDesC);
00554         else
00555             iDocument->UI()->HandleMessageFromModel(YouWon, KErrNone, KNullDesC);
00556         ResetGameData();
00557         SetGameState(EStateReadytoStart);
00558         return;
00559         }
00560     SetGameState(EStateWaitingForOppMove);
00561         }

void CIMOpenAPIExamplemodel::SendEndChallenge (   ) 
 

SendEndChallenge. Send to the opponent that user has ended the challenge. This means that user is now free to play with any other users

Definition at line 567 of file IMOpenAPIExampleModel.cpp.

References iLastMsg, iMessageToSend, iMsgID, and SendMsg().

00568         {
00569     iMessageToSend.Copy(KEndChallenge);
00570         iMsgID = SendMsg(iMessageToSend);
00571     iLastMsg = MsgEndChallenge;
00572         }

void CIMOpenAPIExamplemodel::SetOppID (  const TDesC16 &  aOppID  ) 
 

SetOppID. Set the opponent's user's id locally

Parameters:
aOppID Opponent's user id

Definition at line 667 of file IMOpenAPIExampleModel.cpp.

References iOppID.

00668         {
00669     TBuf<KMaxUserIDLength> oppID(aOppID);
00670     oppID.LowerCase();    
00671     delete iOppID;
00672     iOppID = NULL;
00673     iOppID = oppID.AllocL();
00674     iArrayOfIDs->Reset();
00675         iArrayOfIDs->AppendL(*iOppID);
00676         }

CIMOpenAPIExamplemodel * CIMOpenAPIExamplemodel::NewLC (  CIMOpenAPIExampleDocument *  aDocument  )  [static]
 

NewLC. Factory method for creating the class instance

Parameters:
aDocument Pointer to the application's document

Definition at line 75 of file IMOpenAPIExampleModel.cpp.

References ConstructL().

00076     {
00077     CIMOpenAPIExamplemodel* self = new (ELeave) CIMOpenAPIExamplemodel;
00078     CleanupStack::PushL(self);
00079     self->ConstructL(aDocument);
00080     return self;
00081     }

CIMOpenAPIExamplemodel * CIMOpenAPIExamplemodel::NewL (  CIMOpenAPIExampleDocument *  aDocument  )  [static]
 

NewL. Factory method for creating the class instance

Parameters:
aDocument Pointer to the application's document

Definition at line 88 of file IMOpenAPIExampleModel.cpp.

References NewLC().

00089     {
00090     CIMOpenAPIExamplemodel* self = NewLC(aDocument);
00091     CleanupStack::Pop(self);
00092     return self;
00093         }

TGameState CIMOpenAPIExamplemodel::GetGameState (   )  const
 

GetGameState. Get current game state from model.

Returns:
TGameState current game state

Definition at line 640 of file IMOpenAPIExampleModel.cpp.

References iSending, and iState.

00641         {
00642     if(iSending)
00643         return EStateSending;
00644         return iState;
00645         }

void CIMOpenAPIExamplemodel::SetGameState (  TGameState const   aState  ) 
 

SetGameState. Set current game state

Parameters:
aState current game state

Definition at line 652 of file IMOpenAPIExampleModel.cpp.

References iDocument, iState, ResetGameData(), CIMOpenAPIExampleAppUi::SetGameState(), SetOppID(), and CIMOpenAPIExampleDocument::UI().

00653         {
00654         iState = aState;
00655         iDocument->UI()->SetGameState(aState);
00656     if (iState == EStateReadytoStart)
00657         ResetGameData();
00658     else if (iState == EStateLoginIdle)
00659         SetOppID(KNullDesC);
00660         }

TGameSign CIMOpenAPIExamplemodel::GetOppSign (   )  const
 

GetOppSign. Get opponent's sign

Returns:
TGameSign opponent's sign

Definition at line 695 of file IMOpenAPIExampleModel.cpp.

References iOppSign.

00696     {
00697     return iOppSign;
00698     }

TBool CIMOpenAPIExamplemodel::IsIndexPositionEmpty (  TInt  aIndex  ) 
 

IsIndexPositionEmpty. Find whether a position on game's grid is empty

Parameters:
aIndex location of position in the grid
Returns:
TBool ETrue if position is empty

Definition at line 714 of file IMOpenAPIExampleModel.cpp.

References iGameData.

00715     {
00716     if ( iGameData.At(aIndex) == EEmptySign )
00717         return ETrue;
00718     return EFalse;
00719     }

void CIMOpenAPIExamplemodel::PrepareToExit (   ) 
 

PrepareToExit. Close connections and prepare to exit application

Definition at line 725 of file IMOpenAPIExampleModel.cpp.

References iState, and SendEndChallenge().

00726     {
00727     if ( (iState != EStateNotLogin) && (iState != EStateLoginIdle) )
00728         SendEndChallenge();
00729     }

void CIMOpenAPIExamplemodel::ConstructL (  CIMOpenAPIExampleDocument *  aDocument  )  [private]
 

ConstructL. 2nd phase constructor

Parameters:
aDocument Pointer to the application's document

Definition at line 35 of file IMOpenAPIExampleModel.cpp.

References iArrayOfIDs, iDocument, iIMClient, iIMConnection, iLastMsg, iMsgID, iNotSupported, iSending, iState, ResetGameData(), and SetOppID().

00036     {
00037     TRAPD(err,iIMConnection = CImConnection::NewL(KNullDesC));
00038     if (err!=KErrNone)
00039         {
00040         iNotSupported = ETrue;
00041         iState = EStateNotLogin;
00042         }
00043     else
00044         {
00045         iIMConnection->RegisterObserverL(this); // registering the connection
00046         iIMClient = iIMConnection->CreateImClientL();// creating the client
00047         iIMClient->RegisterObserverL(this); // registering the client
00048             
00049         // Check the IM status and set the application accordingly
00050         if (iIMConnection->ImConnectionStatus() == ELoggedIn)
00051                 iState = EStateLoginIdle;
00052         else
00053             
00054         iNotSupported=EFalse;        
00055         }
00056 
00057     iDocument = aDocument;
00058     ResetGameData(); // reset game data grid
00059         
00060 
00061         iArrayOfIDs = new (ELeave) CDesCArrayFlat(1);
00062    
00063         // msg sent ids are zero in the begining
00064         iMsgID = 0;
00065     iSending = EFalse;
00066     iLastMsg = MsgEndChallenge;
00067     SetOppID(KNullDesC);
00068         }

TBool CIMOpenAPIExamplemodel::CheckForSeries (  TGameSign  aValue,
TInt  aStartIndex,
TInt  aIndexIncrement
)  [private]
 

CheckForSeries. Check for a series of 3 consective values on the grid. The functions checks whether the three positions starting from an starting index and increased by given increament has the same values.

Parameters:
aValue The value which needs to be continuous aStartIndex Starting index on the grid aIndexIncrement Increament for the next position
Returns:
ETrue if series found

Definition at line 627 of file IMOpenAPIExampleModel.cpp.

References iGameData.

00628         {
00629         return ( (iGameData.At(aStartIndex) == aValue) && 
00630                                 (iGameData.At(aStartIndex+aIndexIncrement) == aValue) && 
00631                                 (iGameData.At(aStartIndex+aIndexIncrement+aIndexIncrement) == aValue) );
00632 
00633         }

TBool CIMOpenAPIExamplemodel::MatchOppID (  TDesC16 &  aOppID  )  [private]
 

MatchOppID. Match whether the given id is same as opponent's id, Each message goes through this check in a game.

Parameters:
aOppID id to be matched with opponent's id
Returns:
ETrue if matched

Definition at line 683 of file IMOpenAPIExampleModel.cpp.

References iOppID.

00684         {
00685         if ( iOppID->Compare(aOppID) == 0 )
00686                 return ETrue;
00687         return EFalse;
00688         }

TInt CIMOpenAPIExamplemodel::SendMsg (  TDesC16 &  aContent  )  [private]
 

SendMsg. Send given message

Parameters:
aContent The message needs to be send
Returns:
TInt Message id

Definition at line 444 of file IMOpenAPIExampleModel.cpp.

References iIMClient, and iSending.

00445         {
00446         iSending = ETrue;
00447     //SetGameState (EStateSending);
00448         return iIMClient->SendPToPMessageL(*iArrayOfIDs, aContent);
00449         }

void CIMOpenAPIExamplemodel::ResetGameData (   )  [private]
 

ResetGameData. Resets all game data, usually done before starting a game

Definition at line 704 of file IMOpenAPIExampleModel.cpp.

References iGameData.

00705     {
00706     for (TInt8 i=0; i<9; i++) // set all game matrix data to 'empty'
00707                 iGameData.At(i) = EEmptySign;
00708     }

TBool CIMOpenAPIExamplemodel::IsGameFinnished (  TBool &  aIsDraw  )  [private]
 

IsGameFinnished. Find whether the game is finnished.

Parameters:
aIsDraw ETrue if game is a draw
Returns:
ETrue if game is finnished

Definition at line 577 of file IMOpenAPIExampleModel.cpp.

References CheckForSeries(), and IsGameDraw().

00578         {
00579         // checking for sign ECrossSign (*) win in the matrix
00580         if ( CheckForSeries(ECrossSign,ECrossSign,EZeroSign) || CheckForSeries(ECrossSign,3,EZeroSign) || 
00581         CheckForSeries(ECrossSign,6,EZeroSign) || CheckForSeries(ECrossSign,ECrossSign,3) || 
00582         CheckForSeries(ECrossSign,EZeroSign,3) || CheckForSeries(ECrossSign,EEmptySign,3) ||
00583                 CheckForSeries(ECrossSign,ECrossSign,4) || CheckForSeries(ECrossSign,EEmptySign,EEmptySign) )
00584                 {
00585         aIsDraw = EFalse;
00586                 return ETrue;
00587                 }
00588 
00589         // checking for sign EZeroSign (o) win in the matrix
00590         else if ( CheckForSeries(EZeroSign,ECrossSign,EZeroSign) || CheckForSeries(EZeroSign,3,EZeroSign) || CheckForSeries(EZeroSign,6,EZeroSign) ||
00591                 CheckForSeries(EZeroSign,ECrossSign,3) || CheckForSeries(EZeroSign,EZeroSign,3) || CheckForSeries(EZeroSign,EEmptySign,3) ||
00592                 CheckForSeries(EZeroSign,ECrossSign,4) || CheckForSeries(EZeroSign,EEmptySign,EEmptySign) )
00593                 {
00594         aIsDraw = EFalse;
00595                 return ETrue;
00596                 }
00597 
00598         else if (IsGameDraw())
00599         {
00600         aIsDraw = ETrue;
00601         return ETrue;
00602         }
00603 
00604         return EFalse;
00605         }

TBool CIMOpenAPIExamplemodel::IsGameDraw (   )  [private]
 

IsGameDraw. Find whether the game has draw.

Returns:
ETrue if game is draw

Definition at line 612 of file IMOpenAPIExampleModel.cpp.

References iGameData.

00613     {
00614     for (TInt i=0;i<9;i++)
00615         {
00616         if (iGameData.At(i) == EEmptySign)
00617             return false;
00618         }
00619     return true;
00620     }


Member Data Documentation

CImConnection* CIMOpenAPIExamplemodel::iIMConnection [private]
 

iIMConnection. Pointer to CImConnection. Used for IM connection handling

Definition at line 311 of file IMOpenAPIExampleModel.h.

MImClient* CIMOpenAPIExamplemodel::iIMClient [private]
 

iIMClient. Pointer to MImClient. Used for IM communication

Definition at line 317 of file IMOpenAPIExampleModel.h.

CIMOpenAPIExampleDocument* CIMOpenAPIExamplemodel::iDocument [private]
 

iDocument. A local copy to the document pointer

Definition at line 323 of file IMOpenAPIExampleModel.h.

TFixedArray<TGameSign,9> CIMOpenAPIExamplemodel::iGameData [private]
 

iGameData. This is a 9 member array, represents game data during a game. It can have three posible values. 0, 1 and -1. 0 is equivalent to circle sign, 1 is to cross and -1 represents an empty location in game grid

Definition at line 331 of file IMOpenAPIExampleModel.h.

TGameState CIMOpenAPIExamplemodel::iState [private]
 

iState. This is Game state, all othe classes will get the game state from here. The TGameState is defined in IMOpenAPIExamplemsg.h

Definition at line 338 of file IMOpenAPIExampleModel.h.

TInt CIMOpenAPIExamplemodel::iMsgID [private]
 

iMsgID. This is last sent message id

Definition at line 344 of file IMOpenAPIExampleModel.h.

CDesC16Array* CIMOpenAPIExamplemodel::iArrayOfIDs [private]
 

iArrayOfIDs. Array of oponent ids, to match with returning messages

Definition at line 350 of file IMOpenAPIExampleModel.h.

HBufC* CIMOpenAPIExamplemodel::iOppID [private]
 

iOppID. Opponent ID

Definition at line 356 of file IMOpenAPIExampleModel.h.

TBool CIMOpenAPIExamplemodel::iSending [private]
 

iOppID. ETrue while some message is being send and response didnt yet come

Definition at line 362 of file IMOpenAPIExampleModel.h.

TMsg CIMOpenAPIExamplemodel::iLastMsg [private]
 

iOppID. Stores last message sent. TMsg is defined in IMOpenAPIExamplemsg.h

Definition at line 368 of file IMOpenAPIExampleModel.h.

TGameSign CIMOpenAPIExamplemodel::iMySign [private]
 

iMySign, iOppSign. User's own and opponent's signs. TGameSign is defined in IMOpenAPIExamplemsg.h

Definition at line 374 of file IMOpenAPIExampleModel.h.

TBuf<KMaxProtocolStrLength> CIMOpenAPIExamplemodel::iMessageToSend [private]
 

iMessageToSend Buffer containing message to be sent

Definition at line 380 of file IMOpenAPIExampleModel.h.

TInt64 CIMOpenAPIExamplemodel::iSeed [private]
 

iSeed This seed is used to randomize who's turn is first.

Definition at line 386 of file IMOpenAPIExampleModel.h.

TBool CIMOpenAPIExamplemodel::iNotSupported [private]
 

iNotSupported Whether the IM functionality is supported or not

Definition at line 392 of file IMOpenAPIExampleModel.h.


© Nokia 2006

Back to top