gopenfusion/db/inventory.go
CPunch 8569225ec7 major refactoring; started DB
- DB work has started in db. this will be a direct port of the OpenFusion DB format.
- LoginServer is now less of a dummy. You can create and login to accounts, and create a character to go through the tutorial with.
- config.go will host some commonly changed variables.
- protocol: fixed a bug relating to arrays being ignored while encoding packets
2023-03-09 01:48:13 -06:00

62 lines
1.6 KiB
Go

package db
import (
"github.com/CPunch/GopenFusion/protocol"
"github.com/blockloop/scan"
)
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 * FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, end, PlayerID)
if err != nil {
return nil, err
}
var inven []Inventory
if err := scan.Row(&inven, rows); err != nil {
return make([]protocol.SItemBase, end-start), nil
}
// 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
}
// start is inclusive TODO: needs to be a transaction !!
func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []protocol.SItemBase) error {
// delete inventory slots
_, err := db.Query("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.Query("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
}