Skip to content

Commit

Permalink
wrap items request with additional data
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 19, 2020
1 parent 3d14686 commit 9b0df43
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/server/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ var (
charPattern = regexp.MustCompile("(\\d+)\\.sav")
)

type ItemRequest struct {
Items []item.Item `json:"items"`
Equipped []*pb.EquippedInventorySaveGameData `json:"equipped"`
Active []int32 `json:"active"`
}

func listCharacters(c *gin.Context) {
files, err := ioutil.ReadDir(pwd)
if err != nil {
Expand Down Expand Up @@ -133,7 +139,6 @@ func getItemsRequest(c *gin.Context) {
for _, data := range char.InventoryItems {
d := make([]byte, len(data.ItemSerialNumber))
copy(d, data.ItemSerialNumber)
log.Println(base64.StdEncoding.EncodeToString(data.ItemSerialNumber))
i, err := item.Deserialize(d)
if err != nil {
log.Println(err)
Expand All @@ -144,7 +149,12 @@ func getItemsRequest(c *gin.Context) {
i.Wrapper = data
items = append(items, i)
}
c.JSON(200, &items)
ir := ItemRequest{
Items: items,
Equipped: char.EquippedInventoryList,
Active: char.ActiveWeaponList,
}
c.JSON(200, &ir)
return
}

Expand All @@ -162,18 +172,20 @@ func updateItemsRequest(c *gin.Context) {
c.AbortWithStatus(500)
return
}
var items []item.Item
err = c.BindJSON(&items)
var ir ItemRequest
err = c.BindJSON(&ir)
if err != nil {
c.AbortWithStatus(500)
return
}
backup(pwd, id)
char.InventoryItems, err = itemsToPBArray(items)
char.InventoryItems, err = itemsToPBArray(ir.Items)
if err != nil {
c.AbortWithStatus(500)
return
}
char.ActiveWeaponList = ir.Active
char.EquippedInventoryList = ir.Equipped
f, err = os.Create(pwd + "/" + id + ".sav")
if err != nil {
c.AbortWithStatus(500)
Expand Down

0 comments on commit 9b0df43

Please sign in to comment.