草庐IT

PHP计算游戏能耗时间刻度

coder 2024-01-03 原文

我正在努力实现的目标

在在线游戏中,您可以获得奖励和其他基于能量的目标。一个人可能需要总共 24,000 能量,这是一个基于时间的能量,而其他人只需要 25。基于从 0 能量开始并且在用户 sleep 或诸如此类的情况下坐在那里没有损失的能量,我想计算需要多长时间才能获得所需的能量。

--------------------------------------------------------------------
|   Energy      |   Cooldown   |  Additional  |   Limit   |  wait  |
|               |              |    Energy    |           |        |
--------------------------------------------------------------------
|   Natural     |   10 minutes |     5        |   N/A     |    Y   |
|   Booster 1   |   24 hours   |     150      |   1 p/24h |    Y   |
|   Booster 2   |   6 hours    |     150      |   4 p/24h |    N   |
|   Booster 3   |   6 hours    |     250      |   4 p/24h |    Y   |
--------------------------------------------------------------------

一个人在 24 小时内可以达到的总能量是 2,470(通过 720 自然能量),150 助推器 1,600 助推器 2 和 1,000 助推器 3。

因此,您每 10 分钟(600 秒)获得 5 点自然能量,您可以使用助推器补充能量,这会立即增加“冷却时间”。因此,从 0 能量开始,您可以跳到 1,000 能量。 “等待”表示您需要等到冷却时间结束。


到目前为止我做了什么

为了计算天数,我做了以下操作:

$days = floor($Energy_Needed / 2470) * 86400;

对于所需的剩余能量,我做了以下工作:

$remaining = $Energy_Needed - (floor($Energy_Needed / 2470) * 2470);

问题

一旦计算出 $days,就需要重新开始了,所以 if $remaining > 1000(因为用户可以跳到这里,所以它将只是 N 天)我怎样才能找到最佳剩余时间?

注意:我只是在寻找总秒数,而不是“漂亮”。

最佳答案

尝试这样的事情。我很难理解这个问题!

实际上这是一个经过测试的工作示例。 我做了这些假设:

1)无论是否有助推器的玩家都会获得自然能量

2)等待时间不会影响THE BOOSTER2

试试这个

<?php

/*-------------------------------------*/
// Natural Energy
/*-------------------------------------*/

//the natural energy is 720 in 24 hours (86400s)
// Energy formula E(t) = const * t since they are proportional

 $naturalEnergy = function ($time){
 $energyNatural = number_format((720/86400)*$time);
 return $energyNatural;
};

