mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 07:32:32 +01:00
An MMO server without multiplayer defeats the purpose. PostgreSQL is the right choice and Docker Compose already solves the setup pain. This reverts the common/db wrapper, SQLite schema, config Driver field, modernc.org/sqlite dependency, and all repo type changes while keeping the dashboard, wizard, and CI improvements from the previous commit.
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package channelserver
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// MiscRepository centralizes database access for miscellaneous game tables.
|
|
type MiscRepository struct {
|
|
db *sqlx.DB
|
|
}
|
|
|
|
// NewMiscRepository creates a new MiscRepository.
|
|
func NewMiscRepository(db *sqlx.DB) *MiscRepository {
|
|
return &MiscRepository{db: db}
|
|
}
|
|
|
|
// GetTrendWeapons returns the top 3 weapon IDs for a given weapon type, ordered by count descending.
|
|
func (r *MiscRepository) GetTrendWeapons(weaponType uint8) ([]uint16, error) {
|
|
rows, err := r.db.Query("SELECT weapon_id FROM trend_weapons WHERE weapon_type=$1 ORDER BY count DESC LIMIT 3", weaponType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query trend_weapons: %w", err)
|
|
}
|
|
defer func() { _ = rows.Close() }()
|
|
var result []uint16
|
|
for rows.Next() {
|
|
var id uint16
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, fmt.Errorf("scan trend_weapons: %w", err)
|
|
}
|
|
result = append(result, id)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
// UpsertTrendWeapon increments the count for a weapon, inserting it if it doesn't exist.
|
|
func (r *MiscRepository) UpsertTrendWeapon(weaponID uint16, weaponType uint8) error {
|
|
_, err := r.db.Exec(`INSERT INTO trend_weapons (weapon_id, weapon_type, count) VALUES ($1, $2, 1) ON CONFLICT (weapon_id) DO
|
|
UPDATE SET count = trend_weapons.count+1`, weaponID, weaponType)
|
|
return err
|
|
}
|