gpt4 book ai didi

php - 使代码更高效,减少复制粘贴

转载 作者:行者123 更新时间:2023-11-29 14:33:37 25 4
gpt4 key购买 nike

我一直在编写一段代码,该代码可以提取公会的名称以及该公会在在线游戏中杀死的老板/怪物的信息。游戏中有很多怪物,每个怪物都有三种难度设置。我已经设法让代码执行我想要的操作,但是它有大量的复制和粘贴,而我只完成了输入总量的大约 1/5。我真的想不出如何让这段代码不再那么臃肿。这是针对 3 种难度设置的一只怪物的代码,您可以看到它只是针对一种怪物。可能还有另外 60 个!任何人都可以帮助我了解更好的方法来做到这一点。谢谢!

$sql = 'SELECT * FROM `phpbb_profile_fields_data`';

$result = $db->sql_query($sql);

while ($row = $db->sql_fetchrow($result))
{
/////////////////////////////////// START - 8 MAN BONETHRASHER
$normal = '';
$hard = '';
$nightmare = '';
/////// START - CHECK NORMAL
if ($row['pf_kp_em_no_bonethr'] == '1')
{
$normal = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/no.png" />';
}
else if ($row['pf_kp_em_no_bonethr'] == '2')
{
$normal = '';
}
else if (is_null($row['pf_kp_em_no_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NORMAL
/////// START - CHECK HARD
if ($row['pf_kp_em_ha_bonethr'] == '1')
{
$hard = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/ha.png" />';
}
else if ($row['pf_kp_em_ha_bonethr'] == '2')
{
$hard = '';
}
else if (is_null($row['pf_kp_em_ha_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK HARD
/////// START - CHECK NIGHTMARE
if ($row['pf_kp_em_kn_bonethr'] == '1')
{
$nightmare ='&nbsp;<img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}
else if ($row['pf_kp_em_kn_bonethr'] == '2')
{
$nightmare = '';
}
else if (is_null($row['pf_kp_em_kn_bonethr']))
{
echo "Boss was set as NULL This should not happen!";
}
else
{
echo "Sosia messed up go hit him in the face.";
}
/////// END - CHECK NIGHTMARE

if ($normal == '' && $hard == '' && $nightmare == '')
{

}
else
{
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}

}

$db->sql_freeresult($result);

最佳答案

我对你想要做的事情仍然有点模糊,但我会尽力帮助你。

您可能会创建一个完成所有这些操作的类。

例如:

class checks {

public function checkBosses($normalBoss, $hardBoss, $nightmareBoss) {

$difficulties = array();

$difficulties['normal'] = array('boss' => $normalBoss);
$difficulties['hard'] = array('boss' => $hardBoss);
$difficulties['nightmare'] = array('boss' => $nightmareBoss);

foreach ($this->difficulties as $difficulty -> $boss) {
$this->difficulties[$difficulty]['result'] = checkDifficulty($boss['boss'], $difficulty);
}

$normal = $this->difficulties['normal']['result'];
$hard = $this->difficulties['hard']['result'];
$nightmare = $this->difficulties['nightmare']['result'];


if ($normal == '' && $hard == '' && $nightmare == '') {
return null;
} else {
return array(
'normal' => $normal,
'hard' => $hard,
'nightmare' => $nightmare,
);
}
}

protected function checkDifficulty($boss, $difficulty) {

if ($difficulty == 'normal') {
$image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/no.png" />';
} else if ($difficulty == 'hard') {
$image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/ha.png" />';
} else if ($difficulty == 'nightmare') {
$image = '&nbsp;<img src="/styles/subsilver2/theme/images/soap/kn.png" />';
}

if ($boss == '1') {
return $image;
} else if ($boss == '2') {
return '';
} else if (is_null($boss)) {
echo "Boss was set as NULL This should not happen!";
} else {
echo "Sosia messed up go hit him in the face.";
}
}
}

然后您需要做的就是调用:

$checkResult = checks::checkBosses($row['pf_kp_em_no_bonethr'], $row['pf_kp_em_ha_bonethr'], $row['pf_kp_em_kn_bonethr']);

if ($checkResult != null) {
$template->assign_block_vars('8m_bonethrasher', array(
'VAR1' => $row['pf_guild_name'],
'VAR2' => $normal,
'VAR3' => $hard,
'VAR4' => $nightmare,
));
}

关于php - 使代码更高效,减少复制粘贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com