Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @Controller Name: Apis
- * @Created Date : 14_06_2016
- * @ all Params Usesd Pacel Case
- * @ Error : True/False
- */
- Class ApisListController extends AppController {
- public $name = 'ApisList';
- public $uses = array('User','AppSetting','UserDevice','Room','RoundScore','RoomSlot','ChatInfo','FinalScore','UserPlayGame','RequestRematch','GamePlayData');
- public $components = array('RequestHandler','Email','AWSSES');
- public $helpers = array('Html');
- public function beforeFilter() {
- parent::beforeFilter();
- $this->RequestHandler->ext = 'json';
- }
- public function test(){
- $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')));
- $this->set(array('response' => array('errorflag'=>0,'user_id'=>123),'_serialize' => array('response')));
- }
- /**
- * @method : getSetting()
- * @purpose : get app Setting Option Like time for invitation acceptance etc.
- * @create_date : 14_06_2016
- */
- public function getSetting(){
- $error = '';
- if(empty($_REQUEST['Build'])){
- $error = "Missing Build parameter";
- }
- if(isset($error) && !empty($error )){
- $message = array('Error'=>true,'Message'=>$error);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else{
- $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')));
- $time = $settingData['AppSetting']['time_setting'];
- $build = $settingData['AppSetting']['Build'];
- $UrlIOS = $settingData['AppSetting']['UrlIOS'];
- $UrlAndroid = $settingData['AppSetting']['UrlAndroid'];
- $UrlWin = $settingData['AppSetting']['UrlWin'];
- $UrlOSX = $settingData['AppSetting']['UrlOSX'];
- $autokicktime = $settingData['AppSetting']['AutoKickTimeout'];
- $showads = $settingData['AppSetting']['showads'] ? $settingData['AppSetting']['showads'] : "0";
- $show_effects = $settingData['AppSetting']['show_effects'] ? $settingData['AppSetting']['show_effects'] : "0";
- // echo "<pre>"; print_r($settingData); die;
- $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')));
- }
- }
- /**
- * @method : checkSingIn
- * @purpose : check Current Device Staus
- * @create_date : 14_06_2016
- * #Params : IsAvailable: NotRegistered(if device Id Still not Registred)
- * @room_status: 0- still game not started, 1: game running, 2: game finished
- */
- public function checkSingIn(){
- $error = '';
- if(empty($_REQUEST['DeviceId'])){
- $error = "Missing DeviceId parameter";
- }
- if(isset($error) && !empty($error )){
- $message = array('Error'=>true,'Message'=>$error);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $DeviceId = trim($_REQUEST['DeviceId']);
- $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
- if((empty($deviceData)) || ($DeviceId == 'n/a')){
- $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'NotRegistered'),'_serialize' => array('Response')));
- } else{
- $devicepriId = $deviceData['UserDevice']['id'];
- $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')));
- if(!empty($getUser['User']['current_device_id']))
- $currentdeviceid = $getUser['User']['current_device_id'];
- else
- $currentdeviceid = 0;
- if( ($getUser['User']['online_status']==1 ) && ($currentdeviceid == $devicepriId ) ){
- // Checke User Alredy in Game or Not
- $roomId = '';
- $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));
- //chkuserinvited
- if(isset($getSlotData) && !empty($getSlotData)){
- $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')));
- if(!empty($getroomStatus) && $getroomStatus['Room']['OngoingStatus'] == 1 && $getroomStatus['Room']['join_member_count'] > 1){
- $roomId = $getroomStatus['Room']['id'];
- }elseif((!empty($getroomStatus)) && ($getroomStatus['Room']['OngoingStatus'] == 1) && ($getroomStatus['Room']['join_member_count'] == 1)){
- $roomId = $getroomStatus['Room']['id'];
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $this->Room->delete($roomId);
- $roomId = '';
- }elseif(!empty($getroomStatus) && $getroomStatus['Room']['OngoingStatus'] != 1 && $getroomStatus['Room']['join_member_count'] >= 1){
- $roomId = $getroomStatus['Room']['id'];
- //create empty public room slots
- $getSlot = $this->RoomSlot->find('all',array('conditions' => array('RoomSlot.RoomId' => $roomId),'order'=>array('RoomSlot.SlotId ASC'),'recursive' => -1));
- $lastslotidkey = end($getSlot);
- $lastslotkey = key($getSlot);
- $lastslotid = $getSlot[$lastslotkey]['RoomSlot']['SlotId'];
- switch ($lastslotid) {
- case 0:
- for($i=1;$i<=3;$i++){
- $datetime = strtotime(date('Y-m-d H:i:s'));
- $created_date = date('Y-m-d H:i:s');
- $slotCreat['RoomSlot']['RoomId'] = $roomId;
- $slotCreat['RoomSlot']['UserId'] = 0;
- $slotCreat['RoomSlot']['SlotId'] = $i;
- $slotCreat['RoomSlot']['slot_type'] = 'public';
- $slotCreat['RoomSlot']['SlotStatus'] = 0;
- $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
- $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
- $this->RoomSlot->saveall($slotCreat);
- }
- break;
- case 1:
- for($i=2;$i<=3;$i++){
- $datetime = strtotime(date('Y-m-d H:i:s'));
- $created_date = date('Y-m-d H:i:s') ;
- $slotCreat['RoomSlot']['RoomId'] = $roomId;
- $slotCreat['RoomSlot']['UserId'] = 0;
- $slotCreat['RoomSlot']['SlotId'] = $i;
- $slotCreat['RoomSlot']['slot_type'] = 'public';
- $slotCreat['RoomSlot']['SlotStatus'] = 0;
- $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
- $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
- $this->RoomSlot->saveall($slotCreat);
- }
- break;
- case 2:
- $i = 3;
- $datetime = strtotime(date('Y-m-d H:i:s'));
- $created_date = date('Y-m-d H:i:s');
- $slotCreat['RoomSlot']['RoomId'] = $roomId;
- $slotCreat['RoomSlot']['UserId'] = 0;
- $slotCreat['RoomSlot']['SlotId'] = $i;
- $slotCreat['RoomSlot']['slot_type'] = 'public';
- $slotCreat['RoomSlot']['SlotStatus'] = 0;
- $slotCreat['RoomSlot']['created_time'] = strtotime(date('Y-m-d H:i:s')) ;
- $slotCreat['RoomSlot']['created_date'] = date('Y-m-d H:i:s') ;
- $this->RoomSlot->saveall($slotCreat);
- break;
- default:
- $roomId = $getroomStatus['Room']['id'];
- }
- }else{
- //check if user is not sitting in a complete game
- $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')));
- if(!empty($getcompleteroomStatus)){
- $joinemembercount = $getcompleteroomStatus['Room']['join_member_count'];
- $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']));
- $newjoinmemebercount = $joinemembercount - 1;
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmemebercount),array('Room.id' => $getSlotData['RoomSlot']['RoomId']));
- $roomId = '';
- }
- }
- }
- // END
- $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'True','UserStatus'=>$getUser['User']['visible'],'UserId'=>$getUser['User']['id'],'RoomId'=>$roomId),'_serialize' => array('Response')));
- } else if(($getUser['User']['online_status']==1 ) && ($currentdeviceid != $devicepriId )){
- $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'AlreadySignin','UserStatus'=>$getUser['User']['visible']),'_serialize' => array('Response')));
- } else{
- // Check User Alredy in Game or Not
- $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));
- $roomId = '';
- if(!empty($getSlotData)){
- $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));
- if(!empty($getroomStatus) && ('Room.OngoingStatus' == 1) && $getroomStatus['Room']['join_member_count'] > 1){
- $roomId = $getroomStatus['Room']['id'];
- }else{
- $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')));
- //check if user is not sitting in a complete game
- if(!empty($getcompleteroomStatus)){
- $joinemembercount = $getcompleteroomStatus['Room']['join_member_count'];
- $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']));
- $newjoinmemebercount = $joinemembercount - 1;
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmemebercount),array('Room.id' => $getSlotData['RoomSlot']['RoomId']));
- $roomId = '';
- }
- }
- }
- // END Above Code
- $userId = $getUser['User']['id'];
- $DeviceId = trim($_REQUEST['DeviceId']);
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>"'$devicepriId'",'User.last_seen'=>$lastseen), array('User.id' => $userId));
- $this->set(array('Response' => array('Error'=>false,'IsAvailable'=>'True','UserStatus'=>$getUser['User']['visible'],'UserId'=>$getUser['User']['id'],'RoomId'=>$roomId),'_serialize' => array('Response')));
- }
- }
- }
- }
- /**
- * @method : signIn
- * @purpose : Login with username/Nickname/Email, Password
- * @creadted_date : 15_06_2016
- * @ 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
- */
- public function signIn(){
- $error = array();
- if(empty($this->request->data['Username'])){
- $error[] = "Missing Username parameter";
- }
- if(empty($_REQUEST['Password'])){
- $error[] = "Missing Password parameter";
- }
- if(empty($_REQUEST['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $DeviceId = trim($_REQUEST['DeviceId']);
- if($DeviceId == 'n/a'){
- $DeviceId = uniqid();
- }
- $nick = trim($this->request->data['Username']);
- $password = md5($this->request->data['Password']);
- $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)));
- $roomId = '';
- if(!empty($UserData)){
- if($UserData['User']['visible']==1){
- // Check Device and User Online Status
- $userId = $UserData['User']['id'];
- $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
- /* if user already Login With Other Device */
- if($UserData['User']['online_status']==1){
- // Already Played
- $message = array('Error'=>false,'Status'=>3,'UserId'=>$userId,'Message'=>'User already Played');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- // Check Current Device
- $deviceData = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.DeviceId'=>$DeviceId),'order' =>array('UserDevice.id DESC')));
- if(empty($deviceData)){
- $saveDevData['UserDevice']['UserId'] = $userId;
- $saveDevData['UserDevice']['DeviceId'] = $DeviceId;
- $saveDevData['UserDevice']['PlayStatus'] = 1;
- $this->UserDevice->save($saveDevData);
- $newDeviceId = $this->UserDevice->getInsertID();
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$newDeviceId,'User.last_seen'=>$lastseen), array('User.id' => $userId));
- // Checke User Alredy in Game or Not
- $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$userId,'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
- if(!empty($getSlotData)){
- $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));
- if(!empty($getroomStatus)){
- $roomId = $getroomStatus['Room']['id'];
- }
- }
- // END Above Code
- $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success', 'DeviceId' => $DeviceId);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- if(($UserData['User']['current_device_id']==$deviceData['UserDevice']['id'] ) || ($UserData['User']['id']==$deviceData['UserDevice']['UserId'])){
- // Checke User Alredy in Game or Not
- $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));
- if(!empty($getSlotData)){
- $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));
- if(!empty($getroomStatus)){
- $roomId = $getroomStatus['Room']['id'];
- }
- }
- // END Above Code
- $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success','DeviceId' => $DeviceId);
- $devId = $deviceData['UserDevice']['id'];
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$devId), array('User.id' => $userId));
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else if($deviceData['UserDevice']['UserId'] !=$UserData['User']['id']){
- $devDbId = $deviceData['UserDevice']['id'];
- $userId = $UserData['User']['id'];
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $this->UserDevice->updateAll(array('UserDevice.UserId' =>$userId), array('UserDevice.id' => $devDbId));
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$devDbId,'User.last_seen'=>$lastseen), array('User.id' => $userId));
- // Checke User Alredy in Game or Not
- $getSlotData = $this->RoomSlot->find('first',array('conditions'=>array('RoomSlot.UserId'=>$userId,'RoomSlot.SlotStatus'=>1),'fields'=>array('RoomSlot.RoomId','RoomSlot.UserId','RoomSlot.SlotStatus'),'recursive'=>-1));
- if(!empty($getSlotData)){
- $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));
- if(!empty($getroomStatus)){
- $roomId = $getroomStatus['Room']['id'];
- }
- }
- // END Above Code
- $message = array('Error'=>false,'Status'=>1,'UserId'=>$userId,'RoomId'=>$roomId,'Message'=>'Login Success2', 'DeviceId' => $DeviceId);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $message = array('Error'=>false,'Status'=>4,'UserId'=>$userId,'Message'=>'Device Regitered for other user');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- }
- } else{
- $message = array('Error'=>false,'Status'=>0,'Message'=>'User Inactive');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }else{
- $message = array('Error'=>false,'Status'=>2,'Message'=>'Username OR password Wrong');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @method : verifyEmail
- * @purpose : Verify OTP but verification from App Developer
- * @created_date: 16_06_2016
- */
- public function verifyEmail(){
- $error = array();
- if(empty($this->request->data['Email'])){
- $error[] = "Missing Email parameter";
- }
- if(!empty($error )){
- $message = array('Error'=>true,'Message'=>$error);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $checkemail = $this->checkEmail(trim($this->request->data['Email']));
- if($checkemail){
- $message = array('Error'=>true,'OTP'=>'','Message'=>'This email already register');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $token = mt_rand(10000, 99999);
- $aMsg = "Your access token is: ".$token;
- $this->Session->write('Person.eyeColor', $aMsg);
- $this->AWSSES->to = trim($this->request->data['Email']);
- if($this->AWSSES->_aws_ses('sendmail',$aMsg)){
- $arr = array('Response' => array('Error' => false, 'OTP' => $token, 'Status' => 1, 'Message' => 'OTP Number'));
- echo json_encode($arr);die;
- }else{
- $message = array('Response' =>array('Error'=>true,'OTP'=>2,'Message'=>'Mail Not Send'));
- echo json_encode($message);die;
- }
- }
- }
- }
- /**
- * @method : signUp
- * @purpose : Create New Account
- * @creadted_date : 16_06_2016
- * Email, Username, Password, DeviceId
- */
- public function signUp(){
- $error = array();
- if(empty($this->request->data['Username'])){
- $error[] = "Missing Username parameter";
- }
- if(empty($this->request->data['Email'])){
- $error[] = "Missing Email parameter";
- }
- if(empty($this->request->data['Password'])){
- $error[] = "Missing Password parameter";
- }
- if(empty($this->request->data['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- // Check Username checkUserName
- $cheUname = $this->checkUserName(trim($this->request->data['Username']));
- $checkemail = $this->checkEmail(trim($this->request->data['Email']));
- if($cheUname){
- $message = array('Error'=>false,'Status'=>2,'Message'=>'This username already register');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else if($checkemail){
- $message = array('Error'=>false,'Status'=>3,'Message'=>'This email already register');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $userData['User']['username'] = $this->request->data['Username'];
- $userData['User']['nick'] = $this->request->data['Username'];
- $userData['User']['firstname'] = $this->request->data['Username'];
- $userData['User']['email'] = $this->request->data['Email'];
- $userData['User']['password'] = md5(trim($this->request->data['Password']));
- $this->User->save($userData);
- $userID = $this->User->getInsertID();
- // Save Device Id
- $deviceData['UserDevice']['DeviceId'] = $this->request->data['DeviceId'];
- $deviceData['UserDevice']['UserId'] = $userID;
- $this->UserDevice->save($deviceData);
- // Set Device Id */
- $newDeviceId = $this->UserDevice->getInsertID();
- $this->User->updateAll(array('User.current_device_id'=>$newDeviceId), array('User.id' => $userID));
- $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$userID),'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @purpose : tranfer old account, here login will work according to nick name if login details corrent then set nick name as usename
- * @ Params : Username, Password, DeviceId
- * @created_date : 16_06_2016
- * @ Status : 0- account already transfered, 1: tranasfer success, 2: Username or Password wrong
- */
- public function transferAccount(){
- $error = array();
- if(empty($_REQUEST['Username'])){
- $error[] = "Missing Username parameter";
- }
- if(empty($_REQUEST['Password'])){
- $error[] = "Missing Password parameter";
- }
- if(empty($_REQUEST['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $DeviceId = trim($_REQUEST['DeviceId']);
- $nick = trim($_REQUEST['Username']);
- $password = md5($_REQUEST['Password']);
- $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)));
- if(!empty($UserData)){
- $played_games = $UserData['User']['played_games'];
- $victories = $UserData['User']['victories'];
- $losses = $UserData['User']['losses'];
- $userid = $UserData['User']['id'];
- if($played_games == 0){
- $winpercentage = 0;
- }else{
- $winpercentage = ($victories/$played_games) * 100 ;
- $winpercentage = round($winpercentage);
- }
- if(isset($UserData['User']['username']) && !empty($UserData['User']['username'])){
- $this->set(array('Response' => array('Error'=>false,'Status'=>0,'UserId'=>$UserData['User']['id'],'Message'=>'This account already trasfered'),'_serialize' => array('Response')));
- } else{
- // Assign Device
- $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
- $deviceData['UserDevice']['UserId'] = $UserData['User']['id'];
- $this->UserDevice->save($deviceData);
- $newDeviceId = $this->UserDevice->getInsertID();
- $username = $UserData['User']['nick'];
- $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']));
- $userplaydata['UserPlayGame']['total_game'] = $played_games;
- $userplaydata['UserPlayGame']['win_game'] = $victories;
- $userplaydata['UserPlayGame']['lost_game'] = $losses;
- $userplaydata['UserPlayGame']['win_percent'] = $winpercentage;
- $userplaydata['UserPlayGame']['user_id'] = $userid;
- $this->UserPlayGame->save($userplaydata);
- $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$UserData['User']['id'],'Message'=>'Account transfered Success'),'_serialize' => array('Response')));
- }
- } else{
- $message = array('Error'=>false,'Status'=>2,'Message'=>'Username OR password Wrong');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @method : FbRegister
- * @ created_date : 14_06_2016
- * @ Status : 0- user laready online, 1: Registred Success, 2: email alredy exist, 3: fbid alredy registred 4:username already exists
- */
- public function fbRegister(){
- $error = array();
- if(empty($this->request->data['FbId'])){
- $error[] = "Missing FbId parameter";
- }
- if(empty($this->request->data['Email'])){
- $email = "";
- }else{
- $email = trim($this->request->data['Email']);
- }
- if(empty($this->request->data['Name'])){
- $error[] = "Missing Name parameter";
- }
- if(empty($this->request->data['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(empty($this->request->data['Username'])){
- $error[] = "Missing Username parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $fbemail = $email;
- $fbusername = trim($this->request->data['Username']);
- $fbId = trim($this->request->data['FbId']);
- $socialData = $this->checkfbId($fbId);
- if(empty($socialData)){
- // Check Email Id here
- $chefbemilData = $this->checkFbEmail($fbemail);
- if(empty($fbemail)) {
- $chefbemilData = '';
- }
- $checkuserfbdata = $this->checkFbUsername($fbusername);
- if(empty($fbusername)){
- $checkuserfbdata = "";
- }
- //check if Username or Email Already Exists
- if(!empty($chefbemilData)){
- $this->set(array('Response' => array('Error'=>false,'Status'=>2,'UserId'=>$chefbemilData['User']['id'],'Message'=>'Email already exists'),'_serialize' => array('Response')));
- }
- elseif(!empty($checkuserfbdata)){
- $this->set(array('Response' => array('Error'=>false,'Status'=>4,'UserId'=>$checkuserfbdata['User']['id'],'Message'=>'Username already exists'),'_serialize' => array('Response')));
- }
- else{
- //Fully inseret Data
- $saveUser['User']['fbid'] = trim($this->request->data['FbId']);
- $saveUser['User']['email'] = $email;
- $saveUser['User']['firstname'] = trim($this->request->data['Name']);
- $saveUser['User']['username'] = trim($this->request->data['Username']);
- $this->User->save($saveUser);
- $userId = $this->User->getInsertID();
- $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
- $deviceData['UserDevice']['UserId'] = $userId;
- $this->UserDevice->save($deviceData);
- $DataDeviceId = $this->UserDevice->getInsertID();
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$DataDeviceId,'User.last_seen'=>$lastseen), array('User.id' =>$userId));
- $this->set(array('Response' => array('Error'=>false,'Status'=>1,'UserId'=>$userId,'Message'=>'Success'),'_serialize' => array('Response')));
- }
- } else{
- // Updation
- if($socialData['User']['online_status']==1){
- $this->set(array('Response' => array('Error'=>false,'Status'=>0,'UserId'=>$socialData['User']['id'],'Message'=>'User already online'),'_serialize' => array('Response')));
- } else{
- $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
- $deviceData['UserDevice']['UserId'] = $socialData['User']['id'];
- $this->UserDevice->save($deviceData);
- $newDeviceId = $this->UserDevice->getInsertID();
- $fname = $this->request->data['Name'];
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $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']));
- $this->set(array('Response' => array('Error'=>false,'Status'=>3,'UserId'=>$socialData['User']['id'],'Message'=>'FBid already registred'),'_serialize' => array('Response')));
- }
- }
- }
- }
- /**
- * @method : updateProfile
- * @purpose : Update Login User Data like: Name, Bio etc
- * @params : Picture, Name, Bio,UserId
- * created_date: 17_06_2016
- * updated_date: 21_06_2016
- */
- public function updateProfile(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(empty($this->request->data['Name'])){
- $error[] = "Missing Name parameter";
- }
- if(empty($this->request->data['Bio'])){
- $error[] = "Missing Bio parameter";
- }
- if($this->request->data['PictureIndex']==''){
- $error[] = "Missing PictureIndex parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $filename ='';
- $firstname = trim($this->request->data['Name']);
- $info = trim($this->request->data['Bio']);
- $UserId = trim($this->request->data['UserId']);
- $PictureIndex = trim($this->request->data['PictureIndex']);
- $userData = $this->User->find('first',array('fields'=>array('PictureIndex'),'conditions'=>array('User.id'=>$UserId)));
- if(!empty($userData['User']['fbid']) || ($userData['User']['fbid'] != 0) ){
- $this->User->updateAll(array('User.PictureIndex'=>4), array('User.id' => $UserId));
- }else{
- $this->User->updateAll(array('User.PictureIndex'=>$PictureIndex), array('User.id' => $UserId));
- }
- if(!empty($_FILES)){
- // Update User Profile
- $filename = time().$_FILES['Picture']['name'];
- move_uploaded_file($_FILES['Picture']['tmp_name'], WWW_ROOT . 'img/members/' . $filename);
- $this->User->updateAll(array('User.firstname' =>'"'.$firstname.'"','User.info' =>'"'.$info.'"','User.pic'=>'"'.$filename.'"'), array('User.id' => $UserId));
- $message = array('Error'=>false,'Message'=>'Success');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $this->User->updateAll(array('User.firstname' =>'"'.$firstname.'"','User.info' =>'"'.$info.'"'), array('User.id' => $UserId));
- $message = array('Error'=>false,'Message'=>'Success');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @ method : getProfile
- * @ params : UserId
- * @ created_date : 17_06_2016
- */
- public function getProfile(){
- if(empty($_REQUEST['UserId'])){
- $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
- } else{
- $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'])));
- $picUrl = ($userData['User']['pic'])?HTTP_ROOT.'app/webroot/img/members/'.$userData['User']['pic']: $userData['User']['pic'];
- if(!empty($userData['User']['fbid']) || ($userData['User']['fbid'] != 0) ){
- $userData['User']['PictureIndex'] = 4;
- }
- $data['UserId'] = $userData['User']['id'];
- $data['FbId'] = $userData['User']['fbid'];
- $data['Username'] = $userData['User']['username'];
- $data['Name'] = $userData['User']['firstname'];
- $data['Bio'] = $userData['User']['Bio'];
- $data['Statas'] = $userData['User']['Statas'];
- $data['PictureIndex'] = $userData['User']['PictureIndex'];
- $data['Star'] = $userData['User']['star'];
- if(empty($userData['UserPlayGame']['fairPlay'])){
- $data['FairPlay'] = 0;
- }else{
- $data['FairPlay'] = $userData['UserPlayGame']['fairPlay'];
- }
- if(empty($userData['UserPlayGame']['win_percent'])){
- $data['WinPercent'] = 0 ;
- }else{
- $data['WinPercent'] = $userData['UserPlayGame']['win_percent'];
- }
- if(empty($userData['UserPlayGame']['total_game'])){
- $data['TotalGame'] = 0;
- }else{
- $data['TotalGame'] = $userData['UserPlayGame']['total_game'];
- }
- $picUrl = ($userData['User']['pic'])? HTTP_ROOT.'app/webroot/img/members/'.$userData['User']['pic']: $userData['User']['pic'];
- $data['Picture'] = $picUrl;
- $this->set(array('Response' => array('Error'=>false,'Message'=>'User data','UserData'=>$data),'_serialize' => array('Response')));
- }
- }
- /**
- * @ method : getBadges
- * @ params : UserId
- * @ created_date : 17_06_2016
- */
- public function getAchievements(){
- if(empty($_REQUEST['UserId'])){
- $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
- } else{
- $this->loadmodel('UserBadge');
- $userId = trim($_REQUEST['UserId']);
- $BadgesData = $this->UserBadge->find('first',array('fields'=>array('UserBadge.AchievementData'),'conditions'=>array('UserBadge.user_id'=>$userId)));
- if(!empty($BadgesData)){
- $this->set(array('Response' => array('Error'=>false,'AchievementData'=>$BadgesData['UserBadge']['AchievementData'],'Message'=>'Achievements data'),'_serialize' => array('Response')));
- } else{
- $this->set(array('Response' => array('Error'=>false,'AchievementData'=>'','Message'=>'Achievements data'),'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @method : setBadges
- * @prams : UserId,name
- * @created_date : 17_06_2016
- */
- public function setBadges(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(empty($this->request->data['AchievementData'])){
- $error[] = "Missing AchievementData parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $this->loadmodel('UserBadge');
- // Check this Badge alredy added or not
- $badgename = trim($this->request->data['AchievementData']);
- $userId = trim($this->request->data['UserId']);
- $checkBadge = $this->UserBadge->find('count',array('conditions'=>array('UserBadge.user_id'=>$userId)));
- if($checkBadge>0){
- // Update
- $this->UserBadge->updateAll(array('UserBadge.AchievementData' =>'"'.$badgename.'"'),array('UserBadge.user_id' =>$userId));
- $message = array('Error'=>false,'Message'=>'Badges updated');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else{
- $saveData['UserBadge']['user_id'] = $userId;
- $saveData['UserBadge']['AchievementData'] = $badgename;
- $this->UserBadge->save($saveData);
- $message = array('Error'=>false,'Message'=>'Success');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @method : updateSetting
- * @params : UserId, StatasStatus
- * @created_date : 17_06_2016
- */
- public function updateSetting(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if($this->request->data['StatasStatus']==''){
- $error[] = "Missing StatasStatus parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $UserId = trim($this->request->data['UserId']);
- $status = trim($this->request->data['StatasStatus']);
- $this->User->updateAll(array('User.statas' =>$status), array('User.id' =>$UserId));
- $message = array('Error'=>false,'Message'=>'Success');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- /**
- * @method : forgotPassword
- * @params : Email
- * @created : 18_06_2016
- *
- */
- public function forgotPassword(){
- if(empty($_REQUEST['Email'])){
- $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing Email field'),'_serialize' => array('Response')));
- } else{
- $email = trim($_REQUEST['Email']);
- $userData = $this->User->find('first',array('conditions'=>array('User.email'=>$email,'User.username !='=>''),'fields'=>array('User.id','User.email','User.username'),'recursive'=>-1));
- if(!empty($userData)){
- // Send Mail
- $userId = $userData['User']['id'];
- $rnadPass = $this->randPass();
- $password = md5($rnadPass);
- $checkup = $this->User->updateAll(array('User.password' =>'"'.$password.'"'), array('User.id' =>$userId));
- if($checkup){
- $aMsg = "<p>Your Username is: ".$userData['User']['username']."</p>";
- $aMsg .= "Your password is: ".$rnadPass;
- $this->Session->write('Person.forgotpass', $aMsg);
- $this->AWSSES->to = trim($email);
- if($this->AWSSES->_aws_ses('forgot',$aMsg)){
- $arr = array('Response' => array('Error'=>false,'Message'=>'Your Login password has been sent to your mail. Please check.'));
- echo json_encode($arr);
- }else{
- $message = array('Response' =>array('Error'=>true,'Message'=>'Email failed'));
- echo json_encode($message);
- }
- } else{
- $message = array('Error'=>true,'Message'=>'Failed !');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- $message = array('Error'=>false,'Message'=>'Your Login password has been sent to your mail. Please check.');
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- /**
- * @get 6 digint random password
- *
- */
- public function randPass(){
- $alphabets = range('a','z');
- $numbers = range('0','9');
- $final_array = array_merge($alphabets,$numbers);
- $password = '';
- $length = 7;
- while($length--) {
- $key = array_rand($final_array);
- $password .= $final_array[$key];
- }
- return $password;
- }
- /**
- * @method : logOut
- * @purpose : Delete User's Current DeviceId
- * @created_date : 20_06_2016
- */
- public function logOut(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(empty($this->request->data['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- try{
- $userId = trim($this->request->data['UserId']);
- $this->removidleuser($userId);
- $DeviceId = trim($this->request->data['DeviceId']);
- $this->UserDevice->query("DELETE FROM user_devices where UserId=? and DeviceId = ?",[$userId,$DeviceId]);
- // Now Update Current Device field in Users Tabls
- $this->User->query("UPDATE users SET current_device_id = '', online_status=0 where id=$userId");
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
- } catch (Exception $e) {
- $this->set(array('Response' => array('Error'=>true,'Message'=>$e->getMessage()),'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @check EmailId already Registrd or not
- * @created_date : 15_06_2016
- */
- public function checkEmail($email=null) {
- $records1 = $this->User->find('count',array('conditions'=>array('User.email'=>$email)));
- if($records1>0){
- return 1;
- } else{
- return 0;
- }
- }
- /**
- * @check username user_badges UserBadge
- * @ check useranem unique
- * @ create_date: 16_06_2016
- */
- public function checkUserName($username=null){
- $usernameCount = $this->User->find('count',array('conditions'=>array('OR' => array(array('User.username' =>$username),array('User.nick' =>$username)))));
- if($usernameCount>0){
- return 1;
- } else{
- return 0;
- }
- }
- /**
- * @ check fbId already registred or Not
- * @ created_date: 16_06_2016
- */
- public function checkfbId($fbid=null){
- $userData = $this->User->find('first',array('conditions' =>array('fbid'=>$fbid)));
- return $userData;
- }
- /**
- * @ Check fb email already registred or Not
- *
- */
- public function checkFbEmail($fbemail=null){
- $userData2 = $this->User->find('first',array('fields'=>array('id','email','online_status'),'conditions'=>array('User.email'=>$fbemail)));
- return $userData2;
- }
- public function checkFbUsername($username=null) {
- $checkUsername = $this->User->find('first',array('fields'=>array('id','email','online_status'),
- 'conditions'=>array('User.username'=>$username)));
- return $checkUsername;
- }
- /*
- status: 0 not registered, 1 online, 2 offline
- */
- public function checkdevice() {
- $error = array();
- if(empty($this->request->data['FbId'])){
- $error[] = "Missing FbId parameter";
- }
- if(empty($this->request->data['DeviceId'])){
- $error[] = "Missing DeviceId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $userfbid = $this->request->data['FbId'];
- $deviceid = $this->request->data['DeviceId'];
- $userdata = $this->User->find('first',array('conditions'=>array('User.fbid' => $userfbid)));
- if(empty($userdata)){
- $this->set(array('Response' => array('Error'=> false,'Status'=> 0,'Message' => 'The user is not registered'),'_serialize'=>array('Response')));
- }else{
- $userstatus = $userdata['User']['online_status'];
- $currentdevice = $userdata['User']['current_device_id'];
- $userid = $userdata['User']['id'];
- $devicedata = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.UserId' => $userid,'UserDevice.id' => $currentdevice),'order' =>array('UserDevice.id DESC')));
- if(empty($devicedata)) {
- $currentdeviceid = "";
- }else{
- $currentdeviceid = $devicedata['UserDevice']['DeviceId'];
- }
- if($userstatus == 1 && $currentdeviceid != $deviceid ) {
- $this->set(array('Response' => array('UserId' => $userid,'Error'=> false,'Status'=> 1,'Message' => 'The user is registered'),'_serialize'=>array('Response')));
- }else{
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $deviceData['UserDevice']['DeviceId'] = trim($this->request->data['DeviceId']);
- $deviceData['UserDevice']['UserId'] = $userid;
- $this->UserDevice->save($deviceData);
- $DataDeviceId = $this->UserDevice->getInsertID();
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- $this->User->updateAll(array('User.online_status' =>1,'User.current_device_id'=>$DataDeviceId,'User.last_seen'=>$lastseen), array('User.id' =>$userid));
- $this->set(array('Response' => array('UserId' => $userid,'Error'=> false,'Status'=> 2,'Message' => 'The user is registered'),'_serialize'=>array('Response')));
- }
- }
- }
- }
- /**
- * @method : merge
- * Params : UserId, FbId
- * Purpose : Merge FBId after Siple Email Login
- * Create_date : 28_06_2016
- */
- public function mergeFacebookId(){
- $error = array();
- if(empty($this->request->data['FbId'])){
- $error[] = "Missing FbId parameter";
- }
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $fbId = trim($this->request->data['FbId']);
- $userId = trim($this->request->data['UserId']);
- $fbuser = $this->User->find('first',array('conditions'=>array('User.fbid'=>$fbId),'fields'=>array('User.id','User.fbid')));
- if(!empty($fbuser)){
- // if FbId for Othe User
- if( $fbuser['User']['id'] !=$userId){
- $this->set(array('Response' => array('Error'=> false,'Status'=> 2,'Message' => 'This FbId alredy set for other user'),'_serialize'=>array('Response')));
- } else{
- $this->set(array('Response' => array('Error'=> false,'Status'=> 3,'Message' => 'This FbId alredy set for current user'),'_serialize'=>array('Response')));
- }
- } else{
- // Set This FB id to Current User
- // One New Case: if Fb Id new but this user alredy connected with other FB then Status: 4
- $getUser=$this->User->find('first',array('conditions'=>array('User.id'=>$userId),'fields'=>array('User.id','User.fbid')));
- if(isset($getUser['User']['fbid']) && !empty($getUser['User']['fbid']) ){
- $this->set(array('Response' => array('Error'=> false,'Status'=> 4,'Message' => 'This user alredy connected with other fbid'),'_serialize'=>array('Response')));
- } else{
- // Now Update
- $this->User->updateAll(array('User.fbid' =>'"'.$fbId.'"'), array('User.id' =>$userId));
- $this->set(array('Response' => array('Error'=> false,'Status'=> 1,'Message' => 'Success'),'_serialize'=>array('Response')));
- }
- }
- }
- }
- /**
- * @method : addFriend
- * @Params : UserId, FriendId
- * Created_date : 29_06_2016
- */
- public function addFriend(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(empty($this->request->data['FriendId'])){
- $error[] = "Missing FriendId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- /** Check User Already friend List */
- $this->loadmodel('UserFriend');
- $userId = trim($this->request->data['UserId']);
- $FriendId = trim($this->request->data['FriendId']);
- $Data = $this->UserFriend->find('first',array('conditions'=>array('UserFriend.UserId'=>$userId,'UserFriend.FriendId'=>$FriendId),'fields'=>array('UserFriend.id'),'recursive'=>-1));
- if(!empty($Data)){
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Already in friend list'),'_serialize' => array('Response')));
- } else{
- // Add friend
- $this->UserFriend->save($this->request->data);
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
- }
- }
- }
- /**
- * @method : removeFriend
- * @Params : UserId, FriendId
- * @created_date : 29_06_2016
- */
- public function removeFriend(){
- $error = array();
- if(empty($this->request->data['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(empty($this->request->data['FriendId'])){
- $error[] = "Missing FriendId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- } else{
- $this->loadmodel('UserFriend');
- $userId = trim($this->request->data['UserId']);
- $FriendId = trim($this->request->data['FriendId']);
- $this->UserFriend->deleteAll(array('UserFriend.UserId'=>$userId,'UserFriend.FriendId'=>$FriendId));
- $this->set(array('Response'=>array('Error'=>false,'Message'=>'Success'),'_serialize' => array('Response')));
- }
- }
- /**
- * @method : getFriendList
- * @Params : UserId
- */
- public function getFriendList(){
- if(empty($_REQUEST['UserId'])){
- $this->set(array('Response' => array('Error'=>true,'Message'=>'Missing UserId field'),'_serialize' => array('Response')));
- } else{
- //
- $this->loadmodel('UserFriend');
- $userId = trim($_REQUEST['UserId']);
- $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') ));
- $data = array();
- if(!empty($friendData)){
- foreach($friendData as $key=>$val){
- if(isset($val['User']['id'])){
- $data[$key]['UserId'] = $val['User']['id'];
- $data[$key]['Star'] = $val['User']['star'];
- $data[$key]['Name'] = $val['User']['username'];
- $data[$key]['FbId'] = $val['User']['fbid'];
- $data[$key]['OnlineStatus'] = $val['User']['online_status'];
- $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
- $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
- $data[$key]['Picture'] = $picUrl;
- }
- }
- $data=array_values($data);
- }
- $this->set(array('Response' => array('Error'=>false,'CustomFriendData'=>$data),'_serialize' => array('Response')));
- }
- }
- /**
- * @method : lastSeen
- * @Params : UserId
- */
- public function lastSeen(){
- $error = array();
- if(empty($_REQUEST['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else{
- $userId = $_REQUEST['UserId'];
- $data=array();
- $userplaydata = $this->UserPlayGame->find('all', array('conditions' => array('UserPlayGame.user_id'=> $userId), 'recursive' => -1));
- if(count($userplaydata) > 0){
- $homerun = $userplaydata[0]['UserPlayGame']['HomeRun'];
- $Forcer = $userplaydata[0]['UserPlayGame']['Forcer'];
- $Ace = $userplaydata[0]['UserPlayGame']['Ace'];
- $Unstoppable = $userplaydata[0]['UserPlayGame']['Unstoppable'];
- $Winner = $userplaydata[0]['UserPlayGame']['Winner'];
- $Player = $userplaydata[0]['UserPlayGame']['Player'];
- $Looser = $userplaydata[0]['UserPlayGame']['Looser'];
- $FullHouse = $userplaydata[0]['UserPlayGame']['FullHouse'];
- $WinStreak = $userplaydata[0]['UserPlayGame']['WinStreak'];
- $XthreeWinStreak = $userplaydata[0]['UserPlayGame']['XthreeWinStreak'];
- $XfiveWinstreak = $userplaydata[0]['UserPlayGame']['XfiveWinstreak'];
- $XtenWinstreak = $userplaydata[0]['UserPlayGame']['XtenWinstreak'];
- $data = array($Ace,$Unstoppable,$Winner,$Player,$XthreeWinStreak,$XfiveWinstreak,$XtenWinstreak,$Looser,$FullHouse,$homerun,$Forcer);
- }
- $lastseen = strtotime(date('Y-m-d H:i:s'));
- if(empty($_REQUEST['DeviceId'])){
- $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data),'_serialize' => array('Response')));
- }else{
- $deviceid = $_REQUEST['DeviceId'];
- $devicedata = $this->UserDevice->find('first',array('conditions'=>array('UserDevice.UserId' => $userId,'UserDevice.DeviceId' => $deviceid),'order' =>array('UserDevice.id DESC')));
- if(empty($devicedata)) {
- $currentdeviceid = "";
- }else{
- $currentdeviceid = $devicedata['UserDevice']['id'];
- }
- $chkusersdevice = $this->User->find('first',array('conditions' => array('User.id' => $userId),'recursive' => -1));
- if($chkusersdevice['User']['online_status'] == 1){
- if($chkusersdevice['User']['current_device_id'] == $currentdeviceid){
- $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
- //*******room termination 21-11-2016 *****//
- if(empty($_REQUEST['RoomId'])){
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data, 'AutoTerminateTable' => false),'_serialize' => array('Response')));
- }else{
- $autoterminate = true;
- $curentRoomId = $_REQUEST['RoomId'];
- $chkRoomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $curentRoomId), 'recursive' => -1));
- //check if room exists or not
- if((!empty($chkRoomdata)) ) {
- //to check if current time is greater that time limit for room termination
- $roomterminateTime = $chkRoomdata['Room']['auto_terminate_update'];
- $joinmemberCount = $chkRoomdata['Room']['join_member_count'];
- $roomStatus = $chkRoomdata['Room']['room_status'];
- $getGameplayData = $this->GamePlayData->find('first', array('conditions' => array('GamePlayData.RoomId' => $curentRoomId), 'recursive' => -1));
- if((strtotime(date('Y-m-d H:i:s')) - strtotime(date($roomterminateTime))) >= 150 && !empty($chkRoomdata['Room']['PhotonId']) && ( empty($getGameplayData) || ($joinmemberCount < 4 && $roomStatus != 2))) {
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $curentRoomId));
- $this->Room->delete($curentRoomId);
- $autoterminate = true;
- }
- else {
- $autoterminate = false;
- }
- }
- $this->set(array('Response'=>array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data, 'AutoTerminateTable' => $autoterminate),'_serialize'=>array('Response')));
- }
- //*******room termination *****//
- }elseif($chkusersdevice['User']['current_device_id'] != $currentdeviceid){
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>'AlreadySignin'),'_serialize' => array('Response')));
- }
- }else{
- $this->User->updateAll(array('User.online_status' =>1,'User.last_seen'=>$lastseen), array('User.id' => $userId));
- $this->set(array('Response' => array('Error'=>false,'Message'=>'Success','IsAvailable' =>true,'UserAchievementData' => $data),'_serialize' => array('Response')));
- }
- }
- }
- }
- /**
- * @created_date : 15_07_2016
- */
- public function searchUser() {
- $error = array();
- if(empty($_REQUEST['UserId'])){
- $error[] = "Missing UserId parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else{
- $this->loadmodel('UserFriend');
- $userId = trim($_REQUEST['UserId']);
- $limit = 50;
- if(!empty($_REQUEST['Limit'])){
- $limit = $_REQUEST['Limit'];
- }
- $page = 1;
- if(!empty($this->request->data['page'])){
- $page = $this->request->data['page'];
- }
- $offset = ( $page - 1 ) * $limit;
- $UserFrinds = $this->UserFriend->find('list',array('conditions'=>array('UserFriend.UserId'=>$userId),'fields'=>array('UserFriend.FriendId'),'recursive'=>-1));
- array_push($UserFrinds,$userId);
- if(isset($_REQUEST['Username']) && !empty($_REQUEST['Username'])){
- // Get List Of UserFriend
- $username = trim($_REQUEST['Username']);
- $userData = $this->User->find('all', array(
- 'conditions' => array("NOT" => array( "User.id" => $UserFrinds),'Username LIKE'=>'%'.$username.'%','User.username !='=>''),
- 'fields'=>array('User.id','User.username','User.fbid','User.star','User.pic','User.online_status','User.PictureIndex'),
- 'limit' => $limit,
- 'page' => $page,
- 'offset'=>$offset,'recursive'=>-1));
- $data = array();
- if(!empty($userData)){
- foreach($userData as $key=>$val){
- $data[$key]['UserId'] = $val['User']['id'];
- $data[$key]['Name'] = $val['User']['username'];
- $data[$key]['FbId'] = $val['User']['fbid'];
- $data[$key]['Star'] = $val['User']['star'];
- $data[$key]['OnlineStatus'] = $val['User']['online_status'];
- $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
- $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
- $data[$key]['Picture'] = $picUrl;
- }
- }
- $this->set(array('Response' => array('Error'=>false,'UserData'=>$data),'_serialize' => array('Response')));
- } else{
- // Game Played by This User
- $userData = $this->User->find('all', array(
- 'conditions' => array("NOT" => array( "User.id" => $UserFrinds),'User.username !='=>''),
- 'fields'=>array('User.id','User.username','User.fbid','User.star','User.pic','User.online_status','User.PictureIndex'),
- 'limit' => $limit,
- 'page' => $page,
- 'offset'=>$offset,'order' => 'rand()','recursive'=>-1));
- $data = array();
- if(!empty($userData)){
- foreach($userData as $key=>$val){
- $data[$key]['UserId'] = $val['User']['id'];
- $data[$key]['Name'] = $val['User']['username'];
- $data[$key]['FbId'] = $val['User']['fbid'];
- $data[$key]['Star'] = $val['User']['star'];
- $data[$key]['OnlineStatus'] = $val['User']['online_status'];
- $data[$key]['PictureIndex'] = $val['User']['PictureIndex'];
- $picUrl = ($val['User']['pic']) ? HTTP_ROOT.'img/members/'.$val['User']['pic']: $val['User']['pic'];
- $data[$key]['Picture'] = $picUrl;
- }
- }
- $this->set(array('Response' => array('Error'=>false,'UserData'=>$data),'_serialize' => array('Response')));
- }
- }
- }
- public function GetStar(){
- $error = array();
- if(empty($_REQUEST['Id'] )){
- $error[] = "Missing Id parameter";
- }
- if(empty($_REQUEST['IdType'] )){
- $error[] = "Missing IdType parameter";
- }
- if(!empty($error )){
- $erroMsg = implode(',', $error);
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }else{
- if($_REQUEST['IdType'] == 1){
- $userid = $_REQUEST['Id'];
- $fbid = '';
- $getstarinfo = $this->User->find('first' , array('conditions' => array('User.id' => $userid), 'fields' => array('star'), 'recursive' => -1));
- if(!empty($getstarinfo)){
- $star = $getstarinfo['User']['star'];
- }else{
- $star = 0 ;
- }
- $this->set(array('Response' => array('Error'=>false,'Star'=> $star),'_serialize' => array('Response')));
- }elseif($_REQUEST['IdType'] == 2){
- $fbid = $_REQUEST['Id'];
- $userid = '';
- $getstarinfo = $this->User->find('first' , array('conditions' => array('User.fbid ' => $fbid), 'fields' => array('star'), 'recursive' => -1));
- if(!empty($getstarinfo)){
- $star = $getstarinfo['User']['star'];
- }else
- $star = 0 ;
- $this->set(array('Response' => array('Error'=>false,'Star'=> $star),'_serialize' => array('Response')));
- }else{
- $idtype = $_REQUEST['IdType'];
- $this->set(array('Response'=>array('Error'=>false,'Message'=>'wrong IdType '.$idtype.''),'_serialize' => array('Response')));
- }
- }
- }
- public function saveRoundScore() {
- $data = $_POST['JsonData'];
- $json = ''.$data.'';
- if(empty($json)){
- $errorData = array('Error' => true, 'Message' => 'No Data Parameters');
- $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
- }
- $yummy = json_decode($json);
- $RoomData = $yummy;
- if(!empty($RoomData)){
- $error = array();
- $senderId = $RoomData->SenderId ;
- $RoomId = $RoomData->RoomId;
- $RoundIndex = $RoomData->RoundIndex;
- if(empty($senderId)) {
- $error[] = "SenderId parameter is empty.";
- }
- if(empty($RoomId)){
- $error[] = "RoomId parameter is empty.";
- }
- if(empty($RoundIndex)){
- $error[] = "RoundIndex parameter is empty.";
- }
- $ScoreDataArray = $RoomData->ScoreData ;
- if(empty($ScoreDataArray)){
- $error[] = "ScoreData parameter is empty.";
- }
- if(!empty($error)){
- $errorMessage = implode(',',$error);
- $errorData = array('Error' => true, 'Message' => $errorMessage);
- $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
- }else{
- $checkdata = $this->RoundScore->find('all',array('conditions'=>array('RoundScore.RoomId' => $RoomId,'RoundScore.RoundIndex' => $RoundIndex)));
- if(count($checkdata) > 0 ){
- $this->set(array('Response'=>array('Error'=>false , 'Message' => 'Round already exist'), '_serialize' => array('Response')));
- }else {
- $teamAarray = $ScoreDataArray[0]->TeamA ;
- $teamAscore = $teamAarray->Score;
- $teamBarray = $ScoreDataArray[1]->TeamB ;
- $teamBscore = $teamBarray->Score;
- if(!empty($teamAscore)){
- $teamAscore = $teamAscore ;
- }else{
- $teamAscore = 0 ;
- }
- $TeamAusers = implode(',',$teamAarray->Users);
- $created_at = strtotime(date('Y-m-d H:i:s'));
- if( $teamAscore > $teamBscore){
- $roundResultA = 'W';
- }elseif($teamAscore < $teamBscore){
- $roundResultA = 'R';
- }elseif($teamAscore == $teamBscore){
- $roundResultA = 'D';
- }
- $teamAdata = array('Result' => $roundResultA,'SenderId' => $senderId, 'RoomId' => $RoomId ,'Score' => $teamAscore ,'RoundIndex' => $RoundIndex ,'Team' => 'A' , 'TeamUsers' => $TeamAusers, 'created_at' => $created_at);
- if(!empty($teamBscore)){
- $teamBscore = $teamBscore ;
- }else{
- $teamBscore = 0 ;
- }
- if($teamBscore > $teamAscore){
- $roundResultB = 'W';
- }elseif($teamBscore < $teamAscore){
- $roundResultB = 'R';
- }elseif($teamBscore == $teamAscore){
- $roundResultB = 'D';
- }
- $TeamBusers = implode(',',$teamBarray->Users);
- $teamBdata = array('Result' => $roundResultB,'SenderId' => $senderId, 'RoomId' => $RoomId ,'Score' => $teamBscore ,'RoundIndex' => $RoundIndex ,'Team' => 'B' , 'TeamUsers' => $TeamBusers, 'created_at' => $created_at);
- $data1 = $teamAdata ;
- $data2 = $teamBdata ;
- $allData = array($data1, $data2);
- $saveData = $this->RoundScore->saveAll($allData);
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
- }
- }
- }else{
- $erroMsg = "JsonData parameter is empty";
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- public function SaveFinalScore() {
- $data = $_POST['JsonData'];
- $json = ''.$data.'';
- $yummy = json_decode($json);
- $RoomData = $yummy;
- if(!empty($RoomData)){
- $error = array();
- $senderId = $RoomData->SenderId ;
- $RoomId = $RoomData->RoomId;
- if(empty($senderId)) {
- $error[] = "SenderId parameter is empty.";
- }
- if(empty($RoomId)){
- $error[] = "RoomId parameter is empty.";
- }
- $ScoreDataArray = $RoomData->ScoreData ;
- if(empty($ScoreDataArray)){
- $error[] = "ScoreData parameter is empty.";
- }
- if(!empty($error)){
- $errorMessage = implode(',',$error);
- $errorData = array('Error' => true, 'Message' => $errorMessage);
- $this->set(array('Response' => $errorData , '_serialize' => array('Response')));
- }else{
- $checkdata = $this->FinalScore->find('all',array('conditions'=>array('FinalScore.RoomId' => $RoomId)));
- if(count($checkdata) > 0 ){
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Room data already exists.'), '_serialize' => array('Response')));
- } else {
- $createdAt = strtotime(date('Y-m-d H:i:s'));
- $WinnerArray = $ScoreDataArray[0]->Winner;
- $WinnerScore = $WinnerArray->Score;
- $WinPlayers = implode(',',$WinnerArray->Users);
- $WinnerData = array('SenderId' => $senderId, 'RoomId' => $RoomId, 'Result' => 'W', 'Players' => $WinPlayers, 'Score' => $WinnerScore, 'CreatedAt' => $createdAt);
- $RunnerArray = $ScoreDataArray[1]->Runner;
- $RunnerScore = $RunnerArray->Score;
- $RunnerPlayers = implode(',',$RunnerArray->Users);
- $RunnerData = array('SenderId' => $senderId, 'RoomId' => $RoomId, 'Result' => 'R', 'Players' => $RunnerPlayers, 'Score' => $RunnerScore, 'CreatedAt' => $createdAt);
- $allPlayers = array_merge($WinnerArray->Users,$RunnerArray->Users);
- $total_rounds = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.TeamUsers' => $WinPlayers ), 'recursive' => -1));
- $getRoundDatawinnner = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.Result' => 'W', 'RoundScore.TeamUsers' => $WinPlayers ), 'recursive' => -1));
- $whouseVal = array();
- foreach($getRoundDatawinnner as $key => $val){
- $roundScore = $val['RoundScore']['Score'];
- if($roundScore == 14){
- $whouseVal[] = $roundScore ;
- }else{
- $whouseVal[] = '';
- }
- }
- $WinnerFullHouse = count(array_filter($whouseVal));
- $getRoundDatarunner = $this->RoundScore->find('all',array('conditions' => array('RoundScore.RoomId' => $RoomId,'RoundScore.Result' => 'R', 'RoundScore.TeamUsers' => $RunnerPlayers ), 'recursive' => -1));
- $rhouseVal = array();
- foreach($getRoundDatarunner as $key => $val){
- $roundScore = $val['RoundScore']['Score'];
- if($roundScore == 14){
- $rhouseVal[] = $roundScore ;
- }else{
- $rhouseVal[] = '';
- }
- }
- $RunnerFullHouse = count(array_filter($rhouseVal));
- //Save Data For Winnner
- $newplayerdata = array();
- $UpdateWinArray = '';
- foreach($WinnerArray->Users as $key => $val) {
- $playerid = $val;
- $currentdate =date('Y-m-d');
- $dateexplode = explode('-',$currentdate);
- $currentmonth = $dateexplode['1'];
- $playDateData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Date(CreatedOn)' => $currentdate)));
- if(!empty($playDateData)) {
- $noOfgaemesplayedtoday = count($playDateData);
- }else{
- $noOfgaemesplayedtoday = 1;
- }
- $playMonthData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Month(CreatedOn)' => $currentmonth)));
- if(!empty($playMonthData)) {
- $noOfGameplaythismonth = count($playMonthData);
- }else{
- $noOfGameplaythismonth = 1;
- }
- $playWeekData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','YEARWEEK(CreatedOn)=YEARWEEK(CURDATE(), 1)')));
- if(!empty($playWeekData)){
- $noOfGameplayedthisWeek = count($playWeekData);
- }else{
- $noOfGameplayedthisWeek = 1;
- }
- $userGameplayData = $this->UserPlayGame->find('all',array('conditions' => array('UserPlayGame.user_id' => $playerid)));
- if(empty($userGameplayData)){
- $total_game = 1;
- $user_id = $playerid;
- $win_game = 1;
- $lost_game = 0;
- $win_percent = 100;
- if($WinnerScore == 75)
- $Ace = 1;
- else
- $Ace = 0;
- if(count($total_rounds) == 5)
- $Unstoppable = 1;
- else
- $Unstoppable = 0;
- $Winner = 1;
- $player = 1;
- $WfullHouse = $WinnerFullHouse;
- $WinStreak = 1;
- $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);
- }else{
- $total_game = $userGameplayData[0]['UserPlayGame']['total_game'];
- $user_id = $playerid;
- $win_game = $userGameplayData[0]['UserPlayGame']['win_game'];
- $lost_game = $userGameplayData[0]['UserPlayGame']['lost_game'];
- $oldAce = $userGameplayData[0]['UserPlayGame']['Ace'];
- if($WinnerScore == 75){
- $newAce = $oldAce + 1;
- }else{
- $newAce = $oldAce ;
- }
- $oldWinnnerVal = $userGameplayData[0]['UserPlayGame']['Winner'];
- $newWinnerVal = $oldWinnnerVal + 1;
- $oldPalyerVal = $userGameplayData[0]['UserPlayGame']['Player'];
- $newPlayerval = $oldPalyerVal + 1;
- $oldUnstoppable = $userGameplayData[0]['UserPlayGame']['Unstoppable'];
- if(count($total_rounds) == 5){
- $Unstoppable = $oldUnstoppable + 1;
- }else{
- $Unstoppable = $oldUnstoppable;
- }
- $oldwinfullhouse = $userGameplayData[0]['UserPlayGame']['FullHouse'];
- $WfullHouse = $WinnerFullHouse + $oldwinfullhouse;
- $oldWinstreak = $userGameplayData[0]['UserPlayGame']['WinStreak'];
- $newWinstreak = $oldWinstreak + 1;
- $divisiblebyten = 0;
- $divisiblebyfive = 0;
- $divisiblebythree = 0;
- if($newWinstreak % 10 == 0) {
- $divisiblebyten = 1 ;
- }
- if ($newWinstreak % 5 == 0) {
- $divisiblebyfive = 1 ;
- }
- if ($newWinstreak % 3 == 0) {
- $divisiblebythree = 1 ;
- }
- $oldXtenWinstreak = $userGameplayData[0]['UserPlayGame']['XtenWinstreak'];
- $oldXfiveWinstreak = $userGameplayData[0]['UserPlayGame']['XfiveWinstreak'];
- $oldXthreeWinStreak = $userGameplayData[0]['UserPlayGame']['XthreeWinStreak'];
- if($divisiblebyten == 1){
- $newXtenWinstreak = $oldXtenWinstreak + 1;
- }else{
- $newXtenWinstreak = $oldXtenWinstreak ;
- }
- if($divisiblebyfive == 1){
- $newXfiveWinstreak = $oldXfiveWinstreak + 1;
- }else{
- $newXfiveWinstreak = $oldXfiveWinstreak;
- }
- if($divisiblebythree == 1){
- $newXthreeWinStreak = $oldXthreeWinStreak + 1;
- }else{
- $newXthreeWinStreak = $oldXthreeWinStreak ;
- }
- $newtotal_game = $total_game + 1;
- $newin_game = $win_game + 1;
- $newwinpercentage = ($newin_game/$newtotal_game) * 100 ;
- $newpercentage = round($newwinpercentage);
- $created_date = strtotime(date('Y-m-d H:i:s'));
- $oldfairplay = $userGameplayData[0]['UserPlayGame']['fairPlay'];
- if($oldfairplay>0)
- $newfirplay = $oldfairplay-1;
- else
- $newfirplay=0;
- $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));
- }
- }
- $this->UserPlayGame->saveAll($newplayerdata);
- //Save Data For Runner
- $newRunnerplayerdata = array();
- $UpdateRunnerArray = '';
- foreach($RunnerArray->Users as $rkey => $rval) {
- $playerid = $rval;
- //if user is other than old user of roomslot then looser data will not be saved
- $checknewjoinee = $this->RoomSlot->find('first',array('conditions' => array('RoomSlot.RoomId' => $RoomId,'RoomSlot.UserId' => $playerid ),'recursive' => -1));
- if(!empty($checknewjoinee)){
- $olduser = $checknewjoinee['RoomSlot']['OldSlotUser'];
- }else{
- $olduser = 0;
- }
- if($olduser == 0){
- $currentdate =date('Y-m-d');
- $dateexplode = explode('-',$currentdate);
- $currentmonth = $dateexplode['1'];
- $playDateData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Date(CreatedOn)' => $currentdate)));
- if(!empty($playDateData)) {
- $noOfgaemesplayedtoday = count($playDateData);
- }else{
- $noOfgaemesplayedtoday = 1;
- }
- $playMonthData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','Month(CreatedOn)' => $currentmonth)));
- if(!empty($playMonthData)) {
- $noOfGameplaythismonth = count($playMonthData);
- }else{
- $noOfGameplaythismonth = 1;
- }
- $playWeekData= $this->FinalScore->find("all",array( 'fields'=>array('FinalScore.Players','FinalScore.CreatedOn'),'conditions'=>array('Players LIKE'=>'%' . $playerid . '%','YEARWEEK(CreatedOn)=YEARWEEK(CURDATE(), 1)')));
- if(!empty($playWeekData)){
- $noOfGameplayedthisWeek = count($playWeekData);
- }else{
- $noOfGameplayedthisWeek = 1;
- }
- $userGameplayData = $this->UserPlayGame->find('all',array('conditions' => array('UserPlayGame.user_id' => $playerid)));
- if(empty($userGameplayData)){
- $total_game = 1;
- $user_id = $playerid;
- $win_game = 0;
- $lost_game = 1;
- $win_percent = 0;
- $player = 1;
- if($RunnerScore < 0){
- $Looser = 1;
- }else{
- $Looser = 0;
- }
- $RfullHouse = $RunnerFullHouse;
- $Winstreak = 0 ;
- $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);
- }else{
- $total_game = $userGameplayData[0]['UserPlayGame']['total_game'];
- $user_id = $playerid;
- $win_game = $userGameplayData[0]['UserPlayGame']['win_game'];
- $lost_game = $userGameplayData[0]['UserPlayGame']['lost_game'];
- $oldunstoppable = $userGameplayData[0]['UserPlayGame']['Unstoppable'];
- $oldPalyerVal = $userGameplayData[0]['UserPlayGame']['Player'];
- $newPlayerval = $oldPalyerVal + 1;
- if($RunnerScore < 0){
- $oldLooserVal = $userGameplayData[0]['UserPlayGame']['Looser'];
- $newLooserVal = $oldLooserVal + 1;
- }else{
- $oldLooserVal = $userGameplayData[0]['UserPlayGame']['Looser'];
- $newLooserVal = $oldLooserVal;
- }
- $oldLooserfullhouse = $userGameplayData[0]['UserPlayGame']['FullHouse'];
- $RfullHouse = $RunnerFullHouse + $oldLooserfullhouse;
- $Winstreak = 0 ;
- $newtotal_game = $total_game + 1;
- $newin_game = $win_game;
- $newlost_game = $lost_game + 1;
- $newwinpercentage = ($newin_game/$newtotal_game) * 100 ;
- $newpercentage = round($newwinpercentage);
- $created_date = strtotime(date('Y-m-d H:i:s'));
- $oldfairplay = $userGameplayData[0]['UserPlayGame']['fairPlay'];
- if($oldfairplay>0)
- $newfirplay = $oldfairplay-1;
- else
- $newfirplay=0;
- $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));
- }
- }else{
- $newRunnerplayerdata = array();
- }
- }
- $this->UserPlayGame->saveAll($newRunnerplayerdata);
- $GameFinalData = array($WinnerData,$RunnerData);
- $saveData = $this->FinalScore->saveAll($GameFinalData);
- //***************update room status*****************//
- $updateRoomStatus = $this->Room->updateAll(array('Room.room_status' => 2, 'Room.join_member_count' => 4),array('Room.id' => $RoomId));
- //update below line on 10/6/2016 RoomSlot.play_status means game is played and score is submitted
- $updateSlotplaystatus = $this->RoomSlot->updateAll(array('RoomSlot.play_status' => 1),array('RoomSlot.RoomId' => $RoomId));
- $this->set(array('Response' => array('Error' => false, 'Message' => "Success"), '_serialize' => array('Response')));
- }
- }
- }else{
- $erroMsg = "RoomData parameter is empty";
- $message = array('Error'=>true,'Message'=>$erroMsg);
- $this->set(array('Response' => $message,'_serialize' => array('Response')));
- }
- }
- public function resetGamePlay($userId,$roomId,$fairPlayStatus) {
- $userplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $userId)));
- $check_replacement = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' => $roomId,'RoomSlot.UserId'=>$userId,'RoomSlot.OldSlotUser'=>0)) );
- $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
- $roomstatus = $roomdata['Room']['room_status'];
- $ongoingstatus = $roomdata['Room']['OngoingStatus'];
- if(!empty($userplaydata)){
- $totalGame = $userplaydata['UserPlayGame']['total_game'];
- $winGame = $userplaydata['UserPlayGame']['win_game'];
- $lostGame = $userplaydata['UserPlayGame']['lost_game'];
- $winpercent = $userplaydata['UserPlayGame']['win_percent'];
- $perdayGameplayed = $userplaydata['UserPlayGame']['PerdayGameplayed'];
- $Monthlygameplay = $userplaydata['UserPlayGame']['Monthlygameplay'];
- $Weeklygameplay = $userplaydata['UserPlayGame']['Weeklygameplay'];
- $fairPlay = $userplaydata['UserPlayGame']['fairPlay'];
- $unixtime = $userplaydata['UserPlayGame']['created_date'];
- $currentdate =date('Y-m-d');
- $dateexplode = explode('-',$currentdate);
- $currentmonth = $dateexplode['1'];
- $newtotal_game = $totalGame + 1;
- $newloss_game = $lostGame + 1;
- $newwinpercentage = ($winGame/$newtotal_game) * 100 ;
- $newpercentage = round($newwinpercentage);
- $created_date = strtotime(date('Y-m-d H:i:s'));
- if($fairPlayStatus == 1 && $fairPlay < 10){
- $newFailplayval = $fairPlay + 1;
- }else
- $newFailplayval = $fairPlay;
- if((!empty($check_replacement)) && $ongoingstatus==1 && $roomstatus!=2 && $fairPlayStatus==1){
- $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));
- }
- } else{
- if((!empty($check_replacement)) && $ongoingstatus==1 && $roomstatus!=2 && $fairPlayStatus==1){
- $newUserdata = array('user_id' => $userId, 'fairPlay' => 1,'lost_game'=>1,'total_game'=>1);
- $this->UserPlayGame->saveAll($newUserdata);
- }else{
- $newUserdata = array('user_id' => $userId, 'fairPlay' => 0);
- $this->UserPlayGame->saveAll($newUserdata);
- }
- }
- }
- public function leaveGame() {
- $error = array();
- if(empty($this->request->query['RoomId'])){
- $error[] = "RoomId parameter is empty" ;
- }
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty" ;
- }
- if($this->request->query['FairPlayStatus']== ''){
- $error[] = "FairPlayStatus parameter is empty" ;
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => True, 'Message' => $errorMsg);
- $this->set(array('Response' => $response, '_serialize' => array('Response')));
- }else{
- $userId = $this->request->query['UserId'];
- $roomId = $this->request->query['RoomId'];
- //if user exists in any rematch update it as rejected>>>>>Start
- $getrematchdata = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $userId,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
- if(!empty($getrematchdata)){
- $this->RequestRematch->updateAll(array('RequestRematch.status' => 2),array('RequestRematch.user_id' =>$userId,'RequestRematch.room_id' => $roomId));
- }
- //if user exists in any rematch update it as rejected>>>>>end
- $fairPlayStatus = $this->request->query['FairPlayStatus'];
- //check if RoomSlot exists
- $roomslotexists = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' => $roomId)) );
- $this->resetGamePlay($userId,$roomId,$fairPlayStatus);
- if(empty($roomslotexists)) {
- $this->set(array('Response' => array('Error' => false , 'Message' => 'No Roomslot exists for this Room' ), '_serialize' => array('Response')));
- }else{
- //check if user is host
- $checkUser = $this->Room->find('all',array('conditions' => array('UserId' => $userId ,'id' => $roomId)));
- $thisroomsdata = $this->Room->find('first' , array('conditions' => array('id' => $roomId), 'recursive' => -1));
- $roomjoinnumbers = $thisroomsdata['Room']['join_member_count'];
- if(empty($checkUser)){
- //Given UserId is not host of the room So we have to remove the other user from slot
- $checkleavingUser = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $userId, 'RoomSlot.RoomId' => $roomId)));
- if(!empty($checkleavingUser)){
- if($checkleavingUser['RoomSlot']['slot_type'] == 'public'){
- $this->RoomSlot->updateAll(array('RoomSlot.UserId' =>null,'RoomSlot.slot_type'=>"'public'",'RoomSlot.SlotStatus' => "0",'RoomSlot.OldSlotUser' => $userId),array('RoomSlot.UserId' => $userId ,'RoomSlot.RoomId' => $roomId ));
- }else{
- $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 ));
- }
- $newjoinmembercount = $roomjoinnumbers - 1;
- $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
- $roomstatus = $roomdata['Room']['room_status'];
- if($roomstatus != 2){
- $autoterminateTime = date('Y-m-d H:i:s');
- if($roomjoinnumbers==4)
- $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId));
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
- }else{
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
- }
- $userplaydata = $this->UserPlayGame->find('first',array('conditions' => array('UserPlayGame.user_id'=> $userId),'fields' => array('fairPlay')));
- if(!empty($userplaydata)){
- $oldFairplayval = $userplaydata['UserPlayGame']['fairPlay'];
- if($fairPlayStatus == 1 && $oldFairplayval < 10){
- $newFailplayval = $oldFairplayval + 1;
- }else{
- $newFailplayval = $oldFairplayval;
- }
- $this->UserPlayGame->updateAll(array('UserPlayGame.fairPlay' => $newFailplayval),array('UserPlayGame.user_id'=> $userId));
- }else{
- //new user to be added here
- $newUesrData = array('user_id' => $userId, 'fairPlay' => 1);
- $this->UserPlayGame->saveAll($newUesrData );
- }
- //to check if all the slots are empty in that case we have to delete the room and its slots
- // get all slots
- $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
- if(empty($getallRoomSlotdata)) {
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $this->Room->deleteAll(array('Room.id' => $roomId));
- }
- //$this->Room->find('first', array('conditions' => array('Room')));
- $this->set(array('Response'=>array('Error' => false, 'Message' => "Success"), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => false , 'Message' => 'This RoomSlot Does Not Match our records'), '_serialize' => array('Response') ));
- }
- }else{
- $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 ));
- $userplaydata = $this->UserPlayGame->find('first',array('conditions' => array('UserPlayGame.user_id'=> $userId),'fields' => array('fairPlay')));
- $newjoinmembercount = $roomjoinnumbers - 1;
- $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
- $roomstatus = $roomdata['Room']['room_status'];
- if($roomstatus != 2){
- $autoterminateTime = date('Y-m-d H:i:s');
- if($roomjoinnumbers==4)
- $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId));
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
- }else{
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
- }
- if(!empty($userplaydata)){
- $oldFairplayval = $userplaydata['UserPlayGame']['fairPlay'];
- if($fairPlayStatus == 1 && $oldFairplayval < 10){
- $newFailplayval = $oldFairplayval + 1;
- }else{
- $newFailplayval = $oldFairplayval;
- }
- $this->UserPlayGame->updateAll(array('UserPlayGame.fairPlay' => $newFailplayval),array('UserPlayGame.user_id'=> $userId));
- }else{
- $newUesrData = array('user_id' => $userId, 'fairPlay' => 0);
- $this->UserPlayGame->saveAll($newUesrData );
- }
- $getRoomSlotdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' =>$roomId,'RoomSlot.SlotStatus' => 1 ),'order' =>array('RoomSlot.created_date ASC'),'recursive' => -1));
- if(!empty($getRoomSlotdata)){
- $newHostid = $getRoomSlotdata['RoomSlot']['UserId'];
- $username = $this->User->find('first' , array('conditions' => array('User.id' => $newHostid), 'fields' => array('username'), 'recursive' => -1));
- $roomusername = $username['User']['username'];
- $this->Room->updateAll(array('Room.UserId' => $newHostid,"Room.name" => '"'.$roomusername.'"', 'Room.HostName' => '"'.$roomusername.'"'),array('Room.id' => $roomId));
- }
- //to check if all the slots are empty in that case we have to delete the room and its slots
- // get all slots
- $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
- if(empty($getallRoomSlotdata)) {
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $this->Room->deleteAll(array('Room.id' => $roomId));
- }
- $this->set(array('Response' => array('Error' => false , 'Message' => 'The host left the room'), '_serialize' => array('Response')));
- }
- }
- }
- }
- public function saveChatHistory() {
- $error = array();
- if(empty($_POST['RoomId'])){
- $error[] = "RoomId parameter is empty";
- }
- if(empty($_POST['UserId'])){
- $error[] = "UserId parameter is empty";
- }
- if(empty($_POST['Message']) && $_POST['Message']!=0){
- $error[] = "Message parameter is empty";
- }
- if(empty($_POST['Type'])){
- $error[] = "Type parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => True, 'Message' => $errorMsg);
- $this->set(array('Response' => $response, '_serialize' => array('Response')));
- }else{
- $roomId = $_POST['RoomId'];
- $userId = $_POST['UserId'];
- $Message = $_POST['Message'];
- $Type = $_POST['Type'];
- if($Type == 'E') {
- if(empty($Message)){
- $Emoerror = "EmojiId parameter is empty";
- $this->set(array('Response' => $Emoerror,'_serialize' => array('Response')));
- }else{
- $Message = $Message;
- $data = array('UserId' => $userId, 'RoomId' => $roomId, 'ChatMessage' => $Message , 'Type' => 'E');
- $this->ChatInfo->save($data);
- // chatid in response
- $chatid = $this->ChatInfo->getLastInsertId();
- $this->set(array('Response' => array('Error' => false, 'ChatId' => $chatid, 'Message' => 'Success'),'_serialize' => array('Response')));
- }
- }else{
- $data = array('UserId' => $userId, 'RoomId' => $roomId, 'ChatMessage' => $Message , 'Type' => 'C');
- $this->ChatInfo->save($data);
- $chatid = $this->ChatInfo->getLastInsertId();
- $this->set(array('Response' => array('Error' => false,'ChatId' => $chatid, 'Message' => 'Success'),'_serialize' => array('Response')));
- }
- }
- }
- /***
- Accept status 0: for declined, 1 for Accept
- send null instead of 0
- */
- public function InviteResponse() {
- $error = array();
- if(empty($this->request->query['RoomId'])){
- $error[] = "RoomId parameter is empty." ;
- }
- if(empty($this->request->query['SlotId'])){
- $error[] = "SlotId parameter is empty.";
- }
- if($this->request->query['Status'] == ""){
- $error[] = "Status parameter is empty";
- }
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty.";
- }
- if(!empty($error)) {
- $errMessage = implode(',',$error);
- $Message = array('Error'=>true, 'Message' => $errMessage);
- $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
- }else{
- $roomid = $this->request->query['RoomId'];
- $slotid = $this->request->query['SlotId'];
- $status = $this->request->query['Status'];
- $UserId = trim($this->request->query['UserId']) ;
- if($status == 1){
- //check if room exists in case it has not been updated or deleted already.
- $Roomdata = $this->Room->find('first',array('conditions'=>array('Room.id' => $roomid),'recursive'=>-1));
- if(!empty($Roomdata)){
- $joinmembers = $Roomdata['Room']['join_member_count'];
- //check if roomslot exists
- if($joinmembers < 4) {
- $slotexists = $this->RoomSlot->find('all', array('conditons' => array('RomSlot.RoomId' => $roomid, 'RoomSlot.SlotId' => $slotid), 'recursive' => -1));
- if(!empty($slotexists)) {
- $slotcheck = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.SlotStatus' =>1,'RoomSlot.RoomId' => $roomid, 'RoomSlot.SlotId'=> $slotid),'recursive' => -1));
- if(empty($slotcheck)){
- $chkUser= $this->RoomSlot->find('first' , array('conditions' => array('RoomSlot.UserId' =>$UserId,'RoomSlot.RoomId' => $roomid,'RoomSlot.SlotStatus' =>1)));
- if(empty($chkUser)){
- $chkUserInRooms = $this->RoomSlot->find('first' , array('conditions' => array('RoomSlot.UserId' =>$UserId,'RoomSlot.SlotStatus' =>1)));
- if(empty($chkUserInRooms)){
- $this->RoomSlot->updateAll(array('RoomSlot.SlotStatus' => 1,'RoomSlot.UserId'=>$UserId),array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid));
- $joinmenbercount = $joinmembers + 1 ;
- $this->Room->updateAll(array('Room.join_member_count' => $joinmenbercount ), array('Room.id' => $roomid));
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This user is already assigned to other room'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This user is already assigned'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This slot is already assigned'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot does not exists'),'_serialize'=> array('Response'))) ;
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'All slots are already filled'), '_serialize' => array('Response'))) ;
- }
- }
- else{
- $this->set(array('Response' => array('Error' => true, 'Message' => 'Room Does not exist.'), '_serialize'=> array('Response')));
- }
- }else{
- $Roomdata = $this->Room->find('first',array('conditions'=>array('Room.id' => $roomid),'recursive'=>-1));
- if(!empty($Roomdata)){
- $slotexists = $this->RoomSlot->find('all', array('conditons' => array('RomSlot.RoomId' => $roomid, 'RoomSlot.SlotId' => $slotid), 'recursive' => -1));
- if(!empty($slotexists)) {
- $public = 'public';
- $slotstatus = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid,'RoomSlot.SlotStatus' => 1)));
- if(empty($slotstatus)){
- $this->RoomSlot->updateAll(array("RoomSlot.slot_type" =>'"'.$public.'"',"RoomSlot.UserId"=>'0',"RoomSlot.SlotKey" => 0),array('RoomSlot.RoomId'=>$roomid,'RoomSlot.SlotId'=>$slotid));
- $this->set(array('Response' => array('Error' => false , 'Message' => 'RoomSlot Updated Success'),'_serialize'=> array('Response'))) ;
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot is not available'),'_serialize'=> array('Response'))) ;
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'RoomSlot does not exists'),'_serialize'=> array('Response'))) ;
- }
- }else{
- $this->set(array('Response' => array('Error' => true, 'Message' => 'Room Does not exist.'), '_serialize'=> array('Response')));
- }
- }
- }
- }
- public function removidleuser($userId){
- $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));
- if(!empty($getuSlotData)){
- $roomId = $getuSlotData['RoomSlot']['RoomId'];
- //remove user from rematch
- $getrematchdata = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $userId,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
- if(!empty($getrematchdata)){
- $this->RequestRematch->updateAll(array('RequestRematch.status' => 2),array('RequestRematch.user_id' =>$userId,'RequestRematch.room_id' => $uroomid));
- //removed user from rematch
- }
- $checkUser = $this->Room->find('all',array('conditions' => array('UserId' => $userId ,'id' => $roomId)));
- $thisroomsdata = $this->Room->find('first' , array('conditions' => array('id' => $roomId), 'recursive' => -1));
- $roomjoinnumbers = $thisroomsdata['Room']['join_member_count'];
- if(empty($checkUser)){
- //Given UserId is not host of the room So we have to remove the other user from slot
- $checkleavingUser = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $userId, 'RoomSlot.RoomId' => $roomId)));
- if(!empty($checkleavingUser)){
- $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 ));
- $newjoinmembercount = $roomjoinnumbers - 1;
- $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
- $roomstatus = $roomdata['Room']['room_status'];
- if($roomstatus != 2){
- $autoterminateTime = date('Y-m-d H:i:s');
- $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId,'Room.join_member_count'=>4));
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
- }else{
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
- }
- $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
- if(empty($getallRoomSlotdata)) {
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $this->Room->deleteAll(array('Room.id' => $roomId));
- }
- }
- //get room slot count
- }else{
- $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 ));
- $newjoinmembercount = $roomjoinnumbers - 1;
- $roomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId),'recursive' => -1));
- $roomstatus = $roomdata['Room']['room_status'];
- if($roomstatus != 2){
- $autoterminateTime = date('Y-m-d H:i:s');
- $this->Room->updateAll(array('Room.auto_terminate_update' => '"'.$autoterminateTime.'"'), array('Room.id' => $roomId,'Room.join_member_count'=>4));
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount,'Room.room_status'=>0), array('Room.id' => $roomId));
- }else{
- $this->Room->updateAll(array('Room.join_member_count' => $newjoinmembercount), array('Room.id' => $roomId));
- }
- $getRoomSlotdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.RoomId' =>$roomId,'RoomSlot.SlotStatus' => 1 ),'order' =>array('RoomSlot.created_date ASC'),'recursive' => -1));
- if(!empty($getRoomSlotdata)){
- $newHostid = $getRoomSlotdata['RoomSlot']['UserId'];
- $username = $this->User->find('first' , array('conditions' => array('User.id' => $newHostid), 'fields' => array('username'), 'recursive' => -1));
- $roomusername = $username['User']['username'];
- $this->Room->updateAll(array('Room.UserId' => $newHostid,"Room.name" => '"'.$roomusername.'"', 'Room.HostName' => '"'.$roomusername.'"'),array('Room.id' => $roomId));
- }
- //to check if all the slots are empty in that case we have to delete the room and its slots
- // get all slots
- $getallRoomSlotdata = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' =>$roomId, 'RoomSlot.SlotStatus' => 1),'order' =>array('RoomSlot.created_date ASC'),'recursive'=>-1));
- if(empty($getallRoomSlotdata)) {
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $this->Room->deleteAll(array('Room.id' => $roomId));
- }
- }
- }
- }
- public function getRoomSearch() {
- $onlineusers = $this->User->find('count',array('conditions' => array('User.online_status' => 1)));
- $onlinegames = $this->Room->find('count',array('conditions' => array('Room.OngoingStatus' => 1,'Room.room_status' => 1)));
- if(!empty($onlineusers)){
- $onlineuserslist = $onlineusers;
- }else{
- $onlineuserslist = 0;
- }
- if(!empty($onlinegames)){
- $ongoingameslist = $onlinegames;
- }else{
- $ongoingameslist = 0;
- }
- $searchList = array();
- if(empty($this->request->query['SearchKey'])) {
- $error = array();
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty.";
- }
- if(!empty($error)) {
- $errMessage = implode(',',$error);
- $Message = array('Error'=>true, 'Message' => $errMessage);
- $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
- }else{
- $useridforsearch = $this->request->query['UserId'];
- $this->removidleuser($useridforsearch);
- // $limit = 1000 ;
- $searchList = $this->Room->find( 'all', array(
- 'fields'=>array('Room.name','Room.id','Room.room_status','Room.OngoingStatus'),
- // 'limit' => $limit,
- 'conditions'=>array('Room.room_status' => 0, 'Room.join_member_count !=' => 4 ),
- //'recursive' => -1
- ));
- if(!empty($searchList)){
- $roomData = array();
- $newRoomData = array();
- foreach($searchList as $k => $v) {
- $roomid = $v['Room']['id'];
- $OngoingStatus = $v['Room']['OngoingStatus'];
- $roomSlots = $v['RoomSlot'];
- if(count($roomSlots) == 4) {
- $rslottype = array();
- $rslotstatus = array();
- $oldslotUser = array();
- $currentuser = array();
- foreach($roomSlots as $rkey => $rval){
- $rslottype[] = $rval['slot_type'];
- $rslotstatus[] = $rval['SlotStatus'];
- $oldslotUser[] = $rval['OldSlotUser'];
- $currentuser[] = $rval['UserId'];
- }
- if((in_array($useridforsearch,$currentuser))) {
- $slotData = '';
- }else{
- if((in_array('public',$rslottype)) || (in_array('private',$rslottype))) {
- if(in_array(1,$rslotstatus)){
- $slotData = array();
- $filledslots = array();
- foreach ($roomSlots as $key => $row) {
- $mid[$key] = $row['SlotId'];
- }
- array_multisort($mid, SORT_ASC, $roomSlots);
- foreach($roomSlots as $rk => $rv) {
- $roomUserid = $rv['UserId'];
- $slottype = $rv['slot_type'];
- $slotKey = $rv['SlotKey'];
- $slotId = $rv['SlotId'];
- $slotStatus = $rv['SlotStatus'];
- if($slotStatus == 1){
- $filledslots[] = $rv;
- }
- $userdata = $this->User->find('first', array('conditions' => array('User.id' => $roomUserid), 'fields' => array('User.fbid','User.pic','User.PictureIndex','User.username','User.star')));
- if(!empty($userdata)){
- $userfbid = $userdata['User']['fbid'];
- $uPic = $userdata['User']['pic'];
- $userPic = ($userdata['User']['pic']) ? HTTP_ROOT .'app/webroot/img/members/'.$uPic: $uPic;
- $picindex = $userdata['User']['PictureIndex'];
- $username = $userdata['User']['username'];
- $star = $userdata['User']['star'];
- }else{
- $userfbid = '';
- $userPic = '';
- $picindex = '';
- $username = '';
- $star = '';
- }
- $slotData[] = array('Star' => $star,'UserName' => $username,'UserId' => $roomUserid, 'Type' => $slottype, 'Key' => $slotKey, 'SlotId' => $slotId, 'SlotStatus' => $slotStatus, 'UserFbId' => $userfbid, 'Picture' => $userPic, 'UserPicIndex' => $picindex) ;
- }
- $slotData = $slotData ;
- }else{
- $slotData = '';
- }
- }else{
- $slotData = '';
- }
- }
- if(!empty($slotData)) {
- $slotFilled = count($filledslots);
- $newRoomData[] = array('RoomId' => $roomid, 'SlotData' => $slotData, 'SlotsFilled' => $slotFilled,'OngoingStatus' => $OngoingStatus);
- }else{
- $newRoomData[] = '';
- $slotFilled = 0 ;
- }
- }else{
- $newRoomData[] = '';
- $slotFilled = 0 ;
- }
- }
- $newRoomData = array_filter($newRoomData);
- if(!empty($newRoomData)){
- $newRoomData = array_filter($newRoomData);
- $newRoomData = array_values($newRoomData);
- $newRoomData = array_slice($newRoomData, 0, 10);
- $this->set(array('Response' => array('Error' => false , 'RoomData' => $newRoomData,
- 'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => false , 'Message' => 'No results found','condition' => 'no search key', 'RoomData'=> array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => false , 'Message' => 'No Results found' ,'RoomData'=> array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }
- }
- }else{
- $error = array();
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty.";
- }
- if(!empty($error)) {
- $errMessage = implode(',',$error);
- $Message = array('Error'=>true, 'Message' => $errMessage);
- $this->set(array('Response' => $Message ,'_serialize' => array('Response')));
- }else{
- $useridforsearch = $this->request->query['UserId'];
- $searchname = $this->request->query['SearchKey'];
- // $limit = 1000;
- $searchList = $this->Room->find('all', array(
- 'fields' => array('Room.name','Room.id','Room.room_status','Room.OngoingStatus'),
- // 'limit' => $limit,
- 'conditions'=>array('name LIKE'=>'%' . $searchname . '%','room_status' => 0,'Room.join_member_count !=' => 4)
- //'recursive' => -1
- ));
- if(!empty($searchList)){
- $roomData = array();
- $newRoomData = array();
- foreach($searchList as $k => $v) {
- $roomid = $v['Room']['id'];
- $OngoingStatus = $v['Room']['OngoingStatus'];
- //$roomData['RoomId'] = $roomid ;
- $roomSlots = $v['RoomSlot'];
- if(count($roomSlots) == 4) {
- $rslottype = array();
- $rslotstatus = array();
- $oldslotUser = array();
- $currentuser = array();
- foreach($roomSlots as $rkey => $rval){
- $rslottype[] = $rval['slot_type'];
- $rslotstatus[] = $rval['SlotStatus'];
- $oldslotUser[] = $rval['OldSlotUser'];
- $currentuser[] = $rval['UserId'];
- }
- if((in_array($useridforsearch,$currentuser))) {
- $slotData = '';
- }else{
- if((in_array('public',$rslottype)) || (in_array('private',$rslottype))){
- if(in_array(1,$rslotstatus)){
- $slotData = array();
- $filledslots = array();
- foreach ($roomSlots as $key => $row) {
- $mid[$key] = $row['SlotId'];
- }
- array_multisort($mid, SORT_ASC, $roomSlots);
- foreach($roomSlots as $rk => $rv) {
- $roomUserid = $rv['UserId'];
- $slottype = $rv['slot_type'];
- $slotKey = $rv['SlotKey'];
- $slotId = $rv['SlotId'];
- $slotStatus = $rv['SlotStatus'];
- if($slotStatus == 1){
- $filledslots[] = $rv;
- }
- $userdata = $this->User->find('first', array('conditions' => array('User.id' => $roomUserid), 'fields' => array('User.fbid','User.pic','User.PictureIndex','User.username','User.star')));
- if(!empty($userdata)){
- $userfbid = $userdata['User']['fbid'];
- $uPic = $userdata['User']['pic'];
- $userPic = ($userdata['User']['pic']) ? HTTP_ROOT .'app/webroot/img/members/'.$uPic: $uPic;
- $picindex = $userdata['User']['PictureIndex'];
- $username = $userdata['User']['username'];
- $star = $userdata['User']['star'];
- }else{
- $userfbid = '';
- $userPic = '';
- $picindex = '';
- $username = '';
- $star = '';
- }
- $slotData[] = array('Star' => $star,'UserName' => $username,'UserId' => $roomUserid, 'Type' => $slottype, 'Key' => $slotKey, 'SlotId' => $slotId, 'SlotStatus' => $slotStatus, 'UserFbId' => $userfbid, 'Picture' => $userPic, 'UserPicIndex' => $picindex) ;
- }
- }else{
- $slotData = '';
- }
- }else{
- $slotData = '';
- }
- }
- if(!empty($slotData)) {
- $slotFilled = count($filledslots);
- $newRoomData[] = array('RoomId' => $roomid, 'SlotData' => $slotData, 'SlotsFilled' => $slotFilled,'OngoingStatus' => $OngoingStatus);
- }else{
- $newRoomData[] = '';
- $slotFilled = 0 ;
- }
- }else{
- $newRoomData[] = '';
- $slotFilled = 0 ;
- }
- }
- if(!empty($newRoomData) ){
- $newRoomData = array_filter($newRoomData);
- $newRoomData = array_values($newRoomData);
- $newRoomData = array_slice($newRoomData, 0, 10);
- $this->set(array('Response' => array('Error' => false , 'RoomData' => $newRoomData,'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => false , 'Message' => 'No results found', 'RoomData' => array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => false, 'Message' => 'No Results Found','RoomData' => array(),'PlayersOnline' => $onlineuserslist,'OngoingGames' => $ongoingameslist), '_serialize' => array('Response')));
- }
- }
- }
- }
- public function requestRematch() {
- $error = array();
- if(empty($_POST['RoomId'])){
- $error[] = "RoomId parameter is Empty.";
- }
- if(empty($_POST['UserId'])){
- $error[] = "UserId parameter is Empty";
- }
- if($_POST['Status'] == ""){
- $error[] = "Status parameter is Empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- $roomId = $_POST['RoomId'];
- $userId = $_POST['UserId'];
- $status = $_POST['Status'];
- $selectRoomslot = $this->RoomSlot->find('all', array('conditions' => array('RoomSlot.RoomId' => $roomId), 'recursive' => -1, 'order' => array('RoomSlot.SlotId' => 'asc')));
- $slotuserid = array();
- if(!empty($selectRoomslot)){
- foreach($selectRoomslot as $k=>$v){
- $slotuserid[] = $v['RoomSlot']['UserId'];
- }
- if(in_array(0, $slotuserid)){
- $this->set(array('Response' =>array('Error' => true , 'Message' => "Not Enough Users !"), '_serialize' => array('Response')));
- }else{
- if(in_array($userId, $slotuserid)){
- $slotid=-1;
- $data = array();
- $datastatus = '';
- foreach($slotuserid as $key=>$val){
- $slotuser = $val;
- //first check if data already exists
- $rrematchdata = $this->RequestRematch->find('all', array('conditions' => array('RequestRematch.user_id' => $slotuser, 'RequestRematch.room_id' => $roomId)));
- if(empty($rrematchdata)){
- $slotid++;
- if($slotuser == $userId){
- $status = 1 ;
- }else{
- $status = 0;
- }
- $data[] = array('room_id' => $roomId,'host_id' => $userId ,'user_id' => $slotuser, 'slot_id' => $slotid , 'status' => $status);
- $datastatus = 'true';
- }else{
- $data[] = '';
- $datastatus = 'false';
- }
- }
- if($datastatus == 'true'){
- $this->RequestRematch->saveAll($data);
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Success'), '_serialize' => array('Response')));
- }else{
- if($status == 0){
- $status = 1;
- }else{
- $status = $status;
- }
- $this->RequestRematch->updateAll(array('RequestRematch.status' => $status),array('RequestRematch.user_id'=> $userId, 'RequestRematch.room_id' => $roomId));
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Rematch Status Updated'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' =>array('Error' => true , 'Message' => "This user have no slot for this room"), '_serialize' => array('Response')));
- }
- }
- }else{
- $this->set(array('Response' => array('Error' => true ,'Message' => "No room slots available.") , '_serialize' => array('Response')));
- }
- }
- }
- public function FetchRequestRematch() {
- $error = array();
- if(empty($_POST['RoomId'])){
- $error[] = "RoomId parameter is empty";
- }
- if(empty($_POST['UserId'])) {
- $error[] = "UserId parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- $roomId = $_POST['RoomId'];
- $UserId = $_POST['UserId'];
- //check if this room exist on rematch table
- $roomdata = $this->RequestRematch->find('first' , array('conditions' => array('RequestRematch.room_id' => $roomId), 'recursive' => -1));
- if(empty($roomdata)){
- //Case 1
- $this->set(array('Response' => array('Error' => false ,'Message' => "No Room Data.", 'Status' => 'NotSet', 'RoomData'=>array('UserId' => $UserId, 'Name' => '', 'Status' => 'NotSet', 'NewRoomId' => "")) , '_serialize' => array('Response')));
- }else{
- //***********check for status 2 **************//
- $chkRejected = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.room_id' => $roomId ,'RequestRematch.status' => 2 )));
- if(empty($chkRejected )){
- $checkAccepted = $this->RequestRematch->find('all', array('conditions' => array('RequestRematch.room_id' => $roomId ,'RequestRematch.status' => 1 )));
- if(count($checkAccepted) == 4){
- $oldRoomdata = $this->Room->find('first',array('conditions' => array('Room.id' => $roomId) ,'recursive' => -1) );
- $roomtype = $oldRoomdata['Room']['room_type'];
- $roomphotonid = $oldRoomdata['Room']['PhotonId'];
- $oldroomid = $roomId;
- //chk for room
- $newuserdata = $this->RoomSlot->find('first', array('conditions' => array('RoomSlot.UserId' => $UserId,'RoomSlot.RoomId !=' => $roomId), 'recursive' => -1));
- if(!empty($newuserdata)){
- $newroomid = $newuserdata['RoomSlot']['RoomId'];
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- $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')));
- }else{
- //create new room
- $gethostid = $this->RequestRematch->find('all' , array('conditions' => array('RequestRematch.room_id' => $roomId), 'fields' => array('host_id','user_id')) );
- $hostId = $gethostid[0]['RequestRematch']['host_id'];
- $hostData = $this->User->find('first', array('conditions' => array('User.id' => $hostId),'recursive' => -1));
- $hostName = $hostData['User']['username'];
- $UserId = $hostId;
- $name = $hostName;
- $HostName = $hostName;
- $join_member_count = 4;
- $created_time = strtotime(date('Y-m-d H:i:s')) ;
- $created_date = date('Y-m-d H:i:s');
- //add room type and photonid
- $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');
- $this->Room->save($roomData);
- $newRoomId = $this->Room->getInsertID();
- $this->RoomSlot->updateAll(array('RoomSlot.RoomId' => $newRoomId,'RoomSlot.OldSlotUser' => 0,'RoomSlot.play_status' => 0),array('RoomSlot.RoomId' => $roomId));
- $this->RoomSlot->deleteAll(array('RoomSlot.RoomId' => $roomId));
- //create room slots
- //delete requestrematch data
- $this->set(array('Response' => array('Error' => false , 'RoomData' => array('UserId' => $UserId, 'Name' => $name, 'Status' => 'Accepted', 'NewRoomId' => $newRoomId) ) , '_serialize' => array('Response') ));
- }
- }else{
- //Case2
- $chkUser = $this->RequestRematch->find('first', array('conditions' => array('RequestRematch.user_id' => $UserId ,'RequestRematch.room_id' => $roomId), 'recursive' => -1));
- if(empty($chkUser)){
- $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')));
- }else{
- $userStatus = $chkUser['RequestRematch']['status'];
- if($userStatus == 0){
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Ask', 'Status' => 'Ask','RoomData' => array('UserId' => $UserId,'Name' => '', 'Status' => 'Ask', 'NewRoomId' => '')), '_serialize' => array('Response')));
- }elseif($userStatus == 1){
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Waiting', 'Status' => 'Waiting','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Waiting', 'NewRoomId' => '')), '_serialize' => array('Response')));
- }elseif($userStatus == 2){
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Rejected','Status' => 'Rejected','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Rejected', 'NewRoomId' => '')), '_serialize' => array('Response')));
- }
- }
- }
- }else{
- $this->set(array('Response' => array('Error' => false , 'Message' => 'Rejected', 'Status' => 'Rejected','RoomData' => array('UserId' => $UserId, 'Name' => '', 'Status' => 'Rejected', 'NewRoomId' => '')), '_serialize' => array('Response')));
- }
- }
- }
- }
- public function time_elapsed_string($datetime, $full = false) {
- $now = new DateTime;
- $ago = new DateTime($datetime);
- $diff = $now->diff($ago);
- $diff->w = floor($diff->d / 7);
- $diff->d -= $diff->w * 7;
- $string = array(
- 'y' => 'year',
- 'm' => 'month',
- 'w' => 'week',
- 'd' => 'day',
- 'h' => 'hour',
- 'i' => 'minute',
- 's' => 'second',
- );
- foreach ($string as $k => &$v) {
- if ($diff->$k) {
- $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
- } else {
- unset($string[$k]);
- }
- }
- if (!$full) $string = array_slice($string, 0, 1);
- return $string ? implode(', ', $string) . ' ago' : 'just now';
- }
- public function UserStats() {
- $error = array();
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errMessage);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- $userid = $this->request->query['UserId'];
- $userStats = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $userid)));
- if(!empty($userStats)){
- $totalGame = $userStats['UserPlayGame']['total_game'];
- $winGame = $userStats['UserPlayGame']['win_game'];
- $lostGame = $userStats['UserPlayGame']['lost_game'];
- $winpercent = $userStats['UserPlayGame']['win_percent'];
- $perdayGameplayed = $userStats['UserPlayGame']['PerdayGameplayed'];
- $Monthlygameplay = $userStats['UserPlayGame']['Monthlygameplay'];
- $Weeklygameplay = $userStats['UserPlayGame']['Weeklygameplay'];
- $fairPlay = $userStats['UserPlayGame']['fairPlay'];
- $unixtime = $userStats['UserPlayGame']['created_date'];
- $lastgameplayed = date("m/d/Y h:i:s",strtotime($unixtime));
- $lastgameplayed = $this->time_elapsed_string(@$unixtime);
- if($lastgameplayed == "2016 years ago"){
- $lastgameplayed = 'Today';
- }
- //last game played to be included
- $userStatsdata = array('WinPercent' => $winpercent,'LostGames' => $lostGame,'TotalGame' => $totalGame, 'WinGames' => $winGame, 'LastGamePlayed' => $lastgameplayed );
- $this->set(array('Response' => array('Error' => false ,'UserStatsMultiPlayer' => $userStatsdata,'Message' => "Success") , '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => true ,'Message' => "NO UserStats available.") , '_serialize' => array('Response')));
- }
- }
- }
- // Type : 1 private , 2 public
- // Room Status: 0 game not started, 1 game started
- //Online Status : 1 for online, 0 for offline
- public function roomjoinaftersearch(){
- $error = array();
- if(empty($this->request->query['RoomId'])){
- $error[] = "RoomId parameter is empty" ;
- }
- if(empty($this->request->query['UserId'])){
- $error[] = "UserId parameter is empty" ;
- }
- if($this->request->query['SlotId']==''){
- $error[] = "SlotId parameter is empty" ;
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => True, 'Message' => $errorMsg);
- $this->set(array('Response' => $response, '_serialize' => array('Response')));
- }else{
- $userId = $this->request->query['UserId'];
- $slotId = $this->request->query['SlotId'];
- $roomId = $this->request->query['RoomId'];
- $roomData = $this->Room->find('first', array('conditions' => array('Room.id' => $roomId)));
- if(!empty($roomData)){
- $getslotdata = $this->RoomSlot->find('first', array('conditions' => array( 'RoomSlot.SlotId' => $slotId, 'RoomSlot.RoomId' => $roomId ), 'recursive' => -1)) ;
- if(!empty($getslotdata)){
- $currentSlotuser = $getslotdata['RoomSlot']['UserId'];
- if($currentSlotuser != 0){
- $this->set(array('Response' => array('Error' => true , 'Message' => 'SlotAlreadyTaken') , '_serialize' => array('Response')));
- }else{
- $chkuserAssigned = $this->RoomSlot->find('first',array('conditions' => array('RoomSlot.UserId' => $userId)));
- if(!empty($chkuserAssigned)){
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This User is already assigned ') , '_serialize' => array('Response')));
- }else{
- $this->RoomSlot->updateAll(array('RoomSlot.UserId' => $userId, 'RoomSlot.SlotStatus' =>'1'),array('RoomSlot.RoomId' => $roomId, 'RoomSlot.SlotId' => $slotId));
- $newslotdata = $this->RoomSlot->find('all', array('conditions' => array( 'RoomSlot.RoomId' => $roomId ))) ;
- $allSlotdata = array();
- foreach($newslotdata as $k => $v){
- $slotUser = $v['RoomSlot']['UserId'];
- $type = $v['RoomSlot']['slot_type'];
- if($type = 'public') {
- $v['Password'] = null;
- }elseif($type == 'private') {
- $v['Password'] = $v['SlotKey'];
- }elseif($type == 'invite'){
- $v['Password'] = null ;
- }
- $userData =$this->User->find('first' , array('fields' => array('User.fbid', 'User.online_status'), 'conditions' => array('User.id' => $slotUser )));
- if(!empty($userData)){
- $userfbId = $userData['User']['fbid'];
- $onlinestatus = $userData['User']['online_status'];
- $allSlotdata[] = array('OnlineStatus' => $onlinestatus,'FbId' => $userfbId,'UserId' => $slotUser, 'Type' => $type , 'Password' => $v['Password']);
- }else{
- $allSlotdata[] = array('UserId' =>$slotUser );
- }
- }
- $roomType = $roomData['Room']['room_type'];
- $roomStatus = $roomData['Room']['room_status'];
- $hostname = $roomData['Room']['HostName'];
- $RoomCreationTime = $roomData['Room']['created_date'];
- $slotlist = $allSlotdata;
- $totalusers = $roomData['Room']['join_member_count'];
- $photonid = $roomData['Room']['PhotonId'];
- $newRoomJoinCount = $totalusers + 1 ;
- if($newRoomJoinCount == 4){
- $this->Room->updateAll(array('Room.join_member_count' => $newRoomJoinCount, 'Room.room_status' => 1,'OngoingStatus' => 1),array('Room.id' =>$roomId));
- }else{
- $this->Room->updateAll(array('Room.join_member_count' => $newRoomJoinCount),array('Room.id' =>$roomId));
- }
- $this->set(array('Response' => array('Error' => false , 'Status' => 'True',
- 'RoomDetails' => array('RoomId' => $roomId, 'Type' => $roomType, 'RoomStatus' => $roomStatus , 'HostName' => $hostname, 'RoomCreationTime' => $RoomCreationTime ),
- 'SlotList' => $slotlist, 'Message' => 'Success'), '_serialize' => array('Response')));
- }
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This slot does not exist'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => true , 'Message' => 'This room Does Not exist.'), '_serialize' => array('Response')));
- }
- }
- }
- /*
- @API: TopTenScores
- Databy : 1 by Date, 2 by Week, 3 by Month
- */
- public function TopTenScores(){
- $error = array();
- if(empty($this->request->query['DataBy'])) {
- $error[] = "DataBy parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- $dataBy = $this->request->query['DataBy'];
- $currentdate =date('Y-m-d');
- $dateexplode = explode('-',$currentdate);
- $currentmonth = $dateexplode['1'];
- if($dataBy == 1) {
- //Game played today
- $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND CURDATE()')));
- // players list for today
- if(!empty($playDateData)){
- $playeronearr = array();
- $playertwoarr = array();
- foreach($playDateData as $k=>$v){
- $playerids = explode(',',$v['FinalScore']['Players']);
- $playerone = $playerids[0];
- $playertwo = $playerids[1];
- $playeronearr[] = $playerone;
- $playertwoarr[] = $playertwo;
- }
- $allplayerarray = array_merge($playeronearr,$playertwoarr);
- if(!empty($allplayerarray)){
- $uniqueplayerslist = array_unique($allplayerarray);
- $resetplayerkeys = array_values($uniqueplayerslist);
- // got the users list until here . Now we have file for user stats 1 from users table
- $requiredids = array();
- foreach($resetplayerkeys as $key=>$playerid) {
- $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
- if(!empty($chkuserstats)){
- if($chkuserstats['User']['statas'] == 1){
- $validuserids = $playerid;
- }else{
- $validuserids = '';
- }
- }else{
- $validuserids = '';
- }
- $requiredids[] = $validuserids;
- }
- $reqids = array_values(array_filter($requiredids));
- if(!empty($reqids)){
- $userdata = array();
- foreach($reqids as $idkey=>$idval){
- $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')));
- $noofgameplaytoday = count($playDateData);
- $minimumplaylimit = 10;
- if($noofgameplaytoday >= $minimumplaylimit){
- $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')));
- $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
- $username = $this->User->find("first",array('conditions'=>array('User.id' => $idval),'fields' => array('username')));
- if($winpercentageofuser > 0 ){
- $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
- }else{
- $userdata[] = array();
- }
- }else{
- $userdata[] = array();
- }
- }
- $userdata = array_values(array_filter($userdata));
- if(!empty($userdata)){
- foreach ($userdata as $key => $row) {
- $mid[$key] = $row['WinPercent'];
- }
- array_multisort($mid, SORT_DESC, $userdata);
- $allUserData = array_slice($userdata, 0, 10);
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response'=>array('Error'=>false,'UserPlayData' => array()),'_serialize' => array('Response')));
- }
- }
- }else{
- $this->set(array('Response'=>array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }elseif($dataBy == 2){
- //Game played this week
- $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()')));
- // players list for this week
- if(!empty($playDateData)){
- $playeronearr = array();
- $playertwoarr = array();
- foreach($playDateData as $k=>$v){
- $playerids = explode(',',$v['FinalScore']['Players']);
- $playerone = $playerids[0];
- $playertwo = $playerids[1];
- $playeronearr[] = $playerone;
- $playertwoarr[] = $playertwo;
- }
- $allplayerarray = array_merge($playeronearr,$playertwoarr);
- if(!empty($allplayerarray)){
- $uniqueplayerslist = array_unique($allplayerarray);
- $resetplayerkeys = array_values($uniqueplayerslist);
- // got the users list until here . Now we have file for user stats 1 from users table
- $requiredids = array();
- foreach($resetplayerkeys as $key=>$playerid) {
- $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
- if(!empty($chkuserstats)){
- if($chkuserstats['User']['statas'] == 1){
- $validuserids = $playerid;
- }else{
- $validuserids = '';
- }
- }else{
- $validuserids = '';
- }
- $requiredids[] = $validuserids;
- }
- $reqids = array_values(array_filter($requiredids));
- if(!empty($reqids)){
- $userdata = array();
- foreach($reqids as $idkey=>$idval){
- $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')));
- $noofgameplaytoday = count($playDateData);
- $minimumplaylimit = 35;
- if($noofgameplaytoday >= $minimumplaylimit){
- $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')));
- $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
- $username = $this->User->find("first",array('conditions' => array('User.id' => $idval),'fields' => array('username')));
- if($winpercentageofuser > 0 ){
- $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
- }else{
- $userdata[] = array();
- }
- }else{
- $userdata[] = array();
- }
- }
- $userdata = array_values(array_filter($userdata));
- if(!empty($userdata)){
- foreach ($userdata as $key => $row) {
- $mid[$key] = $row['WinPercent'];
- }
- array_multisort($mid, SORT_DESC, $userdata);
- $allUserData = array_slice($userdata, 0, 10);
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response'=>array('Error'=>false,'UserPlayData'=>array()), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }
- }else{
- $this->set(array('Response'=>array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }elseif($dataBy == 3){
- //Game played this month
- $playDateData= $this->FinalScore->find("all",array('conditions'=>array('DATE(CreatedOn) BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE()')));
- // players list for this month
- if(!empty($playDateData)){
- $playeronearr = array();
- $playertwoarr = array();
- foreach($playDateData as $k=>$v){
- $playerids = explode(',',$v['FinalScore']['Players']);
- $playerone = $playerids[0];
- $playertwo = $playerids[1];
- $playeronearr[] = $playerone;
- $playertwoarr[] = $playertwo;
- }
- $allplayerarray = array_merge($playeronearr,$playertwoarr);
- if(!empty($allplayerarray)){
- $uniqueplayerslist = array_unique($allplayerarray);
- $resetplayerkeys = array_values($uniqueplayerslist);
- // got the users list until here . Now we have file for user stats 1 from users table
- $requiredids = array();
- foreach($resetplayerkeys as $key=>$playerid) {
- $chkuserstats = $this->User->find("first",array('conditions' => array('User.id' => $playerid),'fields' => array('statas')));
- if(!empty($chkuserstats)){
- if($chkuserstats['User']['statas'] == 1){
- $validuserids = $playerid;
- }else{
- $validuserids = '';
- }
- }else{
- $validuserids = '';
- }
- $requiredids[] = $validuserids;
- }
- $reqids = array_values(array_filter($requiredids));
- if(!empty($reqids)){
- $userdata = array();
- foreach($reqids as $idkey=>$idval){
- $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')));
- $noofgameplaytoday = count($playDateData);
- $minimumplaylimit = 100;
- if($noofgameplaytoday >= $minimumplaylimit){
- $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')));
- $winpercentageofuser = round((count($winplayDateData)/$noofgameplaytoday) * 100);
- $username = $this->User->find("first",array('conditions'=>array('User.id' => $idval),'fields' => array('username')));
- if($winpercentageofuser > 0 ){
- $userdata[] = array('Username' => $username['User']['username'],'WinPercent' => $winpercentageofuser);
- }else{
- $userdata[] = array();
- }
- }else{
- $userdata[] = array();
- }
- }
- $userdata = array_values(array_filter($userdata));
- if(!empty($userdata)){
- foreach ($userdata as $key => $row) {
- $mid[$key] = $row['WinPercent'];
- }
- array_multisort($mid, SORT_DESC, $userdata);
- $allUserData = array_slice($userdata, 0, 10);
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => $allUserData), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response'=>array('Error'=>false,'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response' => array('Error' => false, 'UserPlayData' => array()), '_serialize' => array('Response')));
- }
- }
- }else{
- $this->set(array('Response'=>array('Error' => false, 'UserPlayData'=>array()), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response'=>array('Error'=>true, 'Message' => 'No data available'), '_serialize' => array('Response')));
- }
- }
- }
- public function SetForcerValue(){
- $error = array();
- if(empty($this->request->query['UserId'])) {
- $error[] = "UsedrId parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- //Check User Exists
- $UserId = $this->request->query['UserId'];
- $chkUser = $this->User->find('first', array('conditions' => array('User.id' => $UserId),'recursive' => -1));
- if(count($chkUser) > 0 ){
- $checkuserplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $UserId), 'recursive' => -1));
- if(count($checkuserplaydata)> 0){
- $oldForcervalue = $checkuserplaydata['UserPlayGame']['Forcer'];
- $newForcervalue = $oldForcervalue + 1;
- $this->UserPlayGame->updateAll(array('UserPlayGame.Forcer' => $newForcervalue),array('UserPlayGame.user_id' => $UserId ));
- $this->set(array('Response' => array('Error' => false, 'Message' => 'Successfully Updated'), '_serialize' => array('Response')));
- }else{
- $data['UserPlayGame']['user_id'] = $UserId;
- $data['UserPlayGame']['Forcer'] = 1;
- $this->UserPlayGame->save($data);
- $this->set(array('Response' => array('Error' => false, 'Message' => 'Success'), '_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response'=>array('Error'=>true,'Message' => 'User Does Not exist'), '_serialize' => array('Response')));
- }
- }
- }
- public function SetHomeRunValue(){
- $error = array();
- if(empty($this->request->query['UserId'])) {
- $error[] = "UsedrId parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- //Check User Exists
- $UserId = $this->request->query['UserId'];
- $chkUser = $this->User->find('first', array('conditions' => array('User.id' => $UserId),'recursive' => -1));
- if(count($chkUser) > 0 ){
- $checkuserplaydata = $this->UserPlayGame->find('first', array('conditions' => array('UserPlayGame.user_id' => $UserId), 'recursive' => -1));
- if(count($checkuserplaydata)> 0){
- $oldHomerunvalue = $checkuserplaydata['UserPlayGame']['HomeRun'];
- $newHomeRunvalue = $oldHomerunvalue + 1;
- $this->UserPlayGame->updateAll(array('UserPlayGame.HomeRun' => $newHomeRunvalue),array('UserPlayGame.user_id' => $UserId ));
- $this->set(array('Response' => array('Error' => false, 'Message' => 'Successfully Updated'), '_serialize' => array('Response')));
- }else{
- $data['UserPlayGame']['user_id'] = $UserId;
- $data['UserPlayGame']['HomeRun'] = 1;
- $this->UserPlayGame->save($data);
- $this->set(array('Response' => array('Error' => false, 'Message' => 'Success'),'_serialize' => array('Response')));
- }
- }else{
- $this->set(array('Response'=>array('Error'=>true,'Message' => 'User Does Not exist'),'_serialize' => array('Response')));
- }
- }
- }
- public function GetAchievementValues(){
- $error = array();
- if(empty($this->request->query['UserId'])) {
- $error[] = "UserId parameter is empty";
- }
- if(!empty($error)){
- $errorMsg = implode(',',$error);
- $response = array('Error' => true, 'Message' => $errorMsg);
- $this->set(array('Response' => $response , '_serialize' => array('Response')));
- }else{
- $userId = $this->request->query['UserId'] ;
- //check if gameplaydata exists for this user
- $userplaydata = $this->UserPlayGame->find('all', array('conditions' => array('UserPlayGame.user_id'=> $userId), 'recursive' => -1));
- if(count($userplaydata) > 0){
- $homerun = $userplaydata[0]['UserPlayGame']['HomeRun'];
- $Forcer = $userplaydata[0]['UserPlayGame']['Forcer'];
- $Ace = $userplaydata[0]['UserPlayGame']['Ace'];
- $Unstoppable = $userplaydata[0]['UserPlayGame']['Unstoppable'];
- $Winner = $userplaydata[0]['UserPlayGame']['Winner'];
- $Player = $userplaydata[0]['UserPlayGame']['Player'];
- $Looser = $userplaydata[0]['UserPlayGame']['Looser'];
- $FullHouse = $userplaydata[0]['UserPlayGame']['FullHouse'];
- $WinStreak = $userplaydata[0]['UserPlayGame']['WinStreak'];
- $XthreeWinStreak = $userplaydata[0]['UserPlayGame']['XthreeWinStreak'];
- $XfiveWinstreak = $userplaydata[0]['UserPlayGame']['XfiveWinstreak'];
- $XtenWinstreak = $userplaydata[0]['UserPlayGame']['XtenWinstreak'];
- $data = array($Ace,$Unstoppable,$Winner,$Player,$XthreeWinStreak,$XfiveWinstreak,$XtenWinstreak,$Looser,$FullHouse,$homerun,$Forcer);
- $this->set(array('Response' => array('Error' => false, 'UserAchievementData' => $data, 'UserId' => $userId), '_serialize' => array('Response')));
- }else{
- $this->set(array('Response' => array('Error' => true, 'Message' => 'User Data Not available'), '_serialize' => array('Response')));
- }
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment