gopenfusion/core/db/inventory.go
CPunch 5b2a8b838e major refactoring: db.Player is now core.Player
- misc. cleanup
- core/db/players.go: works with core.Player types, will also grab inventory table
2023-03-22 00:30:58 -05:00

63 lines
1.5 KiB
Go

package db
import (
"database/sql"
"github.com/CPunch/gopenfusion/core/protocol"
)
type Inventory struct {
PlayerID int
Slot int
ID int
Type int
Opt int
TimeLimit int
}
// start && end are both inclusive
func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) ([]protocol.SItemBase, error) {
rows, err := db.Query("SELECT Slot, Type, ID, Opt, TimeLimit FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, end, PlayerID)
if err != nil {
return nil, err
}
items := make([]protocol.SItemBase, end-start)
for rows.Next() {
var slot int
item := protocol.SItemBase{}
if err := rows.Scan(
&slot, &item.IType, &item.IID, &item.IOpt, &item.ITimeLimit); err != nil {
return nil, err
}
items[slot-start] = item
}
return items, nil
}
// start is inclusive
func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []protocol.SItemBase) error {
return db.Transaction(func(tx *sql.Tx) error {
// delete inventory slots
_, err := db.Exec("DELETE FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, start+len(items)-1, PlayerID)
if err != nil {
return err
}
// insert inventory
for i, item := range items {
if item.IID != 0 {
_, err := db.Exec("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt, TimeLimit) VALUES (?, ?, ?, ?, ?, ?)", PlayerID, start+i, item.IID, item.IType, item.IOpt, item.ITimeLimit)
if err != nil {
return err
}
}
}
return nil
})
}