Guest User

ApisListController

a guest
Jan 2nd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 147.62 KB | None | 0 0
  1. <?php
  2. /**
  3. * @Controller Name: Apis
  4. * @Created Date : 14_06_2016
  5. * @ all Params Usesd Pacel Case
  6. * @ Error : True/False
  7. */
  8.  
  9. Class ApisListController extends AppController {
  10.  
  11. public $name = 'ApisList';
  12. public $uses = array('User','AppSetting','UserDevice','Room','RoundScore','RoomSlot','ChatInfo','FinalScore','UserPlayGame','RequestRematch','GamePlayData');
  13. public $components = array('RequestHandler','Email','AWSSES');
  14. public $helpers = array('Html');
  15.  
  16.  
  17. public function beforeFilter() {
  18. parent::beforeFilter();
  19. $this->RequestHandler->ext = 'json';
  20. }
  21.  
  22.  
  23. public function test(){
  24. $settingData = $this->AppSetting->find('first',array('fields'=>array('AppSetting.time_setting','AppSetting.Build','AppSetting.UrlIOS','AppSetting.UrlAndroid','AppSetting.UrlWin','AppSetting.UrlOSX','AppSetting.AutoKickTimeout','AppSetting.showads','AppSetting.show_effects')));
  25.  
  26. $this->set(array('response' => array('errorflag'=>0,'user_id'=>123),'_serialize' => array('response')));
  27. }
  28.  
  29. /**
  30. * @method : getSetting()
  31. * @purpose : get app Setting Option Like time for invitation acceptance etc.
  32. * @create_date : 14_06_2016
  33. */
  34.  
  35. public function getSetting(){
  36. $error = '';
  37.  
  38. if(empty($_REQUEST['Build'])){
  39. $error = "Missing Build parameter";
  40. }
  41.  
  42. if(isset($error) && !empty($error )){
  43. $message = array('Error'=>true,'Message'=>$error);
  44. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  45. }else{
  46. $settingData = $this->AppSetting->find('first',array('fields'=>array('AppSetting.time_setting','AppSetting.Build','AppSetting.UrlIOS','AppSetting.UrlAndroid','AppSetting.UrlWin','AppSetting.UrlOSX','AppSetting.AutoKickTimeout','AppSetting.showads','AppSetting.show_effects')));
  47. $time = $settingData['AppSetting']['time_setting'];
  48. $build = $settingData['AppSetting']['Build'];
  49. $UrlIOS = $settingData['AppSetting']['UrlIOS'];
  50. $UrlAndroid = $settingData['AppSetting']['UrlAndroid'];
  51. $UrlWin = $settingData['AppSetting']['UrlWin'];
  52. $UrlOSX = $settingData['AppSetting']['UrlOSX'];
  53. $autokicktime = $settingData['AppSetting']['AutoKickTimeout'];
  54. $showads = $settingData['AppSetting']['showads'] ? $settingData['AppSetting']['showads'] : "0";
  55. $show_effects = $settingData['AppSetting']['show_effects'] ? $settingData['AppSetting']['show_effects'] : "0";
  56. // echo "<pre>"; print_r($settingData); die;
  57.  
  58. $this->set(array('Response' => array('Error'=>false,'Time'=>$time,'Build' => $build, 'UrlIOS' =>$UrlIOS, 'UrlAndroid' => $UrlAndroid,'UrlWin' => $UrlWin, 'UrlOSX' => $UrlOSX,'AutoKickTimeout' => $autokicktime,'ShowAds' => $showads,'ShowEffects' => $show_effects),'_serialize'=>array('Response')));
  59. }
  60. }
  61.  
  62.  
  63. /**
  64. * @method : checkSingIn
  65. * @purpose : check Current Device Staus
  66. * @create_date : 14_06_2016
  67. * #Params : IsAvailable: NotRegistered(if device Id Still not Registred)
  68. * @room_status: 0- still game not started, 1: game running, 2: game finished
  69. */
  70.  
  71. public function checkSingIn(){
  72. $error = '';
  73. if(empty($_REQUEST['DeviceId'])){
  74. $error = "Missing DeviceId parameter";
  75. }
  76.  
  77. if(isset($error) && !empty($error )){
  78. $message = array('Error'=>true,'Message'=>$error);
  79. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  80. } else{
  81. $DeviceId = trim($_REQUEST['DeviceId']);
  82.  
  83. $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
  84.  
  85. if((empty($deviceData)) || ($DeviceId == 'n/a')){
  86. $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'NotRegistered'),'_serialize' => array('Response')));
  87. } else{
  88.  
  89. $devicepriId = $deviceData['UserDevice']['id'];
  90.  
  91. $getUser = $this->User->find('first',array('conditions'=>array('User.id'=>$deviceData['UserDevice']['UserId']),'fields'=>array('User.id','User.online_status','User.visible','User.current_device_id')));
  92.  
  93. if(!empty($getUser['User']['current_device_id']))
  94. $currentdeviceid = $getUser['User']['current_device_id'];
  95. else
  96. $currentdeviceid = 0;
  97.  
  98. if( ($getUser['User']['online_status']==1 ) && ($currentdeviceid == $devicepriId ) ){
  99.  
  100. // Checke User Alredy in Game or Not
  101. $roomId = '';
  102. $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$getUser['User']['id'],'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus','RoomSlot.SlotId'),'recursive'=>-1));
  103.  
  104. //chkuserinvited
  105.  
  106. if(isset($getSlotData) && !empty($getSlotData)){
  107. $getroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status !=' =>2),'fields'=>array('Room.room_status','Room.id','Room.OngoingStatus','Room.join_member_count')));
  108.  
  109. if(!empty($getroomStatus) && $getroomStatus['Room']['OngoingStatus'] == 1 && $getroomStatus['Room']['join_member_count'] > 1){
  110. $roomId = $getroomStatus['Room']['id'];
  111. }elseif((!empty($getroomStatus)) && ($getroomStatus['Room']['OngoingStatus'] == 1) && ($getroomStatus['Room']['join_member_count'] == 1)){
  112.  
  113. $roomId = $getroomStatus['Room']['id'];
  114. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  115. $this->Room->delete($roomId);
  116. $roomId = '';
  117.  
  118. }elseif(!empty($getroomStatus) && $getroomStatus['Room']['OngoingStatus'] != 1 && $getroomStatus['Room']['join_member_count'] >= 1){
  119.  
  120. $roomId = $getroomStatus['Room']['id'];
  121. //create empty public room slots
  122.  
  123. $getSlot = $this->RoomSlot->find('all',array('conditions' => array('RoomSlot.RoomId' => $roomId),'order'=>array('RoomSlot.SlotId ASC'),'recursive' => -1));
  124.  
  125. $lastslotidkey = end($getSlot);
  126. $lastslotkey = key($getSlot);
  127. $lastslotid = $getSlot[$lastslotkey]['RoomSlot']['SlotId'];
  128.  
  129. switch ($lastslotid) {
  130. case 0:
  131. for($i=1;$i<=3;$i++){
  132. $datetime = strtotime(date('Y-m-d H:i:s'));
  133. $created_date = date('Y-m-d H:i:s');
  134.  
  135. $slotCreat['RoomSlot']['RoomId'] = $roomId;
  136. $slotCreat['RoomSlot']['UserId'] = 0;
  137. $slotCreat['RoomSlot']['SlotId'] = $i;
  138. $slotCreat['RoomSlot']['slot_type'] = 'public';
  139. $slotCreat['RoomSlot']['SlotStatus'] = 0;
  140. $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
  141. $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
  142. $this->RoomSlot->saveall($slotCreat);
  143.  
  144.  
  145. }
  146. break;
  147. case 1:
  148. for($i=2;$i<=3;$i++){
  149. $datetime = strtotime(date('Y-m-d H:i:s'));
  150. $created_date = date('Y-m-d H:i:s') ;
  151.  
  152. $slotCreat['RoomSlot']['RoomId'] = $roomId;
  153. $slotCreat['RoomSlot']['UserId'] = 0;
  154. $slotCreat['RoomSlot']['SlotId'] = $i;
  155. $slotCreat['RoomSlot']['slot_type'] = 'public';
  156. $slotCreat['RoomSlot']['SlotStatus'] = 0;
  157. $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
  158. $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
  159. $this->RoomSlot->saveall($slotCreat);
  160.  
  161.  
  162. }
  163. break;
  164. case 2:
  165. $i = 3;
  166. $datetime = strtotime(date('Y-m-d H:i:s'));
  167. $created_date = date('Y-m-d H:i:s');
  168.  
  169. $slotCreat['RoomSlot']['RoomId'] = $roomId;
  170. $slotCreat['RoomSlot']['UserId'] = 0;
  171. $slotCreat['RoomSlot']['SlotId'] = $i;
  172. $slotCreat['RoomSlot']['slot_type'] = 'public';
  173. $slotCreat['RoomSlot']['SlotStatus'] = 0;
  174. $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
  175. $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
  176. $this->RoomSlot->saveall($slotCreat);
  177.  
  178. break;
  179. default:
  180. $roomId = $getroomStatus['Room']['id'];
  181. }
  182.  
  183. }else{
  184. //check if user is not sitting in a complete game
  185.  
  186. $getcompleteroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status' =>2),'fields'=>array('Room.room_status','Room.id','Room.OngoingStatus','Room.join_member_count')));
  187.  
  188. if(!empty($getcompleteroomStatus)){
  189. $joinemembercount = $getcompleteroomStatus['Room']['join_member_count'];
  190. $this->RoomSlot->updateAll(array('RoomSlot.UserId' => 0,'RoomSlot.OldSlotUser' => $getUser['User']['id'],'RoomSlot.SlotStatus' => 0),array('RoomSlot.RoomId' => $getSlotData['RoomSlot']['RoomId'],'RoomSlot.SlotId' => $getSlotData['RoomSlot']['SlotId']));
  191.  
  192. $newjoinmemebercount = $joinemembercount - 1;
  193.  
  194. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmemebercount),array('Room.id' => $getSlotData['RoomSlot']['RoomId']));
  195.  
  196. $roomId = '';
  197. }
  198. }
  199. }
  200. // END
  201.  
  202. $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'True','UserStatus'=>$getUser['User']['visible'],'UserId'=>$getUser['User']['id'],'RoomId'=>$roomId),'_serialize' => array('Response')));
  203.  
  204. } else if(($getUser['User']['online_status']==1 ) && ($currentdeviceid != $devicepriId )){
  205. $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'AlreadySignin','UserStatus'=>$getUser['User']['visible']),'_serialize' => array('Response')));
  206. } else{
  207.  
  208. // Check User Alredy in Game or Not
  209.  
  210. $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$getUser['User']['id'],'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
  211.  
  212. $roomId = '';
  213. if(!empty($getSlotData)){
  214. $getroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status !=' =>2),'fields'=>array('Room.room_status','Room.id','Room.join_member_count'),'recursive'=>-1));
  215. if(!empty($getroomStatus) && ('Room.OngoingStatus' == 1) && $getroomStatus['Room']['join_member_count'] > 1){
  216. $roomId = $getroomStatus['Room']['id'];
  217.  
  218. }else{
  219.  
  220. $getcompleteroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status' =>2),'fields'=>array('Room.room_status','Room.id','Room.OngoingStatus','Room.join_member_count')));
  221.  
  222. //check if user is not sitting in a complete game
  223. if(!empty($getcompleteroomStatus)){
  224. $joinemembercount = $getcompleteroomStatus['Room']['join_member_count'];
  225. $this->RoomSlot->updateAll(array('RoomSlot.UserId' => 0,'RoomSlot.OldSlotUser' => $getUser['User']['id'],'RoomSlot.SlotStatus' => 0),array('RoomSlot.RoomId' => $getSlotData['RoomSlot']['RoomId'],'RoomSlot.SlotId' => $getSlotData['RoomSlot']['SlotId']));
  226.  
  227. $newjoinmemebercount = $joinemembercount - 1;
  228.  
  229. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmemebercount),array('Room.id' => $getSlotData['RoomSlot']['RoomId']));
  230.  
  231. $roomId = '';
  232. }
  233. }
  234. }
  235. // END Above Code
  236.  
  237. $userId = $getUser['User']['id'];
  238. $DeviceId = trim($_REQUEST['DeviceId']);
  239.  
  240. $lastseen = strtotime(date('Y-m-d H:i:s'));
  241.  
  242. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>"'$devicepriId'",'User.last_seen'=>$lastseen), array('User.id' => $userId));
  243. $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'True','UserStatus'=>$getUser['User']['visible'],'UserId'=>$getUser['User']['id'],'RoomId'=>$roomId),'_serialize' => array('Response')));
  244. }
  245. }
  246. }
  247. }
  248.  
  249.  
  250.  
  251. /**
  252. * @method : signIn
  253. * @purpose : Login with username/Nickname/Email, Password
  254. * @creadted_date : 15_06_2016
  255. * @ Status: 0 - User diasbled by Admin, 2 - NotMatched, 3 :Already Online , 1: device not registred for any users,4: Device is Active for other User
  256. */
  257.  
  258. public function signIn(){
  259. $error = array();
  260. if(empty($this->request->data['Username'])){
  261. $error[] = "Missing Username parameter";
  262. }
  263. if(empty($_REQUEST['Password'])){
  264. $error[] = "Missing Password parameter";
  265. }
  266. if(empty($_REQUEST['DeviceId'])){
  267. $error[] = "Missing DeviceId parameter";
  268. }
  269. if(!empty($error )){
  270. $erroMsg = implode(',', $error);
  271. $message = array('Error'=>true,'Message'=>$erroMsg);
  272. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  273. } else{
  274. $DeviceId = trim($_REQUEST['DeviceId']);
  275.  
  276. if($DeviceId == 'n/a'){
  277. $DeviceId = uniqid();
  278. }
  279.  
  280. $nick = trim($this->request->data['Username']);
  281. $password = md5($this->request->data['Password']);
  282. $UserData = $this->User->find('first',array('fields'=>array('id','nick','firstname','lastname','email','visible','online_status','current_device_id'),'conditions'=>array('User.username'=>$nick,'User.password'=>$password)));
  283. $roomId = '';
  284.  
  285. if(!empty($UserData)){
  286. if($UserData['User']['visible']==1){
  287.  
  288. // Check Device and User Online Status
  289. $userId = $UserData['User']['id'];
  290. $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
  291.  
  292. /* if user already Login With Other Device */
  293. if($UserData['User']['online_status']==1){
  294.  
  295. // Already Played
  296. $message = array('Error'=>false,'Status'=>3,'UserId'=>$userId,'Message'=>'User already Played');
  297. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  298.  
  299. } else{
  300.  
  301. // Check Current Device
  302. $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
  303.  
  304. if(empty($deviceData)){
  305.  
  306. $saveDevData['UserDevice']['UserId'] = $userId;
  307. $saveDevData['UserDevice']['DeviceId'] = $DeviceId;
  308. $saveDevData['UserDevice']['PlayStatus'] = 1;
  309. $this->UserDevice->save($saveDevData);
  310. $newDeviceId = $this->UserDevice->getInsertID();
  311. $lastseen = strtotime(date('Y-m-d H:i:s'));
  312. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$newDeviceId,'User.last_seen'=>$lastseen), array('User.id' => $userId));
  313.  
  314.  
  315. // Checke User Alredy in Game or Not
  316.  
  317. $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$userId,'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
  318.  
  319. if(!empty($getSlotData)){
  320. $getroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status !=' =>2),'fields'=>array('Room.room_status','Room.id'),'recursive'=>-1));
  321. if(!empty($getroomStatus)){
  322. $roomId = $getroomStatus['Room']['id'];
  323. }
  324.  
  325. }
  326. // END Above Code
  327.  
  328. $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success', 'DeviceId' => $DeviceId);
  329. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  330. } else{
  331.  
  332. if(($UserData['User']['current_device_id']==$deviceData['UserDevice']['id'] ) || ($UserData['User']['id']==$deviceData['UserDevice']['UserId'])){
  333.  
  334. // Checke User Alredy in Game or Not
  335.  
  336. $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$UserData['User']['id'],'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
  337.  
  338. if(!empty($getSlotData)){
  339. $getroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status !=' =>2),'fields'=>array('Room.room_status','Room.id'),'recursive'=>-1));
  340.  
  341. if(!empty($getroomStatus)){
  342. $roomId = $getroomStatus['Room']['id'];
  343. }
  344.  
  345. }
  346.  
  347. // END Above Code
  348.  
  349. $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success','DeviceId' => $DeviceId);
  350. $devId = $deviceData['UserDevice']['id'];
  351. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$devId), array('User.id' => $userId));
  352. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  353. }else if($deviceData['UserDevice']['UserId'] !=$UserData['User']['id']){
  354.  
  355. $devDbId = $deviceData['UserDevice']['id'];
  356. $userId = $UserData['User']['id'];
  357. $lastseen = strtotime(date('Y-m-d H:i:s'));
  358.  
  359. $this->UserDevice->updateAll(array('UserDevice.UserId' =>$userId), array('UserDevice.id' => $devDbId));
  360.  
  361. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$devDbId,'User.last_seen'=>$lastseen), array('User.id' => $userId));
  362.  
  363. // Checke User Alredy in Game or Not
  364.  
  365. $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$userId,'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
  366.  
  367. if(!empty($getSlotData)){
  368. $getroomStatus = $this->Room->find('first',array('conditions'=>array('Room.id'=>$getSlotData['RoomSlot']['RoomId'],'Room.room_status !=' =>2),'fields'=>array('Room.room_status','Room.id'),'recursive'=>-1));
  369. if(!empty($getroomStatus)){
  370. $roomId = $getroomStatus['Room']['id'];
  371. }
  372.  
  373. }
  374.  
  375. // END Above Code
  376.  
  377. $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success2', 'DeviceId' => $DeviceId);
  378.  
  379. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  380.  
  381. } else{
  382. $message = array('Error'=>false,'Status'=>4,'UserId'=>$userId,'Message'=>'Device Regitered for other user');
  383. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  384. }
  385. }
  386. }
  387.  
  388. } else{
  389. $message = array('Error'=>false,'Status'=>0,'Message'=>'User Inactive');
  390. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  391. }
  392. }else{
  393. $message = array('Error'=>false,'Status'=>2,'Message'=>'Username OR password Wrong');
  394. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  395. }
  396. }
  397.  
  398. }
  399.  
  400.  
  401.  
  402. /**
  403. * @method : verifyEmail
  404. * @purpose : Verify OTP but verification from App Developer
  405. * @created_date: 16_06_2016
  406. */
  407.  
  408. public function verifyEmail(){
  409.  
  410. $error = array();
  411. if(empty($this->request->data['Email'])){
  412. $error[] = "Missing Email parameter";
  413. }
  414. if(!empty($error )){
  415. $message = array('Error'=>true,'Message'=>$error);
  416. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  417. } else{
  418. $checkemail = $this->checkEmail(trim($this->request->data['Email']));
  419.  
  420. if($checkemail){
  421. $message = array('Error'=>true,'OTP'=>'','Message'=>'This email already register');
  422. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  423. } else{
  424.  
  425. $token = mt_rand(10000, 99999);
  426. $aMsg = "Your access token is: ".$token;
  427. $this->Session->write('Person.eyeColor', $aMsg);
  428.  
  429. $this->AWSSES->to = trim($this->request->data['Email']);
  430.  
  431. if($this->AWSSES->_aws_ses('sendmail',$aMsg)){
  432. $arr = array('Response' => array('Error' => false, 'OTP' => $token, 'Status' => 1, 'Message' => 'OTP Number'));
  433.  
  434. echo json_encode($arr);die;
  435. }else{
  436. $message = array('Response' =>array('Error'=>true,'OTP'=>2,'Message'=>'Mail Not Send'));
  437.  
  438. echo json_encode($message);die;
  439. }
  440. }
  441. }
  442. }
  443.  
  444.  
  445. /**
  446. * @method : signUp
  447. * @purpose : Create New Account
  448. * @creadted_date : 16_06_2016
  449. * Email, Username, Password, DeviceId
  450. */
  451.  
  452. public function signUp(){
  453.  
  454. $error = array();
  455. if(empty($this->request->data['Username'])){
  456. $error[] = "Missing Username parameter";
  457. }
  458. if(empty($this->request->data['Email'])){
  459. $error[] = "Missing Email parameter";
  460. }
  461. if(empty($this->request->data['Password'])){
  462. $error[] = "Missing Password parameter";
  463. }
  464. if(empty($this->request->data['DeviceId'])){
  465. $error[] = "Missing DeviceId parameter";
  466. }
  467. if(!empty($error )){
  468. $erroMsg = implode(',', $error);
  469. $message = array('Error'=>true,'Message'=>$erroMsg);
  470. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  471. } else{
  472. // Check Username checkUserName
  473. $cheUname = $this->checkUserName(trim($this->request->data['Username']));
  474. $checkemail = $this->checkEmail(trim($this->request->data['Email']));
  475.  
  476. if($cheUname){
  477. $message = array('Error'=>false,'Status'=>2,'Message'=>'This username already register');
  478. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  479. } else if($checkemail){
  480. $message = array('Error'=>false,'Status'=>3,'Message'=>'This email already register');
  481. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  482. } else{
  483.  
  484. $userData['User']['username'] = $this->request->data['Username'];
  485. $userData['User']['nick'] = $this->request->data['Username'];
  486. $userData['User']['firstname'] = $this->request->data['Username'];
  487. $userData['User']['email'] = $this->request->data['Email'];
  488. $userData['User']['password'] = md5(trim($this->request->data['Password']));
  489. $this->User->save($userData);
  490. $userID = $this->User->getInsertID();
  491. // Save Device Id
  492. $deviceData['UserDevice']['DeviceId'] = $this->request->data['DeviceId'];
  493. $deviceData['UserDevice']['UserId'] = $userID;
  494. $this->UserDevice->save($deviceData);
  495. // Set Device Id */
  496.  
  497. $newDeviceId = $this->UserDevice->getInsertID();
  498. $this->User->updateAll(array('User.current_device_id'=>$newDeviceId), array('User.id' => $userID));
  499.  
  500. $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$userID),'_serialize' => array('Response')));
  501. }
  502. }
  503. }
  504.  
  505.  
  506. /**
  507. * @purpose : tranfer old account, here login will work according to nick name if login details corrent then set nick name as usename
  508. * @ Params : Username, Password, DeviceId
  509. * @created_date : 16_06_2016
  510. * @ Status : 0- account already transfered, 1: tranasfer success, 2: Username or Password wrong
  511. */
  512.  
  513. public function transferAccount(){
  514. $error = array();
  515. if(empty($_REQUEST['Username'])){
  516. $error[] = "Missing Username parameter";
  517. }
  518. if(empty($_REQUEST['Password'])){
  519. $error[] = "Missing Password parameter";
  520. }
  521. if(empty($_REQUEST['DeviceId'])){
  522. $error[] = "Missing DeviceId parameter";
  523. }
  524. if(!empty($error )){
  525. $erroMsg = implode(',', $error);
  526. $message = array('Error'=>true,'Message'=>$erroMsg);
  527. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  528. } else{
  529. $DeviceId = trim($_REQUEST['DeviceId']);
  530. $nick = trim($_REQUEST['Username']);
  531. $password = md5($_REQUEST['Password']);
  532. $UserData = $this->User->find('first',array('fields'=>array('id','nick','username','firstname','lastname','email','visible','online_status','current_device_id','played_games','victories','losses'),'conditions'=>array('User.nick'=>$nick,'User.password'=>$password)));
  533.  
  534. if(!empty($UserData)){
  535. $played_games = $UserData['User']['played_games'];
  536. $victories = $UserData['User']['victories'];
  537. $losses = $UserData['User']['losses'];
  538. $userid = $UserData['User']['id'];
  539. if($played_games == 0){
  540. $winpercentage = 0;
  541. }else{
  542. $winpercentage = ($victories/$played_games) * 100 ;
  543. $winpercentage = round($winpercentage);
  544. }
  545. if(isset($UserData['User']['username']) && !empty($UserData['User']['username'])){
  546. $this->set(array('Response' => array('Error'=>false,'Status'=>0,'UserId'=>$UserData['User']['id'],'Message'=>'This account already trasfered'),'_serialize' => array('Response')));
  547. } else{
  548.  
  549. // Assign Device
  550. $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
  551. $deviceData['UserDevice']['UserId'] = $UserData['User']['id'];
  552. $this->UserDevice->save($deviceData);
  553. $newDeviceId = $this->UserDevice->getInsertID();
  554. $username = $UserData['User']['nick'];
  555. $this->User->updateAll(array('User.online_status' =>1,'User.visible' =>1,'User.current_device_id'=>$newDeviceId,'User.username'=>'"'.$username.'"'), array('User.id' => $UserData['User']['id']));
  556.  
  557. $userplaydata['UserPlayGame']['total_game'] = $played_games;
  558. $userplaydata['UserPlayGame']['win_game'] = $victories;
  559. $userplaydata['UserPlayGame']['lost_game'] = $losses;
  560. $userplaydata['UserPlayGame']['win_percent'] = $winpercentage;
  561. $userplaydata['UserPlayGame']['user_id'] = $userid;
  562.  
  563. $this->UserPlayGame->save($userplaydata);
  564.  
  565. $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$UserData['User']['id'],'Message'=>'Account transfered Success'),'_serialize' => array('Response')));
  566. }
  567.  
  568. } else{
  569. $message = array('Error'=>false,'Status'=>2,'Message'=>'Username OR password Wrong');
  570. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  571. }
  572. }
  573. }
  574.  
  575.  
  576. /**
  577. * @method : FbRegister
  578. * @ created_date : 14_06_2016
  579. * @ Status : 0- user laready online, 1: Registred Success, 2: email alredy exist, 3: fbid alredy registred 4:username already exists
  580. */
  581.  
  582. public function fbRegister(){
  583. $error = array();
  584. if(empty($this->request->data['FbId'])){
  585. $error[] = "Missing FbId parameter";
  586. }
  587.  
  588. if(empty($this->request->data['Email'])){
  589. $email = "";
  590. }else{
  591. $email = trim($this->request->data['Email']);
  592. }
  593.  
  594. if(empty($this->request->data['Name'])){
  595. $error[] = "Missing Name parameter";
  596. }
  597.  
  598. if(empty($this->request->data['DeviceId'])){
  599. $error[] = "Missing DeviceId parameter";
  600. }
  601. if(empty($this->request->data['Username'])){
  602. $error[] = "Missing Username parameter";
  603. }
  604. if(!empty($error )){
  605. $erroMsg = implode(',', $error);
  606. $message = array('Error'=>true,'Message'=>$erroMsg);
  607. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  608. } else{
  609.  
  610. $fbemail = $email;
  611.  
  612. $fbusername = trim($this->request->data['Username']);
  613. $fbId = trim($this->request->data['FbId']);
  614. $socialData = $this->checkfbId($fbId);
  615.  
  616. if(empty($socialData)){
  617. // Check Email Id here
  618.  
  619. $chefbemilData = $this->checkFbEmail($fbemail);
  620.  
  621. if(empty($fbemail)) {
  622. $chefbemilData = '';
  623. }
  624.  
  625. $checkuserfbdata = $this->checkFbUsername($fbusername);
  626.  
  627. if(empty($fbusername)){
  628. $checkuserfbdata = "";
  629. }
  630.  
  631. //check if Username or Email Already Exists
  632. if(!empty($chefbemilData)){
  633.  
  634. $this->set(array('Response' => array('Error'=>false,'Status'=>2,'UserId'=>$chefbemilData['User']['id'],'Message'=>'Email already exists'),'_serialize' => array('Response')));
  635. }
  636. elseif(!empty($checkuserfbdata)){
  637. $this->set(array('Response' => array('Error'=>false,'Status'=>4,'UserId'=>$checkuserfbdata['User']['id'],'Message'=>'Username already exists'),'_serialize' => array('Response')));
  638. }
  639. else{
  640. //Fully inseret Data
  641.  
  642. $saveUser['User']['fbid'] = trim($this->request->data['FbId']);
  643. $saveUser['User']['email'] = $email;
  644. $saveUser['User']['firstname'] = trim($this->request->data['Name']);
  645. $saveUser['User']['username'] = trim($this->request->data['Username']);
  646.  
  647. $this->User->save($saveUser);
  648.  
  649. $userId = $this->User->getInsertID();
  650.  
  651. $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
  652. $deviceData['UserDevice']['UserId'] = $userId;
  653. $this->UserDevice->save($deviceData);
  654. $DataDeviceId = $this->UserDevice->getInsertID();
  655.  
  656. $lastseen = strtotime(date('Y-m-d H:i:s'));
  657. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$DataDeviceId,'User.last_seen'=>$lastseen), array('User.id' =>$userId));
  658. $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$userId,'Message'=>'Success'),'_serialize' => array('Response')));
  659. }
  660. } else{
  661.  
  662. // Updation
  663. if($socialData['User']['online_status']==1){
  664. $this->set(array('Response' => array('Error'=>false,'Status'=>0,'UserId'=>$socialData['User']['id'],'Message'=>'User already online'),'_serialize' => array('Response')));
  665. } else{
  666. $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
  667. $deviceData['UserDevice']['UserId'] = $socialData['User']['id'];
  668. $this->UserDevice->save($deviceData);
  669. $newDeviceId = $this->UserDevice->getInsertID();
  670. $fname = $this->request->data['Name'];
  671. $lastseen = strtotime(date('Y-m-d H:i:s'));
  672. $this->User->updateAll(array('User.fbid' =>$fbId,'User.online_status' =>1,'User.current_device_id'=>$newDeviceId,'User.firstname'=>'"'.$fname.'"','User.last_seen'=>$lastseen), array('User.id' => $socialData['User']['id']));
  673.  
  674. $this->set(array('Response' => array('Error'=>false,'Status'=>3,'UserId'=>$socialData['User']['id'],'Message'=>'FBid already registred'),'_serialize' => array('Response')));
  675.  
  676. }
  677. }
  678. }
  679. }
  680.  
  681.  
  682. /**
  683. * @method : updateProfile
  684. * @purpose : Update Login User Data like: Name, Bio etc
  685. * @params : Picture, Name, Bio,UserId
  686. * created_date: 17_06_2016
  687. * updated_date: 21_06_2016
  688. */
  689.  
  690. public function updateProfile(){
  691.  
  692. $error = array();
  693. if(empty($this->request->data['UserId'])){
  694. $error[] = "Missing UserId parameter";
  695. }
  696. if(empty($this->request->data['Name'])){
  697. $error[] = "Missing Name parameter";
  698. }
  699.  
  700. if(empty($this->request->data['Bio'])){
  701. $error[] = "Missing Bio parameter";
  702. }
  703. if($this->request->data['PictureIndex']==''){
  704. $error[] = "Missing PictureIndex parameter";
  705. }
  706.  
  707. if(!empty($error )){
  708. $erroMsg = implode(',', $error);
  709. $message = array('Error'=>true,'Message'=>$erroMsg);
  710. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  711. } else{
  712. $filename ='';
  713. $firstname = trim($this->request->data['Name']);
  714. $info = trim($this->request->data['Bio']);
  715. $UserId = trim($this->request->data['UserId']);
  716. $PictureIndex = trim($this->request->data['PictureIndex']);
  717.  
  718. $userData = $this->User->find('first',array('fields'=>array('PictureIndex'),'conditions'=>array('User.id'=>$UserId)));
  719.  
  720. if(!empty($userData['User']['fbid']) || ($userData['User']['fbid'] != 0) ){
  721. $this->User->updateAll(array('User.PictureIndex'=>4), array('User.id' => $UserId));
  722. }else{
  723. $this->User->updateAll(array('User.PictureIndex'=>$PictureIndex), array('User.id' => $UserId));
  724. }
  725.  
  726. if(!empty($_FILES)){
  727. // Update User Profile
  728. $filename = time().$_FILES['Picture']['name'];
  729.  
  730. move_uploaded_file($_FILES['Picture']['tmp_name'], WWW_ROOT . 'img/members/' . $filename);
  731. $this->User->updateAll(array('User.firstname' =>'"'.$firstname.'"','User.info' =>'"'.$info.'"','User.pic'=>'"'.$filename.'"'), array('User.id' => $UserId));
  732. $message = array('Error'=>false,'Message'=>'Success');
  733. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  734.  
  735. } else{
  736. $this->User->updateAll(array('User.firstname' =>'"'.$firstname.'"','User.info' =>'"'.$info.'"'), array('User.id' => $UserId));
  737. $message = array('Error'=>false,'Message'=>'Success');
  738. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  739. }
  740. }
  741. }
  742.  
  743.  
  744. /**
  745. * @ method : getProfile
  746. * @ params : UserId
  747. * @ created_date : 17_06_2016
  748. */
  749.  
  750. public function getProfile(){
  751. if(empty($_REQUEST['UserId'])){
  752. $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
  753. } else{
  754. $userData = $this->User->find('first',array('fields'=>array('id','fbid','star','username','firstname','statas as Statas','info as Bio','pic','PictureIndex','UserPlayGame.total_game','UserPlayGame.win_percent','UserPlayGame.fairPlay'),'conditions'=>array('User.id'=>$_REQUEST['UserId'])));
  755.  
  756. $picUrl = ($userData['User']['pic'])?HTTP_ROOT.'app/webroot/img/members/'.$userData['User']['pic']: $userData['User']['pic'];
  757.  
  758. if(!empty($userData['User']['fbid']) || ($userData['User']['fbid'] != 0) ){
  759. $userData['User']['PictureIndex'] = 4;
  760. }
  761.  
  762. $data['UserId'] = $userData['User']['id'];
  763. $data['FbId'] = $userData['User']['fbid'];
  764. $data['Username'] = $userData['User']['username'];
  765. $data['Name'] = $userData['User']['firstname'];
  766. $data['Bio'] = $userData['User']['Bio'];
  767. $data['Statas'] = $userData['User']['Statas'];
  768. $data['PictureIndex'] = $userData['User']['PictureIndex'];
  769. $data['Star'] = $userData['User']['star'];
  770.  
  771. if(empty($userData['UserPlayGame']['fairPlay'])){
  772. $data['FairPlay'] = 0;
  773. }else{
  774. $data['FairPlay'] = $userData['UserPlayGame']['fairPlay'];
  775. }
  776.  
  777. if(empty($userData['UserPlayGame']['win_percent'])){
  778. $data['WinPercent'] = 0 ;
  779. }else{
  780. $data['WinPercent'] = $userData['UserPlayGame']['win_percent'];
  781. }
  782.  
  783. if(empty($userData['UserPlayGame']['total_game'])){
  784. $data['TotalGame'] = 0;
  785. }else{
  786. $data['TotalGame'] = $userData['UserPlayGame']['total_game'];
  787. }
  788.  
  789. $picUrl = ($userData['User']['pic'])? HTTP_ROOT.'app/webroot/img/members/'.$userData['User']['pic']: $userData['User']['pic'];
  790. $data['Picture'] = $picUrl;
  791. $this->set(array('Response' => array('Error'=>false,'Message'=>'User data','UserData'=>$data),'_serialize' => array('Response')));
  792. }
  793. }
  794.  
  795.  
  796. /**
  797. * @ method : getBadges
  798. * @ params : UserId
  799. * @ created_date : 17_06_2016
  800. */
  801.  
  802. public function getAchievements(){
  803. if(empty($_REQUEST['UserId'])){
  804. $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
  805. } else{
  806. $this->loadmodel('UserBadge');
  807. $userId = trim($_REQUEST['UserId']);
  808. $BadgesData = $this->UserBadge->find('first',array('fields'=>array('UserBadge.AchievementData'),'conditions'=>array('UserBadge.user_id'=>$userId)));
  809.  
  810. if(!empty($BadgesData)){
  811.  
  812. $this->set(array('Response' => array('Error'=>false,'AchievementData'=>$BadgesData['UserBadge']['AchievementData'],'Message'=>'Achievements data'),'_serialize' => array('Response')));
  813. } else{
  814. $this->set(array('Response' => array('Error'=>false,'AchievementData'=>'','Message'=>'Achievements data'),'_serialize' => array('Response')));
  815. }
  816. }
  817. }
  818.  
  819.  
  820. /**
  821. * @method : setBadges
  822. * @prams : UserId,name
  823. * @created_date : 17_06_2016
  824. */
  825.  
  826. public function setBadges(){
  827. $error = array();
  828. if(empty($this->request->data['UserId'])){
  829. $error[] = "Missing UserId parameter";
  830. }
  831. if(empty($this->request->data['AchievementData'])){
  832. $error[] = "Missing AchievementData parameter";
  833. }
  834. if(!empty($error )){
  835. $erroMsg = implode(',', $error);
  836. $message = array('Error'=>true,'Message'=>$erroMsg);
  837. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  838. } else{
  839. $this->loadmodel('UserBadge');
  840. // Check this Badge alredy added or not
  841. $badgename = trim($this->request->data['AchievementData']);
  842. $userId = trim($this->request->data['UserId']);
  843. $checkBadge = $this->UserBadge->find('count',array('conditions'=>array('UserBadge.user_id'=>$userId)));
  844. if($checkBadge>0){
  845.  
  846. // Update
  847. $this->UserBadge->updateAll(array('UserBadge.AchievementData' =>'"'.$badgename.'"'),array('UserBadge.user_id' =>$userId));
  848. $message = array('Error'=>false,'Message'=>'Badges updated');
  849. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  850. }else{
  851.  
  852. $saveData['UserBadge']['user_id'] = $userId;
  853. $saveData['UserBadge']['AchievementData'] = $badgename;
  854. $this->UserBadge->save($saveData);
  855. $message = array('Error'=>false,'Message'=>'Success');
  856. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  857. }
  858. }
  859. }
  860.  
  861. /**
  862. * @method : updateSetting
  863. * @params : UserId, StatasStatus
  864. * @created_date : 17_06_2016
  865. */
  866.  
  867. public function updateSetting(){
  868. $error = array();
  869. if(empty($this->request->data['UserId'])){
  870. $error[] = "Missing UserId parameter";
  871. }
  872. if($this->request->data['StatasStatus']==''){
  873. $error[] = "Missing StatasStatus parameter";
  874. }
  875. if(!empty($error )){
  876. $erroMsg = implode(',', $error);
  877. $message = array('Error'=>true,'Message'=>$erroMsg);
  878. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  879. } else{
  880. $UserId = trim($this->request->data['UserId']);
  881. $status = trim($this->request->data['StatasStatus']);
  882. $this->User->updateAll(array('User.statas' =>$status), array('User.id' =>$UserId));
  883. $message = array('Error'=>false,'Message'=>'Success');
  884. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  885. }
  886. }
  887.  
  888.  
  889. /**
  890. * @method : forgotPassword
  891. * @params : Email
  892. * @created : 18_06_2016
  893. *
  894. */
  895.  
  896. public function forgotPassword(){
  897. if(empty($_REQUEST['Email'])){
  898. $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing Email field'),'_serialize' => array('Response')));
  899. } else{
  900. $email = trim($_REQUEST['Email']);
  901. $userData = $this->User->find('first',array('conditions'=>array('User.email'=>$email,'User.username !='=>''),'fields'=>array('User.id','User.email','User.username'),'recursive'=>-1));
  902. if(!empty($userData)){
  903. // Send Mail
  904. $userId = $userData['User']['id'];
  905. $rnadPass = $this->randPass();
  906. $password = md5($rnadPass);
  907. $checkup = $this->User->updateAll(array('User.password' =>'"'.$password.'"'), array('User.id' =>$userId));
  908.  
  909. if($checkup){
  910. $aMsg = "<p>Your Username is: ".$userData['User']['username']."</p>";
  911. $aMsg .= "Your password is: ".$rnadPass;
  912. $this->Session->write('Person.forgotpass', $aMsg);
  913. $this->AWSSES->to = trim($email);
  914.  
  915. if($this->AWSSES->_aws_ses('forgot',$aMsg)){
  916. $arr = array('Response' => array('Error'=>false,'Message'=>'Your Login password has been sent to your mail. Please check.'));
  917. echo json_encode($arr);
  918. }else{
  919. $message = array('Response' =>array('Error'=>true,'Message'=>'Email failed'));
  920. echo json_encode($message);
  921. }
  922. } else{
  923. $message = array('Error'=>true,'Message'=>'Failed !');
  924. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  925. }
  926. }
  927. $message = array('Error'=>false,'Message'=>'Your Login password has been sent to your mail. Please check.');
  928. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  929. }
  930. }
  931.  
  932.  
  933. /**
  934. * @get 6 digint random password
  935. *
  936. */
  937.  
  938. public function randPass(){
  939. $alphabets = range('a','z');
  940. $numbers = range('0','9');
  941.  
  942. $final_array = array_merge($alphabets,$numbers);
  943. $password = '';
  944. $length = 7;
  945. while($length--) {
  946. $key = array_rand($final_array);
  947. $password .= $final_array[$key];
  948. }
  949.  
  950. return $password;
  951.  
  952. }
  953.  
  954. /**
  955. * @method : logOut
  956. * @purpose : Delete User's Current DeviceId
  957. * @created_date : 20_06_2016
  958. */
  959.  
  960. public function logOut(){
  961. $error = array();
  962. if(empty($this->request->data['UserId'])){
  963. $error[] = "Missing UserId parameter";
  964. }
  965. if(empty($this->request->data['DeviceId'])){
  966. $error[] = "Missing DeviceId parameter";
  967. }
  968. if(!empty($error )){
  969. $erroMsg = implode(',', $error);
  970. $message = array('Error'=>true,'Message'=>$erroMsg);
  971. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  972. } else{
  973. try{
  974. $userId = trim($this->request->data['UserId']);
  975.  
  976. $this->removidleuser($userId);
  977.  
  978. $DeviceId = trim($this->request->data['DeviceId']);
  979.  
  980. $this->UserDevice->query("DELETE FROM user_devices where UserId=? and DeviceId = ?",[$userId,$DeviceId]);
  981.  
  982. // Now Update Current Device field in Users Tabls
  983. $this->User->query("UPDATE users SET current_device_id = '', online_status=0 where id=$userId");
  984.  
  985. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
  986. } catch (Exception $e) {
  987. $this->set(array('Response' => array('Error'=>true,'Message'=>$e->getMessage()),'_serialize' => array('Response')));
  988. }
  989. }
  990. }
  991.  
  992. /**
  993. * @check EmailId already Registrd or not
  994. * @created_date : 15_06_2016
  995. */
  996.  
  997. public function checkEmail($email=null) {
  998. $records1 = $this->User->find('count',array('conditions'=>array('User.email'=>$email)));
  999. if($records1>0){
  1000. return 1;
  1001. } else{
  1002. return 0;
  1003. }
  1004.  
  1005. }
  1006.  
  1007. /**
  1008. * @check username user_badges UserBadge
  1009. * @ check useranem unique
  1010. * @ create_date: 16_06_2016
  1011. */
  1012.  
  1013. public function checkUserName($username=null){
  1014. $usernameCount = $this->User->find('count',array('conditions'=>array('OR' => array(array('User.username' =>$username),array('User.nick' =>$username)))));
  1015. if($usernameCount>0){
  1016. return 1;
  1017. } else{
  1018. return 0;
  1019. }
  1020. }
  1021.  
  1022. /**
  1023. * @ check fbId already registred or Not
  1024. * @ created_date: 16_06_2016
  1025. */
  1026.  
  1027. public function checkfbId($fbid=null){
  1028. $userData = $this->User->find('first',array('conditions' =>array('fbid'=>$fbid)));
  1029.  
  1030. return $userData;
  1031. }
  1032.  
  1033. /**
  1034. * @ Check fb email already registred or Not
  1035. *
  1036. */
  1037.  
  1038. public function checkFbEmail($fbemail=null){
  1039. $userData2 = $this->User->find('first',array('fields'=>array('id','email','online_status'),'conditions'=>array('User.email'=>$fbemail)));
  1040. return $userData2;
  1041. }
  1042.  
  1043. public function checkFbUsername($username=null) {
  1044. $checkUsername = $this->User->find('first',array('fields'=>array('id','email','online_status'),
  1045. 'conditions'=>array('User.username'=>$username)));
  1046. return $checkUsername;
  1047. }
  1048.  
  1049.  
  1050. /*
  1051. status: 0 not registered, 1 online, 2 offline
  1052. */
  1053. public function checkdevice() {
  1054. $error = array();
  1055. if(empty($this->request->data['FbId'])){
  1056. $error[] = "Missing FbId parameter";
  1057. }
  1058. if(empty($this->request->data['DeviceId'])){
  1059. $error[] = "Missing DeviceId parameter";
  1060. }
  1061.  
  1062. if(!empty($error )){
  1063. $erroMsg = implode(',', $error);
  1064. $message = array('Error'=>true,'Message'=>$erroMsg);
  1065. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1066. } else{
  1067. $userfbid = $this->request->data['FbId'];
  1068. $deviceid = $this->request->data['DeviceId'];
  1069. $userdata = $this->User->find('first',array('conditions'=>array('User.fbid' => $userfbid)));
  1070.  
  1071. if(empty($userdata)){
  1072. $this->set(array('Response' => array('Error'=> false,'Status'=> 0,'Message' => 'The user is not registered'),'_serialize'=>array('Response')));
  1073. }else{
  1074. $userstatus = $userdata['User']['online_status'];
  1075. $currentdevice = $userdata['User']['current_device_id'];
  1076. $userid = $userdata['User']['id'];
  1077.  
  1078. $devicedata = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.UserId' => $userid,'UserDevice.id' => $currentdevice),'order' =>array('UserDevice.id DESC')));
  1079.  
  1080. if(empty($devicedata)) {
  1081. $currentdeviceid = "";
  1082. }else{
  1083. $currentdeviceid = $devicedata['UserDevice']['DeviceId'];
  1084. }
  1085.  
  1086. if($userstatus == 1 && $currentdeviceid != $deviceid ) {
  1087. $this->set(array('Response' => array('UserId' => $userid,'Error'=> false,'Status'=> 1,'Message' => 'The user is registered'),'_serialize'=>array('Response')));
  1088. }else{
  1089. $lastseen = strtotime(date('Y-m-d H:i:s'));
  1090. $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
  1091. $deviceData['UserDevice']['UserId'] = $userid;
  1092. $this->UserDevice->save($deviceData);
  1093. $DataDeviceId = $this->UserDevice->getInsertID();
  1094. $lastseen = strtotime(date('Y-m-d H:i:s'));
  1095. $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$DataDeviceId,'User.last_seen'=>$lastseen), array('User.id' =>$userid));
  1096. $this->set(array('Response' => array('UserId' => $userid,'Error'=> false,'Status'=> 2,'Message' => 'The user is registered'),'_serialize'=>array('Response')));
  1097. }
  1098. }
  1099. }
  1100. }
  1101.  
  1102. /**
  1103. * @method : merge
  1104. * Params : UserId, FbId
  1105. * Purpose : Merge FBId after Siple Email Login
  1106. * Create_date : 28_06_2016
  1107. */
  1108.  
  1109. public function mergeFacebookId(){
  1110. $error = array();
  1111. if(empty($this->request->data['FbId'])){
  1112. $error[] = "Missing FbId parameter";
  1113. }
  1114. if(empty($this->request->data['UserId'])){
  1115. $error[] = "Missing UserId parameter";
  1116. }
  1117.  
  1118. if(!empty($error )){
  1119. $erroMsg = implode(',', $error);
  1120. $message = array('Error'=>true,'Message'=>$erroMsg);
  1121. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1122. } else{
  1123. $fbId = trim($this->request->data['FbId']);
  1124. $userId = trim($this->request->data['UserId']);
  1125. $fbuser = $this->User->find('first',array('conditions'=>array('User.fbid'=>$fbId),'fields'=>array('User.id','User.fbid')));
  1126. if(!empty($fbuser)){
  1127. // if FbId for Othe User
  1128. if( $fbuser['User']['id'] !=$userId){
  1129. $this->set(array('Response' => array('Error'=> false,'Status'=> 2,'Message' => 'This FbId alredy set for other user'),'_serialize'=>array('Response')));
  1130. } else{
  1131. $this->set(array('Response' => array('Error'=> false,'Status'=> 3,'Message' => 'This FbId alredy set for current user'),'_serialize'=>array('Response')));
  1132. }
  1133. } else{
  1134. // Set This FB id to Current User
  1135. // One New Case: if Fb Id new but this user alredy connected with other FB then Status: 4
  1136.  
  1137. $getUser=$this->User->find('first',array('conditions'=>array('User.id'=>$userId),'fields'=>array('User.id','User.fbid')));
  1138.  
  1139. if(isset($getUser['User']['fbid']) && !empty($getUser['User']['fbid']) ){
  1140. $this->set(array('Response' => array('Error'=> false,'Status'=> 4,'Message' => 'This user alredy connected with other fbid'),'_serialize'=>array('Response')));
  1141. } else{
  1142. // Now Update
  1143. $this->User->updateAll(array('User.fbid' =>'"'.$fbId.'"'), array('User.id' =>$userId));
  1144. $this->set(array('Response' => array('Error'=> false,'Status'=> 1,'Message' => 'Success'),'_serialize'=>array('Response')));
  1145. }
  1146. }
  1147. }
  1148. }
  1149.  
  1150.  
  1151. /**
  1152. * @method : addFriend
  1153. * @Params : UserId, FriendId
  1154. * Created_date : 29_06_2016
  1155. */
  1156.  
  1157. public function addFriend(){
  1158. $error = array();
  1159. if(empty($this->request->data['UserId'])){
  1160. $error[] = "Missing UserId parameter";
  1161. }
  1162. if(empty($this->request->data['FriendId'])){
  1163. $error[] = "Missing FriendId parameter";
  1164. }
  1165.  
  1166. if(!empty($error )){
  1167. $erroMsg = implode(',', $error);
  1168. $message = array('Error'=>true,'Message'=>$erroMsg);
  1169. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1170. } else{
  1171. /** Check User Already friend List */
  1172. $this->loadmodel('UserFriend');
  1173. $userId = trim($this->request->data['UserId']);
  1174. $FriendId = trim($this->request->data['FriendId']);
  1175. $Data = $this->UserFriend->find('first',array('conditions'=>array('UserFriend.UserId'=>$userId,'UserFriend.FriendId'=>$FriendId),'fields'=>array('UserFriend.id'),'recursive'=>-1));
  1176. if(!empty($Data)){
  1177. $this->set(array('Response' => array('Error'=>false,'Message'=>'Already in friend list'),'_serialize' => array('Response')));
  1178. } else{
  1179. // Add friend
  1180. $this->UserFriend->save($this->request->data);
  1181. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
  1182. }
  1183. }
  1184. }
  1185.  
  1186. /**
  1187. * @method : removeFriend
  1188. * @Params : UserId, FriendId
  1189. * @created_date : 29_06_2016
  1190. */
  1191.  
  1192. public function removeFriend(){
  1193. $error = array();
  1194. if(empty($this->request->data['UserId'])){
  1195. $error[] = "Missing UserId parameter";
  1196. }
  1197. if(empty($this->request->data['FriendId'])){
  1198. $error[] = "Missing FriendId parameter";
  1199. }
  1200.  
  1201. if(!empty($error )){
  1202. $erroMsg = implode(',', $error);
  1203. $message = array('Error'=>true,'Message'=>$erroMsg);
  1204. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1205. } else{
  1206. $this->loadmodel('UserFriend');
  1207. $userId = trim($this->request->data['UserId']);
  1208. $FriendId = trim($this->request->data['FriendId']);
  1209. $this->UserFriend->deleteAll(array('UserFriend.UserId'=>$userId,'UserFriend.FriendId'=>$FriendId));
  1210. $this->set(array('Response'=>array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
  1211. }
  1212. }
  1213.  
  1214. /**
  1215. * @method : getFriendList
  1216. * @Params : UserId
  1217. */
  1218.  
  1219. public function getFriendList(){
  1220. if(empty($_REQUEST['UserId'])){
  1221. $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
  1222. } else{
  1223. //
  1224. $this->loadmodel('UserFriend');
  1225. $userId = trim($_REQUEST['UserId']);
  1226. $friendData = $this->UserFriend->find('all',array('conditions'=>array('UserFriend.UserId'=>$userId),'fields'=>array('User.id','User.username','User.fbid','User.star','User.pic','User.online_status','User.PictureIndex') ));
  1227.  
  1228. $data = array();
  1229. if(!empty($friendData)){
  1230.  
  1231. foreach($friendData as $key=>$val){
  1232. if(isset($val['User']['id'])){
  1233. $data[$key]['UserId'] = $val['User']['id'];
  1234. $data[$key]['Star'] = $val['User']['star'];
  1235. $data[$key]['Name'] = $val['User']['username'];
  1236. $data[$key]['FbId'] = $val['User']['fbid'];
  1237. $data[$key]['OnlineStatus'] = $val['User']['online_status'];
  1238. $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
  1239. $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
  1240. $data[$key]['Picture'] = $picUrl;
  1241. }
  1242. }
  1243. $data=array_values($data);
  1244. }
  1245.  
  1246. $this->set(array('Response' => array('Error'=>false,'CustomFriendData'=>$data),'_serialize' => array('Response')));
  1247. }
  1248. }
  1249.  
  1250. /**
  1251. * @method : lastSeen
  1252. * @Params : UserId
  1253. */
  1254.  
  1255. public function lastSeen(){
  1256.  
  1257. $error = array();
  1258. if(empty($_REQUEST['UserId'])){
  1259. $error[] = "Missing UserId parameter";
  1260. }
  1261.  
  1262. if(!empty($error )){
  1263. $erroMsg = implode(',', $error);
  1264. $message = array('Error'=>true,'Message'=>$erroMsg);
  1265. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1266. }else{
  1267. $userId = $_REQUEST['UserId'];
  1268. $data=array();
  1269.  
  1270. $userplaydata = $this->UserPlayGame->find('all', array('conditions' => array('UserPlayGame.user_id'=> $userId), 'recursive' => -1));
  1271. if(count($userplaydata) > 0){
  1272. $homerun = $userplaydata[0]['UserPlayGame']['HomeRun'];
  1273. $Forcer = $userplaydata[0]['UserPlayGame']['Forcer'];
  1274. $Ace = $userplaydata[0]['UserPlayGame']['Ace'];
  1275. $Unstoppable = $userplaydata[0]['UserPlayGame']['Unstoppable'];
  1276. $Winner = $userplaydata[0]['UserPlayGame']['Winner'];
  1277. $Player = $userplaydata[0]['UserPlayGame']['Player'];
  1278. $Looser = $userplaydata[0]['UserPlayGame']['Looser'];
  1279. $FullHouse = $userplaydata[0]['UserPlayGame']['FullHouse'];
  1280. $WinStreak = $userplaydata[0]['UserPlayGame']['WinStreak'];
  1281. $XthreeWinStreak = $userplaydata[0]['UserPlayGame']['XthreeWinStreak'];
  1282. $XfiveWinstreak = $userplaydata[0]['UserPlayGame']['XfiveWinstreak'];
  1283. $XtenWinstreak = $userplaydata[0]['UserPlayGame']['XtenWinstreak'];
  1284.  
  1285. $data = array($Ace,$Unstoppable,$Winner,$Player,$XthreeWinStreak,$XfiveWinstreak,$XtenWinstreak,$Looser,$FullHouse,$homerun,$Forcer);
  1286. }
  1287.  
  1288. $lastseen = strtotime(date('Y-m-d H:i:s'));
  1289. if(empty($_REQUEST['DeviceId'])){
  1290. $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
  1291. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data),'_serialize' => array('Response')));
  1292.  
  1293. }else{
  1294. $deviceid = $_REQUEST['DeviceId'];
  1295. $devicedata = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.UserId' => $userId,'UserDevice.DeviceId' => $deviceid),'order' =>array('UserDevice.id DESC')));
  1296.  
  1297. if(empty($devicedata)) {
  1298. $currentdeviceid = "";
  1299. }else{
  1300. $currentdeviceid = $devicedata['UserDevice']['id'];
  1301. }
  1302.  
  1303. $chkusersdevice = $this->User->find('first',array('conditions' => array('User.id' => $userId),'recursive' => -1));
  1304.  
  1305. if($chkusersdevice['User']['online_status'] == 1){
  1306.  
  1307. if($chkusersdevice['User']['current_device_id'] == $currentdeviceid){
  1308.  
  1309. $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
  1310.  
  1311. //*******room termination 21-11-2016 *****//
  1312. if(empty($_REQUEST['RoomId'])){
  1313.  
  1314. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data, 'AutoTerminateTable' => false),'_serialize' => array('Response')));
  1315.  
  1316. }else{
  1317.  
  1318. $autoterminate = true;
  1319. $curentRoomId = $_REQUEST['RoomId'];
  1320. $chkRoomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $curentRoomId), 'recursive' => -1));
  1321.  
  1322. //check if room exists or not
  1323. if((!empty($chkRoomdata)) ) {
  1324. //to check if current time is greater that time limit for room termination
  1325.  
  1326. $roomterminateTime = $chkRoomdata['Room']['auto_terminate_update'];
  1327. $joinmemberCount = $chkRoomdata['Room']['join_member_count'];
  1328. $roomStatus = $chkRoomdata['Room']['room_status'];
  1329.  
  1330. $getGameplayData = $this->GamePlayData->find('first', array('conditions' => array('GamePlayData.RoomId' => $curentRoomId), 'recursive' => -1));
  1331.  
  1332. if((strtotime(date('Y-m-d H:i:s')) - strtotime(date($roomterminateTime))) >= 150 && !empty($chkRoomdata['Room']['PhotonId']) && ( empty($getGameplayData) || ($joinmemberCount < 4 && $roomStatus != 2))) {
  1333. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $curentRoomId));
  1334. $this->Room->delete($curentRoomId);
  1335. $autoterminate = true;
  1336. }
  1337. else {
  1338. $autoterminate = false;
  1339. }
  1340. }
  1341.  
  1342. $this->set(array('Response'=>array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data, 'AutoTerminateTable' => $autoterminate),'_serialize'=>array('Response')));
  1343. }
  1344.  
  1345. //*******room termination *****//
  1346.  
  1347.  
  1348. }elseif($chkusersdevice['User']['current_device_id'] != $currentdeviceid){
  1349. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>'AlreadySignin'),'_serialize' => array('Response')));
  1350. }
  1351. }else{
  1352. $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
  1353. $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data),'_serialize' => array('Response')));
  1354. }
  1355. }
  1356. }
  1357. }
  1358.  
  1359.  
  1360.  
  1361. /**
  1362. * @created_date : 15_07_2016
  1363. */
  1364. public function searchUser() {
  1365. $error = array();
  1366. if(empty($_REQUEST['UserId'])){
  1367. $error[] = "Missing UserId parameter";
  1368. }
  1369.  
  1370. if(!empty($error )){
  1371. $erroMsg = implode(',', $error);
  1372. $message = array('Error'=>true,'Message'=>$erroMsg);
  1373. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1374. }else{
  1375. $this->loadmodel('UserFriend');
  1376. $userId = trim($_REQUEST['UserId']);
  1377.  
  1378.  
  1379. $limit = 50;
  1380. if(!empty($_REQUEST['Limit'])){
  1381. $limit = $_REQUEST['Limit'];
  1382. }
  1383. $page = 1;
  1384. if(!empty($this->request->data['page'])){
  1385. $page = $this->request->data['page'];
  1386. }
  1387. $offset = ( $page - 1 ) * $limit;
  1388.  
  1389. $UserFrinds = $this->UserFriend->find('list',array('conditions'=>array('UserFriend.UserId'=>$userId),'fields'=>array('UserFriend.FriendId'),'recursive'=>-1));
  1390.  
  1391. array_push($UserFrinds,$userId);
  1392.  
  1393. if(isset($_REQUEST['Username']) && !empty($_REQUEST['Username'])){
  1394. // Get List Of UserFriend
  1395. $username = trim($_REQUEST['Username']);
  1396. $userData = $this->User->find('all', array(
  1397. 'conditions' => array("NOT" => array( "User.id" => $UserFrinds),'Username LIKE'=>'%'.$username.'%','User.username !='=>''),
  1398. 'fields'=>array('User.id','User.username','User.fbid','User.star','User.pic','User.online_status','User.PictureIndex'),
  1399. 'limit' => $limit,
  1400. 'page' => $page,
  1401. 'offset'=>$offset,'recursive'=>-1));
  1402.  
  1403. $data = array();
  1404. if(!empty($userData)){
  1405.  
  1406. foreach($userData as $key=>$val){
  1407.  
  1408. $data[$key]['UserId'] = $val['User']['id'];
  1409. $data[$key]['Name'] = $val['User']['username'];
  1410. $data[$key]['FbId'] = $val['User']['fbid'];
  1411. $data[$key]['Star'] = $val['User']['star'];
  1412. $data[$key]['OnlineStatus'] = $val['User']['online_status'];
  1413. $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
  1414. $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
  1415. $data[$key]['Picture'] = $picUrl;
  1416.  
  1417. }
  1418. }
  1419. $this->set(array('Response' => array('Error'=>false,'UserData'=>$data),'_serialize' => array('Response')));
  1420. } else{
  1421. // Game Played by This User
  1422. $userData = $this->User->find('all', array(
  1423. 'conditions' => array("NOT" => array( "User.id" => $UserFrinds),'User.username !='=>''),
  1424. 'fields'=>array('User.id','User.username','User.fbid','User.star','User.pic','User.online_status','User.PictureIndex'),
  1425. 'limit' => $limit,
  1426. 'page' => $page,
  1427. 'offset'=>$offset,'order' => 'rand()','recursive'=>-1));
  1428.  
  1429. $data = array();
  1430. if(!empty($userData)){
  1431.  
  1432. foreach($userData as $key=>$val){
  1433.  
  1434. $data[$key]['UserId'] = $val['User']['id'];
  1435. $data[$key]['Name'] = $val['User']['username'];
  1436. $data[$key]['FbId'] = $val['User']['fbid'];
  1437. $data[$key]['Star'] = $val['User']['star'];
  1438. $data[$key]['OnlineStatus'] = $val['User']['online_status'];
  1439. $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
  1440. $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
  1441. $data[$key]['Picture'] = $picUrl;
  1442. }
  1443. }
  1444. $this->set(array('Response' => array('Error'=>false,'UserData'=>$data),'_serialize' => array('Response')));
  1445. }
  1446. }
  1447. }
  1448.  
  1449.  
  1450. public function GetStar(){
  1451.  
  1452. $error = array();
  1453. if(empty($_REQUEST['Id'] )){
  1454. $error[] = "Missing Id parameter";
  1455. }
  1456. if(empty($_REQUEST['IdType'] )){
  1457. $error[] = "Missing IdType parameter";
  1458. }
  1459.  
  1460. if(!empty($error )){
  1461. $erroMsg = implode(',', $error);
  1462. $message = array('Error'=>true,'Message'=>$erroMsg);
  1463. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1464. }else{
  1465.  
  1466. if($_REQUEST['IdType'] == 1){
  1467. $userid = $_REQUEST['Id'];
  1468. $fbid = '';
  1469. $getstarinfo = $this->User->find('first' , array('conditions' => array('User.id' => $userid), 'fields' => array('star'), 'recursive' => -1));
  1470.  
  1471. if(!empty($getstarinfo)){
  1472. $star = $getstarinfo['User']['star'];
  1473. }else{
  1474. $star = 0 ;
  1475. }
  1476.  
  1477. $this->set(array('Response' => array('Error'=>false,'Star'=> $star),'_serialize' => array('Response')));
  1478.  
  1479. }elseif($_REQUEST['IdType'] == 2){
  1480.  
  1481. $fbid = $_REQUEST['Id'];
  1482. $userid = '';
  1483.  
  1484. $getstarinfo = $this->User->find('first' , array('conditions' => array('User.fbid ' => $fbid), 'fields' => array('star'), 'recursive' => -1));
  1485.  
  1486. if(!empty($getstarinfo)){
  1487. $star = $getstarinfo['User']['star'];
  1488. }else
  1489. $star = 0 ;
  1490.  
  1491. $this->set(array('Response' => array('Error'=>false,'Star'=> $star),'_serialize' => array('Response')));
  1492. }else{
  1493.  
  1494. $idtype = $_REQUEST['IdType'];
  1495. $this->set(array('Response'=>array('Error'=>false,'Message'=>'wrong IdType '.$idtype.''),'_serialize' => array('Response')));
  1496. }
  1497. }
  1498. }
  1499.  
  1500. public function saveRoundScore() {
  1501.  
  1502. $data = $_POST['JsonData'];
  1503. $json = ''.$data.'';
  1504.  
  1505. if(empty($json)){
  1506. $errorData = array('Error' => true, 'Message' => 'No Data Parameters');
  1507. $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
  1508. }
  1509.  
  1510. $yummy = json_decode($json);
  1511. $RoomData = $yummy;
  1512.  
  1513. if(!empty($RoomData)){
  1514. $error = array();
  1515.  
  1516. $senderId = $RoomData->SenderId ;
  1517. $RoomId = $RoomData->RoomId;
  1518. $RoundIndex = $RoomData->RoundIndex;
  1519. if(empty($senderId)) {
  1520. $error[] = "SenderId parameter is empty.";
  1521. }
  1522. if(empty($RoomId)){
  1523. $error[] = "RoomId parameter is empty.";
  1524. }
  1525. if(empty($RoundIndex)){
  1526. $error[] = "RoundIndex parameter is empty.";
  1527. }
  1528.  
  1529. $ScoreDataArray = $RoomData->ScoreData ;
  1530.  
  1531. if(empty($ScoreDataArray)){
  1532. $error[] = "ScoreData parameter is empty.";
  1533. }
  1534.  
  1535. if(!empty($error)){
  1536. $errorMessage = implode(',',$error);
  1537. $errorData = array('Error' => true, 'Message' => $errorMessage);
  1538. $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
  1539. }else{
  1540. $checkdata = $this->RoundScore->find('all',array('conditions'=>array('RoundScore.RoomId' => $RoomId,'RoundScore.RoundIndex' => $RoundIndex)));
  1541.  
  1542. if(count($checkdata) > 0 ){
  1543. $this->set(array('Response'=>array('Error'=>false , 'Message' => 'Round already exist'), '_serialize' => array('Response')));
  1544.  
  1545. }else {
  1546. $teamAarray = $ScoreDataArray[0]->TeamA ;
  1547. $teamAscore = $teamAarray->Score;
  1548. $teamBarray = $ScoreDataArray[1]->TeamB ;
  1549. $teamBscore = $teamBarray->Score;
  1550.  
  1551. if(!empty($teamAscore)){
  1552. $teamAscore = $teamAscore ;
  1553. }else{
  1554. $teamAscore = 0 ;
  1555. }
  1556. $TeamAusers = implode(',',$teamAarray->Users);
  1557. $created_at = strtotime(date('Y-m-d H:i:s'));
  1558.  
  1559. if( $teamAscore > $teamBscore){
  1560. $roundResultA = 'W';
  1561. }elseif($teamAscore < $teamBscore){
  1562. $roundResultA = 'R';
  1563. }elseif($teamAscore == $teamBscore){
  1564. $roundResultA = 'D';
  1565. }
  1566.  
  1567. $teamAdata = array('Result' => $roundResultA,'SenderId' => $senderId, 'RoomId' => $RoomId ,'Score' => $teamAscore ,'RoundIndex' => $RoundIndex ,'Team' => 'A' , 'TeamUsers' => $TeamAusers, 'created_at' => $created_at);
  1568.  
  1569. if(!empty($teamBscore)){
  1570. $teamBscore = $teamBscore ;
  1571. }else{
  1572. $teamBscore = 0 ;
  1573. }
  1574.  
  1575. if($teamBscore > $teamAscore){
  1576. $roundResultB = 'W';
  1577. }elseif($teamBscore < $teamAscore){
  1578. $roundResultB = 'R';
  1579. }elseif($teamBscore == $teamAscore){
  1580. $roundResultB = 'D';
  1581. }
  1582.  
  1583. $TeamBusers = implode(',',$teamBarray->Users);
  1584. $teamBdata = array('Result' => $roundResultB,'SenderId' => $senderId, 'RoomId' => $RoomId ,'Score' => $teamBscore ,'RoundIndex' => $RoundIndex ,'Team' => 'B' , 'TeamUsers' => $TeamBusers, 'created_at' => $created_at);
  1585.  
  1586. $data1 = $teamAdata ;
  1587. $data2 = $teamBdata ;
  1588. $allData = array($data1, $data2);
  1589.  
  1590. $saveData = $this->RoundScore->saveAll($allData);
  1591. $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
  1592. }
  1593. }
  1594.  
  1595. }else{
  1596. $erroMsg = "JsonData parameter is empty";
  1597. $message = array('Error'=>true,'Message'=>$erroMsg);
  1598. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1599. }
  1600. }
  1601.  
  1602. public function SaveFinalScore() {
  1603.  
  1604. $data = $_POST['JsonData'];
  1605. $json = ''.$data.'';
  1606.  
  1607. $yummy = json_decode($json);
  1608.  
  1609. $RoomData = $yummy;
  1610. if(!empty($RoomData)){
  1611.  
  1612. $error = array();
  1613. $senderId = $RoomData->SenderId ;
  1614. $RoomId = $RoomData->RoomId;
  1615.  
  1616. if(empty($senderId)) {
  1617. $error[] = "SenderId parameter is empty.";
  1618. }
  1619. if(empty($RoomId)){
  1620. $error[] = "RoomId parameter is empty.";
  1621. }
  1622.  
  1623. $ScoreDataArray = $RoomData->ScoreData ;
  1624.  
  1625. if(empty($ScoreDataArray)){
  1626. $error[] = "ScoreData parameter is empty.";
  1627. }
  1628. if(!empty($error)){
  1629. $errorMessage = implode(',',$error);
  1630. $errorData = array('Error' => true, 'Message' => $errorMessage);
  1631. $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
  1632. }else{
  1633. $checkdata = $this->FinalScore->find('all',array('conditions'=>array('FinalScore.RoomId' => $RoomId)));
  1634.  
  1635. if(count($checkdata) > 0 ){
  1636. $this->set(array('Response' => array('Error' => false , 'Message' => 'Room data already exists.'), '_serialize' => array('Response')));
  1637. } else {
  1638. $createdAt = strtotime(date('Y-m-d H:i:s'));
  1639.  
  1640. $WinnerArray = $ScoreDataArray[0]->Winner;
  1641. $WinnerScore = $WinnerArray->Score;
  1642. $WinPlayers = implode(',',$WinnerArray->Users);
  1643.  
  1644. $WinnerData = array('SenderId' => $senderId, 'RoomId' => $RoomId, 'Result' => 'W', 'Players' => $WinPlayers, 'Score' => $WinnerScore, 'CreatedAt' => $createdAt);
  1645.  
  1646. $RunnerArray = $ScoreDataArray[1]->Runner;
  1647. $RunnerScore = $RunnerArray->Score;
  1648. $RunnerPlayers = implode(',',$RunnerArray->Users);
  1649.  
  1650. $RunnerData = array('SenderId' => $senderId, 'RoomId' => $RoomId, 'Result' => 'R', 'Players' => $RunnerPlayers, 'Score' => $RunnerScore, 'CreatedAt' => $createdAt);
  1651.  
  1652. $allPlayers = array_merge($WinnerArray->Users,$RunnerArray->Users);
  1653.  
  1654. $total_rounds = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.TeamUsers' => $WinPlayers ), 'recursive' => -1));
  1655.  
  1656. $getRoundDatawinnner = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.Result' => 'W', 'RoundScore.TeamUsers' => $WinPlayers ), 'recursive' => -1));
  1657. $whouseVal = array();
  1658. foreach($getRoundDatawinnner as $key => $val){
  1659. $roundScore = $val['RoundScore']['Score'];
  1660. if($roundScore == 14){
  1661. $whouseVal[] = $roundScore ;
  1662. }else{
  1663. $whouseVal[] = '';
  1664. }
  1665. }
  1666.  
  1667. $WinnerFullHouse = count(array_filter($whouseVal));
  1668.  
  1669. $getRoundDatarunner = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.Result' => 'R', 'RoundScore.TeamUsers' => $RunnerPlayers ), 'recursive' => -1));
  1670.  
  1671. $rhouseVal = array();
  1672. foreach($getRoundDatarunner as $key => $val){
  1673. $roundScore = $val['RoundScore']['Score'];
  1674. if($roundScore == 14){
  1675. $rhouseVal[] = $roundScore ;
  1676. }else{
  1677. $rhouseVal[] = '';
  1678. }
  1679. }
  1680. $RunnerFullHouse = count(array_filter($rhouseVal));
  1681.  
  1682. //Save Data For Winnner
  1683. $newplayerdata = array();
  1684. $UpdateWinArray = '';
  1685.  
  1686. foreach($WinnerArray->Users as $key => $val) {
  1687. $playerid = $val;
  1688.  
  1689. $currentdate =date('Y-m-d');
  1690. $dateexplode = explode('-',$currentdate);
  1691. $currentmonth = $dateexplode['1'];
  1692.  
  1693. $playDateData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Date(CreatedOn)' => $currentdate)));
  1694.  
  1695. if(!empty($playDateData)) {
  1696. $noOfgaemesplayedtoday = count($playDateData);
  1697. }else{
  1698. $noOfgaemesplayedtoday = 1;
  1699. }
  1700.  
  1701. $playMonthData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Month(CreatedOn)' => $currentmonth)));
  1702.  
  1703. if(!empty($playMonthData)) {
  1704. $noOfGameplaythismonth = count($playMonthData);
  1705. }else{
  1706. $noOfGameplaythismonth = 1;
  1707. }
  1708.  
  1709. $playWeekData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','YEARWEEK(CreatedOn)=YEARWEEK(CURDATE(), 1)')));
  1710.  
  1711. if(!empty($playWeekData)){
  1712. $noOfGameplayedthisWeek = count($playWeekData);
  1713. }else{
  1714. $noOfGameplayedthisWeek = 1;
  1715. }
  1716.  
  1717. $userGameplayData = $this->UserPlayGame->find('all',array('conditions' => array('UserPlayGame.user_id' => $playerid)));
  1718.  
  1719. if(empty($userGameplayData)){
  1720. $total_game = 1;
  1721. $user_id = $playerid;
  1722. $win_game = 1;
  1723. $lost_game = 0;
  1724. $win_percent = 100;
  1725.  
  1726.  
  1727. if($WinnerScore == 75)
  1728. $Ace = 1;
  1729. else
  1730. $Ace = 0;
  1731.  
  1732. if(count($total_rounds) == 5)
  1733. $Unstoppable = 1;
  1734. else
  1735. $Unstoppable = 0;
  1736.  
  1737. $Winner = 1;
  1738. $player = 1;
  1739. $WfullHouse = $WinnerFullHouse;
  1740. $WinStreak = 1;
  1741.  
  1742. $newplayerdata[] = array('WinStreak' => $WinStreak,'FullHouse' => $WfullHouse,'Player' => $player,'Winner' => $Winner,'Unstoppable' => $Unstoppable,'Weeklygameplay' => $noOfGameplayedthisWeek,'Monthlygameplay' => $noOfGameplaythismonth,'PerdayGameplayed' => $noOfgaemesplayedtoday,'win_percent' => $win_percent,'lost_game' => $lost_game,'win_game' => $win_game,'user_id' => $user_id,'total_game' => $total_game,'Ace' => $Ace ,'fairPlay' => 0);
  1743.  
  1744. }else{
  1745. $total_game = $userGameplayData[0]['UserPlayGame']['total_game'];
  1746. $user_id = $playerid;
  1747. $win_game = $userGameplayData[0]['UserPlayGame']['win_game'];
  1748. $lost_game = $userGameplayData[0]['UserPlayGame']['lost_game'];
  1749. $oldAce = $userGameplayData[0]['UserPlayGame']['Ace'];
  1750. if($WinnerScore == 75){
  1751. $newAce = $oldAce + 1;
  1752. }else{
  1753. $newAce = $oldAce ;
  1754. }
  1755.  
  1756. $oldWinnnerVal = $userGameplayData[0]['UserPlayGame']['Winner'];
  1757. $newWinnerVal = $oldWinnnerVal + 1;
  1758.  
  1759. $oldPalyerVal = $userGameplayData[0]['UserPlayGame']['Player'];
  1760. $newPlayerval = $oldPalyerVal + 1;
  1761.  
  1762. $oldUnstoppable = $userGameplayData[0]['UserPlayGame']['Unstoppable'];
  1763.  
  1764. if(count($total_rounds) == 5){
  1765. $Unstoppable = $oldUnstoppable + 1;
  1766. }else{
  1767. $Unstoppable = $oldUnstoppable;
  1768. }
  1769.  
  1770. $oldwinfullhouse = $userGameplayData[0]['UserPlayGame']['FullHouse'];
  1771. $WfullHouse = $WinnerFullHouse + $oldwinfullhouse;
  1772.  
  1773. $oldWinstreak = $userGameplayData[0]['UserPlayGame']['WinStreak'];
  1774. $newWinstreak = $oldWinstreak + 1;
  1775.  
  1776. $divisiblebyten = 0;
  1777. $divisiblebyfive = 0;
  1778. $divisiblebythree = 0;
  1779. if($newWinstreak % 10 == 0) {
  1780. $divisiblebyten = 1 ;
  1781. }
  1782.  
  1783. if ($newWinstreak % 5 == 0) {
  1784. $divisiblebyfive = 1 ;
  1785. }
  1786.  
  1787. if ($newWinstreak % 3 == 0) {
  1788. $divisiblebythree = 1 ;
  1789. }
  1790.  
  1791. $oldXtenWinstreak = $userGameplayData[0]['UserPlayGame']['XtenWinstreak'];
  1792. $oldXfiveWinstreak = $userGameplayData[0]['UserPlayGame']['XfiveWinstreak'];
  1793. $oldXthreeWinStreak = $userGameplayData[0]['UserPlayGame']['XthreeWinStreak'];
  1794.  
  1795. if($divisiblebyten == 1){
  1796. $newXtenWinstreak = $oldXtenWinstreak + 1;
  1797. }else{
  1798. $newXtenWinstreak = $oldXtenWinstreak ;
  1799. }
  1800.  
  1801. if($divisiblebyfive == 1){
  1802. $newXfiveWinstreak = $oldXfiveWinstreak + 1;
  1803. }else{
  1804. $newXfiveWinstreak = $oldXfiveWinstreak;
  1805. }
  1806. if($divisiblebythree == 1){
  1807. $newXthreeWinStreak = $oldXthreeWinStreak + 1;
  1808. }else{
  1809. $newXthreeWinStreak = $oldXthreeWinStreak ;
  1810. }
  1811.  
  1812.  
  1813. $newtotal_game = $total_game + 1;
  1814. $newin_game = $win_game + 1;
  1815. $newwinpercentage = ($newin_game/$newtotal_game) * 100 ;
  1816. $newpercentage = round($newwinpercentage);
  1817. $created_date = strtotime(date('Y-m-d H:i:s'));
  1818.  
  1819. $oldfairplay = $userGameplayData[0]['UserPlayGame']['fairPlay'];
  1820. if($oldfairplay>0)
  1821. $newfirplay = $oldfairplay-1;
  1822. else
  1823. $newfirplay=0;
  1824.  
  1825. $UpdateWinArray .= $this->UserPlayGame->updateAll(array('UserPlayGame.XtenWinstreak' => $newXtenWinstreak,'UserPlayGame.XfiveWinstreak' => $newXfiveWinstreak,'UserPlayGame.XthreeWinStreak' => $newXthreeWinStreak,'UserPlayGame.WinStreak' => $newWinstreak,'UserPlayGame.FullHouse' => $WfullHouse,'UserPlayGame.Player' => $newPlayerval,'UserPlayGame.Winner' => $newWinnerVal,'UserPlayGame.Unstoppable' => $Unstoppable,'UserPlayGame.Weeklygameplay' => $noOfGameplayedthisWeek, 'UserPlayGame.Monthlygameplay'=> $noOfGameplaythismonth,'UserPlayGame.PerdayGameplayed' => $noOfgaemesplayedtoday,'UserPlayGame.win_percent' => $newpercentage,'UserPlayGame.win_game' => $newin_game,'UserPlayGame.total_game' => $newtotal_game, 'UserPlayGame.Ace'=> $newAce, 'UserPlayGame.fairPlay' => $newfirplay ),array('UserPlayGame.user_id'=> $user_id));
  1826.  
  1827. }
  1828. }
  1829. $this->UserPlayGame->saveAll($newplayerdata);
  1830.  
  1831.  
  1832. //Save Data For Runner
  1833. $newRunnerplayerdata = array();
  1834. $UpdateRunnerArray = '';
  1835. foreach($RunnerArray->Users as $rkey => $rval) {
  1836. $playerid = $rval;
  1837.  
  1838. //if user is other than old user of roomslot then looser data will not be saved
  1839. $checknewjoinee = $this->RoomSlot->find('first',array('conditions' => array('RoomSlot.RoomId' => $RoomId,'RoomSlot.UserId' => $playerid ),'recursive' => -1));
  1840. if(!empty($checknewjoinee)){
  1841. $olduser = $checknewjoinee['RoomSlot']['OldSlotUser'];
  1842. }else{
  1843. $olduser = 0;
  1844. }
  1845.  
  1846. if($olduser == 0){
  1847.  
  1848. $currentdate =date('Y-m-d');
  1849. $dateexplode = explode('-',$currentdate);
  1850. $currentmonth = $dateexplode['1'];
  1851.  
  1852.  
  1853. $playDateData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Date(CreatedOn)' => $currentdate)));
  1854.  
  1855. if(!empty($playDateData)) {
  1856. $noOfgaemesplayedtoday = count($playDateData);
  1857. }else{
  1858. $noOfgaemesplayedtoday = 1;
  1859. }
  1860.  
  1861. $playMonthData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Month(CreatedOn)' => $currentmonth)));
  1862.  
  1863. if(!empty($playMonthData)) {
  1864. $noOfGameplaythismonth = count($playMonthData);
  1865. }else{
  1866. $noOfGameplaythismonth = 1;
  1867. }
  1868.  
  1869. $playWeekData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','YEARWEEK(CreatedOn)=YEARWEEK(CURDATE(), 1)')));
  1870.  
  1871. if(!empty($playWeekData)){
  1872. $noOfGameplayedthisWeek = count($playWeekData);
  1873. }else{
  1874. $noOfGameplayedthisWeek = 1;
  1875. }
  1876.  
  1877. $userGameplayData = $this->UserPlayGame->find('all',array('conditions' => array('UserPlayGame.user_id' => $playerid)));
  1878. if(empty($userGameplayData)){
  1879. $total_game = 1;
  1880. $user_id = $playerid;
  1881. $win_game = 0;
  1882. $lost_game = 1;
  1883. $win_percent = 0;
  1884.  
  1885.  
  1886.  
  1887. $player = 1;
  1888. if($RunnerScore < 0){
  1889. $Looser = 1;
  1890. }else{
  1891. $Looser = 0;
  1892. }
  1893.  
  1894. $RfullHouse = $RunnerFullHouse;
  1895. $Winstreak = 0 ;
  1896. $newRunnerplayerdata[] = array('WinStreak' => $Winstreak,'FullHouse' => $RfullHouse,'Looser' => $Looser,'Player' => $player,'Weeklygameplay' => $noOfGameplayedthisWeek,'Monthlygameplay' => $noOfGameplaythismonth,'PerdayGameplayed' => $noOfgaemesplayedtoday,'win_percent' => $win_percent,'lost_game' => $lost_game,'win_game' => $win_game,'user_id' => $user_id,'total_game' => $total_game, 'fairPlay' => 0);
  1897.  
  1898. }else{
  1899. $total_game = $userGameplayData[0]['UserPlayGame']['total_game'];
  1900. $user_id = $playerid;
  1901. $win_game = $userGameplayData[0]['UserPlayGame']['win_game'];
  1902. $lost_game = $userGameplayData[0]['UserPlayGame']['lost_game'];
  1903. $oldunstoppable = $userGameplayData[0]['UserPlayGame']['Unstoppable'];
  1904.  
  1905.  
  1906. $oldPalyerVal = $userGameplayData[0]['UserPlayGame']['Player'];
  1907. $newPlayerval = $oldPalyerVal + 1;
  1908.  
  1909. if($RunnerScore < 0){
  1910. $oldLooserVal = $userGameplayData[0]['UserPlayGame']['Looser'];
  1911. $newLooserVal = $oldLooserVal + 1;
  1912. }else{
  1913. $oldLooserVal = $userGameplayData[0]['UserPlayGame']['Looser'];
  1914. $newLooserVal = $oldLooserVal;
  1915. }
  1916.  
  1917. $oldLooserfullhouse = $userGameplayData[0]['UserPlayGame']['FullHouse'];
  1918. $RfullHouse = $RunnerFullHouse + $oldLooserfullhouse;
  1919. $Winstreak = 0 ;
  1920.  
  1921. $newtotal_game = $total_game + 1;
  1922. $newin_game = $win_game;
  1923. $newlost_game = $lost_game + 1;
  1924. $newwinpercentage = ($newin_game/$newtotal_game) * 100 ;
  1925. $newpercentage = round($newwinpercentage);
  1926. $created_date = strtotime(date('Y-m-d H:i:s'));
  1927.  
  1928. $oldfairplay = $userGameplayData[0]['UserPlayGame']['fairPlay'];
  1929. if($oldfairplay>0)
  1930. $newfirplay = $oldfairplay-1;
  1931. else
  1932. $newfirplay=0;
  1933.  
  1934. $UpdateRunnerArray .= $this->UserPlayGame->updateAll(array('UserPlayGame.WinStreak' =>$Winstreak,'UserPlayGame.FullHouse' => $RfullHouse,'UserPlayGame.Looser' => $newLooserVal,'UserPlayGame.Player' => $newPlayerval,'UserPlayGame.Weeklygameplay' => $noOfGameplayedthisWeek,'UserPlayGame.Monthlygameplay' => $noOfGameplaythismonth,'UserPlayGame.PerdayGameplayed' => $noOfgaemesplayedtoday,'UserPlayGame.win_percent' => $newpercentage,'UserPlayGame.lost_game' => $newlost_game,'UserPlayGame.total_game' => $newtotal_game, 'UserPlayGame.fairPlay' => $newfirplay ),array('UserPlayGame.user_id'=> $user_id));
  1935.  
  1936. }
  1937. }else{
  1938. $newRunnerplayerdata = array();
  1939.  
  1940. }
  1941. }
  1942. $this->UserPlayGame->saveAll($newRunnerplayerdata);
  1943.  
  1944.  
  1945. $GameFinalData = array($WinnerData,$RunnerData);
  1946. $saveData = $this->FinalScore->saveAll($GameFinalData);
  1947.  
  1948. //***************update room status*****************//
  1949. $updateRoomStatus = $this->Room->updateAll(array('Room.room_status' => 2, 'Room.join_member_count' => 4),array('Room.id' => $RoomId));
  1950.  
  1951. //update below line on 10/6/2016 RoomSlot.play_status means game is played and score is submitted
  1952. $updateSlotplaystatus = $this->RoomSlot->updateAll(array('RoomSlot.play_status' => 1),array('RoomSlot.RoomId' => $RoomId));
  1953.  
  1954. $this->set(array('Response' => array('Error' => false, 'Message' => "Success"), '_serialize' => array('Response')));
  1955. }
  1956.  
  1957. }
  1958.  
  1959. }else{
  1960. $erroMsg = "RoomData parameter is empty";
  1961. $message = array('Error'=>true,'Message'=>$erroMsg);
  1962. $this->set(array('Response' => $message,'_serialize' => array('Response')));
  1963. }
  1964. }
  1965.  
  1966. public function resetGamePlay($userId,$roomId,$fairPlayStatus) {
  1967.  
  1968. $userplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $userId)));
  1969. $check_replacement = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' => $roomId,'RoomSlot.UserId'=>$userId,'RoomSlot.OldSlotUser'=>0)) );
  1970.  
  1971. $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
  1972. $roomstatus = $roomdata['Room']['room_status'];
  1973. $ongoingstatus = $roomdata['Room']['OngoingStatus'];
  1974.  
  1975. if(!empty($userplaydata)){
  1976.  
  1977. $totalGame = $userplaydata['UserPlayGame']['total_game'];
  1978. $winGame = $userplaydata['UserPlayGame']['win_game'];
  1979. $lostGame = $userplaydata['UserPlayGame']['lost_game'];
  1980. $winpercent = $userplaydata['UserPlayGame']['win_percent'];
  1981. $perdayGameplayed = $userplaydata['UserPlayGame']['PerdayGameplayed'];
  1982. $Monthlygameplay = $userplaydata['UserPlayGame']['Monthlygameplay'];
  1983. $Weeklygameplay = $userplaydata['UserPlayGame']['Weeklygameplay'];
  1984. $fairPlay = $userplaydata['UserPlayGame']['fairPlay'];
  1985. $unixtime = $userplaydata['UserPlayGame']['created_date'];
  1986. $currentdate =date('Y-m-d');
  1987. $dateexplode = explode('-',$currentdate);
  1988. $currentmonth = $dateexplode['1'];
  1989.  
  1990. $newtotal_game = $totalGame + 1;
  1991. $newloss_game = $lostGame + 1;
  1992. $newwinpercentage = ($winGame/$newtotal_game) * 100 ;
  1993. $newpercentage = round($newwinpercentage);
  1994. $created_date = strtotime(date('Y-m-d H:i:s'));
  1995.  
  1996. if($fairPlayStatus == 1 && $fairPlay < 10){
  1997. $newFailplayval = $fairPlay + 1;
  1998. }else
  1999. $newFailplayval = $fairPlay;
  2000.  
  2001. if((!empty($check_replacement)) && $ongoingstatus==1 && $roomstatus!=2 && $fairPlayStatus==1){
  2002. $UpdateWinArray = $this->UserPlayGame->updateAll(array('UserPlayGame.win_percent' => $newpercentage,'UserPlayGame.lost_game' => $newloss_game,'UserPlayGame.total_game' => $newtotal_game,'UserPlayGame.fairPlay' => $newFailplayval ),array('UserPlayGame.user_id'=> $userId));
  2003. }
  2004.  
  2005. } else{
  2006.  
  2007. if((!empty($check_replacement)) && $ongoingstatus==1 && $roomstatus!=2 && $fairPlayStatus==1){
  2008. $newUserdata = array('user_id' => $userId, 'fairPlay' => 1,'lost_game'=>1,'total_game'=>1);
  2009. $this->UserPlayGame->saveAll($newUserdata);
  2010. }else{
  2011. $newUserdata = array('user_id' => $userId, 'fairPlay' => 0);
  2012. $this->UserPlayGame->saveAll($newUserdata);
  2013. }
  2014. }
  2015. }
  2016.  
  2017.  
  2018. public function leaveGame() {
  2019. $error = array();
  2020. if(empty($this->request->query['RoomId'])){
  2021. $error[] = "RoomId parameter is empty" ;
  2022. }
  2023. if(empty($this->request->query['UserId'])){
  2024. $error[] = "UserId parameter is empty" ;
  2025. }
  2026. if($this->request->query['FairPlayStatus']== ''){
  2027. $error[] = "FairPlayStatus parameter is empty" ;
  2028. }
  2029. if(!empty($error)){
  2030. $errorMsg = implode(',',$error);
  2031. $response = array('Error' => True, 'Message' => $errorMsg);
  2032. $this->set(array('Response' => $response, '_serialize' => array('Response')));
  2033. }else{
  2034.  
  2035. $userId = $this->request->query['UserId'];
  2036. $roomId = $this->request->query['RoomId'];
  2037.  
  2038. //if user exists in any rematch update it as rejected>>>>>Start
  2039.  
  2040. $getrematchdata = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $userId,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
  2041. if(!empty($getrematchdata)){
  2042. $this->RequestRematch->updateAll(array('RequestRematch.status' => 2),array('RequestRematch.user_id' =>$userId,'RequestRematch.room_id' => $roomId));
  2043. }
  2044.  
  2045. //if user exists in any rematch update it as rejected>>>>>end
  2046.  
  2047. $fairPlayStatus = $this->request->query['FairPlayStatus'];
  2048. //check if RoomSlot exists
  2049. $roomslotexists = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' => $roomId)) );
  2050.  
  2051. $this->resetGamePlay($userId,$roomId,$fairPlayStatus);
  2052.  
  2053. if(empty($roomslotexists)) {
  2054.  
  2055. $this->set(array('Response' => array('Error' => false , 'Message' => 'No Roomslot exists for this Room' ), '_serialize' => array('Response')));
  2056. }else{
  2057.  
  2058. //check if user is host
  2059.  
  2060. $checkUser = $this->Room->find('all',array('conditions' => array('UserId' => $userId ,'id' => $roomId)));
  2061. $thisroomsdata = $this->Room->find('first' , array('conditions' => array('id' => $roomId), 'recursive' => -1));
  2062. $roomjoinnumbers = $thisroomsdata['Room']['join_member_count'];
  2063.  
  2064. if(empty($checkUser)){
  2065. //Given UserId is not host of the room So we have to remove the other user from slot
  2066.  
  2067. $checkleavingUser = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $userId, 'RoomSlot.RoomId' => $roomId)));
  2068.  
  2069. if(!empty($checkleavingUser)){
  2070.  
  2071. if($checkleavingUser['RoomSlot']['slot_type'] == 'public'){
  2072. $this->RoomSlot->updateAll(array('RoomSlot.UserId' =>null,'RoomSlot.slot_type'=>"'public'",'RoomSlot.SlotStatus' => "0",'RoomSlot.OldSlotUser' => $userId),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
  2073. }else{
  2074. $this->RoomSlot->updateAll(array('RoomSlot.UserId' =>null,'RoomSlot.slot_type'=>"'public'",'RoomSlot.SlotStatus' => "0",'RoomSlot.OldSlotUser' => $userId,'RoomSlot.SlotKey' => 0),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
  2075. }
  2076. $newjoinmembercount = $roomjoinnumbers - 1;
  2077.  
  2078. $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
  2079. $roomstatus = $roomdata['Room']['room_status'];
  2080.  
  2081.  
  2082. if($roomstatus != 2){
  2083. $autoterminateTime = date('Y-m-d H:i:s');
  2084. if($roomjoinnumbers==4)
  2085. $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId));
  2086.  
  2087. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
  2088.  
  2089. }else{
  2090.  
  2091. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
  2092. }
  2093.  
  2094.  
  2095.  
  2096. $userplaydata = $this->UserPlayGame->find('first',array('conditions' => array('UserPlayGame.user_id'=> $userId),'fields' => array('fairPlay')));
  2097.  
  2098. if(!empty($userplaydata)){
  2099. $oldFairplayval = $userplaydata['UserPlayGame']['fairPlay'];
  2100. if($fairPlayStatus == 1 && $oldFairplayval < 10){
  2101. $newFailplayval = $oldFairplayval + 1;
  2102. }else{
  2103. $newFailplayval = $oldFairplayval;
  2104. }
  2105.  
  2106. $this->UserPlayGame->updateAll(array('UserPlayGame.fairPlay' => $newFailplayval),array('UserPlayGame.user_id'=> $userId));
  2107. }else{
  2108. //new user to be added here
  2109.  
  2110. $newUesrData = array('user_id' => $userId, 'fairPlay' => 1);
  2111. $this->UserPlayGame->saveAll($newUesrData );
  2112. }
  2113.  
  2114. //to check if all the slots are empty in that case we have to delete the room and its slots
  2115. // get all slots
  2116. $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
  2117.  
  2118. if(empty($getallRoomSlotdata)) {
  2119. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2120. $this->Room->deleteAll(array('Room.id' => $roomId));
  2121. }
  2122.  
  2123. //$this->Room->find('first', array('conditions' => array('Room')));
  2124. $this->set(array('Response'=>array('Error' => false, 'Message' => "Success"), '_serialize' => array('Response')));
  2125. }else{
  2126.  
  2127. $this->set(array('Response' => array('Error' => false , 'Message' => 'This RoomSlot Does Not Match our records'), '_serialize' => array('Response') ));
  2128. }
  2129. }else{
  2130. $this->RoomSlot->updateAll(array("RoomSlot.UserId" => null,"RoomSlot.SlotStatus" => "0","RoomSlot.slot_type"=>"'public'" ,'RoomSlot.OldSlotUser' => $userId,'RoomSlot.SlotKey' => 0),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
  2131. $userplaydata = $this->UserPlayGame->find('first',array('conditions' => array('UserPlayGame.user_id'=> $userId),'fields' => array('fairPlay')));
  2132.  
  2133. $newjoinmembercount = $roomjoinnumbers - 1;
  2134. $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
  2135. $roomstatus = $roomdata['Room']['room_status'];
  2136.  
  2137. if($roomstatus != 2){
  2138.  
  2139. $autoterminateTime = date('Y-m-d H:i:s');
  2140. if($roomjoinnumbers==4)
  2141. $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId));
  2142.  
  2143. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
  2144.  
  2145. }else{
  2146. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
  2147. }
  2148.  
  2149.  
  2150.  
  2151. if(!empty($userplaydata)){
  2152. $oldFairplayval = $userplaydata['UserPlayGame']['fairPlay'];
  2153.  
  2154. if($fairPlayStatus == 1 && $oldFairplayval < 10){
  2155. $newFailplayval = $oldFairplayval + 1;
  2156. }else{
  2157. $newFailplayval = $oldFairplayval;
  2158. }
  2159.  
  2160.  
  2161. $this->UserPlayGame->updateAll(array('UserPlayGame.fairPlay' => $newFailplayval),array('UserPlayGame.user_id'=> $userId));
  2162. }else{
  2163.  
  2164.  
  2165. $newUesrData = array('user_id' => $userId, 'fairPlay' => 0);
  2166. $this->UserPlayGame->saveAll($newUesrData );
  2167.  
  2168. }
  2169. $getRoomSlotdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' =>$roomId,'RoomSlot.SlotStatus' => 1 ),'order' =>array('RoomSlot.created_date ASC'),'recursive' => -1));
  2170.  
  2171. if(!empty($getRoomSlotdata)){
  2172. $newHostid = $getRoomSlotdata['RoomSlot']['UserId'];
  2173. $username = $this->User->find('first' , array('conditions' => array('User.id' => $newHostid), 'fields' => array('username'), 'recursive' => -1));
  2174. $roomusername = $username['User']['username'];
  2175.  
  2176. $this->Room->updateAll(array('Room.UserId' => $newHostid,"Room.name" => '"'.$roomusername.'"', 'Room.HostName' => '"'.$roomusername.'"'),array('Room.id' => $roomId));
  2177. }
  2178.  
  2179. //to check if all the slots are empty in that case we have to delete the room and its slots
  2180. // get all slots
  2181. $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
  2182.  
  2183. if(empty($getallRoomSlotdata)) {
  2184. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2185. $this->Room->deleteAll(array('Room.id' => $roomId));
  2186. }
  2187.  
  2188. $this->set(array('Response' => array('Error' => false , 'Message' => 'The host left the room'), '_serialize' => array('Response')));
  2189. }
  2190. }
  2191. }
  2192. }
  2193.  
  2194.  
  2195. public function saveChatHistory() {
  2196. $error = array();
  2197.  
  2198. if(empty($_POST['RoomId'])){
  2199. $error[] = "RoomId parameter is empty";
  2200. }
  2201.  
  2202. if(empty($_POST['UserId'])){
  2203. $error[] = "UserId parameter is empty";
  2204. }
  2205.  
  2206. if(empty($_POST['Message']) && $_POST['Message']!=0){
  2207. $error[] = "Message parameter is empty";
  2208. }
  2209.  
  2210. if(empty($_POST['Type'])){
  2211. $error[] = "Type parameter is empty";
  2212. }
  2213.  
  2214. if(!empty($error)){
  2215. $errorMsg = implode(',',$error);
  2216. $response = array('Error' => True, 'Message' => $errorMsg);
  2217. $this->set(array('Response' => $response, '_serialize' => array('Response')));
  2218.  
  2219. }else{
  2220.  
  2221. $roomId = $_POST['RoomId'];
  2222. $userId = $_POST['UserId'];
  2223. $Message = $_POST['Message'];
  2224. $Type = $_POST['Type'];
  2225.  
  2226. if($Type == 'E') {
  2227. if(empty($Message)){
  2228. $Emoerror = "EmojiId parameter is empty";
  2229. $this->set(array('Response' => $Emoerror,'_serialize' => array('Response')));
  2230. }else{
  2231. $Message = $Message;
  2232. $data = array('UserId' => $userId, 'RoomId' => $roomId, 'ChatMessage' => $Message , 'Type' => 'E');
  2233. $this->ChatInfo->save($data);
  2234.  
  2235. // chatid in response
  2236. $chatid = $this->ChatInfo->getLastInsertId();
  2237. $this->set(array('Response' => array('Error' => false, 'ChatId' => $chatid, 'Message' => 'Success'),'_serialize' => array('Response')));
  2238. }
  2239.  
  2240. }else{
  2241. $data = array('UserId' => $userId, 'RoomId' => $roomId, 'ChatMessage' => $Message , 'Type' => 'C');
  2242. $this->ChatInfo->save($data);
  2243. $chatid = $this->ChatInfo->getLastInsertId();
  2244. $this->set(array('Response' => array('Error' => false,'ChatId' => $chatid, 'Message' => 'Success'),'_serialize' => array('Response')));
  2245. }
  2246. }
  2247.  
  2248. }
  2249.  
  2250. /***
  2251. Accept status 0: for declined, 1 for Accept
  2252. send null instead of 0
  2253. */
  2254.  
  2255. public function InviteResponse() {
  2256. $error = array();
  2257. if(empty($this->request->query['RoomId'])){
  2258. $error[] = "RoomId parameter is empty." ;
  2259. }
  2260. if(empty($this->request->query['SlotId'])){
  2261. $error[] = "SlotId parameter is empty.";
  2262. }
  2263. if($this->request->query['Status'] == ""){
  2264. $error[] = "Status parameter is empty";
  2265. }
  2266. if(empty($this->request->query['UserId'])){
  2267. $error[] = "UserId parameter is empty.";
  2268. }
  2269.  
  2270. if(!empty($error)) {
  2271. $errMessage = implode(',',$error);
  2272. $Message = array('Error'=>true, 'Message' => $errMessage);
  2273. $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
  2274. }else{
  2275.  
  2276. $roomid = $this->request->query['RoomId'];
  2277. $slotid = $this->request->query['SlotId'];
  2278. $status = $this->request->query['Status'];
  2279. $UserId = trim($this->request->query['UserId']) ;
  2280. if($status == 1){
  2281. //check if room exists in case it has not been updated or deleted already.
  2282. $Roomdata = $this->Room->find('first',array('conditions'=>array('Room.id' => $roomid),'recursive'=>-1));
  2283.  
  2284. if(!empty($Roomdata)){
  2285.  
  2286. $joinmembers = $Roomdata['Room']['join_member_count'];
  2287. //check if roomslot exists
  2288. if($joinmembers < 4) {
  2289. $slotexists = $this->RoomSlot->find('all', array('conditons' => array('RomSlot.RoomId' => $roomid, 'RoomSlot.SlotId' => $slotid), 'recursive' => -1));
  2290. if(!empty($slotexists)) {
  2291. $slotcheck = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.SlotStatus' =>1,'RoomSlot.RoomId' => $roomid, 'RoomSlot.SlotId'=> $slotid),'recursive' => -1));
  2292. if(empty($slotcheck)){
  2293.  
  2294. $chkUser= $this->RoomSlot->find('first' , array('conditions' => array('RoomSlot.UserId' =>$UserId,'RoomSlot.RoomId' => $roomid,'RoomSlot.SlotStatus' =>1)));
  2295. if(empty($chkUser)){
  2296.  
  2297.  
  2298. $chkUserInRooms = $this->RoomSlot->find('first' , array('conditions' => array('RoomSlot.UserId' =>$UserId,'RoomSlot.SlotStatus' =>1)));
  2299. if(empty($chkUserInRooms)){
  2300. $this->RoomSlot->updateAll(array('RoomSlot.SlotStatus' => 1,'RoomSlot.UserId'=>$UserId),array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid));
  2301. $joinmenbercount = $joinmembers + 1 ;
  2302. $this->Room->updateAll(array('Room.join_member_count' => $joinmenbercount ), array('Room.id' => $roomid));
  2303.  
  2304. $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
  2305. }else{
  2306. $this->set(array('Response' => array('Error' => true , 'Message' => 'This user is already assigned to other room'), '_serialize' => array('Response')));
  2307. }
  2308. }else{
  2309. $this->set(array('Response' => array('Error' => true , 'Message' => 'This user is already assigned'), '_serialize' => array('Response')));
  2310.  
  2311. }
  2312. }else{
  2313. $this->set(array('Response' => array('Error' => true , 'Message' => 'This slot is already assigned'), '_serialize' => array('Response')));
  2314. }
  2315.  
  2316. }else{
  2317. $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot does not exists'),'_serialize'=> array('Response'))) ;
  2318. }
  2319. }else{
  2320.  
  2321. $this->set(array('Response' => array('Error' => true , 'Message' => 'All slots are already filled'), '_serialize' => array('Response'))) ;
  2322. }
  2323. }
  2324. else{
  2325.  
  2326. $this->set(array('Response' => array('Error' => true, 'Message' => 'Room Does not exist.'), '_serialize'=> array('Response')));
  2327. }
  2328.  
  2329. }else{
  2330.  
  2331. $Roomdata = $this->Room->find('first',array('conditions'=>array('Room.id' => $roomid),'recursive'=>-1));
  2332. if(!empty($Roomdata)){
  2333.  
  2334. $slotexists = $this->RoomSlot->find('all', array('conditons' => array('RomSlot.RoomId' => $roomid, 'RoomSlot.SlotId' => $slotid), 'recursive' => -1));
  2335. if(!empty($slotexists)) {
  2336. $public = 'public';
  2337.  
  2338. $slotstatus = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid,'RoomSlot.SlotStatus' => 1)));
  2339. if(empty($slotstatus)){
  2340. $this->RoomSlot->updateAll(array("RoomSlot.slot_type" =>'"'.$public.'"',"RoomSlot.UserId"=>'0',"RoomSlot.SlotKey" => 0),array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid));
  2341. $this->set(array('Response' => array('Error' => false , 'Message' => 'RoomSlot Updated Success'),'_serialize'=> array('Response'))) ;
  2342. }else{
  2343. $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot is not available'),'_serialize'=> array('Response'))) ;
  2344. }
  2345. }else{
  2346. $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot does not exists'),'_serialize'=> array('Response'))) ;
  2347. }
  2348.  
  2349. }else{
  2350. $this->set(array('Response' => array('Error' => true, 'Message' => 'Room Does not exist.'), '_serialize'=> array('Response')));
  2351. }
  2352. }
  2353. }
  2354. }
  2355.  
  2356.  
  2357.  
  2358. public function removidleuser($userId){
  2359.  
  2360. $getuSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$userId,'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus','RoomSlot.SlotId'),'recursive'=>-1));
  2361.  
  2362. if(!empty($getuSlotData)){
  2363.  
  2364. $roomId = $getuSlotData['RoomSlot']['RoomId'];
  2365.  
  2366. //remove user from rematch
  2367. $getrematchdata = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $userId,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
  2368. if(!empty($getrematchdata)){
  2369.  
  2370. $this->RequestRematch->updateAll(array('RequestRematch.status' => 2),array('RequestRematch.user_id' =>$userId,'RequestRematch.room_id' => $uroomid));
  2371. //removed user from rematch
  2372. }
  2373.  
  2374. $checkUser = $this->Room->find('all',array('conditions' => array('UserId' => $userId ,'id' => $roomId)));
  2375. $thisroomsdata = $this->Room->find('first' , array('conditions' => array('id' => $roomId), 'recursive' => -1));
  2376. $roomjoinnumbers = $thisroomsdata['Room']['join_member_count'];
  2377.  
  2378. if(empty($checkUser)){
  2379. //Given UserId is not host of the room So we have to remove the other user from slot
  2380.  
  2381. $checkleavingUser = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $userId, 'RoomSlot.RoomId' => $roomId)));
  2382.  
  2383. if(!empty($checkleavingUser)){
  2384.  
  2385. $this->RoomSlot->updateAll(array('RoomSlot.UserId' =>null,'RoomSlot.slot_type'=>"'public'",'RoomSlot.SlotStatus' => "0",'RoomSlot.OldSlotUser' => $userId,'RoomSlot.SlotKey' => 0),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
  2386.  
  2387. $newjoinmembercount = $roomjoinnumbers - 1;
  2388. $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
  2389. $roomstatus = $roomdata['Room']['room_status'];
  2390.  
  2391. if($roomstatus != 2){
  2392. $autoterminateTime = date('Y-m-d H:i:s');
  2393. $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId,'Room.join_member_count'=>4));
  2394.  
  2395. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
  2396. }else{
  2397. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
  2398. }
  2399.  
  2400. $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
  2401.  
  2402. if(empty($getallRoomSlotdata)) {
  2403. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2404. $this->Room->deleteAll(array('Room.id' => $roomId));
  2405. }
  2406.  
  2407. }
  2408.  
  2409. //get room slot count
  2410.  
  2411. }else{
  2412.  
  2413. $this->RoomSlot->updateAll(array("RoomSlot.UserId" => null,"RoomSlot.SlotStatus" => "0","RoomSlot.slot_type"=>"'public'" ,'RoomSlot.OldSlotUser' => $userId,'RoomSlot.SlotKey' => 0),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
  2414.  
  2415. $newjoinmembercount = $roomjoinnumbers - 1;
  2416. $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
  2417. $roomstatus = $roomdata['Room']['room_status'];
  2418.  
  2419. if($roomstatus != 2){
  2420. $autoterminateTime = date('Y-m-d H:i:s');
  2421. $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId,'Room.join_member_count'=>4));
  2422.  
  2423. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
  2424. }else{
  2425. $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
  2426. }
  2427.  
  2428. $getRoomSlotdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' =>$roomId,'RoomSlot.SlotStatus' => 1 ),'order' =>array('RoomSlot.created_date ASC'),'recursive' => -1));
  2429.  
  2430. if(!empty($getRoomSlotdata)){
  2431. $newHostid = $getRoomSlotdata['RoomSlot']['UserId'];
  2432. $username = $this->User->find('first' , array('conditions' => array('User.id' => $newHostid), 'fields' => array('username'), 'recursive' => -1));
  2433. $roomusername = $username['User']['username'];
  2434.  
  2435. $this->Room->updateAll(array('Room.UserId' => $newHostid,"Room.name" => '"'.$roomusername.'"', 'Room.HostName' => '"'.$roomusername.'"'),array('Room.id' => $roomId));
  2436. }
  2437.  
  2438. //to check if all the slots are empty in that case we have to delete the room and its slots
  2439. // get all slots
  2440. $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
  2441.  
  2442. if(empty($getallRoomSlotdata)) {
  2443. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2444. $this->Room->deleteAll(array('Room.id' => $roomId));
  2445. }
  2446. }
  2447. }
  2448. }
  2449.  
  2450.  
  2451. public function getRoomSearch() {
  2452.  
  2453. $onlineusers = $this->User->find('count',array('conditions' => array('User.online_status' => 1)));
  2454. $onlinegames = $this->Room->find('count',array('conditions' => array('Room.OngoingStatus' => 1,'Room.room_status' => 1)));
  2455. if(!empty($onlineusers)){
  2456. $onlineuserslist = $onlineusers;
  2457. }else{
  2458. $onlineuserslist = 0;
  2459. }
  2460.  
  2461. if(!empty($onlinegames)){
  2462. $ongoingameslist = $onlinegames;
  2463. }else{
  2464. $ongoingameslist = 0;
  2465. }
  2466.  
  2467. $searchList = array();
  2468. if(empty($this->request->query['SearchKey'])) {
  2469. $error = array();
  2470. if(empty($this->request->query['UserId'])){
  2471. $error[] = "UserId parameter is empty.";
  2472. }
  2473.  
  2474. if(!empty($error)) {
  2475. $errMessage = implode(',',$error);
  2476. $Message = array('Error'=>true, 'Message' => $errMessage);
  2477. $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
  2478. }else{
  2479.  
  2480. $useridforsearch = $this->request->query['UserId'];
  2481. $this->removidleuser($useridforsearch);
  2482.  
  2483. // $limit = 1000 ;
  2484. $searchList = $this->Room->find( 'all', array(
  2485. 'fields'=>array('Room.name','Room.id','Room.room_status','Room.OngoingStatus'),
  2486. // 'limit' => $limit,
  2487. 'conditions'=>array('Room.room_status' => 0, 'Room.join_member_count !=' => 4 ),
  2488. //'recursive' => -1
  2489. ));
  2490.  
  2491.  
  2492. if(!empty($searchList)){
  2493.  
  2494. $roomData = array();
  2495. $newRoomData = array();
  2496. foreach($searchList as $k => $v) {
  2497. $roomid = $v['Room']['id'];
  2498. $OngoingStatus = $v['Room']['OngoingStatus'];
  2499. $roomSlots = $v['RoomSlot'];
  2500.  
  2501. if(count($roomSlots) == 4) {
  2502.  
  2503. $rslottype = array();
  2504. $rslotstatus = array();
  2505. $oldslotUser = array();
  2506. $currentuser = array();
  2507. foreach($roomSlots as $rkey => $rval){
  2508. $rslottype[] = $rval['slot_type'];
  2509. $rslotstatus[] = $rval['SlotStatus'];
  2510. $oldslotUser[] = $rval['OldSlotUser'];
  2511. $currentuser[] = $rval['UserId'];
  2512. }
  2513.  
  2514. if((in_array($useridforsearch,$currentuser))) {
  2515. $slotData = '';
  2516. }else{
  2517. if((in_array('public',$rslottype)) || (in_array('private',$rslottype))) {
  2518. if(in_array(1,$rslotstatus)){
  2519.  
  2520. $slotData = array();
  2521. $filledslots = array();
  2522.  
  2523. foreach ($roomSlots as $key => $row) {
  2524. $mid[$key] = $row['SlotId'];
  2525. }
  2526. array_multisort($mid, SORT_ASC, $roomSlots);
  2527.  
  2528. foreach($roomSlots as $rk => $rv) {
  2529.  
  2530. $roomUserid = $rv['UserId'];
  2531. $slottype = $rv['slot_type'];
  2532. $slotKey = $rv['SlotKey'];
  2533. $slotId = $rv['SlotId'];
  2534. $slotStatus = $rv['SlotStatus'];
  2535.  
  2536. if($slotStatus == 1){
  2537. $filledslots[] = $rv;
  2538. }
  2539.  
  2540. $userdata = $this->User->find('first', array('conditions' => array('User.id' => $roomUserid), 'fields' => array('User.fbid','User.pic','User.PictureIndex','User.username','User.star')));
  2541. if(!empty($userdata)){
  2542.  
  2543. $userfbid = $userdata['User']['fbid'];
  2544. $uPic = $userdata['User']['pic'];
  2545. $userPic = ($userdata['User']['pic']) ? HTTP_ROOT .'app/webroot/img/members/'.$uPic: $uPic;
  2546. $picindex = $userdata['User']['PictureIndex'];
  2547. $username = $userdata['User']['username'];
  2548. $star = $userdata['User']['star'];
  2549. }else{
  2550.  
  2551. $userfbid = '';
  2552. $userPic = '';
  2553. $picindex = '';
  2554. $username = '';
  2555. $star = '';
  2556. }
  2557. $slotData[] = array('Star' => $star,'UserName' => $username,'UserId' => $roomUserid, 'Type' => $slottype, 'Key' => $slotKey, 'SlotId' => $slotId, 'SlotStatus' => $slotStatus, 'UserFbId' => $userfbid, 'Picture' => $userPic, 'UserPicIndex' => $picindex) ;
  2558. }
  2559. $slotData = $slotData ;
  2560. }else{
  2561. $slotData = '';
  2562. }
  2563. }else{
  2564. $slotData = '';
  2565. }
  2566. }
  2567.  
  2568. if(!empty($slotData)) {
  2569. $slotFilled = count($filledslots);
  2570. $newRoomData[] = array('RoomId' => $roomid, 'SlotData' => $slotData, 'SlotsFilled' => $slotFilled,'OngoingStatus' => $OngoingStatus);
  2571. }else{
  2572. $newRoomData[] = '';
  2573. $slotFilled = 0 ;
  2574. }
  2575.  
  2576. }else{
  2577. $newRoomData[] = '';
  2578. $slotFilled = 0 ;
  2579. }
  2580. }
  2581.  
  2582. $newRoomData = array_filter($newRoomData);
  2583.  
  2584. if(!empty($newRoomData)){
  2585.  
  2586. $newRoomData = array_filter($newRoomData);
  2587. $newRoomData = array_values($newRoomData);
  2588. $newRoomData = array_slice($newRoomData, 0, 10);
  2589.  
  2590. $this->set(array('Response' => array('Error' => false , 'RoomData' => $newRoomData,
  2591. 'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2592. }else{
  2593. $this->set(array('Response' => array('Error' => false , 'Message' => 'No results found','condition' => 'no search key', 'RoomData'=> array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2594. }
  2595. }else{
  2596. $this->set(array('Response' => array('Error' => false , 'Message' => 'No Results found' ,'RoomData'=> array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2597. }
  2598. }
  2599. }else{
  2600. $error = array();
  2601. if(empty($this->request->query['UserId'])){
  2602. $error[] = "UserId parameter is empty.";
  2603. }
  2604.  
  2605. if(!empty($error)) {
  2606. $errMessage = implode(',',$error);
  2607. $Message = array('Error'=>true, 'Message' => $errMessage);
  2608. $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
  2609. }else{
  2610. $useridforsearch = $this->request->query['UserId'];
  2611. $searchname = $this->request->query['SearchKey'];
  2612. // $limit = 1000;
  2613. $searchList = $this->Room->find('all', array(
  2614. 'fields' => array('Room.name','Room.id','Room.room_status','Room.OngoingStatus'),
  2615. // 'limit' => $limit,
  2616. 'conditions'=>array('name LIKE'=>'%' . $searchname . '%','room_status' => 0,'Room.join_member_count !=' => 4)
  2617. //'recursive' => -1
  2618. ));
  2619.  
  2620. if(!empty($searchList)){
  2621. $roomData = array();
  2622. $newRoomData = array();
  2623. foreach($searchList as $k => $v) {
  2624.  
  2625. $roomid = $v['Room']['id'];
  2626. $OngoingStatus = $v['Room']['OngoingStatus'];
  2627. //$roomData['RoomId'] = $roomid ;
  2628.  
  2629. $roomSlots = $v['RoomSlot'];
  2630. if(count($roomSlots) == 4) {
  2631. $rslottype = array();
  2632. $rslotstatus = array();
  2633. $oldslotUser = array();
  2634. $currentuser = array();
  2635.  
  2636. foreach($roomSlots as $rkey => $rval){
  2637. $rslottype[] = $rval['slot_type'];
  2638. $rslotstatus[] = $rval['SlotStatus'];
  2639. $oldslotUser[] = $rval['OldSlotUser'];
  2640. $currentuser[] = $rval['UserId'];
  2641. }
  2642.  
  2643. if((in_array($useridforsearch,$currentuser))) {
  2644. $slotData = '';
  2645.  
  2646. }else{
  2647.  
  2648. if((in_array('public',$rslottype)) || (in_array('private',$rslottype))){
  2649. if(in_array(1,$rslotstatus)){
  2650.  
  2651. $slotData = array();
  2652. $filledslots = array();
  2653.  
  2654. foreach ($roomSlots as $key => $row) {
  2655. $mid[$key] = $row['SlotId'];
  2656. }
  2657. array_multisort($mid, SORT_ASC, $roomSlots);
  2658.  
  2659. foreach($roomSlots as $rk => $rv) {
  2660.  
  2661. $roomUserid = $rv['UserId'];
  2662. $slottype = $rv['slot_type'];
  2663. $slotKey = $rv['SlotKey'];
  2664. $slotId = $rv['SlotId'];
  2665. $slotStatus = $rv['SlotStatus'];
  2666.  
  2667. if($slotStatus == 1){
  2668. $filledslots[] = $rv;
  2669. }
  2670.  
  2671.  
  2672. $userdata = $this->User->find('first', array('conditions' => array('User.id' => $roomUserid), 'fields' => array('User.fbid','User.pic','User.PictureIndex','User.username','User.star')));
  2673. if(!empty($userdata)){
  2674.  
  2675. $userfbid = $userdata['User']['fbid'];
  2676. $uPic = $userdata['User']['pic'];
  2677. $userPic = ($userdata['User']['pic']) ? HTTP_ROOT .'app/webroot/img/members/'.$uPic: $uPic;
  2678. $picindex = $userdata['User']['PictureIndex'];
  2679. $username = $userdata['User']['username'];
  2680. $star = $userdata['User']['star'];
  2681.  
  2682. }else{
  2683.  
  2684. $userfbid = '';
  2685. $userPic = '';
  2686. $picindex = '';
  2687. $username = '';
  2688. $star = '';
  2689. }
  2690. $slotData[] = array('Star' => $star,'UserName' => $username,'UserId' => $roomUserid, 'Type' => $slottype, 'Key' => $slotKey, 'SlotId' => $slotId, 'SlotStatus' => $slotStatus, 'UserFbId' => $userfbid, 'Picture' => $userPic, 'UserPicIndex' => $picindex) ;
  2691. }
  2692. }else{
  2693. $slotData = '';
  2694. }
  2695.  
  2696. }else{
  2697. $slotData = '';
  2698. }
  2699. }
  2700.  
  2701. if(!empty($slotData)) {
  2702. $slotFilled = count($filledslots);
  2703. $newRoomData[] = array('RoomId' => $roomid, 'SlotData' => $slotData, 'SlotsFilled' => $slotFilled,'OngoingStatus' => $OngoingStatus);
  2704. }else{
  2705. $newRoomData[] = '';
  2706. $slotFilled = 0 ;
  2707. }
  2708.  
  2709. }else{
  2710. $newRoomData[] = '';
  2711. $slotFilled = 0 ;
  2712. }
  2713. }
  2714.  
  2715. if(!empty($newRoomData) ){
  2716. $newRoomData = array_filter($newRoomData);
  2717. $newRoomData = array_values($newRoomData);
  2718. $newRoomData = array_slice($newRoomData, 0, 10);
  2719. $this->set(array('Response' => array('Error' => false , 'RoomData' => $newRoomData,'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2720. }else{
  2721. $this->set(array('Response' => array('Error' => false , 'Message' => 'No results found', 'RoomData' => array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2722. }
  2723. }else{
  2724. $this->set(array('Response' => array('Error' => false, 'Message' => 'No Results Found','RoomData' => array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
  2725. }
  2726.  
  2727. }
  2728. }
  2729. }
  2730.  
  2731.  
  2732. public function requestRematch() {
  2733. $error = array();
  2734. if(empty($_POST['RoomId'])){
  2735. $error[] = "RoomId parameter is Empty.";
  2736. }
  2737. if(empty($_POST['UserId'])){
  2738. $error[] = "UserId parameter is Empty";
  2739. }
  2740. if($_POST['Status'] == ""){
  2741. $error[] = "Status parameter is Empty";
  2742. }
  2743. if(!empty($error)){
  2744. $errorMsg = implode(',',$error);
  2745. $response = array('Error' => true, 'Message' => $errorMsg);
  2746. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  2747. }else{
  2748.  
  2749. $roomId = $_POST['RoomId'];
  2750. $userId = $_POST['UserId'];
  2751. $status = $_POST['Status'];
  2752.  
  2753. $selectRoomslot = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' => $roomId), 'recursive' => -1, 'order' => array('RoomSlot.SlotId' => 'asc')));
  2754.  
  2755. $slotuserid = array();
  2756. if(!empty($selectRoomslot)){
  2757. foreach($selectRoomslot as $k=>$v){
  2758. $slotuserid[] = $v['RoomSlot']['UserId'];
  2759. }
  2760.  
  2761. if(in_array(0, $slotuserid)){
  2762. $this->set(array('Response' =>array('Error' => true , 'Message' => "Not Enough Users !"), '_serialize' => array('Response')));
  2763. }else{
  2764. if(in_array($userId, $slotuserid)){
  2765.  
  2766. $slotid=-1;
  2767. $data = array();
  2768. $datastatus = '';
  2769. foreach($slotuserid as $key=>$val){
  2770. $slotuser = $val;
  2771.  
  2772. //first check if data already exists
  2773. $rrematchdata = $this->RequestRematch->find('all', array('conditions' => array('RequestRematch.user_id' => $slotuser, 'RequestRematch.room_id' => $roomId)));
  2774.  
  2775. if(empty($rrematchdata)){
  2776. $slotid++;
  2777.  
  2778. if($slotuser == $userId){
  2779. $status = 1 ;
  2780. }else{
  2781. $status = 0;
  2782. }
  2783.  
  2784. $data[] = array('room_id' => $roomId,'host_id' => $userId ,'user_id' => $slotuser, 'slot_id' => $slotid , 'status' => $status);
  2785. $datastatus = 'true';
  2786. }else{
  2787. $data[] = '';
  2788. $datastatus = 'false';
  2789. }
  2790. }
  2791.  
  2792. if($datastatus == 'true'){
  2793.  
  2794. $this->RequestRematch->saveAll($data);
  2795.  
  2796.  
  2797.  
  2798. $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
  2799.  
  2800. }else{
  2801.  
  2802. if($status == 0){
  2803. $status = 1;
  2804. }else{
  2805. $status = $status;
  2806. }
  2807. $this->RequestRematch->updateAll(array('RequestRematch.status' => $status),array('RequestRematch.user_id'=> $userId, 'RequestRematch.room_id' => $roomId));
  2808.  
  2809.  
  2810.  
  2811. $this->set(array('Response' => array('Error' => false , 'Message' => 'Rematch Status Updated'), '_serialize' => array('Response')));
  2812. }
  2813.  
  2814. }else{
  2815. $this->set(array('Response' =>array('Error' => true , 'Message' => "This user have no slot for this room"), '_serialize' => array('Response')));
  2816. }
  2817. }
  2818.  
  2819. }else{
  2820. $this->set(array('Response' => array('Error' => true ,'Message' => "No room slots available.") , '_serialize' => array('Response')));
  2821. }
  2822. }
  2823. }
  2824.  
  2825. public function FetchRequestRematch() {
  2826. $error = array();
  2827. if(empty($_POST['RoomId'])){
  2828. $error[] = "RoomId parameter is empty";
  2829. }
  2830.  
  2831. if(empty($_POST['UserId'])) {
  2832. $error[] = "UserId parameter is empty";
  2833. }
  2834. if(!empty($error)){
  2835. $errorMsg = implode(',',$error);
  2836. $response = array('Error' => true, 'Message' => $errorMsg);
  2837. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  2838.  
  2839. }else{
  2840. $roomId = $_POST['RoomId'];
  2841. $UserId = $_POST['UserId'];
  2842.  
  2843. //check if this room exist on rematch table
  2844. $roomdata = $this->RequestRematch->find('first' , array('conditions' => array('RequestRematch.room_id' => $roomId), 'recursive' => -1));
  2845.  
  2846. if(empty($roomdata)){
  2847. //Case 1
  2848.  
  2849. $this->set(array('Response' => array('Error' => false ,'Message' => "No Room Data.", 'Status' => 'NotSet', 'RoomData'=>array('UserId' => $UserId, 'Name' => '', 'Status' => 'NotSet', 'NewRoomId' => "")) , '_serialize' => array('Response')));
  2850. }else{
  2851.  
  2852. //***********check for status 2 **************//
  2853. $chkRejected = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.room_id' => $roomId ,'RequestRematch.status' => 2 )));
  2854.  
  2855. if(empty($chkRejected )){
  2856. $checkAccepted = $this->RequestRematch->find('all', array('conditions' => array('RequestRematch.room_id' => $roomId ,'RequestRematch.status' => 1 )));
  2857.  
  2858. if(count($checkAccepted) == 4){
  2859.  
  2860.  
  2861.  
  2862. $oldRoomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId) ,'recursive' => -1) );
  2863. $roomtype = $oldRoomdata['Room']['room_type'];
  2864. $roomphotonid = $oldRoomdata['Room']['PhotonId'];
  2865. $oldroomid = $roomId;
  2866.  
  2867.  
  2868.  
  2869. //chk for room
  2870.  
  2871. $newuserdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $UserId,'RoomSlot.RoomId !=' => $roomId), 'recursive' => -1));
  2872.  
  2873. if(!empty($newuserdata)){
  2874.  
  2875. $newroomid = $newuserdata['RoomSlot']['RoomId'];
  2876. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2877. $this->set(array('Response' => array('Error' => false , 'Message' => 'Accepted', 'Status' => 'Accepted', 'NewRoomId'=> $newroomid,'RoomData' => array('UserId' => $UserId, 'Name' =>'', 'Status' => 'Accepted', 'NewRoomId' => $newroomid)), '_serialize' => array('Response')));
  2878.  
  2879. }else{
  2880.  
  2881. //create new room
  2882.  
  2883. $gethostid = $this->RequestRematch->find('all' , array('conditions' => array('RequestRematch.room_id' => $roomId), 'fields' => array('host_id','user_id')) );
  2884.  
  2885.  
  2886.  
  2887. $hostId = $gethostid[0]['RequestRematch']['host_id'];
  2888.  
  2889. $hostData = $this->User->find('first', array('conditions' => array('User.id' => $hostId),'recursive' => -1));
  2890.  
  2891. $hostName = $hostData['User']['username'];
  2892. $UserId = $hostId;
  2893. $name = $hostName;
  2894. $HostName = $hostName;
  2895. $join_member_count = 4;
  2896. $created_time = strtotime(date('Y-m-d H:i:s')) ;
  2897. $created_date = date('Y-m-d H:i:s');
  2898.  
  2899. //add room type and photonid
  2900.  
  2901.  
  2902. $roomData = array('UserId' => $UserId,'name' => $name, 'HostName'=>$HostName,' room_type' => $roomtype,'PhotonId' => $roomphotonid, 'join_member_count' => $join_member_count, 'created_time' => $created_time, 'created_date' => $created_date, 'room_status' => '1', 'OngoingStatus' => '1');
  2903. $this->Room->save($roomData);
  2904. $newRoomId = $this->Room->getInsertID();
  2905.  
  2906.  
  2907.  
  2908.  
  2909. $this->RoomSlot->updateAll(array('RoomSlot.RoomId' => $newRoomId,'RoomSlot.OldSlotUser' => 0,'RoomSlot.play_status' => 0),array('RoomSlot.RoomId' => $roomId));
  2910.  
  2911. $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
  2912.  
  2913. //create room slots
  2914.  
  2915. //delete requestrematch data
  2916.  
  2917.  
  2918.  
  2919.  
  2920. $this->set(array('Response' => array('Error' => false , 'RoomData' => array('UserId' => $UserId, 'Name' => $name, 'Status' => 'Accepted', 'NewRoomId' => $newRoomId) ) , '_serialize' => array('Response') ));
  2921.  
  2922. }
  2923.  
  2924. }else{
  2925.  
  2926. //Case2
  2927. $chkUser = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $UserId ,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
  2928.  
  2929.  
  2930. if(empty($chkUser)){
  2931. $this->set(array('Response' => array('Error' => false , 'Message' => 'This User is not set','Status' => 'Ask','RoomData' => array('UserId'=>$UserId, 'Name' => '', 'Status' => 'Ask', 'NewRoomId' => '')),'_serialize' => array('Response')));
  2932.  
  2933. }else{
  2934.  
  2935. $userStatus = $chkUser['RequestRematch']['status'];
  2936. if($userStatus == 0){
  2937. $this->set(array('Response' => array('Error' => false , 'Message' => 'Ask', 'Status' => 'Ask','RoomData' => array('UserId' => $UserId,'Name' => '', 'Status' => 'Ask', 'NewRoomId' => '')), '_serialize' => array('Response')));
  2938. }elseif($userStatus == 1){
  2939. $this->set(array('Response' => array('Error' => false , 'Message' => 'Waiting', 'Status' => 'Waiting','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Waiting', 'NewRoomId' => '')), '_serialize' => array('Response')));
  2940. }elseif($userStatus == 2){
  2941. $this->set(array('Response' => array('Error' => false , 'Message' => 'Rejected','Status' => 'Rejected','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Rejected', 'NewRoomId' => '')), '_serialize' => array('Response')));
  2942. }
  2943. }
  2944. }
  2945.  
  2946. }else{
  2947. $this->set(array('Response' => array('Error' => false , 'Message' => 'Rejected', 'Status' => 'Rejected','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Rejected', 'NewRoomId' => '')), '_serialize' => array('Response')));
  2948.  
  2949. }
  2950. }
  2951. }
  2952. }
  2953.  
  2954.  
  2955. public function time_elapsed_string($datetime, $full = false) {
  2956. $now = new DateTime;
  2957. $ago = new DateTime($datetime);
  2958. $diff = $now->diff($ago);
  2959.  
  2960. $diff->w = floor($diff->d / 7);
  2961. $diff->d -= $diff->w * 7;
  2962.  
  2963. $string = array(
  2964. 'y' => 'year',
  2965. 'm' => 'month',
  2966. 'w' => 'week',
  2967. 'd' => 'day',
  2968. 'h' => 'hour',
  2969. 'i' => 'minute',
  2970. 's' => 'second',
  2971. );
  2972. foreach ($string as $k => &$v) {
  2973. if ($diff->$k) {
  2974. $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  2975. } else {
  2976. unset($string[$k]);
  2977. }
  2978. }
  2979.  
  2980. if (!$full) $string = array_slice($string, 0, 1);
  2981. return $string ? implode(', ', $string) . ' ago' : 'just now';
  2982. }
  2983.  
  2984.  
  2985. public function UserStats() {
  2986. $error = array();
  2987. if(empty($this->request->query['UserId'])){
  2988. $error[] = "UserId parameter is empty";
  2989. }
  2990. if(!empty($error)){
  2991. $errorMsg = implode(',',$error);
  2992. $response = array('Error' => true, 'Message' => $errMessage);
  2993. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  2994. }else{
  2995. $userid = $this->request->query['UserId'];
  2996. $userStats = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $userid)));
  2997. if(!empty($userStats)){
  2998. $totalGame = $userStats['UserPlayGame']['total_game'];
  2999. $winGame = $userStats['UserPlayGame']['win_game'];
  3000. $lostGame = $userStats['UserPlayGame']['lost_game'];
  3001. $winpercent = $userStats['UserPlayGame']['win_percent'];
  3002. $perdayGameplayed = $userStats['UserPlayGame']['PerdayGameplayed'];
  3003. $Monthlygameplay = $userStats['UserPlayGame']['Monthlygameplay'];
  3004. $Weeklygameplay = $userStats['UserPlayGame']['Weeklygameplay'];
  3005. $fairPlay = $userStats['UserPlayGame']['fairPlay'];
  3006.  
  3007. $unixtime = $userStats['UserPlayGame']['created_date'];
  3008.  
  3009. $lastgameplayed = date("m/d/Y h:i:s",strtotime($unixtime));
  3010.  
  3011.  
  3012. $lastgameplayed = $this->time_elapsed_string(@$unixtime);
  3013.  
  3014. if($lastgameplayed == "2016 years ago"){
  3015. $lastgameplayed = 'Today';
  3016. }
  3017.  
  3018. //last game played to be included
  3019. $userStatsdata = array('WinPercent' => $winpercent,'LostGames' => $lostGame,'TotalGame' => $totalGame, 'WinGames' => $winGame, 'LastGamePlayed' => $lastgameplayed );
  3020.  
  3021. $this->set(array('Response' => array('Error' => false ,'UserStatsMultiPlayer' => $userStatsdata,'Message' => "Success") , '_serialize' => array('Response')));
  3022. }else{
  3023. $this->set(array('Response' => array('Error' => true ,'Message' => "NO UserStats available.") , '_serialize' => array('Response')));
  3024. }
  3025. }
  3026. }
  3027.  
  3028.  
  3029. // Type : 1 private , 2 public
  3030. // Room Status: 0 game not started, 1 game started
  3031. //Online Status : 1 for online, 0 for offline
  3032.  
  3033. public function roomjoinaftersearch(){
  3034.  
  3035. $error = array();
  3036. if(empty($this->request->query['RoomId'])){
  3037. $error[] = "RoomId parameter is empty" ;
  3038. }
  3039. if(empty($this->request->query['UserId'])){
  3040. $error[] = "UserId parameter is empty" ;
  3041. }
  3042. if($this->request->query['SlotId']==''){
  3043. $error[] = "SlotId parameter is empty" ;
  3044. }
  3045. if(!empty($error)){
  3046. $errorMsg = implode(',',$error);
  3047. $response = array('Error' => True, 'Message' => $errorMsg);
  3048. $this->set(array('Response' => $response, '_serialize' => array('Response')));
  3049. }else{
  3050. $userId = $this->request->query['UserId'];
  3051. $slotId = $this->request->query['SlotId'];
  3052. $roomId = $this->request->query['RoomId'];
  3053.  
  3054. $roomData = $this->Room->find('first', array('conditions' => array('Room.id' => $roomId)));
  3055.  
  3056. if(!empty($roomData)){
  3057. $getslotdata = $this->RoomSlot->find('first', array('conditions' => array( 'RoomSlot.SlotId' => $slotId, 'RoomSlot.RoomId' => $roomId ), 'recursive' => -1)) ;
  3058. if(!empty($getslotdata)){
  3059. $currentSlotuser = $getslotdata['RoomSlot']['UserId'];
  3060. if($currentSlotuser != 0){
  3061.  
  3062. $this->set(array('Response' => array('Error' => true , 'Message' => 'SlotAlreadyTaken') , '_serialize' => array('Response')));
  3063.  
  3064. }else{
  3065.  
  3066. $chkuserAssigned = $this->RoomSlot->find('first',array('conditions' => array('RoomSlot.UserId' => $userId)));
  3067. if(!empty($chkuserAssigned)){
  3068. $this->set(array('Response' => array('Error' => true , 'Message' => 'This User is already assigned ') , '_serialize' => array('Response')));
  3069. }else{
  3070. $this->RoomSlot->updateAll(array('RoomSlot.UserId' => $userId, 'RoomSlot.SlotStatus' =>'1'),array('RoomSlot.RoomId' => $roomId, 'RoomSlot.SlotId' => $slotId));
  3071.  
  3072. $newslotdata = $this->RoomSlot->find('all', array('conditions' => array( 'RoomSlot.RoomId' => $roomId ))) ;
  3073.  
  3074. $allSlotdata = array();
  3075. foreach($newslotdata as $k => $v){
  3076.  
  3077. $slotUser = $v['RoomSlot']['UserId'];
  3078. $type = $v['RoomSlot']['slot_type'];
  3079. if($type = 'public') {
  3080. $v['Password'] = null;
  3081. }elseif($type == 'private') {
  3082. $v['Password'] = $v['SlotKey'];
  3083. }elseif($type == 'invite'){
  3084. $v['Password'] = null ;
  3085. }
  3086. $userData =$this->User->find('first' , array('fields' => array('User.fbid', 'User.online_status'), 'conditions' => array('User.id' => $slotUser )));
  3087. if(!empty($userData)){
  3088.  
  3089. $userfbId = $userData['User']['fbid'];
  3090. $onlinestatus = $userData['User']['online_status'];
  3091. $allSlotdata[] = array('OnlineStatus' => $onlinestatus,'FbId' => $userfbId,'UserId' => $slotUser, 'Type' => $type , 'Password' => $v['Password']);
  3092. }else{
  3093. $allSlotdata[] = array('UserId' =>$slotUser );
  3094. }
  3095. }
  3096.  
  3097. $roomType = $roomData['Room']['room_type'];
  3098. $roomStatus = $roomData['Room']['room_status'];
  3099. $hostname = $roomData['Room']['HostName'];
  3100. $RoomCreationTime = $roomData['Room']['created_date'];
  3101. $slotlist = $allSlotdata;
  3102. $totalusers = $roomData['Room']['join_member_count'];
  3103. $photonid = $roomData['Room']['PhotonId'];
  3104.  
  3105. $newRoomJoinCount = $totalusers + 1 ;
  3106. if($newRoomJoinCount == 4){
  3107. $this->Room->updateAll(array('Room.join_member_count' => $newRoomJoinCount, 'Room.room_status' => 1,'OngoingStatus' => 1),array('Room.id' =>$roomId));
  3108.  
  3109. }else{
  3110. $this->Room->updateAll(array('Room.join_member_count' => $newRoomJoinCount),array('Room.id' =>$roomId));
  3111. }
  3112.  
  3113.  
  3114. $this->set(array('Response' => array('Error' => false , 'Status' => 'True',
  3115. 'RoomDetails' => array('RoomId' => $roomId, 'Type' => $roomType, 'RoomStatus' => $roomStatus , 'HostName' => $hostname, 'RoomCreationTime' => $RoomCreationTime ),
  3116. 'SlotList' => $slotlist, 'Message' => 'Success'), '_serialize' => array('Response')));
  3117.  
  3118. }
  3119. }
  3120.  
  3121.  
  3122. }else{
  3123. $this->set(array('Response' => array('Error' => true , 'Message' => 'This slot does not exist'), '_serialize' => array('Response')));
  3124. }
  3125. }else{
  3126. $this->set(array('Response' => array('Error' => true , 'Message' => 'This room Does Not exist.'), '_serialize' => array('Response')));
  3127. }
  3128. }
  3129. }
  3130.  
  3131.  
  3132. /*
  3133. @API: TopTenScores
  3134. Databy : 1 by Date, 2 by Week, 3 by Month
  3135. */
  3136.  
  3137. public function TopTenScores(){
  3138.  
  3139. $error = array();
  3140. if(empty($this->request->query['DataBy'])) {
  3141. $error[] = "DataBy parameter is empty";
  3142. }
  3143.  
  3144. if(!empty($error)){
  3145. $errorMsg = implode(',',$error);
  3146. $response = array('Error' => true, 'Message' => $errorMsg);
  3147. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  3148. }else{
  3149.  
  3150. $dataBy = $this->request->query['DataBy'];
  3151. $currentdate =date('Y-m-d');
  3152. $dateexplode = explode('-',$currentdate);
  3153. $currentmonth = $dateexplode['1'];
  3154.  
  3155. if($dataBy == 1) {
  3156. //Game played today
  3157. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE()')));
  3158. // players list for today
  3159.  
  3160. if(!empty($playDateData)){
  3161. $playeronearr = array();
  3162. $playertwoarr = array();
  3163. foreach($playDateData as $k=>$v){
  3164.  
  3165. $playerids = explode(',',$v['FinalScore']['Players']);
  3166.  
  3167. $playerone = $playerids[0];
  3168. $playertwo = $playerids[1];
  3169. $playeronearr[] = $playerone;
  3170. $playertwoarr[] = $playertwo;
  3171.  
  3172. }
  3173. $allplayerarray = array_merge($playeronearr,$playertwoarr);
  3174. if(!empty($allplayerarray)){
  3175.  
  3176. $uniqueplayerslist = array_unique($allplayerarray);
  3177. $resetplayerkeys = array_values($uniqueplayerslist);
  3178. // got the users list until here . Now we have file for user stats 1 from users table
  3179. $requiredids = array();
  3180. foreach($resetplayerkeys as $key=>$playerid) {
  3181. $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
  3182. if(!empty($chkuserstats)){
  3183. if($chkuserstats['User']['statas'] == 1){
  3184. $validuserids = $playerid;
  3185. }else{
  3186. $validuserids = '';
  3187. }
  3188. }else{
  3189. $validuserids = '';
  3190. }
  3191. $requiredids[] = $validuserids;
  3192. }
  3193.  
  3194. $reqids = array_values(array_filter($requiredids));
  3195.  
  3196. if(!empty($reqids)){
  3197. $userdata = array();
  3198. foreach($reqids as $idkey=>$idval){
  3199.  
  3200. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE()'),'order'=>array('FinalScore.Score'=>'desc'),'fields' => array('RoomId')));
  3201.  
  3202. $noofgameplaytoday = count($playDateData);
  3203. $minimumplaylimit = 10;
  3204.  
  3205. if($noofgameplaytoday >= $minimumplaylimit){
  3206.  
  3207. $winplayDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE()','Result'=>'W'),'fields' => array('RoomId','Result')));
  3208.  
  3209. $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
  3210.  
  3211. $username = $this->User->find("first",array('conditions'=>array('User.id' => $idval),'fields' => array('username')));
  3212.  
  3213. if($winpercentageofuser > 0 ){
  3214. $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
  3215. }else{
  3216. $userdata[] = array();
  3217. }
  3218.  
  3219. }else{
  3220. $userdata[] = array();
  3221. }
  3222.  
  3223. }
  3224.  
  3225. $userdata = array_values(array_filter($userdata));
  3226.  
  3227. if(!empty($userdata)){
  3228.  
  3229. foreach ($userdata as $key => $row) {
  3230. $mid[$key] = $row['WinPercent'];
  3231. }
  3232. array_multisort($mid, SORT_DESC, $userdata);
  3233.  
  3234. $allUserData = array_slice($userdata, 0, 10);
  3235. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
  3236.  
  3237. }else{
  3238.  
  3239. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
  3240. }
  3241.  
  3242. }else{
  3243. $this->set(array('Response'=>array('Error'=>false,'UserPlayData' => array()),'_serialize' => array('Response')));
  3244. }
  3245. }
  3246.  
  3247. }else{
  3248. $this->set(array('Response'=>array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
  3249. }
  3250.  
  3251. }elseif($dataBy == 2){
  3252.  
  3253. //Game played this week
  3254.  
  3255. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()')));
  3256.  
  3257. // players list for this week
  3258.  
  3259. if(!empty($playDateData)){
  3260. $playeronearr = array();
  3261. $playertwoarr = array();
  3262. foreach($playDateData as $k=>$v){
  3263.  
  3264. $playerids = explode(',',$v['FinalScore']['Players']);
  3265.  
  3266. $playerone = $playerids[0];
  3267. $playertwo = $playerids[1];
  3268. $playeronearr[] = $playerone;
  3269. $playertwoarr[] = $playertwo;
  3270.  
  3271. }
  3272. $allplayerarray = array_merge($playeronearr,$playertwoarr);
  3273. if(!empty($allplayerarray)){
  3274.  
  3275. $uniqueplayerslist = array_unique($allplayerarray);
  3276. $resetplayerkeys = array_values($uniqueplayerslist);
  3277. // got the users list until here . Now we have file for user stats 1 from users table
  3278. $requiredids = array();
  3279. foreach($resetplayerkeys as $key=>$playerid) {
  3280. $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
  3281. if(!empty($chkuserstats)){
  3282. if($chkuserstats['User']['statas'] == 1){
  3283. $validuserids = $playerid;
  3284. }else{
  3285. $validuserids = '';
  3286. }
  3287. }else{
  3288. $validuserids = '';
  3289. }
  3290.  
  3291. $requiredids[] = $validuserids;
  3292.  
  3293. }
  3294.  
  3295. $reqids = array_values(array_filter($requiredids));
  3296.  
  3297. if(!empty($reqids)){
  3298. $userdata = array();
  3299. foreach($reqids as $idkey=>$idval){
  3300.  
  3301.  
  3302. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()'),'order'=>array('FinalScore.Score'=>'desc'),'fields' => array('RoomId')));
  3303.  
  3304. $noofgameplaytoday = count($playDateData);
  3305. $minimumplaylimit = 35;
  3306.  
  3307. if($noofgameplaytoday >= $minimumplaylimit){
  3308.  
  3309.  
  3310. $winplayDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()','Result'=>'W'),'fields' => array('RoomId','Result')));
  3311.  
  3312. $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
  3313.  
  3314.  
  3315. $username = $this->User->find("first",array('conditions' => array('User.id' => $idval),'fields' => array('username')));
  3316.  
  3317. if($winpercentageofuser > 0 ){
  3318. $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
  3319. }else{
  3320. $userdata[] = array();
  3321. }
  3322. }else{
  3323. $userdata[] = array();
  3324. }
  3325.  
  3326. }
  3327.  
  3328. $userdata = array_values(array_filter($userdata));
  3329.  
  3330. if(!empty($userdata)){
  3331.  
  3332. foreach ($userdata as $key => $row) {
  3333. $mid[$key] = $row['WinPercent'];
  3334. }
  3335. array_multisort($mid, SORT_DESC, $userdata);
  3336.  
  3337. $allUserData = array_slice($userdata, 0, 10);
  3338. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
  3339.  
  3340. }else{
  3341. $this->set(array('Response'=>array('Error'=>false,'UserPlayData'=>array()), '_serialize' => array('Response')));
  3342. }
  3343.  
  3344. }else{
  3345. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
  3346. }
  3347.  
  3348. }
  3349.  
  3350. }else{
  3351. $this->set(array('Response'=>array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
  3352. }
  3353.  
  3354. }elseif($dataBy == 3){
  3355.  
  3356. //Game played this month
  3357. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()')));
  3358.  
  3359. // players list for this month
  3360.  
  3361. if(!empty($playDateData)){
  3362. $playeronearr = array();
  3363. $playertwoarr = array();
  3364. foreach($playDateData as $k=>$v){
  3365.  
  3366. $playerids = explode(',',$v['FinalScore']['Players']);
  3367. $playerone = $playerids[0];
  3368. $playertwo = $playerids[1];
  3369. $playeronearr[] = $playerone;
  3370. $playertwoarr[] = $playertwo;
  3371.  
  3372. }
  3373. $allplayerarray = array_merge($playeronearr,$playertwoarr);
  3374. if(!empty($allplayerarray)){
  3375.  
  3376. $uniqueplayerslist = array_unique($allplayerarray);
  3377. $resetplayerkeys = array_values($uniqueplayerslist);
  3378. // got the users list until here . Now we have file for user stats 1 from users table
  3379. $requiredids = array();
  3380. foreach($resetplayerkeys as $key=>$playerid) {
  3381.  
  3382. $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
  3383. if(!empty($chkuserstats)){
  3384. if($chkuserstats['User']['statas'] == 1){
  3385. $validuserids = $playerid;
  3386. }else{
  3387. $validuserids = '';
  3388. }
  3389. }else{
  3390. $validuserids = '';
  3391. }
  3392.  
  3393. $requiredids[] = $validuserids;
  3394. }
  3395.  
  3396. $reqids = array_values(array_filter($requiredids));
  3397.  
  3398. if(!empty($reqids)){
  3399. $userdata = array();
  3400. foreach($reqids as $idkey=>$idval){
  3401.  
  3402.  
  3403. $playDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()'),'order'=>array('FinalScore.Score'=>'desc'),'fields' => array('RoomId')));
  3404.  
  3405. $noofgameplaytoday = count($playDateData);
  3406.  
  3407. $minimumplaylimit = 100;
  3408.  
  3409. if($noofgameplaytoday >= $minimumplaylimit){
  3410.  
  3411.  
  3412. $winplayDateData= $this->FinalScore->find("all",array('conditions'=>array('Players LIKE'=>'%' . $idval . '%','DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()','Result'=>'W'),'fields' => array('RoomId','Result')));
  3413.  
  3414. $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
  3415.  
  3416. $username = $this->User->find("first",array('conditions'=>array('User.id' => $idval),'fields' => array('username')));
  3417.  
  3418. if($winpercentageofuser > 0 ){
  3419. $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
  3420. }else{
  3421. $userdata[] = array();
  3422. }
  3423.  
  3424. }else{
  3425. $userdata[] = array();
  3426. }
  3427.  
  3428. }
  3429.  
  3430. $userdata = array_values(array_filter($userdata));
  3431.  
  3432. if(!empty($userdata)){
  3433.  
  3434. foreach ($userdata as $key => $row) {
  3435. $mid[$key] = $row['WinPercent'];
  3436. }
  3437. array_multisort($mid, SORT_DESC, $userdata);
  3438.  
  3439. $allUserData = array_slice($userdata, 0, 10);
  3440. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
  3441.  
  3442. }else{
  3443. $this->set(array('Response'=>array('Error'=>false,'UserPlayData' => array()), '_serialize' => array('Response')));
  3444. }
  3445.  
  3446. }else{
  3447. $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
  3448. }
  3449. }
  3450.  
  3451. }else{
  3452. $this->set(array('Response'=>array('Error' => false, 'UserPlayData'=>array()), '_serialize' => array('Response')));
  3453. }
  3454.  
  3455. }else{
  3456. $this->set(array('Response'=>array('Error'=>true, 'Message' => 'No data available'), '_serialize' => array('Response')));
  3457. }
  3458. }
  3459. }
  3460.  
  3461. public function SetForcerValue(){
  3462. $error = array();
  3463. if(empty($this->request->query['UserId'])) {
  3464. $error[] = "UsedrId parameter is empty";
  3465. }
  3466.  
  3467. if(!empty($error)){
  3468. $errorMsg = implode(',',$error);
  3469. $response = array('Error' => true, 'Message' => $errorMsg);
  3470. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  3471.  
  3472. }else{
  3473. //Check User Exists
  3474. $UserId = $this->request->query['UserId'];
  3475. $chkUser = $this->User->find('first', array('conditions' => array('User.id' => $UserId),'recursive' => -1));
  3476.  
  3477. if(count($chkUser) > 0 ){
  3478.  
  3479. $checkuserplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $UserId), 'recursive' => -1));
  3480. if(count($checkuserplaydata)> 0){
  3481. $oldForcervalue = $checkuserplaydata['UserPlayGame']['Forcer'];
  3482. $newForcervalue = $oldForcervalue + 1;
  3483. $this->UserPlayGame->updateAll(array('UserPlayGame.Forcer' => $newForcervalue),array('UserPlayGame.user_id' => $UserId ));
  3484. $this->set(array('Response' => array('Error' => false, 'Message' => 'Successfully Updated'), '_serialize' => array('Response')));
  3485.  
  3486. }else{
  3487. $data['UserPlayGame']['user_id'] = $UserId;
  3488. $data['UserPlayGame']['Forcer'] = 1;
  3489. $this->UserPlayGame->save($data);
  3490. $this->set(array('Response' => array('Error' => false, 'Message' => 'Success'), '_serialize' => array('Response')));
  3491. }
  3492.  
  3493. }else{
  3494. $this->set(array('Response'=>array('Error'=>true,'Message' => 'User Does Not exist'), '_serialize' => array('Response')));
  3495. }
  3496. }
  3497. }
  3498.  
  3499. public function SetHomeRunValue(){
  3500. $error = array();
  3501. if(empty($this->request->query['UserId'])) {
  3502. $error[] = "UsedrId parameter is empty";
  3503. }
  3504.  
  3505. if(!empty($error)){
  3506. $errorMsg = implode(',',$error);
  3507. $response = array('Error' => true, 'Message' => $errorMsg);
  3508. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  3509.  
  3510. }else{
  3511.  
  3512. //Check User Exists
  3513. $UserId = $this->request->query['UserId'];
  3514. $chkUser = $this->User->find('first', array('conditions' => array('User.id' => $UserId),'recursive' => -1));
  3515.  
  3516. if(count($chkUser) > 0 ){
  3517.  
  3518. $checkuserplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $UserId), 'recursive' => -1));
  3519. if(count($checkuserplaydata)> 0){
  3520. $oldHomerunvalue = $checkuserplaydata['UserPlayGame']['HomeRun'];
  3521. $newHomeRunvalue = $oldHomerunvalue + 1;
  3522. $this->UserPlayGame->updateAll(array('UserPlayGame.HomeRun' => $newHomeRunvalue),array('UserPlayGame.user_id' => $UserId ));
  3523. $this->set(array('Response' => array('Error' => false, 'Message' => 'Successfully Updated'), '_serialize' => array('Response')));
  3524.  
  3525. }else{
  3526. $data['UserPlayGame']['user_id'] = $UserId;
  3527. $data['UserPlayGame']['HomeRun'] = 1;
  3528. $this->UserPlayGame->save($data);
  3529. $this->set(array('Response' => array('Error' => false, 'Message' => 'Success'),'_serialize' => array('Response')));
  3530. }
  3531.  
  3532. }else{
  3533. $this->set(array('Response'=>array('Error'=>true,'Message' => 'User Does Not exist'),'_serialize' => array('Response')));
  3534. }
  3535.  
  3536. }
  3537.  
  3538. }
  3539.  
  3540. public function GetAchievementValues(){
  3541.  
  3542. $error = array();
  3543. if(empty($this->request->query['UserId'])) {
  3544. $error[] = "UserId parameter is empty";
  3545. }
  3546.  
  3547. if(!empty($error)){
  3548. $errorMsg = implode(',',$error);
  3549. $response = array('Error' => true, 'Message' => $errorMsg);
  3550. $this->set(array('Response' => $response , '_serialize' => array('Response')));
  3551.  
  3552. }else{
  3553.  
  3554. $userId = $this->request->query['UserId'] ;
  3555. //check if gameplaydata exists for this user
  3556.  
  3557. $userplaydata = $this->UserPlayGame->find('all', array('conditions' => array('UserPlayGame.user_id'=> $userId), 'recursive' => -1));
  3558. if(count($userplaydata) > 0){
  3559. $homerun = $userplaydata[0]['UserPlayGame']['HomeRun'];
  3560. $Forcer = $userplaydata[0]['UserPlayGame']['Forcer'];
  3561. $Ace = $userplaydata[0]['UserPlayGame']['Ace'];
  3562. $Unstoppable = $userplaydata[0]['UserPlayGame']['Unstoppable'];
  3563. $Winner = $userplaydata[0]['UserPlayGame']['Winner'];
  3564. $Player = $userplaydata[0]['UserPlayGame']['Player'];
  3565. $Looser = $userplaydata[0]['UserPlayGame']['Looser'];
  3566. $FullHouse = $userplaydata[0]['UserPlayGame']['FullHouse'];
  3567. $WinStreak = $userplaydata[0]['UserPlayGame']['WinStreak'];
  3568. $XthreeWinStreak = $userplaydata[0]['UserPlayGame']['XthreeWinStreak'];
  3569. $XfiveWinstreak = $userplaydata[0]['UserPlayGame']['XfiveWinstreak'];
  3570. $XtenWinstreak = $userplaydata[0]['UserPlayGame']['XtenWinstreak'];
  3571.  
  3572. $data = array($Ace,$Unstoppable,$Winner,$Player,$XthreeWinStreak,$XfiveWinstreak,$XtenWinstreak,$Looser,$FullHouse,$homerun,$Forcer);
  3573.  
  3574. $this->set(array('Response' => array('Error' => false, 'UserAchievementData' => $data, 'UserId' => $userId), '_serialize' => array('Response')));
  3575. }else{
  3576. $this->set(array('Response' => array('Error' => true, 'Message' => 'User Data Not available'), '_serialize' => array('Response')));
  3577. }
  3578. }
  3579. }
  3580.  
  3581.  
  3582. }
  3583.  
  3584.  
  3585. ?>
Add Comment
Please, Sign In to add comment