示例#1
0
func NewServer() (*Server, error) {
	// connect dao server
dao:
	daocli, err := rpc.Dial("tcp", cfg.DaoAddr())
	if err != nil {
		log.Warningf("dao server connect fail(err=%+v), try again...", err.Error())
		time.Sleep(1 * time.Second)
		goto dao
	}

	ctrl := make(chan string, 10)
game:
	cli, err := fw.NewClient(cfg.GameAddr(), &gameCliModel{}, ctrl)
	if err != nil {
		log.Error(err.Error())
		log.Warningf("game server connect fail(err=%+v), try again...", err.Error())
		time.Sleep(1 * time.Second)
		goto game
	}
	go cli.Loop()

	// daemon routine (to be done), with "ctrl"
	// ......
	////////////////////

	// new server
	serverInst = &Server{
		dao:       daocli,
		gameCli:   cli,
		gameCtrl:  ctrl,
		connIdAcc: 0,
		agents:    make(map[int]*fw.Agent, 1000),
	}
	return serverInst, nil
}
示例#2
0
func NewModel() *Model {
	if modelInst != nil {
		panic("game:Model:NewModel can be invoked only once")
	}
	modelInst := new(Model)

	// connect dao server
dao:
	daocli, err := rpc.Dial("tcp", cfg.DaoAddr())
	if err != nil {
		log.Warningf("dao server connect fail(err=%+v), try again...", err.Error())
		time.Sleep(1 * time.Second)
		goto dao
	}

	modelInst.dao = daocli

	// data init
	modelInst.gameIdAcc = 0
	modelInst.games = map[int]*cow.Game{}
	modelInst.playerGames = map[int]*cow.Game{}
	modelInst.freeGames = [][]*cow.Game{}
	// 1000 games each type
	//	for i, _ := range (modelInst.freeGames) {
	//		modelInst.freeGames[i] = make([]*cow.Game, 0, 1000)
	//	}
	for i := 0; i < int(RoomTypeCount); i++ {
		modelInst.freeGames[i] = make([]*cow.Game, 0, 1000)
	}

	return modelInst
}
示例#3
0
文件: user.go 项目: lkj01010/chesssrv
func (u *User) GetCoin(args *Args, reply *Reply) (err error) {
	var coin int
	coin, err = redis.Int(u.c.Do("HGET", k_user_+args.Id, k_coin))
	if err != nil {
		log.Warningf("Coin:not found, id=%+v", args.Id)
		reply.Code = com.E_ValueNotFound
	} else {
		reply.Code = com.E_Success
		reply.Int = coin
	}
	return
}
示例#4
0
func NewClient(addr string, m ClientModel, out chan string) (*Client, error) {
L:
	client, err := net.Dial("tcp", addr)
	if err != nil {
		log.Warningf("not connected to %+v, try again ...", addr)
		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
	}
	rwc := NewWsReadWriter(conn)
	return &Client{
		ReadWriteCloser: rwc,
		ClientModel:     m,
		send:            make(chan string, 10),
		ctrl:            out,
	}, nil
}
示例#5
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
}