Skip to content

Commit

Permalink
one-off scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jun 5, 2024
1 parent b6606d9 commit c8d51da
Show file tree
Hide file tree
Showing 7 changed files with 1,471 additions and 0 deletions.
6 changes: 6 additions & 0 deletions utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# One-Off Setup Scripts

Various tasks needed doing once, for which I created simple single-use PHP scripts.
These are all within this folder.

Don't try to run them on a live system, Bad Things™ will happen!
56 changes: 56 additions & 0 deletions utils/cooking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
require 'vendor/autoload.php';

$config = json_decode(file_get_contents("config.json"));
$client = OpenAI::client($config->gptkey);
$db = mysqli_connect($config->database->host, $config->database->username, $config->database->password, $config->database->database);

$q = mysqli_query($db, "SELECT json_extract(json_extract(hunting_json, '$.animals.*'), '$[*][*]') as loot FROM `game_locations` where json_extract(hunting_json, '\$.probability') > 0 ORDER BY `game_locations`.`id` ASC");
$list = [];
while ($loot = mysqli_fetch_object($q)) {
$l = json_decode($loot->loot);
foreach ($l ?? [] as $item) {
$list[] = $item;
}
}
$list = array_unique($list);
shuffle($list);
$list = json_encode(array_values($list));

$r = $client->chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
"role" => "system",
"content" => "You are a helpful assistant that does as the user asks."
],
[
"role" => "user",
"content" => "Task: Given below is a json array of various high fantasy ingredient items from hunting in a high fantasy game. For the list, suggest many different combinations of these items, to produce cooked food at a campfire in a similar vein to how this works in Legend of Zelda Tears Of the Kingdom. For each, suggest how it would affect the core stats of the player: Stamina (health), Skill (combat effectiveness), Luck, Speed
Return the output one dish per line, as many lines as possible, with each dish requiring 2 to a maximum of six items to create.
the output of each dish should be in json as follows:
{\"name\":\"food name\", \"description\":\"short description of food\",\"ingredients\":[\"ingredient\",\"...\"], \"stamina_change\":1,\"skill_change\":1,\"luck_change\":1,\"speed_change\":1}
The output should be an ARRAY of these objects.
You should endevour to return as many of the recipies as possible within the allowances of the API response.
recipies predominantly from one animal should imbue the mythical characteristics of that animal, e.g. rabbits = luck, snakes = sneakiness, lions = strength etc.
the numbers for the changes should vary accoridngly. if there is no change applied, the change value may be entirely omitted along with its key.
DO NOT OUTPUT COMMA SEPARATED LISTS for the result.
DO NOT ADD ANY INGREDIENTS THAT ARE NOT PART OF THE ORIGINAL LIST BELOW. NO VEGETABLES, BREAD PRODUCTS, DAIRY OR CEREALS
No raw or inedible items but strange items are acceptable.
No duplicate food names.
STAT CHANGES should be in the range 2-8 depending on how many items are in the recipie.
If the majority of ingredients come from a certain animal, prefix the name of the dish with the animals name e.g. \"boar stew\".
Food with more ingredients will give better buffs.
Display only the json.
Do not prefix or suffix the result. The output is to be fed into a computer program and must be only json.
Text is:
```
$list
```",
],
],
]);
$json = $r->choices[0]->message->content;
echo $json;

59 changes: 59 additions & 0 deletions utils/cooking2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
require 'vendor/autoload.php';

$config = json_decode(file_get_contents("config.json"));
$client = OpenAI::client($config->gptkey);
$db = mysqli_connect($config->database->host, $config->database->username, $config->database->password, $config->database->database);

$q = mysqli_query($db, "SELECT json_extract(json_extract(hunting_json, '$.animals.*'), '$[*][*]') as loot FROM `game_locations` where json_extract(hunting_json, '\$.probability') > 0 ORDER BY `game_locations`.`id` ASC");
$list = [];
while ($loot = mysqli_fetch_object($q)) {
$l = json_decode($loot->loot);
foreach ($l ?? [] as $item) {
$list[] = $item;
}
}
$list = array_unique($list);
$list = array_values($list);
$missing = [];
$foodNames = [];
$foodIngr = [];
$foods = json_decode(file_get_contents("food.json"));
echo "Number of dishes: " . count($foods) . "\n";
mysqli_query($db, "DELETE FROM ingredients");
mysqli_query($db, "DELETE FROM food");

