Refactoring the Economy

posted in KrisWolfe
Published May 16, 2019
Advertisement

The data model merge took place. I broke the code and then rebuilt it. This is actually more fun than I imagined.

So, now I'm working on refactoring the market code to allow it to be extensible as I add in company behavior. All the market decisions were being handled inside the market controller class before, but now with more knowledge of C#, I'm moving it to basically a "flow" controller.

The main loop in the game turn is basically 4 for loops...beginning of the day, middle of the day, end of the day, and finalize. The beginning of the day I build up the lists that I need...which markets are available for consumers to eat from? Who needs employees? Who needs a job?

The middle of the day is where the player's actions gets executed and all the contracts get executed. The UI will collect player actions and here we inject them into the economy. The Job contracts will perform their tasks, pay their employees, remove themselves if they've expired, and send goods to other companies based on the terms of the contracts. Also we randomize the order of the lists so that the same agent doesn't get first mover advantage everyday.

The end of the day is when we execute commands for the AI. Companies hire employees, get contracts, decide on new states for the AI. Finalize is where the economy goes over everything and updates its average market prices and market shares based on the actions of the day.

So my old market code was 500 lines long. Now I'm going to have the actual companies decide what they need to do. Retail companies and manufacturers need to do different things, so I'll have an interface for them that basically say "BeginningOfDay()" but they will be done differently. Interfaces are clean and nice for this.

I also ran into an issue when I was merging data models. I have two interfaces, IAgent, and IEmployee, that are both on the ConsumerAgent class. But if I make an IAgent, it doesn't have access to the IEmployee methods. I started just copying the methods I needed to the IAgent interface, but then I realized I was violating the DRY principle (Don't repeat yourself). I discovered that IAgent can actually implement IEmployee interface, which is hilarious to me. Interfaces are so nice.

I also ran into another issue with interfaces. Production Agents and Retail Agents both have an ICompany interface, which means they both implement a "HireEmployee" method. But it's the exact same method. I'm violating DRY principle by just copy pasting the code. Then I realized I could use both an abstract class Company AND implement interfaces. Mind blown. Seriously. So now when we have financial statements and accounts like "cashOnHand", I don't have to keep copy pasting that property to all these classes.

So, pretty excited to get this code refactored and working. No pictures for awhile since this is all just code, but hopefully show some different employees and companies off as I get them off the ground and rolling.

0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement