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] . "\"," . $tmpAry[8] . "]");
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                 //retv[4]:ユーザーリスト
136                 //userID, userName
137                 $retv4 = array();
138                 $stmt = $db->prepare("select id, name from authUserTable");
139                 $stmt->execute();
140                 if($stmt->errno != 0){
141                         exit("error5673");
142                 }
143                 $result = $stmt->get_result();
144                 for($i = 0; $i < $result->num_rows; $i++){
145                         $tmpAry = $result->fetch_array(MYSQLI_NUM);
146                         array_push($retv4, $tmpAry[0] . ":\"" . $tmpAry[1] . "\"");
147                 }
148                 $stmt->close();
149                 //出力
150                 echo("[");
151                 echo(getTimeStampMs());
152                 echo(",[");
153                 foreach($retv1 as $v){
154                         echo($v . ",");
155                 }
156                 echo("],[");
157                 foreach($retv2 as $v){
158                         echo($v . ",");
159                 }
160                 echo("],[");
161                 foreach($retv3 as $v){
162                         echo($v . ",");
163                 }
164                 echo("],{");
165                 foreach($retv4 as $v){
166                         echo($v . ",");
167                 }
168                 echo("}]");
169         }
170         updateUserTimestamp($db, $uid);
171         exit();
172 }
173
174 function getAllOtherUserObjectArrayString($db, $userID, $stageName)
175 {
176         $retary = array();
177         $stmt = $db->prepare(QUERY_SELECT_ALL_OTHER_USER_OBJECT);
178         $limittime = getTimeStampMs() - USER_ALIVE_TIME_MS;
179         $stmt->bind_param(QUERY_SELECT_ALL_OTHER_USER_OBJECT_TYPES, $stageName, $userID, $limittime);
180         $stmt->execute();
181         if($stmt->errno != 0){
182                 exit("error745");
183         }
184         $result = $stmt->get_result();
185         for($i = 0; $i < $result->num_rows; $i++){
186                 $tmpAry = $result->fetch_array(MYSQLI_NUM);
187                 array_push($retary, "[" . $tmpAry[0] . "," . $tmpAry[1] . "," . $tmpAry[2] . "," . $tmpAry[3] . "," . $tmpAry[4] . ",\"" . $tmpAry[5] . "\",\"" . $tmpAry[6] . "\",\"" . $tmpAry[7] . "\"," . $tmpAry[8] . "]");
188         }
189         $stmt->close();
190         $retstr = "[";
191         foreach($retary as $v){
192                 $retstr = $retstr . $v . ",";
193         }
194         $retstr = $retstr . "]";
195         return $retstr;
196 }
197
198 //
199 // response
200 //
201 //[
202 //      [timestamp],
203 //      
204 //]
205 function responseError()
206 {
207         exit("[0, 0, null];");
208 }
209
210 ?>