package com.huangzj.server.module;

import com.huangzj.base.module.GeneralModule;
import com.huangzj.base.module.ModuleMetricsProvider;
import com.huangzj.base.module.PlayerModule;
import com.huangzj.server.mongo.DemoMongoService;
import com.huangzj.server.mongo.DemoPayloadDoc;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Component;

/**
 * Demo 主模块：每次 tick 打个计数，save 时往 Mongo 扔一条，方便你看链路是活的。
 */
@Component
@PlayerModule(order = 10)
public class SamplePlayerModule extends GeneralModule<DemoPlayerHost> implements ModuleMetricsProvider {

    private final DemoMongoService mongoService;
    private final AtomicLong tickCount = new AtomicLong();

    public SamplePlayerModule(DemoPlayerHost host, DemoMongoService mongoService) {
        super(host);
        this.mongoService = mongoService;
        enableTick();
    }

    @Override
    public long getPlayerId() {
        return getPlayer().getPlayerId();
    }

    @Override
    protected void tick(long runAt) {
        tickCount.incrementAndGet();
    }

    @Override
    public boolean saveData() {
        DemoPayloadDoc doc = new DemoPayloadDoc();
        doc.setPlayerId(getPlayerId());
        doc.setContent("tick-save-" + System.currentTimeMillis());
        mongoService.saveImmediate(doc);
        return true;
    }

    @Override
    public Map<String, Object> metrics() {
        Map<String, Object> m = new LinkedHashMap<>();
        m.put("sampleTickCount", tickCount.get());
        return m;
    }
}