//time needed for to obtain naturally $energyAcquired

 $timeNatural = function ($energyAcquired){
  // Maths : Reversed the previous equation
 $timeNeeded = number_format( $energyAcquired / (720 / 86400) );
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster One
/*-------------------------------------*/

//The booster will help to acquire 150 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per 24hour

 $booster1Energy = function ($time){
  //booster1 + natural
 $energyBooster1 =number_format(( (150/86400) + (720/86400) )*$time) ; 
 return $energyBooster1;
};

 $timeBooster1 = function ($energyAcquired){
  //Reversed the previous equation
 $timeNeeded = number_format(( $energyAcquired / ( (150/86400) + (720/86400) ) ));
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster two
/*-------------------------------------*/

//The booster 2 will help to acquire 600 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per 24 hour

 $booster2Energy = function ($time){
  //booster2 + natural
 $energyBooster = number_format(( (600/86400) + (720/86400) )*$time); 
return $energyBooster;
};

 $timeBooster2 = function ($energyAcquired){
  //Reversed the previous equation
 $timeNeeded = number_format(( $energyAcquired / ( (600/86400) + (720/86400) ) ));
 return $timeNeeded;
};

/*-------------------------------------*/
// Booster three
/*-------------------------------------*/

//The booster 3 will help to acquire 1000 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per hour

  $booster3Energy = function ($time){
  //booster3 + natural
  $energyBooster = number_format(( (1000/86400) + (720/86400) )*$time); 
 return $energyBooster;
 };

  $timeBooster3 = function ($energyAcquired){
  //Reversed the previous equation
  $timeNeeded = number_format(( $energyAcquired / ( (1000/86400) + (720/86400) ) ));
 return $timeNeeded;
 };

/*-------------------------------------*/
// Booster all inlcuded
/*-------------------------------------*/

//Everything will help to acquire 2470 Energy in 24 hours (86400s)
//and will also naturally gain 720 energy per hour

  $allBoosterEnergy = function($time){
   //booster1 + booster2 + booster3 + natural
  $energyBooster = number_format( (2470/86400)* $time ); 
 return $energyBooster;
 };

  $timeAll = function($energyAcquired){
   //Reversed the previous equation
  $timeNeeded = number_format(( $energyAcquired / (2470/8600) ));
 return $timeNeeded;
 };

// introduced elapsed time so far in the game 

function bestTimeRemaining( $energyToAttain, $elapsedTimeSoFar, $naturalEnergy, $booster1Energy, $booster2Energy,$booster3Energy, $allBoosterEnergy, $timeNatural, $timeBooster1, $timeBooster2, $timeBooster3,$timeAll ){

//Remaining energy = EnergyToAttain - Energy obtained so far
//The Energy obtained so far can be calculated with $elapsedTimeSoFar as follow
//----------------------------------------------------------------------------------------------------
//if played with natural energy from the beginning
   $remainingEnergy['usedNatural'] = $energyToAttain - 
   $naturalEnergy($elapsedTimeSoFar);

//if played with booster1 from the beginning*/
   $remainingEnergy['usedBooster1'] = $energyToAttain - $booster1Energy($elapsedTimeSoFar);

//if played with booster2 from the beginning
   $remainingEnergy['usedBooster2'] = $energyToAttain - $booster2Energy($elapsedTimeSoFar);

//if played with booster3 from 
   $remainingEnergy['usedBooster3'] = $energyToAttain - $booster3Energy($elapsedTimeSoFar);

//if played with all boosters from the beginning, the remaining energy will be
   $remainingEnergy['usedAllBoosters'] = $energyToAttain - $allBoosterEnergy( $elapsedTimeSoFar );

//----------------------------------------------------------------------------------------------------
//After the elapsedTime , the player knows the remaining energy so he can decide to use a booster
//---------------------------------------------------------------------------------------------------

//time calculation using the remaining energy

//Remaining time for the user who used natural enrgy with option to finish with natural, booster1, booster2, booster3

   $remainingTime['usedNaturalSoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedNatural']);
   $remainingTime['usedNaturalSoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedNatural']);

//Remaining time for the user who used Booster1 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster1SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster1']);
   $remainingTime['usedBooster1SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster1']);

//Remaining time for the user who used Booster2 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster2SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster2']);
   $remainingTime['usedBooster2SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster2']);

//Remaining time for the user who used Booster3 with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedBooster3SoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedBooster3']);
   $remainingTime['usedBooster3SoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedBooster3']);

//Remaining time for the user who used all the Booster with option to finish with natural, booster1, booster2, booster3
   $remainingTime['usedAllBoosterSoFar']['FinishWithNatural'] = $timeNatural($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster1'] = $timeBooster1($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster2'] = $timeBooster2($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithBooster3'] = $timeBooster3($remainingEnergy['usedAllBoosters']);
   $remainingTime['usedAllBoosterSoFar']['FinishWithAllBooster'] = $timeAll($remainingEnergy['usedAllBoosters']);

  return $remainingTime;
 }
?>


<?php 
//EXAMPLE TIME REMAINING TO AQCUIRE 24000 ENERGY FOR AN ELAPSED TIME OF 86400 S (24HOURS)
$test = bestTimeRemaining(24000, 86400, $naturalEnergy, 
$booster1Energy, $booster2Energy,$booster3Energy, $allBoosterEnergy, 
$timeNatural, $timeBooster1, $timeBooster2, $timeBooster3,$timeAll  ) 
?>


The player who played so far for 86400 seconds (24 hours):</br>:
<pre>
    <?php print_r($test ) ?>
</pre>

到目前为止玩了 86400 秒(24 小时)以获取 24000 ENERGY 的玩家需要以下时间(以秒为单位):

输出:

Array
(
 [usedNaturalSoFar] => Array
    (
        [FinishWithNatural] => 2,793,600
        [FinishWithBooster1] => 2,311,945
        [FinishWithBooster2] => 1,523,782
        [FinishWithBooster3] => 1,169,414
        [FinishWithAllBooster] => 81,056
    )

[usedBooster1SoFar] => Array
    (
        [FinishWithNatural] => 2,775,600
        [FinishWithBooster1] => 2,297,048
        [FinishWithBooster2] => 1,513,964
        [FinishWithBooster3] => 1,161,879
        [FinishWithAllBooster] => 80,534
    )

[usedBooster2SoFar] => Array
    (
        [FinishWithNatural] => 2,879,880
        [FinishWithBooster1] => 2,383,349
        [FinishWithBooster2] => 1,570,844
        [FinishWithBooster3] => 1,205,531
        [FinishWithAllBooster] => 83,559
    )

[usedBooster3SoFar] => Array
    (
        [FinishWithNatural] => 2,879,880
        [FinishWithBooster1] => 2,383,349
        [FinishWithBooster2] => 1,570,844
        [FinishWithBooster3] => 1,205,531
        [FinishWithAllBooster] => 83,559
    )

[usedAllBoosterSoFar] => Array
    (
        [FinishWithNatural] => 2,879,760
        [FinishWithBooster1] => 2,383,250
        [FinishWithBooster2] => 1,570,778
        [FinishWithBooster3] => 1,205,481
        [FinishWithAllBooster] => 83,556
    )

)

