Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:22:47+00:00 2026-06-18T03:22:47+00:00

I recently started programming in PHP. For exercise I’d like to build my own

  • 0

I recently started programming in PHP. For exercise I’d like to build my own PHP text strategy game. One technical aspect needs some thinking.

In my game every player must have the ability to gain resources, for example iron and grain. With these resources a player is able to recruit soldiers or build buildings. The resources are stored in a kind of warehouse.

The resources will be gain in a period of time. In example you gain 100 grain a hour and that grain is stored in the warehouse. This means that every 0.6 minute, 1 grain has to be added. I have been looking to cron jobs but that is not the best answer to my problem because is takes too much system resource. The problem is that I’d like to be able to update my resources in my warehouse every minute. Does anyone have an idea for my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:22:48+00:00Added an answer on June 18, 2026 at 3:22 am

    Rather than trying to solve this by updating state, you could look at trying to solve it in a functional programming way. E.g. write a function that can calculate the amount of resources, given known conditions (Such as when production was started). Very simplified:

    state based model:

    class Gold {
      public $amount = 0;
    }
    
    class GoldDigger {
      public $amount_per_second = 1;
    }
    
    $player_gold = new Gold();
    $player_worker = new GoldDigger();
    while (true) {
      $player_gold->amount += $player_worker->amount_per_second;
      sleep(1);
      echo "Player has {$player_gold->amount} gold pieces\n";
    }
    

    functional based model:

    class Gold {
      public $workers = array();
      function getAmount() {
        $total = 0;
        foreach ($this->workers as $worker) {
          $total += $worker->getAmount();
        }
        return $total;
      }
    }
    
    class GoldDigger {
      public $amount_per_second = 1;
      public $started = 0;
      function __construct() {
        $this->started = time();
      }
      function getAmount() {
        return $this->amount_per_second * (time() - $this->started);
      }
    }
    
    $player_gold = new Gold();
    $player_gold->workers[] = new GoldDigger();
    
    while (true) {
      sleep(1);
      echo "Player has {$player_gold->getAmount()} gold pieces\n";
    }
    

    Both these examples are a bit contrived – you would likely store the data in a database or similar, which complicates matters a bit, but I hope it illustrates the difference between the two strategies.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started learning programming to make my own 3D OpenGL game on the
I have recently started programming in PHP. I am building a cart in PHP.
Like many other people posting questions here, I recently started programming in Python. I'm
I jut recently started learning PHP from a book called PHP/MySQL Programming for the
Having recently started programming in Ruby, I noticed one small language feature that is
I'm new to web programming and recently started getting into PHP. I wrote a
I recently started with android game programming. I feel OK to work with OpenGL
I have been programming in php for a while, and recently I started a
I recently started game programming on the iPhone using Cocos2d and Box2d. So here's
I recently started programming C on Mac OS X (10.7.4). I have the various

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.