-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (123 loc) · 4.08 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Cerebras } from "@cerebras/cerebras_cloud_sdk";
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import path from "path";
import open from "open";
import { dirname } from "path";
import { fileURLToPath } from "url";
const baseURL =
process.env.NODE_ENV === "production"
? "https://poke-battles.vercel.app/"
: "http://localhost:3000";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
dotenv.config();
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.use(cors({ origin: baseURL }));
app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));
const client = new Cerebras({
apiKey: process.env.CEREBRAS_API_KEY,
});
// Helper functions (unchanged)
function formatPokemonForBattle(pokemon, level) {
return {
name: pokemon.name,
level: level,
types: pokemon.types.map((t) => t.type.name),
stats: pokemon.stats.reduce((acc, stat) => {
acc[stat.stat.name] = calculateStats(stat.base_stat, level);
return acc;
}, {}),
moves: pokemon.moves
.sort(() => 0.5 - Math.random())
.slice(0, 4)
.map((m) => m.move.name),
};
}
function calculateStats(baseStat, level) {
return Math.floor((2 * baseStat * level) / 100 + 5);
}
function generateBattlePrompt(pokemon1, pokemon2) {
return `Simulate an exciting Pokemon battle between:
Level ${pokemon1.level} ${pokemon1.name.toUpperCase()} (${pokemon1.types.join(
"/"
)}-type)
Stats: HP ${pokemon1.stats.hp}, Attack ${pokemon1.stats.attack}, Defense ${
pokemon1.stats.defense
}
Special Attack ${pokemon1.stats["special-attack"]}, Special Defense ${
pokemon1.stats["special-defense"]
}, Speed ${pokemon1.stats.speed}
Moves: ${pokemon1.moves.join(", ")}
VS
Level ${pokemon2.level} ${pokemon2.name.toUpperCase()} (${pokemon2.types.join(
"/"
)}-type)
Stats: HP ${pokemon2.stats.hp}, Attack ${pokemon2.stats.attack}, Defense ${
pokemon2.stats.defense
}
Special Attack ${pokemon2.stats["special-attack"]}, Special Defense ${
pokemon2.stats["special-defense"]
}, Speed ${pokemon2.stats.speed}
Moves: ${pokemon2.moves.join(", ")}
Create an exciting battle narrative with these elements:
1. Use "Turn X:" format for each turn
2. Include type advantages and critical hits
3. Add dramatic moments and environmental effects
4. Include crowd reactions and battle atmosphere
5. Make moves dynamic and impactful
6. Keep the narrative engaging and action-packed
7. End with a clear winner
Format guidelines:
- No markdown formatting
- Use clear turn numbers
- Mention "Critical hit!" for critical hits
- End with "[Pokemon Name] wins the battle!"
- Keep paragraphs short and impactful
- Include status effects and strategic moves
Make it exciting and detailed!`;
}
app.post("/simulate-battle", async (req, res) => {
try {
const { pokemon1, pokemon2, level1, level2 } = req.body;
const formattedPokemon1 = formatPokemonForBattle(pokemon1, level1);
const formattedPokemon2 = formatPokemonForBattle(pokemon2, level2);
const battlePrompt = generateBattlePrompt(
formattedPokemon1,
formattedPokemon2
);
console.log(
"Simulating battle between:",
formattedPokemon1.name,
"and",
formattedPokemon2.name
);
const completion = await client.chat.completions.create({
messages: [{ role: "user", content: battlePrompt }],
model: "llama3.1-8b",
});
res.json({
battle: completion.choices[0].message.content,
pokemon1: formattedPokemon1,
pokemon2: formattedPokemon2,
});
} catch (error) {
console.error("Battle simulation error:", error);
res
.status(500)
.json({ error: "Failed to simulate battle", details: error.message });
}
});
// Define the port
const PORT = process.env.PORT || 3000;
// Start the server and open the browser
app.listen(PORT, async () => {
console.log(`Battle simulator running on ${baseURL}`);
await open(baseURL);
});