Commit 2df977f2408d7f0ec6a5a268a00bc2d4ec65dc49

Authored by 陆恒
1 parent b4a751ed
Exists in ver2.3.0

提交从数据库读取数据

src/HttpServer/logic/logic.go
@@ -7,6 +7,7 @@ import ( @@ -7,6 +7,7 @@ import (
7 "common/redis" 7 "common/redis"
8 "encoding/json" 8 "encoding/json"
9 "fmt" 9 "fmt"
  10 + "mysql"
10 "net/http" 11 "net/http"
11 "strconv" 12 "strconv"
12 "strings" 13 "strings"
@@ -1347,37 +1348,28 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) { @@ -1347,37 +1348,28 @@ func HandlerLogin(w http.ResponseWriter, data string, uuid int) {
1347 //不存在 1348 //不存在
1348 //属于新登录的玩家数据 1349 //属于新登录的玩家数据
1349 //需要判断是否存在老的版本的数据 1350 //需要判断是否存在老的版本的数据
1350 - err = InitUserInfo(&rdata, &resp, uuid, uniqueuuid)  
1351 - /*isexist, err = redishandler.GetRedisClient().HExists(redis.USER_DATA_KEY, strconv.Itoa(uuid))  
1352 - if err == nil && isexist {  
1353 - logger.Info("HandlerLogin111111111111")  
1354 - //如果存在老的数据 做数据迁移  
1355 - oldstr, err := redishandler.GetRedisClient().HGet(redis.USER_DATA_KEY, strconv.Itoa(uuid))  
1356 - if err != nil {  
1357 - logger.Error("HandlerLogin redis failed err=%v,uuid=%v", err, uuid)  
1358 - resp.Message = "服务器错误"  
1359 - resp.Code = ERROR_SRV_ERROR  
1360 - break  
1361 - }  
1362 - err = redishandler.GetRedisClient().HSet(redis.USER_DATA_KEY, uniqueuuid, oldstr)  
1363 - if err != nil {  
1364 - logger.Error("HandlerLogin redis failed err=%v,uuid=%v", err, uuid)  
1365 - resp.Message = "服务器错误"  
1366 - resp.Code = ERROR_SRV_ERROR  
1367 - break  
1368 - }  
1369 - logger.Info("HandlerLogin uuid=%v do set oldstr=%v", uuid, oldstr) 1351 + //err = InitUserInfo(&rdata, &resp, uuid, uniqueuuid)
  1352 + err, info, task, ach, isfind := mysql.GetFromBackUp(uniqueuuid)
  1353 + if err != nil {
  1354 + /*logger.Error("mysql failed err=%v", err)
  1355 + resp.Message = "服务器错误"
  1356 + resp.Code = ERROR_SRV_ERROR
  1357 + break*/
  1358 + logger.Error("mysql failed err=%v", err)
  1359 + //err = InitUserInfo(&rdata, &resp, uuid, uniqueuuid)
  1360 + }
  1361 +
  1362 + if isfind {
  1363 + logger.Info("GetUserInfo from mysql uid=%v", uniqueuuid)
  1364 + //先保存数据
  1365 + err = redishandler.GetRedisClient().HSet(redis.USER_DATA_KEY, uniqueuuid, info)
  1366 + err = redishandler.GetRedisClient().HSet(redis.USER_TASKINFO_LIST, uniqueuuid, task)
  1367 + err = redishandler.GetRedisClient().HSet(redis.USER_ACHIEVEMENTINFO_LIST, uniqueuuid, ach)
1370 err = GetUserData(uuid, uniqueuuid, &rdata, &resp) 1368 err = GetUserData(uuid, uniqueuuid, &rdata, &resp)
1371 - if err == nil {  
1372 - //删除老的数据  
1373 - redishandler.GetRedisClient().HDel(redis.USER_DATA_KEY, strconv.Itoa(uuid))  
1374 - logger.Info("HandlerLogin uuid=%v do set", uuid)  
1375 - }  
1376 } else { 1369 } else {
1377 - logger.Info("HandlerLogin22222222222222222")  
1378 //不存在老的数据 之间走新号流程 1370 //不存在老的数据 之间走新号流程
1379 err = InitUserInfo(&rdata, &resp, uuid, uniqueuuid) 1371 err = InitUserInfo(&rdata, &resp, uuid, uniqueuuid)
1380 - }*/ 1372 + }
1381 1373
1382 } else { 1374 } else {
1383 //已经迁移过的数据之间返回 1375 //已经迁移过的数据之间返回
src/mysql/dbmysql.go
@@ -12,7 +12,7 @@ import ( @@ -12,7 +12,7 @@ import (
12 ) 12 )
13 13
14 var ( 14 var (
15 - m_game_db *sql.DB 15 + m_game_db *sql.DB
16 ) 16 )
17 17
18 func InitMysql() error { 18 func InitMysql() error {
@@ -46,7 +46,7 @@ func ExcuteCmd(cmd string) error { @@ -46,7 +46,7 @@ func ExcuteCmd(cmd string) error {
46 return err 46 return err
47 } 47 }
48 48
49 -func TestClearData(uid int) (int,error) { 49 +func TestClearData(uid int) (int, error) {
50 cmd := fmt.Sprintf("select coin from b_user_ext_04 where user_id=%d", uid) 50 cmd := fmt.Sprintf("select coin from b_user_ext_04 where user_id=%d", uid)
51 51
52 rows, err := m_game_db.Query(cmd) 52 rows, err := m_game_db.Query(cmd)
@@ -55,33 +55,32 @@ func TestClearData(uid int) (int,error) { @@ -55,33 +55,32 @@ func TestClearData(uid int) (int,error) {
55 } 55 }
56 if err != nil { 56 if err != nil {
57 logger.Notic("TestClearData mysql select error:%v", err) 57 logger.Notic("TestClearData mysql select error:%v", err)
58 - return 0,err 58 + return 0, err
59 } 59 }
60 60
61 -  
62 var value int 61 var value int
63 for rows.Next() { 62 for rows.Next() {
64 63
65 err = rows.Scan(&value) 64 err = rows.Scan(&value)
66 if err != nil { 65 if err != nil {
67 logger.Notic("TestClearData mysql handle result error:%v", err) 66 logger.Notic("TestClearData mysql handle result error:%v", err)
68 - return 0,err 67 + return 0, err
69 } 68 }
70 - logger.Info("TestClearData coin=%v",value) 69 + logger.Info("TestClearData coin=%v", value)
71 70
72 break 71 break
73 } 72 }
74 - return value,nil 73 + return value, nil
75 } 74 }
76 75
77 -func DoClearData(uuid int) error{ 76 +func DoClearData(uuid int) error {
78 //先删除十张分表的数据 77 //先删除十张分表的数据
79 - for i:=0;i<10;i++ { 78 + for i := 0; i < 10; i++ {
80 tablename := "b_base_data_0" + strconv.Itoa(i) 79 tablename := "b_base_data_0" + strconv.Itoa(i)
81 cmd := "DELETE from " + tablename + " where data_uid= " + strconv.Itoa(uuid) 80 cmd := "DELETE from " + tablename + " where data_uid= " + strconv.Itoa(uuid)
82 err := ExcuteCmd(cmd) 81 err := ExcuteCmd(cmd)
83 if err != nil { 82 if err != nil {
84 - logger.Error("DoClearData err=%v,cmd=%v",err,cmd) 83 + logger.Error("DoClearData err=%v,cmd=%v", err, cmd)
85 return err 84 return err
86 } 85 }
87 } 86 }
@@ -89,12 +88,10 @@ func DoClearData(uuid int) error{ @@ -89,12 +88,10 @@ func DoClearData(uuid int) error{
89 88
90 } 89 }
91 90
92 -  
93 -  
94 -func QueryAllData(f *os.File) error{ 91 +func QueryAllData(f *os.File) error {
95 92
96 //先查询十张分表的数据 93 //先查询十张分表的数据
97 - for i:=0;i<10;i++ { 94 + for i := 0; i < 10; i++ {
98 tablename := "b_user_ext_0" + strconv.Itoa(i) 95 tablename := "b_user_ext_0" + strconv.Itoa(i)
99 cmd := "SELECT user_id,lv,exp,login_time from " + tablename 96 cmd := "SELECT user_id,lv,exp,login_time from " + tablename
100 rows, err := m_game_db.Query(cmd) 97 rows, err := m_game_db.Query(cmd)
@@ -112,7 +109,7 @@ func QueryAllData(f *os.File) error{ @@ -112,7 +109,7 @@ func QueryAllData(f *os.File) error{
112 Ulv := 0 109 Ulv := 0
113 Exp := 0 110 Exp := 0
114 LoginTime := 0 111 LoginTime := 0
115 - err = rows.Scan(&Uid, &Ulv, &Exp,&LoginTime) //不scan会导致连接不释放 112 + err = rows.Scan(&Uid, &Ulv, &Exp, &LoginTime) //不scan会导致连接不释放
116 if err != nil { 113 if err != nil {
117 logger.Error("Scan failed,err:%v", err) 114 logger.Error("Scan failed,err:%v", err)
118 return err 115 return err
@@ -126,10 +123,10 @@ func QueryAllData(f *os.File) error{ @@ -126,10 +123,10 @@ func QueryAllData(f *os.File) error{
126 tm := time.Unix(int64(LoginTime), 0) 123 tm := time.Unix(int64(LoginTime), 0)
127 //fmt.Println(tm.Format("2006-01-02 15:04:05")) 124 //fmt.Println(tm.Format("2006-01-02 15:04:05"))
128 str := strconv.Itoa(Uid) + " " + strconv.Itoa(Ulv) + " " + strconv.Itoa(Exp) + " " + tm.Format("2006-01-02 15:04:05") + "\n" 125 str := strconv.Itoa(Uid) + " " + strconv.Itoa(Ulv) + " " + strconv.Itoa(Exp) + " " + tm.Format("2006-01-02 15:04:05") + "\n"
129 - _,err = f.WriteString(str) 126 + _, err = f.WriteString(str)
130 //logger.Info("ddt str=%v",str) 127 //logger.Info("ddt str=%v",str)
131 if err != nil { 128 if err != nil {
132 - logger.Error("WRITETOFILEFAILED ERR=%v",err) 129 + logger.Error("WRITETOFILEFAILED ERR=%v", err)
133 return err 130 return err
134 } 131 }
135 //fmt.Fprintln(w, str) 132 //fmt.Fprintln(w, str)
@@ -138,4 +135,33 @@ func QueryAllData(f *os.File) error{ @@ -138,4 +135,33 @@ func QueryAllData(f *os.File) error{
138 } 135 }
139 return nil 136 return nil
140 137
141 -}  
142 \ No newline at end of file 138 \ No newline at end of file
  139 +}
  140 +
  141 +func GetFromBackUp(uniqueid string) (error, string, string, string, bool) {
  142 + isfind := false
  143 + cmd := "select info,task,achieve from starstar where uid = '" + uniqueid + "'"
  144 + rows, err := m_game_db.Query(cmd)
  145 + if rows != nil {
  146 + defer rows.Close()
  147 + }
  148 + if err != nil {
  149 + logger.Notic("TestClearData mysql select error:%v", err)
  150 + return err, "", "", "", isfind
  151 + }
  152 + var info, task, ach string
  153 + for rows.Next() {
  154 +
  155 + err = rows.Scan(&info, &task, &ach)
  156 + if err != nil {
  157 + logger.Notic("GetFromBackUp mysql handle result error:%v", err)
  158 + return err, "", "", "", isfind
  159 + }
  160 +
  161 + isfind = true
  162 + logger.Info("GetFromBackUp uniqueid=%v", uniqueid)
  163 +
  164 + break
  165 + }
  166 +
  167 + return nil, info, task, ach, isfind
  168 +}