backendapi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /** @file */
  2. //! Backend error
  3. enum EBackendError
  4. {
  5. EBERR_OK, // all OK
  6. EBERR_UNKNOWN, // unknown error
  7. EBERR_DISABLED, // backend is disabled
  8. EBERR_INVALID_STATE, // called request from state where it is not possible (ie. reading data before logon and such)
  9. EBERR_BUSY, // no request can be called - login/ auth in process
  10. EBERR_ALREADY_OFFLINE, // state is already active
  11. EBERR_ALREADY_ONLINE, // state is already active
  12. EBERR_ALREADY_REQUESTED, // state already requested once!
  13. EBERR_LOGIN_FAILED, // failed to logon
  14. EBERR_AUTH_FAILED, // failed to authenticate
  15. EBERR_LOGIN_SUCCESS, // logon successfull
  16. EBERR_AUTH_SUCCESS, // authenticate successfull
  17. EBERR_CONFIGURATION_GET, // configuration received
  18. EBERR_CEPROFILE_GET, // CE profile configuration received
  19. EBERR_CHARACTER_GET, // character data receieved
  20. EBERR_CHARACTER_UPDATE, // character update done
  21. };
  22. //! Backend request
  23. enum EBackendRequest
  24. {
  25. /* // game api
  26. EBREQ_GAME_Test, // environment test - dummy data read
  27. EBREQ_GAME_World, // static world configuration read
  28. EBREQ_GAME_CEProfile,
  29. EBREQ_GAME_CharacterGet, // character data read
  30. EBREQ_GAME_CharacterUpdate, // character data update
  31. // #if BACKENDAPI_DEV_CHARACTER
  32. EBREQ_GAME_DevCharacterGet, // dev character data read
  33. EBREQ_GAME_DevCharacterUpdate, // dev character data update
  34. // #endif
  35. EBREQ_GAME_Heartbeat,*/
  36. // user api request <> response
  37. EBREQ_USER_Login,
  38. EBREQ_USER_Auth,
  39. /* // lobby api request <> response
  40. EBREQ_LOBBY_RoomsRegister,
  41. EBREQ_LOBBY_RoomsJoin,
  42. EBREQ_LOBBY_RoomsAcceptPlayer,
  43. EBREQ_LOBBY_RoomsHeartBeat,
  44. EBREQ_LOBBY_RoomsUpdate,
  45. EBREQ_LOBBY_RoomsRemovePlayer,
  46. EBREQ_LOBBY_RoomsSearch,
  47. EBREQ_LOBBY_RoomsGetByIds,
  48. EBREQ_LOBBY_RoomsGetByHostIds,
  49. EBREQ_LOBBY_GetActiveScenarios,
  50. // storage api request <> response
  51. EBREQ_STORAGE_GetFileTempURL,
  52. EBREQ_STORAGE_GetFileTempURLS2S,
  53. EBREQ_STORAGE_GetFile,
  54. EBREQ_STORAGE_DeleteFile,
  55. EBREQ_STORAGE_GetFileS2S,
  56. EBREQ_STORAGE_DeleteFileS2S,
  57. EBREQ_STORAGE_PatchFileS2S,
  58. EBREQ_STORAGE_Upload,
  59. EBREQ_STORAGE_UploadS2S,
  60. EBREQ_STORAGE_UploadZip,
  61. EBREQ_STORAGE_UploadZipS2S,
  62. EBREQ_STORAGE_Limits,
  63. EBREQ_STORAGE_LimitsS2S,
  64. // feedback request <> response
  65. // #if ONLINE_SLACKAPI
  66. EBREQ_SLACKAPI_PostMessage,
  67. // #endif*/
  68. };
  69. //! Credential parameters
  70. enum EBackendCredentials
  71. {
  72. EBCRED_NAME,
  73. EBCRED_PWD,
  74. EBCRED_BASEURI,
  75. };
  76. // -------------------------------------------------------------------------
  77. // Callback interface for backend - must exist for the duration of request!
  78. class BackendCallback : Managed
  79. {
  80. /**
  81. \brief Called when data were recieved, you can ignore it when using callback to JsonStruct object with expand feature
  82. \param data Contain received data, may be JSON, plain text, XML or image
  83. \param size
  84. */
  85. void OnDataReceive( string data, int size )
  86. {
  87. Print("[BackendCallback] Data received, size=" + size);
  88. Print(data);
  89. }
  90. /**
  91. \brief Request finished with error result
  92. \param code Error code is type of EBackendError
  93. */
  94. void OnError( int code )
  95. {
  96. Print("[BackendCallback] OnError: " + GetBackendApi().GetErrorCode(code));
  97. }
  98. /**
  99. \brief Request finished with success result
  100. \param code Code is type of EBackendRequest
  101. */
  102. void OnSuccess( int code )
  103. {
  104. Print("[BackendCallback] OnSuccess()");
  105. }
  106. /**
  107. \brief Request not finished due to timeout
  108. */
  109. void OnTimeout()
  110. {
  111. Print("[BackendCallback] OnTimeout");
  112. }
  113. };
  114. // -------------------------------------------------------------------------
  115. // Backend API access
  116. class BackendApi
  117. {
  118. private void BackendApi() {}
  119. private void ~BackendApi() {}
  120. /**
  121. \brief Initiate backend - request processing
  122. */
  123. proto native bool Initiate();
  124. /**
  125. \brief Shutdown backend - request processing
  126. */
  127. proto native bool Shutdown();
  128. /**
  129. \brief Backend offline - authentication may be initiated
  130. */
  131. proto native bool IsDisconnected();
  132. /**
  133. \brief Backend fully working and initialized
  134. */
  135. proto native bool IsRuntime();
  136. /**
  137. \brief Backend is busy - authentication in process
  138. */
  139. proto native bool IsBusy();
  140. //! Error code to string
  141. string GetErrorCode( int code )
  142. {
  143. string result;
  144. if ( code == EBackendError.EBERR_OK )
  145. result = "OK";
  146. else if ( code == EBackendError.EBERR_UNKNOWN )
  147. result = "Offline";
  148. else if ( code == EBackendError.EBERR_DISABLED )
  149. result = "Communication Disabled";
  150. else if ( code == EBackendError.EBERR_INVALID_STATE )
  151. result = "Cannot be called from current state";
  152. else if ( code == EBackendError.EBERR_BUSY )
  153. result = "Busy processing requests";
  154. else if ( code == EBackendError.EBERR_ALREADY_OFFLINE )
  155. result = "Already disconnected";
  156. else if ( code == EBackendError.EBERR_ALREADY_ONLINE )
  157. result = "Already connected";
  158. else if ( code == EBackendError.EBERR_LOGIN_FAILED )
  159. result = "Failed to logon";
  160. else if ( code == EBackendError.EBERR_AUTH_FAILED )
  161. result = "Failed to Authenticate";
  162. else
  163. result = "*";
  164. return result;
  165. }
  166. /**
  167. \brief Called when initiate cannot be called
  168. */
  169. void OnCannotInitiate( int code )
  170. {
  171. Print("!!! [Backend] Cannot Initiate: "+ GetErrorCode(code));
  172. }
  173. /**
  174. \brief Called when shutdown cannot be proceeded
  175. */
  176. void OnCannotShutdown( int code )
  177. {
  178. Print("!!! [Backend] Cannot Shutdown: "+ GetErrorCode(code));
  179. }
  180. /**
  181. \brief Called when step was successfully proceeded
  182. */
  183. void OnSuccess( string step )
  184. {
  185. Print( "[Backend] Successfully Solicited: " + step );
  186. }
  187. /**
  188. \brief Called when step failed
  189. */
  190. void OnFail( string step )
  191. {
  192. Print( "[Backend] Failed to Proceed: " + step );
  193. }
  194. /**
  195. \brief Ask specific request with callback result
  196. \param request Is type of request, which is EBackendRequest
  197. \param cb Is script callback where you will recieve result/ error or even data when request finsihes
  198. \param data Is optional callback when request uses or response return Json data and you want to work with object
  199. */
  200. proto native void Request( int request, BackendCallback cb, JsonApiStruct dataObject );
  201. /**
  202. \brief Ask player request with callback result from controller (Lobby)
  203. \param request Is type of request, which is EBackendRequest
  204. \param cb Is script callback where you will recieve result/ error or even data when request finsihes
  205. \param data Is optional callback when request uses or response return Json data and you want to work with object
  206. \param iPlayerId Is Player Id used on player identity
  207. */
  208. proto native void PlayerRequest( int request, BackendCallback cb, JsonApiStruct dataObject, int iPlayerId );
  209. /**
  210. \brief Send feedback message and/ or script object with whatever data on it (additionally it is possible to handle callback as well)
  211. \param cb Is script callback where you will recieve result/ error or even data when request finsihes
  212. \param data Is optional callback when request uses or response return Json data and you want to work with object
  213. \param message Is custom
  214. */
  215. proto native void FeedbackMessage( BackendCallback cb, JsonApiStruct dataObject, string message );
  216. /**
  217. \brief Set credentials value per item
  218. \param item Is type of EBackendCredentials parameter you want to set
  219. \param str Is value itself
  220. */
  221. proto native void SetCredentialsItem( EBackendCredentials item, string str );
  222. /**
  223. \brief Get credentials value per item
  224. \param item Is type of EBackendCredentials parameter you want to read
  225. */
  226. proto native string GetCredentialsItem( EBackendCredentials item );
  227. /**
  228. \brief Invoke credentials update (authenticate with new name+password)
  229. */
  230. proto native void VerifyCredentials();
  231. };
  232. proto native BackendApi GetBackendApi();
  233. // -------------------------------------------------------------------------