첩보를 join '|' 에서 json dict로 변경

This commit is contained in:
2018-07-07 04:39:44 +09:00
parent 14d9b0dd31
commit 9b4b835c08
11 changed files with 119 additions and 91 deletions
+26 -21
View File
@@ -21,9 +21,7 @@ $query = "select no,nation,level,city from general where owner='{$userID}'";
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); $result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$me = MYDB_fetch_array($result); $me = MYDB_fetch_array($result);
$query = "select nation,level,spy from nation where nation='{$me['nation']}'"; $myNation = $db->queryFirstRow('SELECT nation,level,spy FROM nation WHERE nation=%i', $me['nation']);
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$myNation = MYDB_fetch_array($result);
$templates = new \League\Plates\Engine('templates'); $templates = new \League\Plates\Engine('templates');
@@ -121,27 +119,34 @@ if($me['level'] == 0) {
if($myNation['level'] > 0) { if($myNation['level'] > 0) {
// 첩보도시도 목록에 추가 // 첩보도시도 목록에 추가
$where = 'city=0';
$cities = array_map('intval',explode("|", $myNation['spy'])); $rawSpy = $myNation['spy'];
foreach($cities as $citySpy) {
$city = intdiv($citySpy, 10); if($rawSpy == ''){
$where .= " or city='{$city}'"; $spyCities = [];
}
else if(strpos($rawSpy, '|') !== false || is_integer($rawSpy)){
//TODO: 0.8 버전 이후에는 삭제할 것. 이후 버전은 json으로 변경됨.
$spyCities = array_map(function($val){
$val = intval($val);
return intdiv($val, 10);
}, 'intval',explode('|', $myNation['spy']));
}
else{
$spyCities = array_keys(Json::decode($rawSpy));
} }
$query = "select city,name,nation from city where {$where}";
$cityresult = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$citycount = MYDB_num_rows($cityresult);
for($i=0; $i < $citycount; $i++) { if($spyCities){
$city = MYDB_fetch_array($cityresult); foreach ($db->query('SELECT city,name,nation FROM city WHERE city in %li', $spyCities) as $city) {
echo " echo "<option value={$city['city']}";
<option value={$city['city']}"; if($city['city'] == $citylist) { echo " selected"; $valid = 1; }
if($city['city'] == $citylist) { echo " selected"; $valid = 1; } echo ">==================================================【".StringUtil::padString($city['name'], 4, '_')."】";
echo ">==================================================【".StringUtil::padString($city['name'], 4, '_')."】"; if($city['nation'] == 0) echo "공백지";
if($city['nation'] == 0) echo "공백지"; elseif($me['nation'] == $city['nation']) echo "본국==";
elseif($me['nation'] == $city['nation']) echo "국=="; else echo "국==";
else echo "타국=="; echo "============================================</option>";
echo "============================================</option>"; }
} }
} }
+1 -1
View File
@@ -1128,7 +1128,7 @@ function msgprint($msg, $name, $picture, $imgsvr, $when, $num, $type) {
$db = DB::db(); $db = DB::db();
$connect=$db->get(); $connect=$db->get();
$message = explode("|", $msg); $message = explode('|', $msg);
$count = (count($message) - 2)/2; $count = (count($message) - 2)/2;
$message[0] = Tag2Code($message[0]); $message[0] = Tag2Code($message[0]);
$message[1] = Tag2Code($message[1]); $message[1] = Tag2Code($message[1]);
+26 -16
View File
@@ -395,23 +395,33 @@ function preUpdateMonthly() {
} }
//첩보-1 //첩보-1
$query = "select nation,spy from nation where spy!=''"; foreach($db->queryAllLists("SELECT nation, spy FROM nation WHERE spy!='' AND spy!='{}'") as [$nationNo, $rawSpy]){
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); if (strpos($rawSpy, '|') !== false || is_integer($rawSpy)) {
$nationCount = MYDB_num_rows($result); //TODO: 0.8 버전 이후에는 삭제할 것. 이후 버전은 json으로 변경됨.
for($i=0; $i < $nationCount; $i++) { $spyInfo = [];
$nation = MYDB_fetch_array($result); foreach(explode('|', $rawSpy) as $value){
$spy = ""; $k = 0; $value = intval($value);
$cities = []; $cityNo = intdiv($value, 10);
if($nation['spy'] != "") { $cities = explode("|", (string)$nation['spy']); } $remainMonth = $value % 10;
while(count($cities)) { $spyInfo[$cityNo] = $remainMonth;
$cities[$k]--; }
if($cities[$k]%10 != 0) { $spy .= (string)$cities[$k]; }
$k++;
if($k >= count($cities)) { break; }
if($cities[$k-1]%10 != 0) { $spy .= "|"; }
} }
$query = "update nation set spy='$spy' where nation='{$nation['nation']}'"; else{
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); $spyInfo = Json::decode($rawSpy);
}
foreach($spyInfo as $cityNo => $remainMonth){
if($remainMonth <= 1){
unset($spyInfo[$cityNo]);
}
else{
$spyInfo[$cityNo] -= 1;
}
}
$db->update('nation', [
'spy'=>Json::encode($spyInfo, Json::EMPTY_ARRAY_IS_DICT)
], 'nation=%i', $nationNo);
} }
return true; return true;
+19 -9
View File
@@ -94,19 +94,29 @@ function getWorldMap($req){
$myNation = null; $myNation = null;
} }
$spyInfo = (object)null;
if($myNation){ if($myNation){
$spyList = $db->queryFirstField('select `spy` from `nation` where `nation`=%i', $rawSpy = $db->queryFirstField('select `spy` from `nation` where `nation`=%i',
$myNation); $myNation);
if($spyList){
$spyList = array_map('\sammo\Util::toInt', explode("|", $spyList)); if(strpos($rawSpy, '|') !== false || is_integer($rawSpy)){
//NOTE: 0.8 이전 데이터가 남아있으므로, 0.8버전으로 마이그레이션 이후에도 이곳은 삭제하면 안됨
$spyInfo = [];
foreach(explode('|', $rawSpy) as $value){
$value = intval($value);
$cityNo = intdiv($value, 10);
$remainMonth = $value % 10;
$spyInfo[$cityNo] = $remainMonth;
}
} }
else{ else if($rawSpy != ''){
$spyList = []; $spyInfo = Json::decode($rawSpy);
} }
} }
else{
$spyList = []; if(!$spyInfo){
$spyInfo = (object)null;
} }
$nationList = []; $nationList = [];
@@ -142,7 +152,7 @@ function getWorldMap($req){
'month' => $month, 'month' => $month,
'cityList' => $cityList, 'cityList' => $cityList,
'nationList' => $nationList, 'nationList' => $nationList,
'spyList' => $spyList, 'spyList' => $spyInfo,
'shownByGeneralList' => $shownByGeneralList, 'shownByGeneralList' => $shownByGeneralList,
'myCity' => $myCity, 'myCity' => $myCity,
'myNation' => $myNation, 'myNation' => $myNation,
+4 -4
View File
@@ -361,7 +361,7 @@ function processAI($no) {
} }
break; break;
case 2: //이동 20% case 2: //이동 20%
$paths = explode("|", $city['path']); $paths = explode('|', $city['path']);
$command = EncodeCommand(0, 0, $paths[rand()%count($paths)], 21); $command = EncodeCommand(0, 0, $paths[rand()%count($paths)], 21);
break; break;
default: default:
@@ -429,7 +429,7 @@ function processAI($no) {
return; return;
} elseif(rand()%4 > 0) { } elseif(rand()%4 > 0) {
//이동 //이동
$paths = explode("|", $city['path']); $paths = explode('|', $city['path']);
$command = EncodeCommand(0, 0, $paths[rand()%count($paths)], 21); $command = EncodeCommand(0, 0, $paths[rand()%count($paths)], 21);
$query = "update general set turn0='$command' where no='{$general['no']}'"; $query = "update general set turn0='$command' where no='{$general['no']}'";
MYDB_query($query, $connect) or Error("processAI09 ".MYDB_error($connect),""); MYDB_query($query, $connect) or Error("processAI09 ".MYDB_error($connect),"");
@@ -600,7 +600,7 @@ function processAI($no) {
//현도시가 전방이면 공격 가능성 체크 //현도시가 전방이면 공격 가능성 체크
if($city['front'] > 0) { if($city['front'] > 0) {
//주변도시 체크 //주변도시 체크
$paths = explode("|", $city['path']); $paths = explode('|', $city['path']);
for($i=0; $i < count($paths); $i++) { for($i=0; $i < count($paths); $i++) {
$query = "select city,nation from city where city='$paths[$i]'"; $query = "select city,nation from city where city='$paths[$i]'";
$result = MYDB_query($query, $connect) or Error("processAI20 ".MYDB_error($connect),""); $result = MYDB_query($query, $connect) or Error("processAI20 ".MYDB_error($connect),"");
@@ -766,7 +766,7 @@ function processAI($no) {
$command = EncodeCommand(0, 0, 0, (rand()%2)*8 + 1); // 준비는 됐으나 아직 선포중이면 내정, 조달 $command = EncodeCommand(0, 0, 0, (rand()%2)*8 + 1); // 준비는 됐으나 아직 선포중이면 내정, 조달
} else { } else {
//공격 & 내정 //공격 & 내정
$paths = explode("|", $city['path']); $paths = explode('|', $city['path']);
for($i=0; $i < count($paths); $i++) { for($i=0; $i < count($paths); $i++) {
$query = "select city,nation from city where city='$paths[$i]'"; $query = "select city,nation from city where city='$paths[$i]'";
$result = MYDB_query($query, $connect) or Error("processAI21 ".MYDB_error($connect),""); $result = MYDB_query($query, $connect) or Error("processAI21 ".MYDB_error($connect),"");
+22 -22
View File
@@ -1743,32 +1743,32 @@ function process_31(&$general) {
$query = "update general set resturn='SUCCESS',gold='{$general['gold']}',rice='{$general['rice']}',leader2='{$general['leader2']}',dedication=dedication+'$ded',experience=experience+'$exp' where no='{$general['no']}'"; $query = "update general set resturn='SUCCESS',gold='{$general['gold']}',rice='{$general['rice']}',leader2='{$general['leader2']}',dedication=dedication+'$ded',experience=experience+'$exp' where no='{$general['no']}'";
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$query = "select spy from nation where nation='{$general['nation']}'";
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$nation = MYDB_fetch_array($result); $rawSpy = $db->queryFirstField('SELECT spy FROM nation WHERE nation = %i', $general['nation']);
if($nation['spy'] != "")
{ if($rawSpy == ''){
$cities = array_map('intval', explode("|", $nation['spy'])); $spyInfo = [];
} }
else{ else if(strpos($rawSpy, '|') !== false || is_integer($rawSpy)){
$cities = []; //TODO: 0.8 버전 이후에는 삭제할 것. 이후 버전은 json으로 변경됨.
} $spyInfo = [];
$exist = 0; foreach(explode('|', $rawSpy) as $value){
for($i=0; $i < count($cities); $i++) { $value = intval($value);
if(intdiv($cities[$i], 10) == $destination) { $cityNo = intdiv($value, 10);
$exist = 1; $remainMonth = $value % 10;
break; $spyInfo[$cityNo] = $remainMonth;
} }
} }
// 기존 첩보 목록에 없으면 새로 등록, 있으면 갱신 else{
if($exist == 0) { $spyInfo = Json::decode($rawSpy);
$cities[] = $destination * 10 + 3;
} else {
$cities[$i] = $destination * 10 + 3;
} }
$spy = implode("|", $cities);
$query = "update nation set spy='$spy' where nation='{$general['nation']}'"; $spyInfo[$destination] = 3;
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$db->update('nation', [
'spy'=>Json::encode($spyInfo, Json::EMPTY_ARRAY_IS_DICT)
], 'nation=%i', $general['nation']);
$log = checkAbility($general, $log); $log = checkAbility($general, $log);
} }
+1
View File
@@ -14,6 +14,7 @@ $defaultPost = [
'showMe' => true 'showMe' => true
]; ];
$post = Json::decode(Util::getReq('data', 'string', '{}')); $post = Json::decode(Util::getReq('data', 'string', '{}'));
$post += $defaultPost;
if(!$session->isGameLoggedIn()){ if(!$session->isGameLoggedIn()){
$post['neutralView'] = true; $post['neutralView'] = true;
+1 -13
View File
@@ -119,23 +119,11 @@ function reloadWorldMap(option){
}; };
} }
function convertSpyList(arr){
var result = [];
arr.forEach(function(v){
var cityId = (v / 10) | 0;
var spy = v % 10;
result[cityId] = spy;
});
return result;
}
var cityList = obj.cityList.map(toCityObj); var cityList = obj.cityList.map(toCityObj);
var nationList = obj.nationList.map(toNationObj); var nationList = obj.nationList.map(toNationObj);
nationList = convertDictById(nationList); //array of object -> dict nationList = convertDictById(nationList); //array of object -> dict
var spyList = convertSpyList(obj.spyList);//Array -> Dict var spyList = obj.spyList;
var shownByGeneralList = convertSet(obj.shownByGeneralList);//Array -> Set var shownByGeneralList = convertSet(obj.shownByGeneralList);//Array -> Set
var myCity = obj.myCity; var myCity = obj.myCity;
+1 -1
View File
@@ -199,7 +199,7 @@ CREATE TABLE `nation` (
`tech` INT(8) NULL DEFAULT '0', `tech` INT(8) NULL DEFAULT '0',
`totaltech` INT(8) NULL DEFAULT '0', `totaltech` INT(8) NULL DEFAULT '0',
`power` INT(8) NULL DEFAULT '0', `power` INT(8) NULL DEFAULT '0',
`spy` CHAR(255) NULL DEFAULT '', `spy` CHAR(255) NOT NULL DEFAULT '{}',
`level` INT(1) NULL DEFAULT '0', `level` INT(1) NULL DEFAULT '0',
`type` INT(2) NULL DEFAULT '0', `type` INT(2) NULL DEFAULT '0',
`rule` TEXT NULL DEFAULT '', `rule` TEXT NULL DEFAULT '',
+15 -4
View File
@@ -3,10 +3,11 @@ namespace sammo;
class Json class Json
{ {
const PRETTY = 1; const PRETTY = 1 << 0;
const DELETE_NULL = 2; const DELETE_NULL = 1 << 1;
const NO_CACHE = 4; const NO_CACHE = 1 << 2;
const PASS_THROUGH = 8; const PASS_THROUGH = 1 << 3;
const EMPTY_ARRAY_IS_DICT = 1 << 4;
public static function encode($value, $flag = 0) public static function encode($value, $flag = 0)
{ {
@@ -17,6 +18,10 @@ class Json
if ($flag & static::DELETE_NULL) { if ($flag & static::DELETE_NULL) {
$value = Util::eraseNullValue($value); $value = Util::eraseNullValue($value);
} }
if(($flag & static::EMPTY_ARRAY_IS_DICT) && $value === []){
$value = (object)null;
}
return json_encode($value, $rawFlag); return json_encode($value, $rawFlag);
} }
@@ -25,6 +30,12 @@ class Json
return json_decode($value, true); return json_decode($value, true);
} }
public static function decodeObj($value){
//NOTE: 구 코드가 모두 '배열'을 가정하기 때문에 decode는 연관배열로 반환하였으나,
//호환을 위해서는object로 반환하는 것이 더 나을것
return json_decode($value);
}
public static function die($value, $flag = self::NO_CACHE) public static function die($value, $flag = self::NO_CACHE)
{ {
//NOTE: REST 형식에 맞게, ok(), fail()로 쪼개는게 낫지 않을까 생각해봄. //NOTE: REST 형식에 맞게, ok(), fail()로 쪼개는게 낫지 않을까 생각해봄.
+3
View File
@@ -160,6 +160,9 @@ class Util extends \utilphp\util
if ($val == null) { if ($val == null) {
return null; return null;
} }
if ($val == ''){
return null;
}
return intval($val); return intval($val);
} }
throw new \InvalidArgumentException('올바르지 않은 타입형 :'.$val); throw new \InvalidArgumentException('올바르지 않은 타입형 :'.$val);