// lit un compte en base. Renvoie nil si le compte n'existe pas en base. // Sinon l'appelant est responsable de l'ouverture et de la fermeture de la connexion qu'il fournit func (store *MysqlStore) GetCompte(db *mysql.Client, trollId uint) (c *Compte, err os.Error) { if trollId == 0 { fmt.Println("GetCompte> trollId invalide") return } sql := "select statut, mdp_restreint, pv_max, pv_actuels, x, y, z, fatigue, pa, vue, prochain_tour, duree_tour, mise_a_jour" sql += " from compte where id=" + strconv.Uitoa(trollId) err = db.Query(sql) if err != nil { return } result, err := db.UseResult() if err != nil { return } defer result.Free() row := result.FetchRow() if row == nil { return } c = rowToCompte(trollId, row) return }
// un résultat sans auteur (0) ni dateCdm (valeur 0) signifie qu'on n'a pas la réponse à la question func (store *MysqlStore) GetBlessure(db *mysql.Client, numMonstre uint, trollId int, amis []int) (blessure uint, auteurCDM int, dateCDM int64, err os.Error) { sql := "select blessure, author, date_adition from cdm where" sql += " num_monstre=" + strconv.Uitoa(numMonstre) + " and" sql += " author in (" + strconv.Itoa(trollId) for _, id := range amis { sql += "," + strconv.Itoa(id) } sql += ") order by date_adition desc limit 1" err = db.Query(sql) if err != nil { return } result, err := db.UseResult() if err != nil { return } row := result.FetchRow() db.FreeResult() if row == nil { return } blessure = fieldAsUint(row[0]) auteurCDM = fieldAsInt(row[1]) dateCDM = fieldAsInt64(row[2]) return }
// renvoie la liste des trolls avec qui le troll passé a un partage actif func (store *MysqlStore) GetPartageurs(db *mysql.Client, trollId int) ([]int, error) { st := strconv.Itoa(trollId) sql := "select troll_a, troll_b from partage where (troll_a=" + st + " or troll_b=" + st + ") and statut_a='on' and statut_b='on'" err := db.Query(sql) if err != nil { return nil, err } result, err := db.UseResult() if err != nil { return nil, err } defer result.Free() amis := make([]int, 0, 5) for { row := result.FetchRow() if row == nil { break } r0 := fieldAsInt(row[0]) r1 := fieldAsInt(row[1]) if r0 == trollId { amis = append(amis, r1) } else { amis = append(amis, r0) } } return amis, nil }
func Authenticate(username string, passwordHash string, dbConn *mysql.Client) (reply *AuthenticationReply, err os.Error) { // Default return argument reply = NewAuthenticationReply(false, -1, "") // Escape input username = dbConn.Escape(username) password := dbConn.Escape(passwordHash) fmt.Printf("Authenticating user: '******'\n", username, password) err = dbConn.Query("SELECT * FROM users WHERE nick = '" + username + "' AND password = '******' limit 1") if err != nil { return } result, err := dbConn.UseResult() defer dbConn.FreeResult() if err != nil { return } // Fetch the row row := result.FetchMap() // If we found it the client got the username and password right if row != nil { id := row["id"].(int64) nick := row["nick"].(string) reply = NewAuthenticationReply(true, id, nick) return } else { err = os.NewError("Wrong username or password.") } return }
func (store *MysqlStore) GetAllPartages_old(db *mysql.Client, trollId uint) (partages []*Partage, err error) { sql := "select troll_a, troll_b, statut_a, statut_b from partage where troll_a=" + strconv.Uitoa(trollId) + " or troll_b=" + strconv.Uitoa(trollId) err = db.Query(sql) if err != nil { return } result, err := db.UseResult() if err != nil { return } defer result.Free() partages = make([]*Partage, 0, 10) for { row := result.FetchRow() if row == nil { break } p := new(Partage) p.TrollA = fieldAsUint(row[0]) p.TrollB = fieldAsUint(row[1]) p.StatutA = fieldAsString(row[2]) p.StatutB = fieldAsString(row[3]) l := len(partages) if l == cap(partages) { newSlice := make([]*Partage, (l+1)*3/2) copy(newSlice, partages) partages = newSlice } partages = partages[0 : l+1] partages[l] = p } return }
/** * estime les caractéristiques du monstre. * Si l'id est fourni (i.e. pas 0) et si on a des cdm concernant ce monstre précis, on n'utilise que celles là [EN COURS] */ func (store *MysqlStore) ComputeMonsterStats(db *mysql.Client, completeName string, monsterId uint) (be *BestiaryExtract, err os.Error) { // On utilise des max pour les champs de type chaine. C'est sans doute trop lourd (à moins que MySQL ne mette en place un index // spécifique). L'objectif réel est de récupérer la chaine la plus longue. if monsterId != 0 { sql := "select count(*), count(distinct num_monstre)," sql += namaxmin("niveau") + ", " sql += namaxmin("points_de_vie") + ", " sql += "max(capacite_text), " sql += namaxmin("des_attaque") + ", " sql += namaxmin("des_esquive") + ", " sql += namaxmin("des_degats") + ", " sql += namaxmin("des_regeneration") + ", " sql += namaxmin("armure") + ", " sql += namaxmin("vue") + ", " sql += namaxmin("maitrise_magique") + ", " sql += namaxmin("resistance_magique") + ", " sql += " max(famille_text), " sql += " max(nombre_attaques), " sql += " max(vitesse_deplacement_text), " sql += " max(voir_le_cache_boolean), " sql += " max(attaque_a_distance_boolean), " sql += namaxmin("duree_tour") + ", " sql += " max(portee_du_pouvoir_text)" sql += " from cdm where nom_complet=" + toMysqlString(completeName) sql += " and num_monstre=" + strconv.Uitoa(monsterId) //fmt.Println("SQL :\n" + sql + "\n") err = db.Query(sql) if err != nil { return nil, err } result, err := db.UseResult() if err != nil { return nil, err } row := result.FetchRow() db.FreeResult() if row != nil { be = rowToBestiaryExtract(completeName, row) if be.NbCdm > 0 { be.PreciseMonster = true return be, nil } } } sql := "select count(*), count(distinct num_monstre)," sql += namaxmin("niveau") + ", " // namaxmin car le niveau d'un monstre est fixe pour un nom complet donné sql += naminmax("points_de_vie") + ", " sql += "max(capacite_text), " sql += naminmax("des_attaque") + ", " sql += naminmax("des_esquive") + ", " sql += naminmax("des_degats") + ", " sql += naminmax("des_regeneration") + ", " sql += naminmax("armure") + ", " sql += naminmax("vue") + ", " sql += naminmax("maitrise_magique") + ", " sql += naminmax("resistance_magique") + ", " sql += " max(famille_text), " sql += " max(nombre_attaques), " sql += " max(vitesse_deplacement_text), " sql += " max(voir_le_cache_boolean), " sql += " max(attaque_a_distance_boolean), " sql += naminmax("duree_tour") + ", " sql += " max(portee_du_pouvoir_text)" sql += " from cdm where nom_complet=" + toMysqlString(completeName) //fmt.Println(sql) err = db.Query(sql) if err != nil { return nil, err } result, err := db.UseResult() if err != nil { return nil, err } row := result.FetchRow() db.FreeResult() if row == nil { //fmt.Println("ComputeMonsterStats : no result") return nil, nil } be = rowToBestiaryExtract(completeName, row) be.PreciseMonster = false return be, nil }
// renvoie les actions, en incluant les résumés de CDM func (store *MysqlStore) GetActions(db *mysql.Client, typeCible string, numCible int, trollId int, amis []int) (actions []*Action, err error) { strnum := strconv.Itoa(numCible) //> d'abord les vraies "actions" sql := "select date_action, type_action, auteur, succes, degats, pv, esquive from action where type_cible='" + typeCible + "' and num_cible=" + strnum sql += " and auteur in (" + strconv.Itoa(trollId) for _, id := range amis { sql += "," + strconv.Itoa(id) } sql += ")" fmt.Println(sql) err = db.Query(sql) if err != nil { return } result, err := db.UseResult() if err != nil { return } actions = make([]*Action, 0, 10) for { row := result.FetchRow() fmt.Printf("row : %v\n", row) if row == nil { break } a := new(Action) a.Date = fieldAsInt64(row[0]) a.Type = fieldAsString(row[1]) a.Auteur = fieldAsInt(row[2]) a.TypeCible = typeCible a.NumCible = numCible if fieldAsString(row[3]) == "oui" { a.Succes = true } else { a.Succes = false } a.Degats = fieldAsInt(row[4]) a.PV = fieldAsInt(row[5]) a.Esquive = fieldAsInt(row[6]) actions = append(actions, a) } db.FreeResult() //> ensuite les cdm if typeCible != "monstre" { return } sql = "select date_adition, author , blessure from cdm where num_monstre=" + strnum sql += " and author in (" + strconv.Itoa(trollId) for _, id := range amis { sql += "," + strconv.Itoa(id) } sql += ")" fmt.Println(sql) err = db.Query(sql) if err != nil { return } result, err = db.UseResult() if err != nil { return } for { row := result.FetchRow() fmt.Printf("row : %v\n", row) if row == nil { break } a := new(Action) a.Date = fieldAsInt64(row[0]) a.Auteur = fieldAsInt(row[1]) a.PourcentageBlessure = fieldAsInt(row[2]) a.Type = "CDM" a.TypeCible = typeCible a.NumCible = numCible a.Succes = true actions = append(actions, a) } db.FreeResult() return }