p2pengine.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description: Implementation of application engine. It contains game logic and responds
  15. * to connection events.
  16. */
  17. #include <QTime>
  18. #include "nfcconnection.h"
  19. #include "p2pengine.h"
  20. //! Constant defines number of images in the game for one player.
  21. static const int KMaxNumberOfImageIndexes = 6;
  22. //! Constant for not selected image
  23. static const int KImageNotSelected = -1;
  24. //! Constants for file's names string
  25. static const QLatin1String shape1("0.png");
  26. static const QLatin1String shape2("1.png");
  27. static const QLatin1String shape3("2.png");
  28. static const QLatin1String shape4("3.png");
  29. static const QLatin1String shape5("4.png");
  30. static const QLatin1String shape6("5.png");
  31. P2PEngine::P2PEngine(QObject *parent) : QObject(parent),
  32. m_indexOfImageForExchange(KImageNotSelected),
  33. m_placeOfImageForExchange(KImageNotSelected),
  34. m_exchangeImageSet(false)
  35. {
  36. // Create NFC connection
  37. m_nfcConnection = new NfcConnection(0, this);
  38. // Load list of image filenames
  39. m_arrayOfImageNames << shape1 << shape2 << shape3 << shape4 << shape5 << shape6;
  40. // Initialize local image indices to random numbers.
  41. initLocalImageIndexes();
  42. }
  43. P2PEngine::~P2PEngine()
  44. {
  45. // Empty implementation
  46. }
  47. void P2PEngine::initLocalImageIndexes()
  48. {
  49. // Create seed for the random number generator.
  50. QTime time = QTime::currentTime();
  51. qsrand((uint)time.msec());
  52. for (int i = 0; i<KMaxNumberOfImageIndexes; i++)
  53. {
  54. // Get a random index number.
  55. int randomIndex = qrand()%KMaxNumberOfImageIndexes;
  56. m_localImageIndexes.append( randomIndex );
  57. }
  58. }
  59. QString P2PEngine::getLocalImageFile(int index) const
  60. {
  61. QString filename;
  62. if ( index < m_localImageIndexes.size() )
  63. {
  64. filename = getImageFileNameByIndex( m_localImageIndexes.at(index) );
  65. }
  66. return filename;
  67. }
  68. QString P2PEngine::getRemoteImageFile(int index) const
  69. {
  70. QString filename;
  71. if ( index < m_remoteImageIndexes.size() )
  72. {
  73. filename = getImageFileNameByIndex( m_remoteImageIndexes.at(index) );
  74. }
  75. return filename;
  76. }
  77. QString P2PEngine::getImageFileNameByIndex(int index) const
  78. {
  79. QString filename;
  80. if( ( index < KMaxNumberOfImageIndexes ) && ( index >= 0) )
  81. {
  82. filename = m_arrayOfImageNames.at(index).toLocal8Bit().constData();
  83. }
  84. return filename;
  85. }
  86. void P2PEngine::setRemoteIndexes(QString indexString)
  87. {
  88. // Exchange mode.
  89. if ( indexString.size() == 1 && m_exchangeImageSet )
  90. {
  91. // Do the exchange.
  92. int remoteExchangeIndex = indexString.toInt();
  93. m_localImageIndexes.replace(m_placeOfImageForExchange, remoteExchangeIndex);
  94. m_indexOfImageForExchange = KImageNotSelected;
  95. m_placeOfImageForExchange = KImageNotSelected;
  96. m_exchangeImageSet = false;
  97. emit updateView();
  98. // Check winner condition.
  99. if ( isWinner() )
  100. {
  101. // Show winner message.
  102. emit showWinnerMessage();
  103. }
  104. // Let NFC connection send new local image indices to the remote host.
  105. m_nfcConnection->sendText();
  106. }
  107. else if ( indexString.size() == KMaxNumberOfImageIndexes )
  108. {
  109. // Remote image indexes update mode.
  110. // Clear list of remote images.
  111. m_remoteImageIndexes.clear();
  112. // Append remote indices.
  113. for( int i = 0; i < KMaxNumberOfImageIndexes; i++ )
  114. {
  115. QString string = indexString.at(i);
  116. m_remoteImageIndexes.append( string.toInt() );
  117. }
  118. // Update remote images on the UI.
  119. emit showRemoteImages();
  120. }
  121. }
  122. void P2PEngine::setImageForExchange(int exchangeImageIndex)
  123. {
  124. // Set the image for exchange.
  125. m_indexOfImageForExchange = m_localImageIndexes.at(exchangeImageIndex);
  126. // Update flag.
  127. m_exchangeImageSet = true;
  128. // Set the image exchange index.
  129. m_placeOfImageForExchange = exchangeImageIndex;
  130. }
  131. QString P2PEngine::getLocalIndexString() const
  132. {
  133. QString indexString;
  134. for( int i = 0; i < m_localImageIndexes.size(); i++ )
  135. {
  136. indexString.append( QString::number( m_localImageIndexes.at(i)) );
  137. }
  138. return indexString;
  139. }
  140. QString P2PEngine::getExchangeIndexString() const
  141. {
  142. return QString::number( m_indexOfImageForExchange );
  143. }
  144. bool P2PEngine::isExchangeImageSet()
  145. {
  146. return m_exchangeImageSet;
  147. }
  148. bool P2PEngine::isWinner()
  149. {
  150. bool ret(true);
  151. for ( int i = 1; i<KMaxNumberOfImageIndexes && ret; i++ )
  152. {
  153. if( getLocalImageFile(0) != getLocalImageFile(i) )
  154. {
  155. ret = false;
  156. }
  157. }
  158. return ret;
  159. }
  160. void P2PEngine::doReset()
  161. {
  162. // Clear lists of remote and local images.
  163. m_remoteImageIndexes.clear();
  164. m_localImageIndexes.clear();
  165. // Initialize local image indices to random numbers.
  166. initLocalImageIndexes();
  167. // Initialize image exchange.
  168. m_indexOfImageForExchange = KImageNotSelected;
  169. m_placeOfImageForExchange = KImageNotSelected;
  170. m_exchangeImageSet = false;
  171. emit updateView();
  172. }