Exemplo n.º 1
0
func (this *AccountInfo) Register(name string, pwd string, server_id int32) (int32, int32) {
	this.accountMutex.Lock()
	defer this.accountMutex.Unlock()

	//通过内存数据库 先检查是否username相同
	err := redis.Find("PlayerName:"+name, this.redis_login_base)
	if err != nil { //没有查到数据

		db_count_max += 1
		user := LoginBase{PlayerId: db_count_max, PlayerName: name, PlayerPwd: pwd, ServerId: server_id, IsForBid: false}
		this.redis_login_base.Servers = append(this.redis_login_base.Servers, server_id)
		//内存数据库
		err_redis_player := redis.Modify("PlayerName:"+name, user)
		err_redis_count := redis.Modify("PlayerCount", db_count_max)

		if err_redis_player == nil && err_redis_count == nil {
			return global.REGISTERSUCCESS, db_count_max
		} else {
			fmt.Println("数据写入错误")
		}
	} else {
		Log.Trace("name = %s pwd = %s have same SAMENICK", name, pwd)
	}
	return global.SAMENICK, 0
}
Exemplo n.º 2
0
func init() {
	//初始化随机数
	rand_ = rand.New(rand.NewSource(time.Now().UnixNano()))

	//csv初始化
	Csv = new(CsvConfig)
	Csv.Init()

	//json初始化
	Json_config = new(JsonConfig)
	Json_config.Init()

	//uid初始化
	err := redis.Find("uid", &Global_Uid)
	if err != nil {
		Global_Uid = createbeginUid()
		fmt.Println("Global_Uid", Global_Uid)
	} else {
		Global_Uid += 200 //解决宕机后Global_uid未存储
	}

	//全局world 初始化
	word = new(World)
	word.Init()

	//全局各个关卡挂机玩家
	global_guaji_players = new(GuajiPlayers)
	global_guaji_players.Init()

	//定时器
	timer_Manager = new(TimerManager)
	timer_Manager.Init()
}
Exemplo n.º 3
0
func Register(registerInfo_proto *protocol.S2SSystem_RegisterInfo) error {
	player := new(player.Player)

	//查询玩家名字是否被占用
	err := redis.Find("player:"+registerInfo_proto.GetName(), player)
	if err == nil {
		return errors.New("same nick")
	}
	//玩家id 增加1 读取现在最大玩家数据
	id, err := redis.Incr("Register:MaxId")
	if err != nil {
		return err
	}

	player.Info.ID = fmt.Sprintf("%s", id)
	player.Info.Name = registerInfo_proto.GetName()
	player.Info.Age = registerInfo_proto.GetAge()
	player.Info.Sex = registerInfo_proto.GetSex()

	player.Money = 0
	player.Exp = 0
	player.Password = registerInfo_proto.GetPassworld()
	player.Conn = nil

	//存储现在的玩家数据
	redis.Add("player:"+registerInfo_proto.GetName(), player)
	return err
}
Exemplo n.º 4
0
func (this *AccountInfo) Register(name string, pwd string, server_id string) (int, int) {
	this.accountMutex.Lock()
	defer this.accountMutex.Unlock()

	//通过内存数据库 先检查是否username相同
	redis_login_base := new(LoginBase)
	err := redis.Find("PlayerName:"+name, redis_login_base)
	if err != nil { //没有查到数据
		//写内存数据库与mysql
		this.config.count += 1
		user := LoginBase{PlayerId: this.config.count, PlayerName: name, PlayerPwd: pwd, Gold: 0, ServerId: server_id, IsForBid: false}

		//内存数据库
		err_redis := redis.Add("PlayerName:"+name, user)
		//mysql
		_, err_mysql := O.Insert(&user)

		if err_redis == nil && err_mysql == nil {
			return global.REGISTERSUCCESS, this.config.count
		} else {
			redis.Del("PlayerName:" + name)
			O.Delete(&user)
		}
	}

	Log.Trace("name = %s pwd = %s have same SAMENICK", name, pwd)
	return global.SAMENICK, 0
}
Exemplo n.º 5
0
func LoadPlayer(conn *net.Conn, id string) *Player { //读取玩家数据
	playerInfo := new(Player)
	err := redis.Find(id, playerInfo)
	if err == nil {
		return playerInfo
	}
	return nil
}
Exemplo n.º 6
0
func LoadPlayer(id_str string) *Player { //从数据库读取玩家信息
	player := new(Player)
	player.Init()
	err := redis.Find(id_str, player)
	if err == nil {
		return player
	}
	return nil
}
Exemplo n.º 7
0
func getMaxId() {
	err := redis.Find("PlayerCount", db_count_max)
	fmt.Println("数据库 getMaxId:", db_count_max)
	if err != nil {
		return
	} else {
		fmt.Println("数据库读取错误", err)
	}

}
Exemplo n.º 8
0
func LoadPlayer(id string) *Player { //从数据库读取玩家信息
	player := new(Player)
	err := redis.Find(id, player)
	if err == nil {
		if time.Now().Day() != int(player.Day) {
			player.DayFightNumber = 0
		}
		return player
	}
	return nil
}
Exemplo n.º 9
0
func (this *AccountInfo) VerifyLogin(name string, pwd string, allServerAddress map[int32]string) (result int32, player_id int32, game_address string) {
	err := redis.Find("PlayerName:"+name, this.redis_login_base)
	if err == nil {
		if strings.EqualFold(this.redis_login_base.PlayerName, name) && strings.EqualFold(this.redis_login_base.PlayerPwd, pwd) && !this.redis_login_base.IsForBid {
			serverAddress, _ := allServerAddress[this.redis_login_base.ServerId]
			return global.LOGINSUCCESS, this.redis_login_base.PlayerId, serverAddress
		} else {
			return global.FORBIDLOGIN, 0, ""
		}
	}
	return global.LOGINERROR, 0, ""
}
Exemplo n.º 10
0
func Login(logininfo_proto *protocol.S2SSystem_LoginInfo) (int32, *player.Player) {
	player := new(player.Player)
	err := redis.Find("player:"+logininfo_proto.GetName(), player)

	if err == nil { //查到改数据
		if strings.EqualFold(logininfo_proto.GetPassworld(), player.Password) {
			return global.LOGINSUCCESS, player
		} else {
			return global.PASSWDERROR, nil
		}
	}
	return global.LOGINERROR, nil
}
Exemplo n.º 11
0
func (this *AccountInfo) VerifyLogin(name string, pwd string) (result int, player_id int, game_address string) {
	redis_login_base := new(LoginBase)
	err := redis.Find("PlayerName:"+name, redis_login_base)
	if err == nil {
		if strings.EqualFold(redis_login_base.PlayerName, name) && strings.EqualFold(redis_login_base.PlayerPwd, pwd) && !redis_login_base.IsForBid {
			if v, ok := il8n.Data[redis_login_base.ServerId]; ok {
				return global.LOGINSUCCESS, redis_login_base.PlayerId, v.(string)
			} else {
				Log.Error("user.ServerId not find config for serverList error")
			}
		}
	}
	return global.LOGINERROR, 0, ""
}
Exemplo n.º 12
0
func (this *Deal4C) Handler4C(conn net.Conn) {
	defer conn.Close()
	const MAXLEN = 2048
	buf := make([]byte, MAXLEN)

	defer func() {
		fmt.Println("socket is close")
		conn.Close()
	}()

	for {
		n, err := conn.Read(buf) //接收具体消息
		if err != nil {
			return
		}

		if n > MAXLEN || n < 8 {
			account.Log.Error("recive error n> MAXLEN")
			return
		}

		//接收包头
		var head_len int32 = 0
		var head_pid int32 = 0
		buffer_len := bytes.NewBuffer(buf[0:4])
		buffer_pid := bytes.NewBuffer(buf[4:8])
		binary.Read(buffer_len, binary.BigEndian, &head_len)
		binary.Read(buffer_pid, binary.BigEndian, &head_pid)

		//接收包体
		switch head_pid {

		case 1:
			var db_count_max int32 = 0
			redis.Find("PlayerCount", db_count_max)
			fmt.Println("获取playerCount:", db_count_max)
			//注册
			register := new(protocol.Account_RegisterPlayer)
			if err := proto.Unmarshal(buf[8:n], register); err == nil {
				game_id, _, _ := this.getNewAddress()
				fmt.Println("注册:", register.GetPlayername(), register.GetPassworld(), game_id)
				result, player_id := this.account_info.Register(register.GetPlayername(), register.GetPassworld(), game_id)

				result4C := &protocol.Account_RegisterResult{
					Result: proto.Int32(int32(result)),
				}

				encObj, _ := proto.Marshal(result4C)
				SendPackage(conn, 1, encObj)

				//通知game注册成功
				if global.REGISTERSUCCESS == result {
					err := this.NoteGame(player_id, game_id)
					if err != nil {
						account.Log.Error("err1:", err)
					}
					account.Log.Info("player_id = %d game_id=%d", player_id, game_id)
				}
			}

		case 2:
			//登陆
			login := new(protocol.Account_LoginInfo)
			if err := proto.Unmarshal(buf[8:n], login); err == nil {
				result, player_id, server_address := this.account_info.VerifyLogin(login.GetPlayername(), login.GetPassworld(), config.AllServerAddress)
				//发送登陆并断开连接
				result4C := &protocol.Account_LoginResult{
					Result:     proto.Int32(int32(result)),
					PlayerId:   proto.Int32(int32(player_id)),
					Gameserver: proto.String(server_address),
				}

				encObj, _ := proto.Marshal(result4C)
				SendPackage(conn, 2, encObj)

				if result == global.LOGINSUCCESS { //登录成功断开连接
					conn.Close()
				}

			} else {
				fmt.Println(err)
			}

		case 3: //服务器列表
			var serverInfo_list []*protocol.Account_ServerInfo
			for k, v := range config.AllServerAddress {
				serverInfo := new(protocol.Account_ServerInfo)
				type_ := deal_4g.BusyLevel(k)
				serverInfo.Type = &type_
				serverInfo.ServerId = &k
				serverInfo.ServerAddress = &v
				serverInfo_list = append(serverInfo_list, serverInfo)
			}

			result4C := &protocol.Account_ServerListResult{
				ServerInfo: serverInfo_list,
			}
			encObj, _ := proto.Marshal(result4C)
			SendPackage(conn, 3, encObj)

		case 4: //返回自己登录过得服务器
			var serverInfo_list []*protocol.Account_ServerInfo
			server_id, servers := this.account_info.GetServers()

			for _, v := range servers {
				serverInfo := new(protocol.Account_ServerInfo)
				type_ := deal_4g.BusyLevel(v)
				serverInfo.Type = &type_
				serverInfo.ServerId = &v
				server_address, _ := config.AllServerAddress[v]
				serverInfo.ServerAddress = &server_address
				serverInfo_list = append(serverInfo_list, serverInfo)
			}

			result4C := &protocol.Account_MyServerListResult{
				LastServerId: &server_id,
				MyServerList: serverInfo_list,
			}
			encObj, _ := proto.Marshal(result4C)
			SendPackage(conn, 4, encObj)
		default:
		}
	}
}