Skip to content

Commit 53af3f1

Browse files
2 parents ac40ce7 + ebad755 commit 53af3f1

1 file changed

Lines changed: 92 additions & 9 deletions

File tree

CafeCosmos.AutomationExample/Readme.md

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ This program demonstrates how to work with data on **CafeCosmos** using **Nether
77
The program interacts with Ethereum-based smart contracts to:
88

99
1. Access player information and local state.
10-
2. Interact directly with MUD systems on the blockchain.
11-
3. Query blockchain tables for specific data.
12-
4. Process blockchain events for insights.
13-
5. Retrieve and manage data from a PostgreSQL database using repositories.
10+
2. Automation of common tasks like harvesting, crafting and cooking.
11+
3. Interact directly with MUD systems on the blockchain.
12+
4. Query blockchain tables for specific data.
13+
5. Process blockchain events for insights.
14+
6. Retrieve and manage data from a PostgreSQL database using repositories.
1415

15-
### Dependency on MUD Tables Log Processor
16+
### Dependency on MUD Tables Log Processor for Example 6
1617

1718
This program relies on the **MUD Tables Log Processor** to fetch and process log events from the blockchain and populate the PostgreSQL database (`storerecords` table). If the background processor is not running, you won't be able to run that sample :), so comment it out.
1819

@@ -105,7 +106,89 @@ Console.WriteLine("Advanced local state operations completed and saved.");
105106
Console.WriteLine($"Previous Level: {previousLevel}, Current Level: {localState.PlayerLandInfo.LastLevelClaimed}");
106107
```
107108

108-
### 2. System-Level Interactions
109+
## 2. Automation Strategies
110+
111+
### Continuous Harvesting, Collecting, and Cooking Using Simple Appliances
112+
113+
This automation strategy demonstrates how to continuously interact with land appliances in **CafeCosmos**, such as coffee machines and blenders, to streamline gameplay. It automates the processes of collecting items, crafting recipes, cooking, and planting crops.
114+
115+
#### Key Features
116+
117+
1. **Automated Collection**: Automatically collects all ready-to-harvest items.
118+
2. **Continuous Crafting and Cooking**: Crafts and places items on appliances for further processing.
119+
3. **Smart Inventory Management**: Ensures efficient use of resources by checking recipe requirements and inventory.
120+
4. **Crop Management**: Automates planting and watering seeds.
121+
122+
#### Code Overview
123+
124+
```csharp
125+
public class ContinuousHarvestingCollectingAndCookingSimpleAppliancesStrategy
126+
{
127+
public async Task ExecuteAsync(Web3 web3, VisionsContractAddresses contractAddresses)
128+
{
129+
var playerService = new PlayerService(web3, contractAddresses, web3.TransactionManager.Account.Address);
130+
131+
// Ensure we get the landId of the user
132+
await playerService.EnsureInitialisedSelectedLandIdToFirstLandIfNotSetAsync();
133+
var playerLocalState = await playerService.GetNewPlayerLocalStateAsync();
134+
135+
// Step 1: Collect all ready-to-harvest items
136+
CollectAllItemsReadyToCollect(playerLocalState);
137+
138+
// Step 2: Craft and cook recipes if possible
139+
CraftItemAndCookIfPossible(playerLocalState, DefaultCraftingRecipes.CoffeeMachineRecipes.COFFEE, DefaultItems.Cooking.COFFEE_MACHINE);
140+
141+
// Alternate between smoothie and milkshake
142+
var smoothie = true;
143+
if (smoothie)
144+
{
145+
CraftItemAndCookIfPossible(playerLocalState, DefaultCraftingRecipes.BlenderRecipes.SMOOTHIE, DefaultItems.Cooking.BLENDER);
146+
}
147+
else
148+
{
149+
CraftItemAndCookIfPossible(playerLocalState, DefaultCraftingRecipes.BlenderRecipes.BANANA_MILKSHAKE, DefaultItems.Cooking.BLENDER);
150+
}
151+
152+
// Step 3: Plant seeds and water them
153+
for (int i = 0; i < 10; i++)
154+
{
155+
PlaceItemOnAnother(playerLocalState, DefaultItems.Wheat.WHEAT_SEED, DefaultItems.Gardening.TILLED_SOIL);
156+
PlaceItemOnAnother(playerLocalState, DefaultItems.Tools.WATERING_CAN, DefaultItems.Wheat.WHEAT_SEED);
157+
}
158+
159+
// Step 4: Save changes if required
160+
if (playerLocalState.UpdateLandOperations.Count > 0)
161+
{
162+
await playerService.SavePlayerStateAndWaitForReceiptAsync(playerLocalState);
163+
}
164+
}
165+
166+
private static void CollectAllItemsReadyToCollect(PlayerLocalState playerLocalState) { ... }
167+
private static void CraftItemAndCookIfPossible(PlayerLocalState playerLocalState, CraftingRecipe recipe, Item appliance) { ... }
168+
private static void PlaceItemOnAnother(PlayerLocalState playerLocalState, Item item, Item appliance) { ... }
169+
}
170+
```
171+
172+
#### How to Use
173+
174+
1. Ensure the following appliances are crafted and placed on your land:
175+
- Coffee Machine
176+
- Blender
177+
- Oven
178+
2. Execute the automation strategy:
179+
```csharp
180+
var strategy = new ContinuousHarvestingCollectingAndCookingSimpleAppliancesStrategy();
181+
await strategy.ExecuteAsync(web3, contractAddresses);
182+
```
183+
3. Monitor the logs to track harvesting, crafting, and cooking actions.
184+
185+
#### Additional Notes
186+
187+
- This strategy assumes a simple setup with predefined recipes and appliances.
188+
- You can extend the logic to include more advanced recipes or other appliances.
189+
- Save operations are performed only when there are changes to the land state to optimize blockchain interactions.
190+
191+
### 3. System-Level Interactions
109192

110193
Access systems directly to fetch or manipulate data. For example, retrieving player earnings from the `LandView` system:
111194

@@ -114,7 +197,7 @@ var landNamespace = new LandNamespace(web3, worldAddress);
114197
var totalEarned = await landNamespace.Systems.LandView.GetPlayerTotalEarnedAsync(1);
115198
```
116199

117-
### 3. Interacting with Blockchain Tables
200+
### 4. Interacting with Blockchain Tables
118201

119202
Query tables stored on the blockchain, such as the `PlayerTotalEarned` table:
120203

@@ -127,7 +210,7 @@ foreach (var record in allTotalEarned)
127210
}
128211
```
129212

130-
### 4. Processing Blockchain Events
213+
### 5. Processing Blockchain Events
131214

132215
Retrieve and process blockchain events, such as land purchases or creations:
133216

@@ -143,7 +226,7 @@ foreach (var purchase in allPurchases)
143226
}
144227
```
145228

146-
### 5. Using the PostgreSQL Repository
229+
### 6. Using the PostgreSQL Repository
147230

148231
Retrieve data stored in the PostgreSQL database, such as transformations:
149232

0 commit comments

Comments
 (0)