Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GameController extends Controller
- {
- /**
- * Declares class-based actions.
- */
- public function filters()
- {
- return array(
- 'accessControl', // perform access control for CRUD operations
- );
- }
- /**
- * Specifies the access control rules.
- * This method is used by the 'accessControl' filter.
- * @return array access control rules
- */
- public function accessRules()
- {
- return array(
- array('allow', // allow all users to perform 'index' and 'view' actions
- 'actions'=>array('Login','PointFromPengawas','PointFromMap','GetCooldown','AddHistoriPemain',
- 'SetFaraway','CheckUpdate','PointFromPhoto','PointFromLocation','PointFromReading','Leaderboard','SubmitScore',
- 'SendMessage','CheckMessage','NewMessage','GetMessage','UpdateLocation'),
- 'users'=>array('*'),
- ),
- array('allow', // allow authenticated user to perform 'create' and 'update' actions
- 'actions'=>array('*'),
- 'users'=>array('*'),
- ),
- array('allow', // allow admin user to perform 'admin' and 'delete' actions
- 'actions'=>array('*'),
- 'users'=>array('*'),
- ),
- array('deny', // deny all users
- 'users'=>array('*'),
- ),
- );
- }
- public function actionSendMessage(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $receiver = $_POST['receiver'];
- $content = $_POST['content'];
- $new = new Message;
- $new->tanggal = new CDbExpression('NOW()');
- $new->sender = $username;
- $new->receiver = $receiver;
- $new->content = $content;
- if($new->save()){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>''
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionNewMessage(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $sender = $_POST['sender'];
- $data = Message::Model()->findAll('receiver = "'.$username.'" and sender = "'.$sender.'" and is_read = 0 order by tanggal');
- foreach ($data as $key => $value) {
- $value->is_read = 1;
- $value->save();
- }
- if(count($data) > 0){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$data
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>''
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionGetMessage(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $sender = $_POST['sender'];
- $data = Message::Model()->findAll('(receiver = "'.$username.'" and sender = "'.$sender.'" and is_read = 1) or (receiver = "'.$sender.'" and sender = "'.$username.'") order by tanggal');
- if(count($data)){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$data
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'',
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionCheckMessage()
- {
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $sender = $_POST['sender'];
- $count = Yii::app()->db->createCommand()->select('count(*) as jumlah')
- ->from('message')
- ->where('is_read = 0 and receiver = "'.$username.'" and sender = "'.$sender.'"')
- ->queryScalar();
- if($count > 0){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'1'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'0'
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionLogin()
- {
- if(isset($_POST['external'])){
- $username = $_POST["username"];
- $password = $_POST["password"];
- $user = MasterPengawas::model()->find('(username = "'.$username.'" or email = "'.$username.'") and password = "'.$password.'"');
- if($user == null){
- $user = MasterPemain::model()->find('(username = "'.$username.'" or email = "'.$username.'") and password = "'.$password.'"');
- if($user == null){
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'desc'=>""
- ));
- }else{
- //pemain
- $pengawas = MasterPengawas::model()->find('id = (select id_pengawas from detail_pengawas where id_pemain = '.$user->id.')');
- $data = $user->attributes;
- $data['pengawas'] = $pengawas;
- echo CJSON::encode(array(
- 'result'=>'success',
- 'type'=>1,
- 'data'=>$data
- ));
- }
- }else{
- //pengawas
- $data = $user->attributes;
- echo CJSON::encode(array(
- 'result'=>'success',
- 'type'=>2,
- 'data'=>$data
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionPointFromPengawas(){
- if(isset($_POST['external'])){
- $from = $_POST['from'];
- $to = $_POST['to'];
- $kode = $_POST['kode'];
- $keterangan = $_POST['keterangan'];
- $point = MasterPoint::Model()->find('kode = "'.$kode.'"');
- $pengawas = MasterPengawas::Model()->find('username = "'.$from.'"');
- $pemain = MasterPemain::Model()->find('username = "'.$to.'"');
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $point->id;
- $new->id_user = $pemain->id;
- $new->id_pengawas = $pengawas->id;
- $new->place_id = '';
- $new->extra = $keterangan;
- $new->jumlah = $point->jumlah;
- $new->is_terima = 0;
- if($new->save()){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'Point berhasil dikirim.'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionPointFromMap(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $kode = $_POST['kode'];
- $place = $_POST["place"];
- $namalokasi = $_POST["nama_lokasi"];
- $point = MasterPoint::Model()->find('kode = "'.$kode.'"');
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $point->id;
- $new->id_user = $pemain->id;
- $new->id_pengawas = 0;
- $new->place_id = $place;
- $new->extra = '';
- $new->jumlah = $point->jumlah;
- $new->is_terima = 1;
- if($new->save()){
- $histori = new HistoriPemain;
- $histori->tanggal = new CDbExpression('NOW()');
- $histori->isi = 'Mengunjungi '.$namalokasi.' untuk '.$new->jumlah.' point.';
- $histori->keterangan = '';
- $histori->extra = '';
- $histori->id_pemain = $new->id_user;
- $histori->save();
- if($this->AddSaldo($new->id_user,$new->id_point,$new->jumlah)){
- if($this->SetStatusLokasi($new->id_user,$new->place_id,$point->cooldown)){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'Point berhasil didapatkan.'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'Menyimpan status gagal.'
- ));
- }
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'Menyimpan saldo gagal.'
- ));
- }
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionPointFromLocation(){
- if(isset($_POST['external'])){
- $transaction = Yii::app()->db->beginTransaction();
- $username = $_POST['username'];
- $kode = $_POST['kode'];
- $coordinate = $_POST["coordinate"];
- $lokasi = $_POST["lokasi"];
- $point = MasterPoint::Model()->find('kode = "'.$kode.'"');
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $point->id;
- $new->id_user = $pemain->id;
- $new->data = "LOCATION:".$coordinate;
- $new->extra = $lokasi;
- $new->jumlah = 15;
- if($new->save()){
- // $histori = new HistoriPemain;
- // $histori->tanggal = new CDbExpression('NOW()');
- // $histori->isi = 'Mengunjungi '.$namalokasi.' untuk '.$new->jumlah.' point.';
- // $histori->keterangan = '';
- // $histori->extra = '';
- // $histori->id_pemain = $new->id_user;
- // $histori->save();
- $pemain->current_location = $coordinate;
- $pemain->save();
- if($this->AddSaldo($new->id_user,$new->id_point,$new->jumlah)){
- $transaction->commit();
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$new->jumlah.' poin berhasil diterima.'
- ));
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'Menyimpan saldo gagal.'
- ));
- }
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function AddSaldo($id,$point,$jumlah){
- $saldo = SaldoPoint::Model()->find('id_user = '.$id.' and id_point = '.$point);
- if($saldo == null){
- $saldo = new SaldoPoint;
- $saldo->id_user = $id;
- $saldo->id_point = $point;
- $saldo->jumlah = $jumlah;
- }else{
- $saldo->jumlah = $saldo->jumlah + $jumlah;
- }
- return $saldo->save();
- }
- public function SetStatusLokasi($id,$place_id,$cooldown){
- $status = StatusLokasi::Model()->find('id_pemain = '.$id.' and place_id = "'.$place_id.'"');
- if($status == null){
- $status = new StatusLokasi;
- $status->id_pemain = $id;
- $status->place_id = $place_id;
- }
- $status->tgl_cooldown = new CDbExpression('DATE_ADD(NOW(), INTERVAL '.$cooldown.' SECOND)');
- return $status->save();
- }
- public function actionGetCooldown(){
- if(isset($_POST['external'])){
- $place = $_POST['place'];
- $username = $_POST['username'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $status = StatusLokasi::Model()->find('id_pemain = '.$pemain->id.' and place_id = "'.$place.'"');
- if($status != null){
- $remaining = Yii::app()->db
- ->createCommand('SELECT TIME_TO_SEC(TIMEDIFF("'.$status->tgl_cooldown.'",(select NOW()))) diff')
- ->queryScalar();
- }else{
- $remaining = 0;
- }
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$remaining
- ));
- }else{
- echo "Who are you?";
- }
- }
- public function actionAddHistoriPemain(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $isi = $_POST['isi'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $histori = new HistoriPemain;
- $histori->tanggal = new CDbExpression('NOW()');
- $histori->isi = $isi;
- $histori->keterangan = '';
- $histori->extra = '';
- $histori->id_pemain = $pemain->id;
- if($histori->save()){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'Histori berhasil disimpan.'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionSetFaraway(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $status = $_POST['status'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $pemain->is_faraway = $status;
- if($pemain->save()){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'Status faraway berhasil disimpan.'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$pemain->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionUpdateLocation(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $x = $_POST['x'];
- $y = $_POST['y'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $pemain->current_location = $x.','.$y;
- if($pemain->save()){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>'Update lokasi berhasil.'
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$pemain->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionCheckUpdate(){
- if(isset($_POST['external'])){
- $username = $_POST['username'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $data = array();
- $pendingPoints = HistoriPoint::Model()->findAll('is_terima = 0 and id_user = '.$pemain->id);
- if(count($pendingPoints) > 0){
- foreach ($pendingPoints as $key => $value) {
- $value->is_terima = 1;
- if($value->save()){
- $this->AddSaldo($value->id_user,$value->id_point,$value->jumlah);
- array_push($data,array(
- 'tipe'=>'notification',
- 'from'=>$value->pengawas->nama,
- 'isi'=>$value->extra
- ));
- }
- }
- }
- if(count($data) > 0){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$data
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>"Tidak ada data"
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionPointFromPhoto(){
- if(isset($_POST['external'])){
- $transaction = Yii::app()->db->beginTransaction();
- $username = $_POST['username'];
- $kode = $_POST['kode'];
- $point = MasterPoint::Model()->find('kode = "'.$kode.'"');
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $point->id;
- $new->id_user = $pemain->id;
- $new->extra = $username;
- $new->jumlah = 10;
- $photo=CUploadedFile::getInstanceByName('photo');
- $name = md5(date('Y-m-d h:i:s').$username).".".$photo->extensionName;
- if(!empty($photo)){
- $new->data="PHOTO:".$name;
- }else{$new->data=null;}
- if($new->save()){
- if(!empty($photo)){
- $photo->saveAs(Yii::app()->basePath.'/../images/pemain/'.$name); // image will uplode to rootDirectory/
- }
- if($this->AddSaldo($new->id_user,$new->id_point,$new->jumlah)){
- $transaction->commit();
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$new->jumlah.' poin berhasil diterima.'
- ));
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'error'
- ));
- }
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionPointFromReading(){
- if(isset($_POST['external'])){
- $transaction = Yii::app()->db->beginTransaction();
- $username = $_POST['username'];
- $kode = $_POST['kode'];
- $point = MasterPoint::Model()->find('kode = "'.$kode.'"');
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $point->id;
- $new->id_user = $pemain->id;
- $new->data = "READ:500";
- $new->jumlah = 25;
- if($new->save()){
- if($this->AddSaldo($new->id_user,$new->id_point,$new->jumlah)){
- $transaction->commit();
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$new->jumlah.' poin berhasil diterima.'
- ));
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>'error'
- ));
- }
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionSubmitScore(){
- if(isset($_POST['external'])){
- $transaction = Yii::app()->db->beginTransaction();
- $username = $_POST['username'];
- $score = $_POST['score'];
- $pemain = MasterPemain::Model()->find('username = "'.$username.'"');
- if($score > $pemain->score)
- {
- $pemain->score = $score;
- $result = $pemain->save();
- }else{
- $result = TRUE;
- }
- if($result){
- $saldo = SaldoPoint::Model()->findAll('id_user = '.$pemain->id);
- foreach ($saldo as $key => $each) {
- $each->jumlah = 0;
- $each->save();
- $new = new HistoriPoint;
- $new->tanggal = new CDbExpression('NOW()');
- $new->id_point = $each->id_point;
- $new->id_user = $pemain->id;
- $new->data = "GAME:".$score;
- $new->jumlah = 0;
- $new->save();
- }
- $transaction->commit();
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>''
- ));
- }else{
- $transaction->rollback();
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>$new->getErrors()
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- public function actionLeaderboard(){
- if(isset($_POST['external'])){
- $list = MasterPemain::Model()->
- findAll(array(
- 'condition'=>'',
- 'order'=>'score desc',
- 'limit'=>5
- ));
- $result = array();
- foreach ($list as $key => $value) {
- array_push($result, $value->attributes);
- }
- if(count($result) > 0){
- echo CJSON::encode(array(
- 'result'=>'success',
- 'data'=>$result
- ));
- }else{
- echo CJSON::encode(array(
- 'result'=>'failed',
- 'data'=>"Tidak ada leaderboard."
- ));
- }
- }else{
- echo "Who are you?";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement