本文整理汇总了Golang中github.com/backerman/evego.Database.ItemForName方法的典型用法代码示例。如果您正苦于以下问题:Golang Database.ItemForName方法的具体用法?Golang Database.ItemForName怎么用?Golang Database.ItemForName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/backerman/evego.Database
的用法示例。
在下文中一共展示了Database.ItemForName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getItemPrices
func getItemPrices(
db evego.Database,
mkt evego.Market,
req *[]queryItem,
station *evego.Station,
loc string) (*map[string]responseItem, error) {
respItems := make(map[string]responseItem)
for _, i := range *req {
dbItem, err := db.ItemForName(i.ItemName)
if err != nil {
continue
}
var (
item responseItem
orders *[]evego.Order
)
if station != nil {
orders, err = mkt.OrdersInStation(dbItem, station)
} else {
orders, err = mkt.OrdersForItem(dbItem, loc, evego.AllOrders)
}
if err != nil {
return nil, fmt.Errorf("Unable to retrieve order information for %v: %v", dbItem.Name, err)
}
item = summarizeOrders(db, *orders, dbItem)
respItems[item.ItemName] = item
}
return &respItems, nil
}
示例2: ReprocessItems
// ReprocessItems returns a handler function that takes as input an item list
// and returns the reprocessing output of each inventory line.
func ReprocessItems(db evego.Database, mkt evego.Market) web.HandlerFunc {
jita, err := db.StationForID(60003760) // Jita IV - Moon 4 - Caldari Navy Assembly Plant
if err != nil {
log.Fatalf("Seriously, guys, something's gone wrong with the database!")
}
return func(c web.C, w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
contentType, _, err := mime.ParseMediaType(contentType)
if err != nil {
http.Error(w, "Bad request content type", http.StatusBadRequest)
w.Write([]byte(`{"status": "Error"}`))
return
}
if contentType != "application/json" {
http.Error(w, "Request must be of type application/json", http.StatusUnsupportedMediaType)
w.Write([]byte(`{"status": "Error"}`))
return
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Unable to process request body", http.StatusBadRequest)
w.Write([]byte(`{"status": "Error"}`))
return
}
var req reproQuery
err = json.Unmarshal(reqBody, &req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
w.Write([]byte(`{"status": "Error"}`))
return
}
// Convert 0..100 scale to 0..1.
stationYield := math.Max(math.Min(1, req.StationYield*0.01), 0)
taxRate := math.Max(math.Min(1, req.TaxRate*0.01), 0)
reproSkills := industry.ReproSkills{
ScrapmetalProcessing: req.ScrapmetalSkill,
}
results := make(map[string][]evego.InventoryLine)
for _, i := range req.Items {
item, err := db.ItemForName(i.ItemName)
if err != nil {
continue
}
itemResults, err := industry.ReprocessItem(db, item, i.Quantity, stationYield, taxRate, reproSkills)
if err != nil {
http.Error(w, "Unable to compute reprocessing output", http.StatusInternalServerError)
w.Write([]byte(`{"status": "Error"}`))
return
}
results[item.Name] = itemResults
}
prices := make(map[string]responseItem)
// Loop over each item that was reprocessed.
for _, itemOut := range results {
// For each item in its component materials,
for _, item := range itemOut {
// Check if we already know its price in Jita
itemName := item.Item.Name
_, found := prices[itemName]
if !found {
// If not there, get its price.
myPrices, err := getItemPrices(db, mkt, &[]queryItem{{Quantity: 1, ItemName: itemName}}, jita, "")
if err != nil {
http.Error(w, `{"status": "Error", "error": "Unable to look up prices (reprocessing)"}`,
http.StatusInternalServerError)
return
}
prices[itemName] = (*myPrices)[itemName]
}
}
}
response := reproResults{
Items: results,
Prices: prices,
}
resultsJSON, _ := json.Marshal(response)
w.Write(resultsJSON)
}
}