-1

我有一个关于 php 计算的问题。我正在重组一个基于文本的游戏。游戏现在有两个“本地玩家”。它们由 cron 文件定期插入。插入是:

//玩家 1 登录 id = 54

mysql_query("INSERT INTO {$game}_ships (ship_name,login_name,shipclass,class_name,class_name_abbr,fighters,max_fighters,max_shields,cargo_bays,mine_rate_metal,mine_rate_fuel,config,size,upgrades,move_turn_cost,point_value,location,login_id
    ) values(   'Three 7','A','8','name','WM','4773','4773','8353','2003','5','45','bs:dt:ot','6','99','19','10',FLOOR(RAND() * 298 + 2),'54')");

播放器二具有相同的插入,只有播放器 ID 不同。让我们说 44

它们被插入随机位置FLOOR(RAND() * 298 + 2),从 2 到 300 不等。

我想通过从同一系统中的这两个玩家中选择舰船,比较战斗机数量并从数据库中删除最弱的舰船,在游戏中创建一个AI 。

到目前为止,我有:

mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 54 AND 54 AND `location` = 234");

但我在其他方面有一些问题。

任何意见或帮助将不胜感激。

PS:我随意选择了location= 234“ 。

4

2 回答 2

1

您可以遍历所有位置并检查每个位置,以查看每个玩家是否至少有一艘船;如果是这样,那么你就摧毁了弱者。看:

for ($i = 2; $i < 300; $i++) {
  $p1 = mysql_fetch_array(mysql_query("SELECT count(*) AS 'q' FROM `{$game}_ships` WHERE `login_id` = 54 AND `location` = " . $i));
  $p2 = mysql_fetch_array(mysql_query("SELECT count(*) AS 'q' FROM `{$game}_ships` WHERE `login_id` = 44 AND `location` = " . $i));
  if ($p1['q'] > 0 && $p2['q'] > 0) {
    //Get the first ship; if you want, can select a ship randomly
    $p1s = mysql_fetch_array(mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 54 AND `location` = ".i));
    $p2s = mysql_fetch_array(mysql_query("SELECT * FROM `{$game}_ships` WHERE `login_id` = 44 AND `location` = ".i));
    //Here you compare $p1s and $p2s, and check which one is stronger. I am using the 'fighters' value, but you can change it if I misunderstood you
    if ($p1s['fighters'] > $ps2['fighters'])
      mysql_query ("DELETE FROM `{$game}_ships` WHERE `ship_name` = " + $ps2['ship_name']);
    else //Not checking for ties here, you can add it too
      mysql_query ("DELETE FROM `{$game}_ships` WHERE `ship_name` = " + $ps1['ship_name']);
  }
}

我不确定我是否完全理解你,但我希望这至少是你想要的一部分。

于 2012-06-30T22:35:37.007 回答
1

此查询将为您提供玩家 54 和 44 都有船只的位置列表:

SELECT distinct p1s.location
FROM `{$game}_ships` p1s JOIN `{$game}_ships` p2s ON p1s.location=p2s.location
WHERE p1s.login_id=54 AND p2s.login_id=44;

此查询将删除位置 234 中玩家 54 或 44 中最弱的船:

DELETE from `{$game}_ships`
WHERE login_id IN (54, 44) AND location=234
ORDER BY fighters LIMIT 1;
于 2012-06-30T23:05:51.203 回答