From 42fc0f6aedc2516892fc33fd7b70d5ebe382610b Mon Sep 17 00:00:00 2001 From: Hide_D Date: Mon, 25 Apr 2022 00:30:31 +0900 Subject: [PATCH] =?UTF-8?q?feat,wip:=20=ED=86=A0=EB=84=88=EB=A8=BC?= =?UTF-8?q?=ED=8A=B8=20=EB=B2=A0=ED=8C=85=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .phan/config.php | 1 - hwe/b_betting.php | 93 ++++++++++++++++++++++------------------- hwe/b_tournament.php | 92 ++++++++++++++++++++++++++++++---------- hwe/c_tournament.php | 2 + hwe/func_tournament.php | 63 ++++++++++++---------------- hwe/j_betting.php | 55 ------------------------ hwe/ts/betting.ts | 36 ++++++---------- 7 files changed, 164 insertions(+), 178 deletions(-) delete mode 100644 hwe/j_betting.php diff --git a/.phan/config.php b/.phan/config.php index 56676933..0d09cd0c 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -84,7 +84,6 @@ return [ 'hwe/j_adjust_icon.php', 'hwe/j_autoreset.php', 'hwe/j_basic_info.php', - 'hwe/j_betting.php', 'hwe/j_board_article_add.php', 'hwe/j_board_comment_add.php', 'hwe/j_board_get_articles.php', diff --git a/hwe/b_betting.php b/hwe/b_betting.php index 478dbdea..5408e310 100644 --- a/hwe/b_betting.php +++ b/hwe/b_betting.php @@ -17,40 +17,10 @@ TurnExecutionHelper::executeAllCommand(); $generalID = $session->generalID; -$bettingID = $gameStor->lastTournamentBettingID ?? 0; -if ($bettingID == 0) { - //TODO: 좋은 방법 없나? - die('아직 베팅이 열리지 않았습니다.'); -} -$betting = new Betting($bettingID); -$info = $betting->getInfo(); - -$myBet = []; -$globalBet = []; - -foreach ($db->queryAllLists( - 'SELECT general_id, user_id, amount, betting_type FROM ng_betting WHERE betting_id = %i', - $this->bettingID -) as [$betGeneralID, $userID, $amount, $bettingTypeKey]) { - $bettingKey = Json::decode($bettingTypeKey)[0]; - if ($betGeneralID == $generalID) { - $myBet[$bettingKey] = $amount; - } - - if (key_exists($bettingKey, $globalBet)) { - $globalBet[$bettingKey] += $amount; - } else { - $globalBet[$bettingKey] = $amount; - } -} - $me = $db->queryFirstRow('SELECT no,tournament,con,turntime from general where owner=%i', $userID); -$myBetTotal = array_sum($myBet); -$globalBetTotal = array_sum($globalBet); - $admin = $gameStor->getValues(['tournament', 'phase', 'tnmt_type', 'develcost']); $con = checkLimit($me['con']); @@ -60,34 +30,68 @@ if ($con >= 2) { } switch ($admin['tnmt_type']) { - default: - throw new \RuntimeException('Invalid tnmt_type'); case convertTournamentType('전력전'): - $tnmt_type = "전력전"; + $tnmt_type = "전력전"; + $color = "cyan"; $tp = "total"; $tp2 = "종합"; $tp3 = "total"; break; case convertTournamentType('통솔전'): - $tnmt_type = "통솔전"; + $tnmt_type = "통솔전"; + $color = "cyan"; $tp = "leadership"; $tp2 = "통솔"; $tp3 = "leadership"; break; case convertTournamentType('일기토'): - $tnmt_type = "일기토"; + $tnmt_type = "일기토"; + $color = "cyan"; $tp = "strength"; $tp2 = "무력"; $tp3 = "strength"; break; case convertTournamentType('설전'): - $tnmt_type = "설전"; + $tnmt_type = "설전"; + $color = "cyan"; $tp = "intel"; $tp2 = "지력"; $tp3 = "intel"; break; + default: + throw new \RuntimeException('Invalid tnmt_type'); } +$bettingID = $gameStor->lastTournamentBettingID ?? 0; +$myBet = []; +$globalBet = []; + +if ($bettingID != 0) { + $betting = $bettingID != 0 ? new Betting($bettingID) : null; + $info = $betting->getInfo(); + + foreach ($db->queryAllLists( + 'SELECT general_id, user_id, amount, betting_type FROM ng_betting WHERE betting_id = %i', + $bettingID + ) as [$betGeneralID, $userID, $amount, $bettingTypeKey]) { + $bettingKey = Json::decode($bettingTypeKey)[0]; + if ($betGeneralID == $generalID) { + $myBet[$bettingKey] = $amount; + } + + if (key_exists($bettingKey, $globalBet)) { + $globalBet[$bettingKey] += $amount; + } else { + $globalBet[$bettingKey] = $amount; + } + } +} else { + $info = getDummyBettingInfo($tnmt_type); +} + +$myBetTotal = array_sum($myBet); +$globalBetTotal = array_sum($globalBet); + $str1 = getTournament($admin['tournament']); $str2 = getTournamentTime(); if ($str2) { @@ -114,6 +118,11 @@ if ($str3) { + [ + 'bettingID'=> $bettingID + ] + ])?> @@ -128,7 +137,7 @@ if ($str3) { - () + () @@ -368,7 +377,7 @@ if ($str3) { "; foreach (range(0, 15) as $i) { - $amount = $bet[$keyMap[$i]] ?? 0; + $amount = $bet[$keyMap[$i] ?? -1] ?? 0; echo "{$amount}"; } ?> @@ -394,7 +403,7 @@ if ($str3) { {$amount}"; } ?> @@ -420,7 +429,7 @@ if ($str3) { {$amount}"; } @@ -433,7 +442,7 @@ if ($str3) { "; foreach (range(0, 15) as $i) { - $key = $keyMap[$i]; + $key = $keyMap[$i] ?? -1; echo " "; } diff --git a/hwe/b_tournament.php b/hwe/b_tournament.php index a174cbfc..0be9dae3 100644 --- a/hwe/b_tournament.php +++ b/hwe/b_tournament.php @@ -15,6 +15,7 @@ increaseRefresh("토너먼트", 1); TurnExecutionHelper::executeAllCommand(); $me = $db->queryFirstRow('select no,tournament,con,turntime from general where owner=%i', $userID); +$generalID = $session->generalID; $admin = $gameStor->getValues(['tournament', 'phase', 'tnmt_msg', 'tnmt_type', 'develcost', 'tnmt_trig']); @@ -25,34 +26,76 @@ if ($con >= 2) { } switch ($admin['tnmt_type']) { - default: - throw new \RuntimeException('invalid tnmt_type'); - case 0: - $tnmt_type = "전력전"; + case convertTournamentType('전력전'): + $tnmt_type = "전력전"; + $color = 'cyan'; $tp = "total"; $tp2 = "종합"; $tp3 = "total"; break; - case 1: - $tnmt_type = "통솔전"; + case convertTournamentType('통솔전'): + $tnmt_type = "통솔전"; + $color = 'cyan'; $tp = "leadership"; $tp2 = "통솔"; $tp3 = "leadership"; break; - case 2: - $tnmt_type = "일기토"; + case convertTournamentType('일기토'): + $tnmt_type = "일기토"; + $color = 'cyan'; $tp = "strength"; $tp2 = "무력"; $tp3 = "strength"; break; - case 3: - $tnmt_type = "설전"; + case convertTournamentType('설전'): + $tnmt_type = "설전"; + $color = 'cyan'; $tp = "intel"; $tp2 = "지력"; $tp3 = "intel"; break; + default: + throw new \RuntimeException('invalid tnmt_type'); } + +$bettingID = $gameStor->lastTournamentBettingID ?? 0; +if ($bettingID != 0) { + $betting = $bettingID != 0 ? new Betting($bettingID) : null; + $info = $betting->getInfo(); +} else { + $info = getDummyBettingInfo($tnmt_type); +} + +$myBet = []; +$globalBet = []; + +if ($bettingID != 0) { + $betting = $bettingID != 0 ? new Betting($bettingID) : null; + $info = $betting->getInfo(); + + foreach ($db->queryAllLists( + 'SELECT general_id, user_id, amount, betting_type FROM ng_betting WHERE betting_id = %i', + $bettingID + ) as [$betGeneralID, $userID, $amount, $bettingTypeKey]) { + $bettingKey = Json::decode($bettingTypeKey)[0]; + if ($betGeneralID == $generalID) { + $myBet[$bettingKey] = $amount; + } + + if (key_exists($bettingKey, $globalBet)) { + $globalBet[$bettingKey] += $amount; + } else { + $globalBet[$bettingKey] = $amount; + } + } +} else { + $info = getDummyBettingInfo($tnmt_type); +} + +$myBetTotal = array_sum($myBet); +$globalBetTotal = array_sum($globalBet); + ?> @@ -209,7 +252,7 @@ switch ($admin['tnmt_type']) { - () + () @@ -418,24 +461,31 @@ switch ($admin['tnmt_type']) { echo " "; - $globalBet = $db->queryFirstList('SELECT * FROM betting WHERE general_id = 0') ?? [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]; - $globalBet = array_splice($globalBet, -16); - $globalBetTotal = array_sum($globalBet); $admin = $gameStor->getValues(['tournament', 'phase']); $bet = []; - for ($i = 0; $i < 16; $i++) { - if ($globalBet[$i] == 0) { - $bet[$i] = '∞'; - continue; + $gold = []; + $keyMap = []; + foreach ($info->candidates as $key => $candidate) { + $keyMap[$candidate->aux['idx']] = $key; + } + + foreach ($globalBet as $key => $amount) { + if ($amount == 0) { + $bet[$key] = "∞"; + $gold[$key] = 0; + } else { + $rewardRatio = round($globalBetTotal / $amount, 2); + $bet[$key] = $rewardRatio; + $gold[$key] = $rewardRatio * ($myBet[$key] ?? 0); } - $bet[$i] = round($globalBetTotal / $globalBet[$i], 2); } echo " "; - for ($i = 0; $i < 16; $i++) { - echo "{$bet[$i]}"; + foreach (range(0, 15) as $i) { + $amount = $bet[$keyMap[$i] ?? -1] ?? 0; + echo "{$amount}"; } echo " diff --git a/hwe/c_tournament.php b/hwe/c_tournament.php index e34cc19a..aaf89b61 100644 --- a/hwe/c_tournament.php +++ b/hwe/c_tournament.php @@ -225,6 +225,8 @@ if($btn == "자동개최설정") { finallyAll($admin['tnmt_type'], $admin['tournament'], $admin['phase']); } elseif($btn == "배정") { final16set(); + $phase = 0; + startBetting($admin['tnmt_type'], 720); } elseif($btn == "베팅마감") { $dt = date("Y-m-d H:i:s", time() + 60); $gameStor->tournament=7; diff --git a/hwe/func_tournament.php b/hwe/func_tournament.php index eb2a01c5..2dbb0fd2 100644 --- a/hwe/func_tournament.php +++ b/hwe/func_tournament.php @@ -380,29 +380,12 @@ function startTournament($auto, $type) $gameStor->tnmt_time = (new \DateTimeImmutable())->add(new \DateInterval("PT{$unit}M"))->format('Y-m-d H:i:s'); $gameStor->tournament = 1; $gameStor->tnmt_type = $type; + $gameStor->lastTournamentBettingID = 0; $gameStor->phase = 0; $db->update('general', [ 'tournament' => 0, ], true); - $db->update('betting', [ - 'bet0' => 0, - 'bet1' => 0, - 'bet2' => 0, - 'bet3' => 0, - 'bet4' => 0, - 'bet5' => 0, - 'bet6' => 0, - 'bet7' => 0, - 'bet8' => 0, - 'bet9' => 0, - 'bet10' => 0, - 'bet11' => 0, - 'bet12' => 0, - 'bet13' => 0, - 'bet14' => 0, - 'bet15' => 0 - ], true); $db->query('TRUNCATE TABLE tournament'); $opener = $db->queryFirstField('SELECT `general`.`name` FROM `general` JOIN `nation` ON `general`.`nation` = `nation`.`nation` WHERE `general`.`officer_level` = 12 AND `nation`.`level` = 7 ORDER BY rand() LIMIT 1'); @@ -429,6 +412,21 @@ function startTournament($auto, $type) pushGlobalHistoryLog($history, $admin['year'], $admin['month']); } +function getDummyBettingInfo(string $tnmt_type): BettingInfo +{ + return new BettingInfo( + id: 0, + type: 'tournament', + name: $tnmt_type, + finished: true, + selectCnt: 1, + reqInheritancePoint: false, + openYearMonth: 0, + closeYearMonth: 0, + candidates: [] + ); +} + function startBetting($type, $unit) { $db = DB::db(); @@ -449,24 +447,22 @@ function startBetting($type, $unit) //어차피 강제로 닫을 것이므로 크게 신경쓰지 않는다. - $monthGap = floor(Util::valueFit($unit * 60, 3600) / ($turnterm * 60)) + 1; - $openYearMonth = Util::joinYearMonth($year, $month); - $closeYearMonth = $openYearMonth + $monthGap; + $closeYearMonth = $openYearMonth + 120; /** @var \sammo\DTO\SelectItem[] */ $candidates = []; $generalList = $db->query('SELECT no,npc,name,win,leadership,strength,intel,leadership+strength+intel as total from tournament where grp>=20 order by grp, grp_no LIMIT 16'); - foreach($generalList as $idx=>$general) { - if($general['no'] <= 0){ + foreach ($generalList as $idx => $general) { + if ($general['no'] <= 0) { continue; } $general['idx'] = $idx; $candidates[$general['no']] = new \sammo\DTO\SelectItem( - $general['name'], - "{$statName}: {$general[$statKey]}", - $general + title: $general['name'], + info: "{$statName}: {$general[$statKey]}", + aux: $general ); } $candidateCnt = count($candidates); @@ -491,14 +487,10 @@ function startBetting($type, $unit) $npcList = $db->queryFirstColumn('SELECT no FROM general WHERE npc >= 2 AND gold >= (500 + %i)', $betGold); $npcBet = []; - $betSum = [ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]; + $targetList = array_keys($candidates); foreach ($npcList as $npcID) { - $target = Util::randRangeInt(0, $candidateCnt - 1); - $betSum[$target] += $betGold; + $target = Util::choiceRandom($targetList); $npcBet[] = [$npcID, $target]; } @@ -1216,15 +1208,14 @@ function setRefund() $bettingID = $gameStor->lastTournamentBettingID ?? 0; if (!$bettingID == 0) { $betting = new Betting($bettingID); - if(!$betting->getInfo()->finished){ + if (!$betting->getInfo()->finished) { $betting->giveReward([-1]); } - } - - + + $db->update(['general', [ 'gold' => $db->sqleval('gold + (SELECT bet0+bet1+bet2+bet3+bet4+bet5+bet6+bet7+bet8+bet9+bet10+bet11+bet12+bet13+bet14+bet15 FROM betting WHERE general_id = general.no)') ]], true); diff --git a/hwe/j_betting.php b/hwe/j_betting.php deleted file mode 100644 index 01268109..00000000 --- a/hwe/j_betting.php +++ /dev/null @@ -1,55 +0,0 @@ - 1000) { - Json::die([ - 'result' => false, - 'reason' => '올바르지 않은 입력입니다' - ]); -} - -//로그인 검사 -$session = Session::requireGameLogin()->setReadOnly(); -$userID = Session::getUserID(); -$generalID = $session->generalID; - -$db = DB::db(); -$gameStor = KVStorage::getStorage($db, 'game_env'); - -increaseRefresh("베팅", 1); - -$tournament = $gameStor->tournament; -if ($tournament != 6) { - Json::die([ - 'result' => false, - 'reason' => '베팅 기간이 아닙니다.' - ]); -} - - -$bettingID = $gameStor->lastTournamentBettingID; -$betting = new Betting($bettingID); - -try { - $betting->bet($generalID, $userID, [$betTarget], $betGold); -} catch (\Exception $e) { - Json::die([ - 'result' => false, - 'reason' => $e->getMessage() - ]); -} - -Json::die([ - 'result' => true, - 'reason' => 'SUCCESS' -]); diff --git a/hwe/ts/betting.ts b/hwe/ts/betting.ts index 4aa2b41a..b4c07e18 100644 --- a/hwe/ts/betting.ts +++ b/hwe/ts/betting.ts @@ -4,39 +4,29 @@ import { setAxiosXMLHttpRequest } from '@util/setAxiosXMLHttpRequest'; import type { InvalidResponse } from '@/defs'; import { convertFormData } from '@util/convertFormData'; import { unwrap_any } from '@util/unwrap_any'; +import { SammoAPI } from './SammoAPI'; -$(function($){ - setAxiosXMLHttpRequest(); +declare const staticValues: { + bettingID: number; +} - $('.submitBtn').on('click', async function(e){ +$(function ($) { + $('.submitBtn').on('click', async function (e) { e.preventDefault(); const $this = $(this); const target = parseInt($this.data('target')); const amount = parseInt(unwrap_any($(`#target_${target}`).val())); - let result: InvalidResponse; - - try{ - const response = await axios({ - url: 'j_betting.php', - responseType: 'json', - method: 'post', - data: convertFormData({ - target: target, - amount: amount - }) + try { + await SammoAPI.Betting.Bet({ + bettingID: staticValues.bettingID, + bettingType: [target], + amount: amount, }); - result = response.data; - }catch(e){ + } catch (e) { console.error(e); - alert(`에러: ${e}`); - location.reload(); - return; - } - - if(!result.result){ - alert(`베팅을 실패했습니다: ${result.reason}`); + alert(`베팅을 실패했습니다: ${e}`); location.reload(); return; }