当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.Infof方法代码示例

本文整理汇总了Golang中common.Context.Infof方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.Infof方法的具体用法?Golang Context.Infof怎么用?Golang Context.Infof使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common.Context的用法示例。


在下文中一共展示了Context.Infof方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: nextTurn

func nextTurn(cont appengine.Context, id *datastore.Key, playerNames []string) {
	con := common.Context{Context: cont}
	self := getGameById(con, id)
	self.PlayerNames = playerNames
	if self.Length > maxGameDuration {
		self.State = StateFinished
		self.Save(con)
		con.Infof("Ended %v due to timeout", self.Id)
		return
	}
	errorSavers := []func(){}
	if err := common.Transaction(con, func(c common.Context) (err error) {
		lastTurn := GetLatestTurnByParent(c, self.Id)
		responses := make(chan orderResponse, len(self.Players))
		for _, playerId := range self.Players {
			orderResp := orderResponse{
				DatastorePlayerId: playerId,
				StatePlayerId:     state.PlayerId(playerId.Encode()),
			}
			if foundAi := GetAIById(c, playerId); foundAi != nil {
				go func() {
					// Always deliver the order response
					defer func() {
						responses <- orderResp
					}()

					// create a request
					orderRequest := ai.OrderRequest{
						Me:     orderResp.StatePlayerId,
						State:  lastTurn.State,
						GameId: state.GameId(self.Id.Encode()),
					}

					// encode it into a body, and remember its string representation
					sendBody := &bytes.Buffer{}
					aiCommon.MustEncodeJSON(sendBody, orderRequest)
					sendBodyString := sendBody.String()

					// get a client
					client := urlfetch.Client(c)

					// send the request to the ai
					req, err := http.NewRequest("POST", foundAi.URL, sendBody)
					var resp *http.Response
					if err == nil {
						req.Header.Set("Content-Type", "application/json; charset=UTF-8")
						resp, err = client.Do(req)
					}

					recvBody := &bytes.Buffer{}
					recvBodyString := ""
					if err == nil {
						// check what we received
						_, err = io.Copy(recvBody, resp.Body)
						recvBodyString = recvBody.String()
					}
					// if we have no other errors, but we got a non-200
					if err == nil && resp.StatusCode != 200 {
						err = orderError{
							Request:      req,
							RequestBody:  sendBodyString,
							Response:     resp,
							ResponseBody: recvBodyString,
						}
					}

					// lets try to unserialize
					if err == nil {
						err = json.Unmarshal(recvBody.Bytes(), &orderResp.Orders)
					}

					// store the error, if any
					if err != nil {
						orderResp.Error = err
					}
				}()
			} else {
				responses <- orderResp
			}
		}
		orderMap := map[state.PlayerId]state.Orders{}
		for _, _ = range self.Players {
			// wait for the responses
			orderResp := <-responses
			// store it
			orderMap[orderResp.StatePlayerId] = orderResp.Orders
			// if we got an error
			if orderResp.Error != nil {
				// make sure to save it later
				errorSavers = append(errorSavers, func() {
					if ai := GetAIById(con, orderResp.DatastorePlayerId); ai != nil {
						ai.AddError(con, lastTurn.Id, orderResp.Error)
					}
				})
			}
		}
		// execute the orders
		newTurn, winner := lastTurn.Next(c, orderMap)
		// save the new turn
		newTurn.Save(c, self.Id)
//.........这里部分代码省略.........
开发者ID:robban,项目名称:stockholm-ai,代码行数:101,代码来源:game.go


注:本文中的common.Context.Infof方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。