p2pengine.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef P2PENGINE_H
  18. #define P2PENGINE_H
  19. /*!
  20. * \file
  21. * \brief Implements game logic.
  22. */
  23. #include <QWidget>
  24. #include <QStringList>
  25. #include <QVector>
  26. class NfcConnection;
  27. //! Implements game logic.
  28. /*!
  29. This class creates the game logic engine. It is contains images used in a game,
  30. lists of local and the remote player's images, and the image selected for exchange.
  31. Game state is updated based on signals from the NfcConnection class.
  32. */
  33. class P2PEngine : public QObject
  34. {
  35. Q_OBJECT
  36. public:
  37. //! Class Constructor.
  38. /*!
  39. * \param parent Parent for the object.
  40. */
  41. explicit P2PEngine(QObject *parent = 0);
  42. //! Destructor.
  43. virtual ~P2PEngine();
  44. //! Returns a string that contains the name of the image file referenced from localImageIndexes.at(index).
  45. /*!
  46. \param index The index of the local image, 0 <= index < KMaxNumberOfImageIndexes.
  47. \return QString containing image filename.
  48. */
  49. Q_INVOKABLE QString getLocalImageFile(int index) const;
  50. //! Returns a string that contains the name of the image file referenced from remoteImageIndexes.at(index).
  51. /*!
  52. \param index The index of the remote image, 0 <= index < KMaxNumberOfImageIndexes.
  53. \return QString containing image filename.
  54. */
  55. Q_INVOKABLE QString getRemoteImageFile(int index) const;
  56. //! Set the local image index that will be used for next exchange between players localImageIndexes.at(exchangeImageIndex).
  57. /*!
  58. \param exchangeImageIndex The index of the local image, 0 <= index < KMaxNumberOfImageIndexes, that the player wants to exchange.
  59. */
  60. Q_INVOKABLE void setImageForExchange(int exchangeImageIndex);
  61. //! Returns a string containing local image indices converted to a string that can be transmitted via an NFC connection.
  62. /*!
  63. \return A string containing local image indices.
  64. */
  65. QString getLocalIndexString() const;
  66. //! Returns a string containing the index of the image to exchange converted to a string that can be transmitted via an NFC connection.
  67. /*!
  68. \return A string containing the index of the local image to exchange.
  69. */
  70. QString getExchangeIndexString() const;
  71. //! Checks if an image has been selected for exchange.
  72. /*!
  73. \return false if not selected, true otherwise.
  74. */
  75. bool isExchangeImageSet();
  76. //! Passes the string received via an NFC connection to the engine. The sting is parsed inside this function.
  77. /*!
  78. \param indexString A string containing a message received from the remote player. Contains either the remote index list or the remote image index for exchange.
  79. */
  80. void setRemoteIndexes(QString indexString);
  81. //! Resets the game engine state to initial condition. Called after the winning condition is met.
  82. Q_INVOKABLE void doReset();
  83. signals:
  84. //! Signal emmited when the list of remote images is updated.
  85. void showRemoteImages();
  86. //! Signal emmited when the winning condition is met.
  87. void showWinnerMessage();
  88. //! Signal emmited when image exchange is done, or a new game is started.
  89. void updateView();
  90. private:
  91. //! Initialize local image indices to random numbers.
  92. void initLocalImageIndexes();
  93. //! Returns a string that contains the name of the image file referenced from arrayOfImageNames.at(index).
  94. /*!
  95. \param index The index of the image, 0 <= index < KMaxNumberOfImageIndexes.
  96. \return QString containing image filename.
  97. */
  98. QString getImageFileNameByIndex(int index) const;
  99. //! Checks if winning condition are met.
  100. /*!
  101. \return true if this player has won the game, false otherwise.
  102. */
  103. bool isWinner();
  104. private:
  105. QStringList m_arrayOfImageNames; //! Array of names of image files
  106. QVector<int> m_localImageIndexes; //! Array of indices of local images
  107. QVector<int> m_remoteImageIndexes; //! Array of indices of remote images
  108. NfcConnection *m_nfcConnection; //! Reference to NfcConnection class
  109. int m_indexOfImageForExchange; //! Index of image for exchange
  110. int m_placeOfImageForExchange; //! Exchange image place on pane
  111. bool m_exchangeImageSet; //! Flag denoting whether an image has been selected for exchange.
  112. };
  113. #endif // P2PENGINE_H