关于PHP计算游戏能耗时间刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44895758/

有关PHP计算游戏能耗时间刻度的更多相关文章

  1. ruby-on-rails - 使用一系列等级计算字母等级 - 2

    这里是Ruby新手。完成一些练习后碰壁了。练习:计算一系列成绩的字母等级创建一个方法get_grade来接受测试分数数组。数组中的每个分数应介于0和100之间,其中100是最大分数。计算平均分并将字母等级作为字符串返回,即“A”、“B”、“C”、“D”、“E”或“F”。我一直返回错误:avg.rb:1:syntaxerror,unexpectedtLBRACK,expecting')'defget_grade([100,90,80])^avg.rb:1:syntaxerror,unexpected')',expecting$end这是我目前所拥有的。我想坚持使用下面的方法或.join,

  2. ruby-on-rails - Ruby 检查日期时间是否为 iso8601 并保存 - 2

    我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby​​是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查

  3. ruby-on-rails - 将 Ruby 中的日期/时间格式化为 YYYY-MM-DD HH :MM:SS - 2

    这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build

  4. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  5. 计算机毕业设计ssm+vue基本微信小程序的小学生兴趣延时班预约小程序 - 2

    项目介绍随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱小学生兴趣延时班预约小程序的设计与开发被用户普遍使用,为方便用户能够可以随时进行小学生兴趣延时班预约小程序的设计与开发的数据信息管理,特开发了小程序的设计与开发的管理系统。小学生兴趣延时班预约小程序的设计与开发的开发利用现有的成熟技术参考,以源代码为模板,分析功能调整与小学生兴趣延时班预约小程序的设计与开发的实际需求相结合,讨论了小学生兴趣延时班预约小程序的设计与开发的使用。开发环境开发说明:前端使用微信微信小程序开发工具:后端使用ssm:VU

  6. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  7. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  8. ruby - 使用 Ruby,计算 n x m 数组的每一列中有多少个 true 的简单方法是什么? - 2

    给定一个nxmbool数组:[[true,true,false],[false,true,true],[false,true,true]]有什么简单的方法可以返回“该列中有多少个true?”结果应该是[1,3,2] 最佳答案 使用转置得到一个数组,其中每个子数组代表一列,然后将每一列映射到其中的true数:arr.transpose.map{|subarr|subarr.count(true)}这是一个带有inject的版本,应该在1.8.6上运行,没有任何依赖:arr.transpose.map{|subarr|subarr.in

  9. ruby - 在没有基准或时间的情况下用 Ruby 测量用户时间或系统时间 - 2

    因为我现在正在做一些时间测量,我想知道是否可以在不使用Benchmark类或命令行实用程序time的情况下测量用户时间或系统时间。使用Time类只显示挂钟时间,而不显示系统和用户时间,但是我正在寻找具有相同灵active的解决方案,例如time=TimeUtility.now#somecodeuser,system,real=TimeUtility.now-time原因是我有点不喜欢Benchmark,因为它不能只返回数字(编辑:我错了-它可以。请参阅下面的答案。)。当然,我可以解析输出,但感觉不对。*NIX系统的time实用程序也应该可以解决我的问题,但我想知道是否已经在Ruby中实

  10. ruby - 我需要从 facebook 游戏中抓取数据——使用 ruby - 2

    修改(澄清问题)我已经花了几天时间试图弄清楚如何从Facebook游戏中抓取特定信息;但是,我遇到了一堵又一堵砖墙。据我所知,主要问题如下。我可以使用Chrome的检查元素工具手动查找我需要的html-它似乎位于iframe中。但是,当我尝试抓取该iframe时,它​​是空的(属性除外):如果我使用浏览器的“查看页面源代码”工具,这与我看到的输出相同。我不明白为什么我看不到iframe中的数据。答案不是它是由AJAX之后添加的。(我知道这既是因为“查看页面源代码”可以读取Ajax添加的数据,也是因为我有b/c我一直等到我可以看到数据页面之后才抓取它,但它仍然不存在)。发生这种情况是因为

随机推荐