Exemple #1
0
func loginAction(login, pass string) string {
	result := map[string]interface{}{"result": "invalidCredentials"}
	if isExistUser(login, pass) {
		findSIDAndLogOut(login)
		db := connect.CreateConnect()
		sid := utils.GenerateSID()
		stmt, _ := db.Prepare("CALL add_user_session(?, ?)")
		defer stmt.Close()
		_, err := stmt.Exec(login, sid)
		if err == nil {
			host, _ := os.Hostname()
			//
			result["action"] = "login"
			//
			result["sid"] = sid
			result["result"] = "ok"
			result["webSocket"] = "ws://" + host + consts.SERVER_PORT + "/websocket"
			// result["webSocket"] = "ws://" + "192.168.173.230" + consts.SERVER_PORT + "/websocket"
			p := engine.GetInstance().CreatePlayer(sid)
			result["id"] = p.GetID()
			result["fistId"] = p.GetFistID()
		}
	}
	resJSON, _ := json.Marshal(result)
	return string(resJSON)
}
Exemple #2
0
func findSIDAndLogOut(login string) {
	var sid string
	db := connect.CreateConnect()
	stmt, _ := db.Prepare(`
        SELECT s.sid FROM sessions s
        INNER JOIN users u ON s.user_id = u.id
        WHERE u.login = ?
    `)
	defer stmt.Close()
	err := stmt.QueryRow(login).Scan(&sid)
	if err != sql.ErrNoRows {
		engine.GetInstance().LogoutPlayer(sid)
	}
}
Exemple #3
0
func logoutAction(u4 string) string {
	result := map[string]string{"result": "ok"}
	db := connect.CreateConnect()
	stmt, _ := db.Prepare("DELETE FROM sessions WHERE sid = ?")
	defer stmt.Close()
	res, _ := stmt.Exec(u4)
	if amount, _ := res.RowsAffected(); amount != 1 {
		result["result"] = "badSid"
	} else {
		engine.GetInstance().LogoutPlayer(u4)
	}
	resJSON, _ := json.Marshal(result)
	return string(resJSON)
}