OSDN Git Service

Merge branch 'master' of https://scm.sourceforge.jp/gitroot/h58pcdgame/GameScriptCore...
[h58pcdgame/GameScriptCoreLibrary.git] / www / update.php
1 <?php
2 //FOR DEBUG
3 mysqli_report(MYSQLI_REPORT_ERROR);
4
5 require("header.php");
6         //MySQL 更新データの仕様
7         // URL引数でUserID(uid)を渡す。
8         // update.php?uid=xxx
9         //
10         // 
11
12 $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
13 if (mysqli_connect_error()) {
14         responseError();
15 }
16 // 文字化け防止
17 $db->set_charset("utf8");
18
19 if(isset($_GET['uid']) && isset($_GET['action'])){
20         $uid = $_GET['uid'];
21         $action = $_GET['action'];
22         //ステージ名の取得
23         $stmt = $db->prepare(QUERY_SELECT_USER_STAGE_AND_TIMESTAMP);
24         $stmt->bind_param(QUERY_SELECT_USER_STAGE_AND_TIMESTAMP_TYPES, $uid);
25         $stmt->execute();
26         if($stmt->errno != 0){
27                 exit("error0");
28         }
29         //クエリ成功
30         $stmt->store_result();
31         if($stmt->num_rows == 0){
32                 exit("error1");
33         }
34         $stmt->bind_result($stgName, $lastTimeStamp);
35         $stmt->fetch();
36         $stmt->close();
37         
38         if ($_SERVER['REQUEST_METHOD'] != "POST"){
39                 exit("error2");
40         }
41         $request_param = array();
42         foreach ( $_POST as $k => $v){
43                 // URL デコードを行い値を連想配列に格納する
44                 $request_param{"$k"} = urldecode($v);
45         }
46         
47         if(strcmp($action, 'add') == 0){
48                 //オブジェクトをデータベースに追加
49                 //postデータは、"origin.x,origin.y,speed.x,speed.y,className,attribute,constructorArgs"
50                 //retvデータは、"[timestamp,[objectID, ...], [[objectID,origin.x,origin.y,speed.x,speed.y,className,attribute,constructorArgs]...]]"
51                 $retarray = array();
52                 foreach($request_param as $aParam){
53                         $subparam = explode('|', $aParam);
54                         $stmt = $db->prepare(QUERY_ADD_OBJECT);
55                         $stmt->bind_param(QUERY_ADD_OBJECT_TYPES, $subparam[0], $subparam[1], $subparam[2], $subparam[3], $stgName, $subparam[4], $subparam[5], $subparam[6],$uid, getTimeStampMs(), getTimeStampMs());
56                         $stmt->execute();
57                         if($stmt->errno != 0){
58                                 exit("error3");
59                         }
60                         array_push($retarray, $db->insert_id);
61                         $stmt->close();
62                 }
63                 //出力
64                 echo("[" . getTimeStampMs() . ",[");
65                 foreach($retarray as $v){
66                         echo($v . ",");
67                 }
68                 echo("],");
69                 echo(getAllOtherUserObjectArrayString($db, $uid, $stgName));
70                 echo("]");
71         } else if(strcmp($action, 'refresh') == 0){
72                 //渡されたオブジェクトのデータ更新
73                 //postデータは、"objectID,origin.x,origin.y,speed.x,speed.y,attribute"
74                 foreach($request_param as $aParam){
75                         $subparam = explode('|', $aParam);
76                         $stmt = $db->prepare(QUERY_UPDATE_STAGE_OBJECT);
77                         $stmt->bind_param(QUERY_UPDATE_STAGE_OBJECT_TYPES, $subparam[1], $subparam[2], $subparam[3], $subparam[4], $subparam[5], getTimeStampMs(), $stgName, $subparam[0]);
78                         $stmt->execute();
79                         if($stmt->errno != 0){
80                                 exit("error14");
81                         }
82                         $stmt->close();
83                 }
84                 
85                 
86                 //retv[2]:追加リスト
87                 //[objectID,origin.x,origin.y,speed.x,speed.y,className,attribute,constructorArgs]"
88                 $retv2 = array();
89                 $stmt = $db->prepare(QUERY_SELECT_ADDED_OBJECT);
90                 $stmt->bind_param(QUERY_SELECT_ADDED_OBJECT_TYPES, $stgName, $uid, $lastTimeStamp);
91                 $stmt->execute();
92                 if($stmt->errno != 0){
93                         exit("error7");
94                 }
95                 $result = $stmt->get_result();
96                 for($i = 0; $i < $result->num_rows; $i++){
97                         $tmpAry = $result->fetch_array(MYSQLI_NUM);
98                         array_push($retv2, "[" . $tmpAry[0] . "," . $tmpAry[1] . "," . $tmpAry[2] . "," . $tmpAry[3] . "," . $tmpAry[4] . ",\"" . $tmpAry[5] . "\",\"" . $tmpAry[6] . "\",\"" . $tmpAry[7] . "\"]");
99                 }
100                 $stmt->close();
101                 //retv[1]:更新リスト
102                 //[objectID,origin.x,origin.y,speed.x,speed.y,className,attribute]"
103                 $retv1 = array();
104                 $stmt = $db->prepare(QUERY_SELECT_UPDATED_OBJECT);
105                 if(!$stmt){
106                         exit("error:" . $db->error);
107                 }
108                 $stmt->bind_param(QUERY_SELECT_UPDATED_OBJECT_TYPES, $stgName, $uid, $lastTimeStamp, $lastTimeStamp);
109                 $stmt->execute();
110                 if($stmt->errno != 0){
111                         exit("error8");
112                 }
113                 $result = $stmt->get_result();
114                 for($i = 0; $i < $result->num_rows; $i++){
115                         echo(" ");
116                         $tmpAry = $result->fetch_array(MYSQLI_NUM);
117                         array_push($retv1, "[" . $tmpAry[0] . "," . $tmpAry[1] . "," . $tmpAry[2] . "," . $tmpAry[3] . "," . $tmpAry[4] . ",\"" . $tmpAry[5] . "\",\"" . $tmpAry[6] . "\"]");
118                 }
119                 $stmt->close();
120                 //retv[3]:削除リスト
121                 //objectID
122                 $retv3 = array();
123                 $stmt = $db->prepare(QUERY_SELECT_DELETED_OBJECT);
124                 $stmt->bind_param(QUERY_SELECT_DELETED_OBJECT_TYPES, $stgName, $uid, $lastTimeStamp);
125                 $stmt->execute();
126                 if($stmt->errno != 0){
127                         exit("error9");
128                 }
129                 $result = $stmt->get_result();
130                 for($i = 0; $i < $result->num_rows; $i++){
131                         $tmpAry = $result->fetch_array(MYSQLI_NUM);
132                         array_push($retv3, $tmpAry[0]);
133                 }
134                 $stmt->close();
135                 //出力
136                 echo("[");
137                 echo(getTimeStampMs());
138                 echo(",[");
139                 foreach($retv1 as $v){
140                         echo($v . ",");
141                 }
142                 echo("],[");
143                 foreach($retv2 as $v){
144                         echo($v . ",");
145                 }
146                 echo("],[");
147                 foreach($retv3 as $v){
148                         echo($v . ",");
149                 }
150                 echo("]]");
151         }
152         updateUserTimestamp($db, $uid);
153         exit();
154 }
155
156 function getAllOtherUserObjectArrayString($db, $userID, $stageName)
157 {
158         $retary = array();
159         $stmt = $db->prepare(QUERY_SELECT_ALL_OTHER_USER_OBJECT);
160         $limittime = getTimeStampMs() - USER_ALIVE_TIME_MS;
161         $stmt->bind_param(QUERY_SELECT_ALL_OTHER_USER_OBJECT_TYPES, $stageName, $userID, $limittime);
162         $stmt->execute();
163         if($stmt->errno != 0){
164                 exit("error745");
165         }
166         $result = $stmt->get_result();
167         for($i = 0; $i < $result->num_rows; $i++){
168                 $tmpAry = $result->fetch_array(MYSQLI_NUM);
169                 array_push($retary, "[" . $tmpAry[0] . "," . $tmpAry[1] . "," . $tmpAry[2] . "," . $tmpAry[3] . "," . $tmpAry[4] . ",\"" . $tmpAry[5] . "\",\"" . $tmpAry[6] . "\",\"" . $tmpAry[7] . "\"]");
170         }
171         $stmt->close();
172         $retstr = "[";
173         foreach($retary as $v){
174                 $retstr = $retstr . $v . ",";
175         }
176         $retstr = $retstr . "]";
177         return $retstr;
178 }
179
180 //
181 // response
182 //
183 //[
184 //      [timestamp],
185 //      
186 //]
187 function responseError()
188 {
189         exit("[0, 0, null];");
190 }
191
192 ?>