foreach ($foods as $food) {
if (in_array($food->name, $foodNames)) {
echo "Dupe food name: $food->name\n";
}
asort($food->ingredients);
$m = strtoupper(json_encode(array_values($food->ingredients)));
if (in_array($m, $foodIngr)) {
echo "Dupe ingredients: $food->name $m in " . $foodIngr[$m] . "\n";
}
$foodIngr[$m] = $food->name;
$foodNames[] = $food->name;
}
foreach ($foods as $food) {
$food->name = mysqli_escape_string($db, $food->name);
//echo $food->name . "...\n";
$food->description = mysqli_escape_string($db, $food->description);
$value = (int)($food->stamina_change ?? 0) + (int)($food->skill_change ?? 0) + (int)($food->luck_change ?? 0) + (int)($food->speed_change ?? 0);
foreach ($food->ingredients as $item) {
if (in_array($item, $foodNames)) {
$value *= 4;
}
}
mysqli_query($db, "INSERT INTO food (name, description, stamina_change, skill_change, luck_change, speed_change, value) VALUES('$food->name','$food->description','".($food->stamina_change ?? 0)."','".($food->skill_change ?? 0)."','".($food->luck_change ?? 0)."','".($food->speed_change ?? 0)."', $value)");
$id = mysqli_insert_id($db);
foreach ($food->ingredients as $item) {
if (!in_array($item, $list) && !in_array($item, $foodNames)) {
$missing[$item] = true;
}
$item = mysqli_escape_string($db, $item);
mysqli_query($db, "INSERT INTO ingredients (food_id, ingredient_name) VALUES($id, '$item')");
}
}
$missing = array_keys($missing);
print_r($missing);
45 changes: 45 additions & 0 deletions utils/defaultsfix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

$config = json_decode(file_get_contents("config.json"));
$db = mysqli_connect($config->database->host, $config->database->username, $config->database->password, $config->database->database);

// first, do spells

$over = mysqli_query($db, "SELECT user_id, count(flags) as c FROM `game_default_spells` where flags = 'SPELL' GROUP BY flags, user_id having count(flags) > 3 ORDER BY count(flags) DESC");

while ($row = mysqli_fetch_object($over)) {
$player = mysqli_fetch_object(mysqli_query($db, "SELECT * FROM game_default_users WHERE user_id = $row->user_id"));
if ($player->profession == 2) { // wizard
$max = 5;
} else {
$max = 2;
}
if ($row->c > $max) {
print "Over spells: $row->user_id\n";
}
$spells = mysqli_query($db, "SELECT * FROM game_default_spells WHERE user_id = $row->user_id AND flags = 'SPELL' ORDER BY id");
$index = 1;
while ($s = mysqli_fetch_object($spells)) {
if ($index > $max) {
mysqli_query($db, "DELETE FROM game_default_spells WHERE id = $s->id");
print "Deleted spell at id $s->id (count $index beyond $max)\n";
}
$index++;
}

}

$over = mysqli_query($db, "SELECT user_id, count(flags) as c FROM `game_default_spells` where flags = 'HERB' GROUP BY flags, user_id having count(flags) > 3 ORDER BY count(flags) DESC");

while ($row = mysqli_fetch_object($over)) {
print "Over herbs: $row->user_id\n";
$spells = mysqli_query($db, "SELECT * FROM game_default_spells WHERE user_id = $row->user_id AND flags = 'HERB' ORDER BY id");
$index = 1;
while ($s = mysqli_fetch_object($spells)) {
if ($index > 3) {
mysqli_query($db, "DELETE FROM game_default_spells WHERE id = $s->id");
print "Deleted herb at id $s->id (count $index beyond 3)\n";
}
$index++;
}
}
Loading

0 comments on commit c8d51da

Please sign in to comment.