Пример #1
0
func (u *User) AddCoin(args *Args, reply *Reply) (err error) {
	_, err = u.c.Do("HINCRBY", k_user_+args.Id, k_coin, args.Int)
	if err != nil {
		log.Warning("AddCoin:err=", err.Error())
		reply.Code = com.E_ValueNotFound
	} else {
		reply.Code = com.E_Success
	}
	return
}
Пример #2
0
func newClient() (*websocket.Conn, error) {
	//todo
L:
	client, err := net.Dial("tcp", cfg.AgentAddr())
	if err != nil {
		log.Warning("not connected to agent server, try again ...")
		time.Sleep(1 * time.Second)
		goto L
	}
	conn, err := websocket.NewClient(newConfig_("/"), client)
	if err != nil {
		log.Errorf("WebSocket handshake error: %v", err)
		return nil, err
	}
	return conn, nil
}
Пример #3
0
func (p *playerAgent) handleEnterReq(contentstr string) (err error) {
	_, ok := modelInst.playerGames[p.connId]
	if ok {
		// 已经在游戏中,报错
		err = com.ErrAlreadyInGame
		log.Warning("game:model:handle:player enter req, err=", err.Error())
		return
	} else {
		var content EnterGameReq
		if err = json.Unmarshal([]byte(contentstr), &content); err != nil {
			return
		}
		p.id = content.Id

		//判断钱是否够
		isCoinEnough := (p.info.Coin >= RoomEnterCoin[content.RoomType])

		// 够入场费
		if isCoinEnough {
			// 塞进房间
			rt := RoomType(content.RoomType)
			if rt.IsValid() {
				game := modelInst.GetFreeGameByType(rt)
				game.PlayerEnter(content.Id, p.info, p.toGame, p.Send)
			} else {
				log.Warningf("game:model:handle:roomtype invalid, rt=%+v", content.RoomType)
				err = com.ErrRoomTypeInvalid
				return
			}
		} else {
			// 不够入场费
			// 返回response
			resp := com.MakeMsgString(com.Cmd_Game_EnterResp, com.E_CoinNotEnough, nil)
			p.Send(resp)
			return
		}
	}
	return
}
Пример #4
0
func main() {
	var agent *fw.Agent
	serve := func(ws *websocket.Conn) {
		// only ONE agent server allowed to connect
		log.Debugf("new comes, agent=%+v", agent)
		if agent == nil {
			agent = fw.NewAgent(game.NewModel(), fw.NewWsReadWriter(ws), 1)
			if err := agent.Serve(); err != nil {
				log.Error(err.Error())
			}
		} else {
			log.Warning("already connected by agentsrv, cannont serve more")
		}
		// release
		agent = nil
	}

	http.Handle("/", websocket.Handler(serve))
	log.Debug("game server start on:", cfg.GameAddr())
	http.ListenAndServe(cfg.GameAddr(), nil)

}