import Bun from "bun";
import { redis } from "../utils/server";

export class BattleService {
  public static async publishBattle(eventId: string, jsonData: string) {
    const hasher = new Bun.CryptoHasher('md5'); 
    const hash = hasher.update(jsonData).digest('hex').slice(0, 6);
    console.log(`Battle published with hash: ${hash}`);
    await redis.setex(`my-kaiqiuwang:battle:${eventId}:${hash}`, 60 * 10, jsonData);
    return hash;
  }
  public static async getBattle(eventId: string, hash: string) {
    const battleData = await redis.get(`my-kaiqiuwang:battle:${eventId}:${hash}`);
    if (!battleData) return {};
    try {
      return JSON.parse(battleData);
    } catch (e) {
      console.error("Error", e);
      return {};
    }
  }
}