Example #1
0
func (gkDbCon *GkDbConDef) getMaxChatId() (int32, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var maxId int32 = 0
	var err error

	stmt, err = gkDbCon.sqlDb.Prepare("select max(id) from chat_archives")
	if err != nil {
		return 0, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query()
	if err != nil {
		return 0, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	if rows.Next() {
		err = rows.Scan(&maxId)
		if err != nil {
			return 0, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
	}

	return maxId, nil
}
Example #2
0
// this assumes that the space is called xlink for
// xmlns:xlink="http://www.w3.org/1999/xlink"
func substituteOneAttributeId(idMap map[string]string, space string, name string, value string) (string, *gkerr.GkErrDef) {
	var returnValue string = value
	var ok bool
	var gkErr *gkerr.GkErrDef

	switch name {
	case "style":
		if space == "" {
			var id string

			id, gkErr = getFillIdOutOfStyle(value)
			if gkErr != nil {
				return "", gkErr
			}
			if id != "" {
				var newId string

				newId, ok = idMap[id]
				if !ok {
					gkErr = gkerr.GenGkErr("could not find id in idMap "+id, nil, ERROR_ID_INTERNAL_ID_MAP)
					return "", gkErr
				}
				returnValue = strings.Replace(returnValue, "fill:url(#"+id+")", "fill:url(#"+newId+")", 1)
			}

			id, gkErr = getFilterIdOutOfStyle(value)
			if gkErr != nil {
				return "", gkErr
			}
			if id != "" {
				var newId string

				newId, ok = idMap[id]
				if !ok {
					gkErr = gkerr.GenGkErr("could not find id in idMap "+id, nil, ERROR_ID_INTERNAL_ID_MAP)
					return "", gkErr
				}
				returnValue = strings.Replace(returnValue, "filter:url(#"+id+")", "filter:url(#"+newId+")", 1)
			}
		}
	case "href":
		if space == "xlink" {
			var id string
			var newId string

			id, gkErr = getIdOutOfHref(value)
			if gkErr != nil {
				return "", gkErr
			}
			newId, ok = idMap[id]
			if !ok {
				gkErr = gkerr.GenGkErr("could not find id in idMap "+id, nil, ERROR_ID_INTERNAL_ID_MAP)
				return "", gkErr
			}
			returnValue = strings.Replace(value, "#"+id, "#"+newId, 1)
		}
	}

	return returnValue, nil
}
Example #3
0
// no error return if row is not found
func (gkDbCon *GkDbConDef) GetContextUser(userName string) (*DbContextUserDef, bool, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var err error
	var dbContextUser *DbContextUserDef = new(DbContextUserDef)
	var rowFound bool

	stmt, err = gkDbCon.sqlDb.Prepare("select context_users.id, context_users.last_position_x, context_users.last_position_y, context_users.last_position_z, context_users.last_pod from users, context_users where users.id = context_users.id and users.user_name = $1")
	if err != nil {
		return nil, false, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query(userName)
	if err != nil {
		return nil, false, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	if rows.Next() {
		err = rows.Scan(&dbContextUser.id, &dbContextUser.LastPositionX, &dbContextUser.LastPositionY, &dbContextUser.LastPositionZ, &dbContextUser.LastPod)
		if err != nil {
			return nil, false, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
		rowFound = true
	} else {
		rowFound = false
	}

	return dbContextUser, rowFound, nil
}
Example #4
0
func (gkDbCon *GkDbConDef) getNextChatArchivesId() (int32, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var err error

	stmt, err = gkDbCon.sqlDb.Prepare("select nextval('chat_archives_seq')")
	if err != nil {
		return 0, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query()
	if err != nil {
		return 0, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	var id int32

	if rows.Next() {
		err = rows.Scan(&id)
		if err != nil {
			return 0, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
	} else {
		return 0, gkerr.GenGkErr("select users", nil, ERROR_ID_NO_ROWS_FOUND)
	}

	return id, nil
}
Example #5
0
func GetFileContents(fileName string) ([]byte, *gkerr.GkErrDef) {
	var file *os.File
	var err error
	var gkErr *gkerr.GkErrDef
	var data []byte = make([]byte, 0, 0)

	file, err = os.Open(fileName)
	if err != nil {
		gkErr = gkerr.GenGkErr("os.Open file: "+fileName, err, ERROR_ID_OPEN_FILE)
		return nil, gkErr
	}

	defer file.Close()

	buf := make([]byte, 1024, 1024)
	var readCount int

	for {
		readCount, err = file.Read(buf)
		if readCount > 0 {
			data = append(data, buf[0:readCount]...)
		}
		if err != nil {
			if err == io.EOF {
				break
			}
			gkErr = gkerr.GenGkErr("file.Read file: "+fileName, err, ERROR_ID_READ_FILE)
			return nil, gkErr
		}
	}

	return data, nil
}
Example #6
0
func (gkDbCon *GkDbConDef) GetPodsList() ([]DbPodDef, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var err error
	var dbPodsList []DbPodDef = make([]DbPodDef, 0, 0)

	stmt, err = gkDbCon.sqlDb.Prepare("select id, pod_title from pods")
	if err != nil {
		return nil, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query()
	if err != nil {
		return nil, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	for rows.Next() {
		var dbPod DbPodDef
		err = rows.Scan(&dbPod.Id, &dbPod.Title)
		if err != nil {
			return nil, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
		dbPodsList = append(dbPodsList, dbPod)
	}

	return dbPodsList, nil
}
Example #7
0
// return error if row not found
func (gkDbCon *GkDbConDef) GetUser(userName string) (*DbUserDef, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var err error
	var dbUser *DbUserDef = new(DbUserDef)

	stmt, err = gkDbCon.sqlDb.Prepare("select id, user_name, password_hash, password_salt, email, account_creation_date, last_login_date from users where user_name = $1")
	if err != nil {
		return nil, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query(userName)
	if err != nil {
		return nil, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	if rows.Next() {
		err = rows.Scan(&dbUser.id, &dbUser.UserName, &dbUser.PasswordHash, &dbUser.PasswordSalt, &dbUser.Email, &dbUser.accountCreationDate, &dbUser.lastLoginDate)
		if err != nil {
			return nil, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
	} else {
		return nil, gkerr.GenGkErr("select users", nil, ERROR_ID_NO_ROWS_FOUND)
	}

	return dbUser, nil
}
Example #8
0
func (gkDbCon *GkDbConDef) AddNewUser(userName string, passwordHash string, passwordSalt string, email string) *gkerr.GkErrDef {

	var stmt *sql.Stmt
	var err error

	var id int64
	var gkErr *gkerr.GkErrDef

	id, gkErr = gkDbCon.getNextUsersId()
	if gkErr != nil {
		return gkErr
	}

	stmt, err = gkDbCon.sqlDb.Prepare("insert into users (id, user_name, password_hash, password_salt, email, account_creation_date, last_login_date) values ($1, $2, $3, $4, $5, $6, $7)")
	if err != nil {
		return gkerr.GenGkErr("stmt.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var accountCreationDate time.Time = time.Now()
	var lastLoginDate time.Time = time.Now()

	_, err = stmt.Exec(id, userName, passwordHash, passwordSalt, email, accountCreationDate, lastLoginDate)
	if err != nil {
		if isUniqueViolation(err) {
			return gkerr.GenGkErr("stmt.Exec unique violation", err, ERROR_ID_UNIQUE_VIOLATION)
		}
		return gkerr.GenGkErr("stmt.Exec"+getDatabaseErrorMessage(err), err, ERROR_ID_EXECUTE)
	}

	return nil
}
Example #9
0
func rebuildSvg(node *nodeDef, buf io.Writer) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef
	var result []byte = make([]byte, 0, 128)
	var err error

	result = append(result, '<')
	if node.nameSpace != "" {
		result = append(result, []byte(node.nameSpace)...)
		result = append(result, ':')
	}
	result = append(result, []byte(node.nameLocal)...)

	for _, attribute := range node.attributeList {
		result = append(result, ' ')
		if attribute.nameSpace != "" {
			if attribute.nameSpace == "http://www.w3.org/XML/1998/namespace" {
				result = append(result, []byte("xml")...)
			} else {
				result = append(result, []byte(attribute.nameSpace)...)
			}
			result = append(result, ':')
		}
		result = append(result, []byte(fmt.Sprintf("%s=\"%s\"", attribute.nameLocal, escapeXML([]byte(attribute.value))))...)
	}
	result = append(result, '>')

	_, err = buf.Write(result)
	if err != nil {
		gkErr = gkerr.GenGkErr("write fix svg results", err, ERROR_ID_WRITE_SVG)
		return gkErr
	}
	for _, childNode := range node.childList {
		gkErr = rebuildSvg(childNode, buf)

	}
	result = make([]byte, 0, 16)
	if !isCharDataAllWhiteSpace(node.charData) {
		result = append(result, node.charData...)
	}
	result = append(result, []byte("</")...)

	if node.nameSpace != "" {
		result = append(result, []byte(node.nameSpace)...)
		result = append(result, ':')
	}
	result = append(result, []byte(node.nameLocal)...)
	result = append(result, '>')

	_, err = buf.Write(result)
	if err != nil {
		gkErr = gkerr.GenGkErr("write fix svg results", err, ERROR_ID_WRITE_SVG)
		return gkErr
	}

	return nil
}
Example #10
0
func SendEmail(address string, from string, toArray []string, subject string, message []byte) (string, *gkerr.GkErrDef) {

	var err error
	var conn *smtp.Client
	var dataWriter io.WriteCloser
	var gkErr *gkerr.GkErrDef
	var sendId string

	conn, err = smtp.Dial(address)
	if err != nil {
		gkErr = gkerr.GenGkErr("smtp.Dial", err, ERROR_ID_SMTP_DIAL)
		return "", gkErr
	}

	conn.Mail(from)
	for _, adr := range toArray {
		conn.Rcpt(adr)
	}

	dataWriter, err = conn.Data()
	if err != nil {
		gkErr = gkerr.GenGkErr("conn.Data", err, ERROR_ID_SMTP_DATA)
		return "", gkErr
	}

	defer dataWriter.Close()

	buf := bytes.NewBufferString("From: " + from + "\nTo: " + toArray[0] + "\nSubject: " + subject + "\n")
	_, err = buf.WriteTo(dataWriter)
	if err != nil {
		gkErr = gkerr.GenGkErr("buf.WriteTo", err, ERROR_ID_SMTP_WRITE)
		return "", gkErr
	}

	buf = bytes.NewBufferString(string(message))
	_, err = buf.WriteTo(dataWriter)
	if err != nil {
		gkErr = gkerr.GenGkErr("buf.WriteTo", err, ERROR_ID_SMTP_WRITE)
		return "", gkErr
	}

	err = conn.Quit()
	if err != nil {
		localError := fmt.Sprintf("%v", err)
		if strings.Index(localError, "Ok") != -1 &&
			strings.Index(localError, "queued as") != -1 {
			sendId = localError
		} else {
			gkErr = gkerr.GenGkErr("conn.Quit", err, ERROR_ID_SMTP_QUIT)
			//gklog.LogTrace(fmt.Sprintf("smtp quit %T [%v] %v",err, err, gkErr))
			return "", gkErr
		}
	}

	return sendId, nil
}
Example #11
0
func NewTemplate(templateDir string, templateName string) (*TemplateDef, *gkerr.GkErrDef) {
	var gkTemplate *TemplateDef = new(TemplateDef)

	gkTemplate.tmpl = template.New(templateName)

	var file *os.File
	var templateListFileName string
	var err error

	templateListFileName = templateDir + string(os.PathSeparator) + templateName + ".txt"
	file, err = os.Open(templateListFileName)
	if err != nil {
		return nil, gkerr.GenGkErr("os.Open", err, ERROR_ID_OPEN_TEMPLATE_LIST)
	}

	defer file.Close()

	var br *bufio.Reader

	localFileNames := make([]string, 0, 0)

	br = bufio.NewReader(file)
	for {
		var line string

		line, err = br.ReadString('\n')

		line = strings.Trim(line, "\r\n\t ")

		if line != "" {
			localFileNames = append(localFileNames, templateDir+string(os.PathSeparator)+line)
		}

		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, gkerr.GenGkErr("br.ReadString", err, ERROR_ID_READ_TEMPLATE_LIST)
		}
	}

	//	localFileNames = make([]string, len(fileNames), len(fileNames))
	//	for i := 0; i < len(fileNames); i++ {
	//		localFileNames[i] = templateDir + string(os.PathSeparator) + fileNames[i] + ".html"
	//	}

	gklog.LogTrace(fmt.Sprintf("localFileNames: %+v", localFileNames))

	_, err = gkTemplate.tmpl.ParseFiles(localFileNames...)
	if err != nil {
		return nil, gkerr.GenGkErr("tmpl.ParseFiles", err, ERROR_ID_PARSE_FILES)
	}

	return gkTemplate, nil
}
Example #12
0
// return error if row not found
// this could be improved by saving recent "id" values
func (gkDbCon *GkDbConDef) GetLastChatArchiveEntries(count int) ([]LugChatArchiveDef, *gkerr.GkErrDef) {
	var stmt *sql.Stmt
	var err error
	var results []LugChatArchiveDef = make([]LugChatArchiveDef, 0, count)
	var gkErr *gkerr.GkErrDef

	var maxId int32
	var startId int32 = 0
	maxId, gkErr = gkDbCon.getMaxChatId()
	if gkErr != nil {
		return nil, gkErr
	}

	stmt, err = gkDbCon.sqlDb.Prepare("select chat_archives.message_creation_date, chat_archives.chat_message, users.user_name from users, chat_archives where users.id = chat_archives.user_id and chat_archives.id > $1 order by chat_archives.message_creation_date desc")
	if err != nil {
		return nil, gkerr.GenGkErr("sql.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	if maxId > 0 {
		startId = maxId - (int32(count) + 20)
	}
	rows, err = stmt.Query(startId)
	if err != nil {
		return nil, gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	for rows.Next() {
		var lugChatArchive LugChatArchiveDef

		err = rows.Scan(&lugChatArchive.MessageCreationDate, &lugChatArchive.ChatMessage, &lugChatArchive.UserName)
		if err != nil {
			return nil, gkerr.GenGkErr("rows.Scan"+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
		results = append(results, lugChatArchive)

		if len(results) >= count {
			break
		}
	}

	return results, nil
}
Example #13
0
func NewGkRandContext() *GkRandContextDef {
	var gkRandContext *GkRandContextDef = new(GkRandContextDef)
	var seed int64
	var err error
	var gkErr *gkerr.GkErrDef

	seed = time.Now().UnixNano()
	buf := make([]byte, 6, 6)
	_, err = c_rand.Read(buf)
	if err != nil {
		// log the error
		// but this error is not fatal
		gkErr = gkerr.GenGkErr("c_rand.Read", err, ERROR_ID_RAND_READ)
		gklog.LogGkErr("", gkErr)
	}

	seed ^= int64(buf[0]) << 16
	seed ^= int64(buf[1]) << 24
	seed ^= int64(buf[2]) << 32
	seed ^= int64(buf[3]) << 40
	seed ^= int64(buf[4]) << 48
	seed ^= int64(buf[5]) << 56

	gkRandContext.mRandContext = m_rand.New(m_rand.NewSource(seed))

	return gkRandContext
}
Example #14
0
func (fieldContext *FieldContextDef) queueMessageToClient(sessionId string, messageToClient *message.MessageToClientDef) {

	var websocketConnectionContext *websocketConnectionContextDef
	var gkErr *gkerr.GkErrDef

	gklog.LogTrace("queu up message " + messageToClient.Command)

	websocketConnectionContext, gkErr =
		fieldContext.getWebsocketConnectionContextById(sessionId)

	if gkErr != nil {
		gklog.LogGkErr("", gkErr)
	} else {
		var localSize int

		websocketConnectionContext.toClientQueue.mutex.Lock()
		localSize = websocketConnectionContext.toClientQueue.queueSize
		websocketConnectionContext.toClientQueue.mutex.Unlock()

		if localSize > MAX_MESSAGES_TO_CLIENT_QUEUE {
			gkErr = gkerr.GenGkErr("messageToClient queue overflow, dropping message", nil, ERROR_ID_MESSAGE_TO_CLIENT_QUEUE_OVERFLOW)
			gklog.LogGkErr("", gkErr)
		} else {
			websocketConnectionContext.toClientQueue.mutex.Lock()
			websocketConnectionContext.toClientQueue.queueSize += 1
			websocketConnectionContext.toClientQueue.mutex.Unlock()
			websocketConnectionContext.toClientQueue.messagesChan <- messageToClient
		}
	}
}
Example #15
0
func (messageToClient *MessageToClientDef) BuildSvgMessageToClient(svgDir string, command string, fileName string, svgJsonData *SvgJsonDataDef) *gkerr.GkErrDef {
	var gkErr *gkerr.GkErrDef

	if !validateSvgFileName(fileName) {
		gkErr = gkerr.GenGkErr("invalid svg fileName", nil, ERROR_ID_INVALID_SVG_FILENAME)
		return gkErr
	}

	messageToClient.Command = command

	jsonFileName := svgDir + string(os.PathSeparator) + fileName + ".json"
	svgFileName := svgDir + string(os.PathSeparator) + fileName + ".svg"

	messageToClient.JsonData, gkErr = gkcommon.GetFileContents(jsonFileName)
	if gkErr != nil {
		return gkErr
	}
	messageToClient.Data, gkErr = gkcommon.GetFileContents(svgFileName)
	if gkErr != nil {
		return gkErr
	}
	messageToClient.Data, gkErr = gksvg.FixSvgData(messageToClient.Data, fileName)
	if gkErr != nil {
		return gkErr
	}

	if svgJsonData != nil {
		messageToClient.JsonData, gkErr = templateTranslateJsonData(messageToClient.JsonData, svgJsonData)
		if gkErr != nil {
			return gkErr
		}
	}

	return nil
}
Example #16
0
func (gkTemplate *TemplateDef) GetBytes() ([]byte, *gkerr.GkErrDef) {
	if gkTemplate.dataBuffer == nil {
		return nil, gkerr.GenGkErr("missing call to Build", nil, ERROR_ID_MISSING_BUILD)
	}

	return gkTemplate.dataBuffer.Bytes(), nil
}
Example #17
0
func loadConfigFile(fileName string) (loginConfigDef, *gkerr.GkErrDef) {
	var err error
	var loginConfig loginConfigDef

	var file *os.File
	file, err = os.Open(fileName)
	if err != nil {
		return loginConfig, gkerr.GenGkErr(fmt.Sprintf("os.Open file: %s", fileName), err, ERROR_ID_OPEN_CONFIG)
	}
	defer file.Close()

	err = xml.NewDecoder(file).Decode(&loginConfig)
	if err != nil {
		return loginConfig, gkerr.GenGkErr(fmt.Sprintf("xml.NewDecoder file: %s", fileName), err, ERROR_ID_DECODE_CONFIG)
	}

	return loginConfig, nil
}
func (fieldContext *FieldContextDef) handleMessageFromClient(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef

	switch messageFromClient.Command {
	case message.GetAvatarSvgReq:
		gkErr = fieldContext.handleGetAvatarSvgReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.DelAvatarSvgReq:
		gkErr = fieldContext.handleDelAvatarSvgReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.MoveAvatarSvgReq:
		gkErr = fieldContext.handleMoveAvatarSvgReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.SetAvatarSvgReq:
		gkErr = fieldContext.handleSetAvatarSvgReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.PingReq:
		gkErr = fieldContext.handlePingReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.ChatReq:
		gkErr = fieldContext.handleChatReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.NewPodReq:
		gkErr = fieldContext.handleNewPodReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.UserPrefSaveReq:
		gkErr = fieldContext.handleUserPrefReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	case message.SaveTerrainEditReq:
		gkErr = fieldContext.handleSaveTerrainEditReq(messageFromClient)
		if gkErr != nil {
			return gkErr
		}
	default:
		gkErr = gkerr.GenGkErr("unknonwn websocket request: "+messageFromClient.Command, nil, ERROR_ID_UNKNOWN_WEBSOCKET_COMMAND)
		return gkErr
	}

	return nil
}
Example #19
0
func (gkDbCon *GkDbConDef) UpdateUserPref(id int32, prefName string, prefValue string) *gkerr.GkErrDef {

	var stmt *sql.Stmt
	var err error

	stmt, err = gkDbCon.sqlDb.Prepare("update user_prefs set pref_value = $1 where user_id = $2 and pref_name = $3")
	if err != nil {
		return gkerr.GenGkErr("stmt.Prepare "+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	_, err = stmt.Exec(prefValue, id, prefName)
	if err != nil {
		return gkerr.GenGkErr("stmt.Exec "+getDatabaseErrorMessage(err), err, ERROR_ID_EXECUTE)
	}

	return nil
}
Example #20
0
func (gkDbCon *GkDbConDef) InsertUserPref(id int32, prefName string, prefValue string) *gkerr.GkErrDef {

	var stmt *sql.Stmt
	var err error

	stmt, err = gkDbCon.sqlDb.Prepare("insert into user_prefs (user_id, pref_value, pref_name) values ($1, $2, $3)")
	if err != nil {
		return gkerr.GenGkErr("stmt.Prepare "+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	_, err = stmt.Exec(id, prefValue, prefName)
	if err != nil {
		return gkerr.GenGkErr("stmt.Exec "+getDatabaseErrorMessage(err), err, ERROR_ID_EXECUTE)
	}

	return nil
}
Example #21
0
func (fieldContext *FieldContextDef) handleGetAvatarSvgReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	var getSvg getSvgDef
	var gkErr *gkerr.GkErrDef
	var err error

	err = json.Unmarshal(messageFromClient.JsonData, &getSvg)
	if err != nil {
		gkErr = gkerr.GenGkErr("json.Unmarshal", err, ERROR_ID_JSON_UNMARSHAL)
		return gkErr
	}

	var randomByte byte
	randomByte = fieldContext.sessionContext.RandContext.GetRandomByte()
	var svgJsonData *message.SvgJsonDataDef = new(message.SvgJsonDataDef)
	svgJsonData.Id = fieldContext.getNextObjectId()
	svgJsonData.IsoXYZ.X = int16((randomByte&0x07)*3) - 10
	svgJsonData.IsoXYZ.Y = int16(((randomByte>>4)&0x07)*3) - 10

	gkErr = messageToClient.BuildSvgMessageToClient(fieldContext.avatarSvgDir, message.GetAvatarSvgRes, getSvg.SvgName, svgJsonData)
	if gkErr != nil {
		return gkErr
	}

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(messageFromClient.SessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	fieldContext.queueMessageToClient(messageFromClient.SessionId, messageToClient)

	var fieldObject *fieldObjectDef = new(fieldObjectDef)
	fieldObject.id = svgJsonData.Id
	fieldObject.fileName = getSvg.SvgName
	fieldObject.isoXYZ = svgJsonData.IsoXYZ
	fieldObject.sourceSessionId = messageFromClient.SessionId
	fieldContext.addAvatarObject(podId, fieldObject)

	var websocketConnectionContext *websocketConnectionContextDef

	websocketConnectionContext, gkErr = fieldContext.getWebsocketConnectionContextById(messageFromClient.SessionId)

	if gkErr != nil {
		return gkErr
	}

	websocketConnectionContext.avatarId = fieldObject.id

	gkErr = fieldContext.sendNewAvatarToAll(podId, messageFromClient.SessionId, fieldObject.id)
	if gkErr != nil {
		return gkErr
	}

	return nil
}
Example #22
0
func (gkDbCon *GkDbConDef) SetUserPref(userName string, prefName string, prefValue string) *gkerr.GkErrDef {
	var dbUser *DbUserDef
	var gkErr *gkerr.GkErrDef

	dbUser, gkErr = gkDbCon.GetUser(userName)
	if gkErr != nil {
		return gkErr
	}

	var stmt *sql.Stmt
	var err error

	stmt, err = gkDbCon.sqlDb.Prepare("select pref_value from user_prefs where user_id = $1 and pref_name = $2")
	if err != nil {
		return gkerr.GenGkErr("stmt.Prepare "+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query(dbUser.id, prefName)
	if err != nil {
		return gkerr.GenGkErr("stmt.Query"+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	if rows.Next() {
		gkErr = gkDbCon.UpdateUserPref(dbUser.id, prefName, prefValue)
		if gkErr != nil {
			return gkErr
		}
	} else {
		gkErr = gkDbCon.InsertUserPref(dbUser.id, prefName, prefValue)
		if gkErr != nil {
			return gkErr
		}
	}

	return nil
}
Example #23
0
func getIdOutOfHref(input string) (string, *gkerr.GkErrDef) {
	if len(input) > 2 {
		if input[0] == '#' {
			var id string

			id = input[1:]
			return id, nil
		}
	}
	return "", gkerr.GenGkErr("href id parse: "+input, nil, ERROR_ID_HREF_PARSE)
}
Example #24
0
func (gkDbCon *GkDbConDef) ChangePassword(userName string, passwordHash string, passwordSalt string) *gkerr.GkErrDef {

	var stmt *sql.Stmt
	var err error

	//var gkErr *gkerr.GkErrDef

	stmt, err = gkDbCon.sqlDb.Prepare("update users set password_hash = $1, password_salt = $2 where user_name = $3")
	if err != nil {
		return gkerr.GenGkErr("stmt.Prepare"+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	_, err = stmt.Exec(passwordHash, passwordSalt, userName)
	if err != nil {
		return gkerr.GenGkErr("stmt.Exec"+getDatabaseErrorMessage(err), err, ERROR_ID_EXECUTE)
	}

	return nil
}
Example #25
0
func (gkTemplate *TemplateDef) Send(res http.ResponseWriter, req *http.Request) *gkerr.GkErrDef {

	var writeCount int
	var err error

	if gkTemplate.dataBuffer == nil {
		return gkerr.GenGkErr("missing call to Build", nil, ERROR_ID_MISSING_BUILD)
	}

	writeCount, err = res.Write(gkTemplate.dataBuffer.Bytes())
	if err != nil {
		return gkerr.GenGkErr("res.Write", err, ERROR_ID_TEMPLATE_WRITE)
	}
	if writeCount != gkTemplate.dataBuffer.Len() {
		return gkerr.GenGkErr("write count short", nil, ERROR_ID_SHORT_WRITE_COUNT)
	}

	gkTemplate.dataBuffer = nil

	return nil
}
Example #26
0
func (gkDbCon *GkDbConDef) GetUserPrefsList(userName string) ([]DbUserPrefDef, *gkerr.GkErrDef) {
	var dbUser *DbUserDef
	var gkErr *gkerr.GkErrDef

	var stmt *sql.Stmt
	var err error
	var dbUserPrefsList []DbUserPrefDef = make([]DbUserPrefDef, 0, 0)

	dbUser, gkErr = gkDbCon.GetUser(userName)
	if gkErr != nil {
		return nil, gkErr
	}

	stmt, err = gkDbCon.sqlDb.Prepare("select user_id, pref_name, pref_value from user_prefs where user_id = $1")
	if err != nil {
		return nil, gkerr.GenGkErr("sql.Prepare "+getDatabaseErrorMessage(err), err, ERROR_ID_PREPARE)
	}

	defer stmt.Close()

	var rows *sql.Rows

	rows, err = stmt.Query(dbUser.id)
	if err != nil {
		return nil, gkerr.GenGkErr("stmt.Query "+getDatabaseErrorMessage(err), err, ERROR_ID_QUERY)
	}

	defer rows.Close()

	for rows.Next() {
		var dbUserPref DbUserPrefDef
		err = rows.Scan(&dbUserPref.Id, &dbUserPref.PrefName, &dbUserPref.PrefValue)
		if err != nil {
			return nil, gkerr.GenGkErr("rows.Scan "+getDatabaseErrorMessage(err), err, ERROR_ID_ROWS_SCAN)
		}
		dbUserPrefsList = append(dbUserPrefsList, dbUserPref)
	}

	return dbUserPrefsList, nil
}
Example #27
0
func getSingleTerrainMap(jsonFileName string) (*terrainJsonDef, *gkerr.GkErrDef) {
	var terrainJson *terrainJsonDef = new(terrainJsonDef)
	var jsonFile *os.File
	var err error
	var jsonData []byte = make([]byte, 0, 256)
	var gkErr *gkerr.GkErrDef

	jsonFile, err = os.Open(jsonFileName)
	if err != nil {
		gkErr = gkerr.GenGkErr("could not open: "+jsonFileName, err, ERROR_ID_OPEN_TERRAIN_MAP)
		return nil, gkErr
	}
	defer jsonFile.Close()

	var buf []byte
	var readCount int
	for {
		buf = make([]byte, 128, 128)
		readCount, err = jsonFile.Read(buf)
		if readCount > 0 {
			jsonData = append(jsonData, buf[:readCount]...)
		}
		if err != nil {
			if err == io.EOF {
				break
			}
			gkErr = gkerr.GenGkErr("could not read "+jsonFileName, err, ERROR_ID_READ_TERRAIN_MAP)
			return nil, gkErr
		}
	}

	err = json.Unmarshal(jsonData, &terrainJson.jsonMapData)
	if err != nil {
		gkErr = gkerr.GenGkErr("json.Unmarshal", err, ERROR_ID_JSON_UNMARSHAL)
		return nil, gkErr
	}

	return terrainJson, nil
}
Example #28
0
func (messageFromClient *MessageFromClientDef) PopulateFromMessage(sessionId string, message []byte) *gkerr.GkErrDef {
	var index1, index2 int

	index1 = bytes.IndexByte(message, '~')
	if index1 == -1 {
		return gkerr.GenGkErr("missing ~ from websocket message", nil, ERROR_ID_UNKNOWN_WEBSOCKET_INPUT)
	}

	index2 = bytes.IndexByte(message[index1+1:], '~')
	if index2 == -1 {
		return gkerr.GenGkErr("missing second ~ from websocketMessage", nil, ERROR_ID_UNKNOWN_WEBSOCKET_INPUT)
	}

	index2 += index1 + 1

	messageFromClient.SessionId = sessionId
	messageFromClient.Command = string(message[0:index1])
	messageFromClient.JsonData = message[index1+1 : index2]
	messageFromClient.data = message[index2+1:]

	return nil
}
Example #29
0
func (gkTemplate *TemplateDef) Build(buildData interface{}) *gkerr.GkErrDef {
	gkTemplate.dataBuffer = bytes.NewBuffer(make([]byte, 0, 0))
	var err error

	gklog.LogTrace(fmt.Sprintf("buildData: %+v", buildData))

	err = gkTemplate.tmpl.ExecuteTemplate(gkTemplate.dataBuffer, "main", buildData)
	if err != nil {
		return gkerr.GenGkErr("tmpl.ExecuteTemplate", err, ERROR_ID_EXECUTE_TEMPLATE)
	}

	return nil
}
Example #30
0
func (fieldContext *FieldContextDef) handleSetAvatarSvgReq(messageFromClient *message.MessageFromClientDef) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef
	var err error
	var setSvg setSvgDef

	gklog.LogTrace("json raw: " + string(messageFromClient.JsonData))

	err = json.Unmarshal(messageFromClient.JsonData, &setSvg)
	if err != nil {
		gkErr = gkerr.GenGkErr("json.Unmarshal", err, ERROR_ID_JSON_UNMARSHAL)
		return gkErr
	}

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(messageFromClient.SessionId)
	var podId int32 = singleSession.GetCurrentPodId()

	var fieldObject *fieldObjectDef
	var ok bool
	fieldObject, ok = fieldContext.podMap[podId].avatarMap[setSvg.Id]
	if ok {
		var cord int
		cord, _ = strconv.Atoi(setSvg.X)
		fieldObject.isoXYZ.X = int16(cord)
		cord, _ = strconv.Atoi(setSvg.Y)
		fieldObject.isoXYZ.Y = int16(cord)
		cord, _ = strconv.Atoi(setSvg.Z)
		fieldObject.isoXYZ.Z = int16(cord)

		gklog.LogTrace("one")
		fieldContext.setAllAvatars(messageFromClient.SessionId, fieldObject)
	} else {
		gkErr = gkerr.GenGkErr("move object", nil, ERROR_ID_COULD_NOT_FIND_OBJECT_TO_MOVE)
		gklog.LogGkErr("", gkErr)
	}

	return nil
}