-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
119 lines (106 loc) · 3.73 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
stats = {
partyMembers: [
{
name: "Bob the Wizard",
emotion: "😲",
bars: [{
name: "Health",
percent: 100,
color: "#ff0000",
},
{
name: "Mana",
percent: 50,
color: "#0000ff",
},
{
name: "Gold",
percent: 69,
color: "#ffff00",
},
],
},
{
name: "Doggy",
emotion: "😋",
bars: [
{
name: "Health",
percent: 100,
color: "#ff0000",
},
{
name: "Bark Power",
percent: 75,
color: "#00ffff",
},
],
},
],
inventory: [
{
name: "Wet Stick",
emoji: "🦴",
},
{
name: "Glasses",
emoji: "👓",
},
{
name: "Apple",
emoji: "🍎",
},
],
nextPartOfStory: "You see the bear galloping its furry and muscular legs towards you! Doggy doesn't notice, licking its bite-marks-covered stick on the ground.",
playerChoices: ["Send a fireball spell", "Alert doggy"],
previousStoryPart: "",
lastChoice: "",
}
//backstory: "After walking out of your wizard tower, you saw a dog. You gave it a bone you had in your inventory. You and the dog went into the forest. You saw a bear and hid in the bushes. Your dog made a sound and alerted the bear. The bear is now running towards you.",
async function sendAction(action, regen) {
// Disable all buttons
document.querySelectorAll('div.gameContainer button').forEach((button) => { button.disabled = true
})
// Back up current stats object incase of error
const backupStats = structuredClone(stats)
stats.previousStoryPart = stats.nextPartOfStory
defaultNextPartOfStory = "(Assistant, replace this with what happens next in the story.)"
defaultPlayerChoices = ["(Assistant, replace this with multiple choices for the player.)"]
stats.nextPartOfStory = defaultNextPartOfStory
stats.playerChoices = defaultPlayerChoices
stats.lastChoice = action
// Since smartGen could result in an error, we define a different variable, and set stats to it afterwards if it's safe
statsAttempt = await smartGen(actionPrompt(stats), true)
if (statsAttempt == null) {
stats = structuredClone(backupStats)
return
} else {
stats = structuredClone(statsAttempt)
}
// If output's nextPartOfStory or playerChoices is untouched, or if the length of them is 0, regenerate.
if (stats.nextPartOfStory == defaultNextPartOfStory ||
!stats.nextPartOfStory.replace(/\s/g,"").length > 0 ||
stats.playerChoices == defaultPlayerChoices ||
!stats.playerChoices[0].replace(/\s/g,"").length > 0) {
// Restore backup stats object
stats = structuredClone(backupStats)
updateStats(stats)
if (!debug_mode) sendAction(action, true)
return
}
try {
updateStats(stats)
} catch (error) {
console.log("no")
// Only notify if the error is a parsing error and not a smartGen error
if (stats) {
updateAIStatus("format")
}
console.info("error: out was YAML compatible but didn't fit game format. and/or smartGen() returned a not-good output. regenerating...")
// Restore backup stats object
stats = structuredClone(backupStats)
updateStats(stats)
if (!debug_mode) sendAction(action, regen)
}
}
updateStats(stats)