2019-05-25 09:25:11 +02:00
|
|
|
package main
|
|
|
|
|
2019-05-25 09:37:45 +02:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-05-25 09:25:11 +02:00
|
|
|
var (
|
|
|
|
cacheObjCastle map[string]ChatWarsCastle
|
|
|
|
cacheObjGuild map[string]ChatWarsGuild
|
2019-05-25 10:59:14 +02:00
|
|
|
cacheObjUser map[string]ChatWarsUser
|
2019-05-25 09:25:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func getObjTypeId(objId int64) (int64, error) {
|
|
|
|
var objTypeId int64
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(`SELECT o.obj_type_id FROM obj o WHERE o.id = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
err = stmt.QueryRow(objId).Scan(&objTypeId)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return objTypeId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjSubTypeId(objId int64) (int64, error) {
|
|
|
|
var objSubTypeId int64
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(`SELECT o.obj_sub_type_id FROM obj o WHERE o.id = ?`)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
err = stmt.QueryRow(objId).Scan(&objSubTypeId)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return objSubTypeId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setObjSubTypeId(objId int64, objSubTypeID64 int64) error {
|
|
|
|
stmt, err := db.Prepare(`UPDATE obj o SET o.obj_sub_type_id = ? WHERE o.id = ?;`)
|
|
|
|
logOnError(err, "setObjSubTypeId : prepare update")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objSubTypeID64, objId)
|
|
|
|
logOnError(err, "setObjSubTypeId : exec update")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func addObjMsg(msgID64 int64, msgChatID64 int64, msgUserID64 int64, msgSenderUserID64 int64, msgDate time.Time, msgText string) (int64, error) {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
logOnError(err, "addObjMsg : start transaction")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (` + strconv.Itoa(objTypeMessage) + `,` + strconv.Itoa(objSubTypeMessageUnknown) + `);`)
|
|
|
|
logOnError(err, "addObjMsg : exec insert obj")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjMsg : rollback insert obj")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjMsg : rollback get lastInsertId")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date, text)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?);`)
|
|
|
|
logOnError(err, "addObjMsg : prepare insert obj_msg")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjMsg : rollback prepare insert obj_msg")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objId, msgID64, msgChatID64, msgUserID64, msgSenderUserID64, msgDate, msgText)
|
|
|
|
logOnError(err, "addObjMsg : exec insert obj_msg")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
2019-05-31 04:02:21 +02:00
|
|
|
logOnError(err2, "addObjMsg : rollback exec insert obj_msg")
|
2019-05-25 09:25:11 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
logOnError(err, "addObjMsg : commit")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return objId, nil
|
|
|
|
}
|
|
|
|
|
2019-05-25 09:29:24 +02:00
|
|
|
func addObjCastle(logo string, name string) (int64, error) {
|
2019-05-25 09:25:11 +02:00
|
|
|
tx, err := db.Begin()
|
|
|
|
logOnError(err, "addObjCastle : start transaction")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (` + strconv.Itoa(objTypeCastle) + `,` + strconv.Itoa(objSubTypeCastle) + `);`)
|
|
|
|
logOnError(err, "addObjCastle : exec insert obj")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjCastle : rollback insert obj")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjCastle : rollback get lastInsertId")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`INSERT INTO obj_castle (obj_id, logo, name)
|
|
|
|
VALUES (?, ?, ?);`)
|
|
|
|
logOnError(err, "addObjCastle : prepare insert obj_castle")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjCastle : rollback prepare insert obj_castle")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objId, logo, name)
|
|
|
|
logOnError(err, "addObjCastle : exec insert obj_castle")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
2019-05-31 04:02:21 +02:00
|
|
|
logOnError(err2, "addObjCastle : rollback exec insert obj_castle")
|
2019-05-25 09:25:11 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
logOnError(err, "addObjCastle : commit")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return objId, nil
|
|
|
|
}
|
|
|
|
|
2019-05-25 09:48:34 +02:00
|
|
|
func getObjCastleID(s string) int64 {
|
|
|
|
return cacheObjCastle[s].ObjID64
|
2019-05-25 09:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadObjCastle() error {
|
|
|
|
var (
|
|
|
|
id int64
|
|
|
|
logo string
|
|
|
|
name string
|
|
|
|
)
|
|
|
|
|
|
|
|
cacheObjCastle = make(map[string]ChatWarsCastle)
|
|
|
|
|
2019-05-25 10:06:33 +02:00
|
|
|
castles, err := db.Query(`SELECT oc.obj_id, oc.logo, oc.name FROM obj_castle oc;`)
|
2019-05-25 09:25:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer castles.Close()
|
|
|
|
|
|
|
|
for castles.Next() {
|
2019-05-25 09:37:45 +02:00
|
|
|
err = castles.Scan(&id, &logo, &name)
|
2019-05-25 09:25:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c := new(ChatWarsCastle)
|
|
|
|
c.ObjID64 = id
|
|
|
|
c.Logo = logo
|
|
|
|
c.Name = name
|
|
|
|
cacheObjCastle[logo] = *c
|
|
|
|
cacheObjCastle[name] = *c
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-25 10:56:13 +02:00
|
|
|
func addObjGuild(tag string, name string) (int64, error) {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
logOnError(err, "addObjGuild : start transaction")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (` + strconv.Itoa(objTypeGuild) + `,` + strconv.Itoa(objSubTypeGuild) + `);`)
|
|
|
|
logOnError(err, "addObjGuild : exec insert obj")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjGuild : rollback insert obj")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjGuild : rollback get lastInsertId")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`INSERT INTO obj_guild (obj_id, tag, name, chat_id, deposit_chat_id)
|
|
|
|
VALUES (?, ?, ?, NULL, NULL);`)
|
|
|
|
logOnError(err, "addObjGuild : prepare insert obj_guild")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjGuild : rollback prepare insert obj_guild")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objId, tag, name)
|
|
|
|
logOnError(err, "addObjGuild : exec insert obj_guild")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
2019-05-31 04:02:21 +02:00
|
|
|
logOnError(err2, "addObjGuild : rollback exec insert obj_guild")
|
2019-05-25 10:56:13 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
logOnError(err, "addObjGuild : commit")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return objId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjGuildID(s string) int64 {
|
|
|
|
if g, ok := cacheObjGuild[s]; ok {
|
|
|
|
return g.ObjID64
|
|
|
|
} else {
|
|
|
|
objID64, err := addObjGuild(s, ``)
|
|
|
|
logOnError(err, "getObjGuildID")
|
|
|
|
g := new(ChatWarsGuild)
|
|
|
|
g.ObjID64 = objID64
|
|
|
|
g.Tag = s
|
|
|
|
g.Name = ``
|
|
|
|
cacheObjGuild[s] = *g
|
|
|
|
}
|
|
|
|
return cacheObjGuild[s].ObjID64
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadObjGuild() error {
|
|
|
|
var (
|
|
|
|
id int64
|
|
|
|
tag string
|
|
|
|
name string
|
|
|
|
)
|
|
|
|
|
|
|
|
cacheObjGuild = make(map[string]ChatWarsGuild)
|
|
|
|
|
|
|
|
guilds, err := db.Query(`SELECT og.obj_id, og.tag, og.name FROM obj_guild og;`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer guilds.Close()
|
|
|
|
|
|
|
|
for guilds.Next() {
|
|
|
|
err = guilds.Scan(&id, &tag, &name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
g := new(ChatWarsGuild)
|
|
|
|
g.ObjID64 = id
|
|
|
|
g.Tag = tag
|
|
|
|
g.Name = name
|
|
|
|
cacheObjGuild[tag] = *g
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addObjUser(name string) (int64, error) {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
logOnError(err, "addObjUser : start transaction")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (` + strconv.Itoa(objTypeUser) + `,` + strconv.Itoa(objSubTypeUser) + `);`)
|
|
|
|
logOnError(err, "addObjUser : exec insert obj")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjUser : rollback insert obj")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjUser : rollback get lastInsertId")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`INSERT INTO obj_user (obj_id, name)
|
|
|
|
VALUES (?, ?);`)
|
|
|
|
logOnError(err, "addObjUser : prepare insert obj_user")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjUser : rollback prepare insert obj_user")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objId, name)
|
|
|
|
logOnError(err, "addObjUser : exec insert obj_user")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
2019-05-31 04:02:21 +02:00
|
|
|
logOnError(err2, "addObjUser : rollback exec insert obj_user")
|
2019-05-25 10:56:13 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
logOnError(err, "addObjUser : commit")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return objId, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getObjUserID(s string) int64 {
|
|
|
|
if u, ok := cacheObjUser[s]; ok {
|
|
|
|
return u.ObjID64
|
|
|
|
} else {
|
2019-05-25 11:02:24 +02:00
|
|
|
objID64, err := addObjUser(s)
|
2019-05-25 10:56:13 +02:00
|
|
|
logOnError(err, "getObjUserID")
|
|
|
|
u := new(ChatWarsUser)
|
|
|
|
u.ObjID64 = objID64
|
|
|
|
u.Name = s
|
|
|
|
cacheObjUser[s] = *u
|
|
|
|
}
|
|
|
|
return cacheObjUser[s].ObjID64
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadObjUser() error {
|
|
|
|
var (
|
|
|
|
id int64
|
|
|
|
name string
|
|
|
|
)
|
|
|
|
|
2019-05-25 10:59:14 +02:00
|
|
|
cacheObjUser = make(map[string]ChatWarsUser)
|
2019-05-25 10:56:13 +02:00
|
|
|
|
2019-05-25 10:59:14 +02:00
|
|
|
users, err := db.Query(`SELECT ou.obj_id, ou.name FROM obj_user ou;`)
|
2019-05-25 10:56:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer users.Close()
|
|
|
|
|
|
|
|
for users.Next() {
|
2019-05-25 10:59:14 +02:00
|
|
|
err = users.Scan(&id, &name)
|
2019-05-25 10:56:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-25 10:59:14 +02:00
|
|
|
u := new(ChatWarsUser)
|
|
|
|
u.ObjID64 = id
|
|
|
|
u.Name = name
|
2019-05-25 11:05:22 +02:00
|
|
|
cacheObjUser[name] = *u
|
2019-05-25 10:56:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-05-30 07:49:11 +02:00
|
|
|
|
|
|
|
func getObjMsgDate(objID64 int64) (time.Time, error) {
|
2019-05-30 08:08:39 +02:00
|
|
|
m, err := getMsg(objID64)
|
2019-05-30 07:49:11 +02:00
|
|
|
if err != nil {
|
2019-05-30 08:08:39 +02:00
|
|
|
return time.Now(), err
|
|
|
|
} else {
|
|
|
|
return m.Date, nil
|
2019-05-30 07:49:11 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-31 04:02:21 +02:00
|
|
|
|
|
|
|
func addObjXP(userID64 int64, val int64, target int64, level int64, date time.Time) (int64, error) {
|
|
|
|
tx, err := db.Begin()
|
|
|
|
logOnError(err, "addObjXP : start transaction")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := tx.Exec(`INSERT INTO obj (obj_type_id, obj_sub_type_id)
|
|
|
|
VALUES (` + strconv.Itoa(objTypeExperience) + `,` + strconv.Itoa(objSubTypeExperience) + `);`)
|
|
|
|
logOnError(err, "addObjXP : exec insert obj")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjXP : rollback insert obj")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
objId, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjXP : rollback get lastInsertId")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(`INSERT INTO obj_xp (obj_id, user_id, val, target, level, date)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?);`)
|
|
|
|
logOnError(err, "addObjXP : prepare insert obj_xp")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjXP : rollback prepare insert obj_xp")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(objId, userID64, val, target, level, date)
|
|
|
|
logOnError(err, "addObjXP : exec insert obj_xp")
|
|
|
|
if err != nil {
|
|
|
|
err2 := tx.Rollback()
|
|
|
|
logOnError(err2, "addObjXP : rollback exec insert obj_xp")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
logOnError(err, "addObjXP : commit")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return objId, nil
|
|
|
|
}
|