db: GetPlayerInventorySlots() can just build the array the first pass, no?

This commit is contained in:
CPunch 2023-03-11 02:06:39 -06:00
parent a0a108a605
commit c62ed5d101
2 changed files with 8 additions and 22 deletions

View File

@ -17,33 +17,24 @@ type Inventory struct {
// start && end are both inclusive // start && end are both inclusive
func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) ([]protocol.SItemBase, error) { func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) ([]protocol.SItemBase, error) {
rows, err := db.Query("SELECT PlayerID, Slot, ID, Type, Opt, TimeLimit FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, end, PlayerID) rows, err := db.Query("SELECT Slot, Type, ID, Opt, TimeLimit FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, end, PlayerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var inven []Inventory items := make([]protocol.SItemBase, end-start)
for rows.Next() { for rows.Next() {
item := Inventory{} var slot int
item := protocol.SItemBase{}
if err := rows.Scan( if err := rows.Scan(
&item.PlayerID, &item.Slot, &item.ID, &item.Type, &item.Opt, &item.TimeLimit); err != nil { &slot, &item.IType, &item.IID, &item.IOpt, &item.ITimeLimit); err != nil {
return nil, err return nil, err
} }
inven = append(inven, item) items[slot-start] = item
} }
// populate an SItemBase array
items := make([]protocol.SItemBase, end-start)
for _, item := range inven {
items[item.Slot-start] = protocol.SItemBase{
IID: int16(item.ID),
IType: int16(item.Type),
IOpt: int32(item.Opt),
ITimeLimit: int32(item.TimeLimit),
}
}
return items, nil return items, nil
} }
@ -51,7 +42,7 @@ func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) (
func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []protocol.SItemBase) error { func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []protocol.SItemBase) error {
return db.Transaction(func(tx *sql.Tx) error { return db.Transaction(func(tx *sql.Tx) error {
// delete inventory slots // delete inventory slots
_, err := db.Query("DELETE FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, start+len(items)-1, PlayerID) _, err := db.Exec("DELETE FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, start+len(items)-1, PlayerID)
if err != nil { if err != nil {
return err return err
} }
@ -59,7 +50,7 @@ func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []pr
// insert inventory // insert inventory
for i, item := range items { for i, item := range items {
if item.IID != 0 { if item.IID != 0 {
_, err := db.Query("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt, TimeLimit) VALUES (?, ?, ?, ?, ?, ?)", PlayerID, start+i, item.IID, item.IType, item.IOpt, item.ITimeLimit) _, 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 { if err != nil {
return err return err
} }

View File

@ -17,11 +17,6 @@ type DBHandler struct {
db *sql.DB db *sql.DB
} }
type DBQuery interface {
Query(query string, args ...any) (*sql.Rows, error)
Exec(query string, args ...any) (sql.Result, error)
}
//go:embed migrations/new.sql //go:embed migrations/new.sql
var createDBQuery string var createDBQuery string