func (g *Game) clearDB() { var tables = []string{"users_inventory", "users_slots"} db := connect.CreateConnect() for _, table := range tables { db.Exec(fmt.Sprintf("DELETE FROM %s", table)) } }
func (p *Player) Equip(item gameObjectsBase.Itemer, slotIota int) (int, consts.JsonType) { var res consts.JsonType = nil slot := p.slots[slotIota] if slot == nil || !slot.isSuitableType(item.GetItemType()) { return consts.BADSLOT, res } if item.GetItemClass() != consts.ITEM_CLASS_GARMENT || p.Equipped(item) { return consts.BADID, res } p.Unequip(slotIota) db := connect.CreateConnect() _, err := db.Exec("CALL equip_item(?, ?, ?, ?)", p.DBId, item.GetKindId(), item.EquipItem(p.Inventory), slotIota) if err == nil { if ok, s := p.getNearbySlot(slotIota, item.GetItemSubtype()); ok { if p.slots[s].item != nil && p.slots[s].item.GetID() != item.GetID() { p.Unequip(s) } res = consts.JsonType{} res[consts.SlotNameMapping[s]] = item.GetID() p.slots[s].item = nil } else { p.Unequip(slotIota) } slot.item = item } if err == nil { return consts.OK, res } else { return consts.BADID, res } }
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) }
func (s *playerList) save() { db := connect.CreateConnect() defer db.Close() stmnt, _ := db.Prepare("UPDATE users_position SET x = ?, y = ? WHERE id = ?") defer stmnt.Close() for { for _, p := range s.players { stmnt.Exec(p.Center.X, p.Center.Y, p.DBId) } time.Sleep(consts.DATABASE_TICK_DURATION) } }
func (p *Player) DropItem(item gameObjectsBase.Itemer, amount int) (int, gameObjectsBase.Itemer) { db := connect.CreateConnect() place, new_item := p.ActiveObject.DropItem(item, amount) if s := p.getSlotByItem(item); s != consts.SLOT_DEFAULT { if item.GetAmount()-amount <= 0 { p.slots[s].item = nil } db.Exec("CALL dec_user_slot_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), s, amount) } else { db.Exec("CALL dec_user_item_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), place, amount) } return place, new_item }
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) } }
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) }
func (p *Player) PickUpItem(item gameObjectsBase.Itemer) (bool, gameObjectsBase.Itemer) { var err error = nil db := connect.CreateConnect() place, i := p.AddItem(item) if item.IsHeapItem() && place == -1 { _, err = db.Exec("CALL inc_user_slot_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), p.getSlotByItemType(item.GetItemType()), item.GetAmount()) } else { _, err = db.Exec("CALL inc_user_item_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), place, item.GetAmount()) } if err != nil { // fmt.Println(err) } return err == nil, i }
func (p *Player) MoveItem(item gameObjectsBase.Itemer, to_cell int) bool { if owner := item.GetOwner(); (owner != nil && owner.GetID() != p.GetID()) || p.Equipped(item) { return false } from_cell := p.Inventory.GetPlace(item.GetID()) if from_cell == to_cell { return true } else { db := connect.CreateConnect() _, err := db.Exec("CALL move_item(?, ?, ?)", p.DBId, from_cell, to_cell) if err == nil { p.Inventory.MoveItem(item, from_cell, to_cell) } return err == nil } }
func isExistUser(login, pass string) bool { db := connect.CreateConnect() var ( where string = "login = ?" isLogin bool ) if isLogin = pass != ""; isLogin { where = where + " AND password = ?" } stmt, _ := db.Prepare(connect.MakeSelect("users", where, "id")) defer stmt.Close() if isLogin { return stmt.QueryRow(login, pass).Scan() != sql.ErrNoRows } else { return stmt.QueryRow(login).Scan() != sql.ErrNoRows } }
func (ml *mobList) initializeMobTypes() consts.JsonType { blows.InitMobBlows() gameObjectsFlags.InitFlags(&GetInstance().field, GetInstance().msgsChannel) db := connect.CreateConnect() rows, _ := db.Query("SELECT name, base_hp, hp_inc, symbol, description, blow_method, flags, level_info FROM mobs_types") mobDictionary := make(consts.JsonType) for rows.Next() { var ( base_hp int name, hp_inc, symbol, desc, flags, blowMethods, level_info string ) rows.Scan(&name, &base_hp, &hp_inc, &symbol, &desc, &blowMethods, &flags, &level_info) depth := utils.ParseInt64(strings.Split(level_info, "|")[0]) ml.mobsDepth[depth] = append(ml.mobsDepth[depth], gameObjects.CreateMobKind(name, base_hp, hp_inc, desc, blowMethods, flags)) mobDictionary[symbol] = name } return mobDictionary }
func (g *Game) CreatePlayer(sid string) *gameObjects.Player { db := connect.CreateConnect() stmt, _ := db.Prepare(` SELECT u.id, u.login, u.class, up.x, up.y FROM users u INNER JOIN users_position as up ON u.id = up.user_id INNER JOIN sessions s ON s.user_id = u.id AND s.sid = ? `) defer stmt.Close() var dbId int64 var login string var class int var x, y float64 stmt.QueryRow(sid).Scan(&dbId, &login, &class, &x, &y) if x == consts.DEFAULT_PLAYER_POS_X || y == consts.DEFAULT_PLAYER_POS_Y { x, y = g.newPlayerPos(x, y) } p := gameObjects.NewPlayer(utils.GenerateId(), dbId, class, login, sid, x, y) g.players.add(p) g.field.LinkToCells(p) rows, _ := db.Query("SELECT item_id, amount, place FROM users_inventory WHERE user_id = ?", dbId) for rows.Next() { var ( iid int64 amount, place int ) rows.Scan(&iid, &amount, &place) item := gameObjectsBase.NewItemByID(iid, p, amount) p.RestoreItem(item, place) g.items.addItem(item) } rows, _ = db.Query("SELECT item_id, amount, slot FROM users_slots WHERE user_id = ?", dbId) for rows.Next() { var ( iid int64 amount, slot int ) rows.Scan(&iid, &amount, &slot) item := gameObjectsBase.NewItemByID(iid, p, amount) p.RestoreSlot(item, slot) g.items.addItem(item) } return p }
func (p *Player) DeleteItem(item gameObjectsBase.Itemer, amount int) (bool, gameObjectsBase.Itemer) { var ( i gameObjectsBase.Itemer = item res bool = false ) db := connect.CreateConnect() var place int place, i = p.Inventory.DeleteItem(item, amount) if s := p.getSlotByItem(item); s != consts.SLOT_DEFAULT { if item.GetAmount() <= 0 { p.slots[s].item = nil i = nil } _, err := db.Exec("CALL dec_user_slot_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), s, amount) res = err == nil } else { _, err := db.Exec("CALL dec_user_item_amount(?, ?, ?, ?)", p.DBId, item.GetKindId(), place, amount) res = err == nil } return res, i }
func InitGameItems() { db := connect.CreateConnect() rows, _ := db.Query("SELECT id, name, atype, weight, allocation_info, power_info, message, description, bonus, effects FROM artifacts") for rows.Next() { var ( id int64 weight int atype_str, name, alloc_info_str, power_info_str, msg, desc, bonusesStr, effectsStr, power string ) rows.Scan(&id, &name, &atype_str, &weight, &alloc_info_str, &power_info_str, &msg, &desc, &bonusesStr, &effectsStr) atype := strings.Split(atype_str, ":") power_arry := strings.Split(power_info_str, ":") if len(power_arry) > 1 { power = power_arry[1] } gameItems.items[id] = &ItemKind{id, name, weight, power, msg, desc, utils.ParseInt(atype[0]), utils.ParseInt(atype[1]), utils.ParseInt(atype[2]), make([]*Bonus, 0, 30), make([]Effecter, 0, 30)} alloc_info := strings.Split(alloc_info_str, ":") prob := utils.ParseInt(alloc_info[0]) min_d := utils.ParseInt64(alloc_info[1]) - 1 max_d := utils.ParseInt64(alloc_info[2]) - 1 for i := min_d; i <= max_d; i++ { gameItems.items_depth_gen[i] = append(gameItems.items_depth_gen[i], &gameItemGen{gameItems.items[id], prob}) } if len(bonusesStr) > 0 { for _, bonusStr := range strings.Split(bonusesStr, "@") { for _, bonus := range parseBonusFromDB(bonusStr) { gameItems.items[id].bonuses = append(gameItems.items[id].bonuses, bonus) } } } if len(effectsStr) > 0 { for _, effectStr := range strings.Split(effectsStr, "@") { for _, effect := range parseEffectFromDB(effectStr) { gameItems.items[id].effects = append(gameItems.items[id].effects, effect) } } } } }
func (p *Player) Unequip(slotIota int) (bool, consts.JsonType) { var res consts.JsonType = nil slot := p.slots[slotIota] if slot.item == nil { return false, res } db := connect.CreateConnect() k := slot.item.GetKindId() place := slot.item.UnequipItem(p.Inventory) _, err := db.Exec("CALL unequip_item(?, ?, ?, ?)", p.DBId, k, place, slotIota) if err == nil { if ok, s := p.getNearbySlot(slotIota, slot.item.GetItemSubtype()); ok { if p.slots[s].item != nil && p.slots[s].item.GetID() == slot.item.GetID() { res = consts.JsonType{} res[consts.SlotNameMapping[s]] = slot.item.GetID() p.slots[s].item = nil } } slot.item = nil } return err == nil, res }
func registerAction(login, pass, class string) string { result := map[string]string{"result": "ok"} class_numb, class_exists := consts.NamePlayerClassMapping[class] if !matchRegexp("^[a-zA-Z0-9]{2,36}$", login) { result["result"] = "badLogin" } else if !matchRegexp("^.{6,36}$", pass) { result["result"] = "badPassword" } else if isExistUser(login, "") { result["result"] = "loginExists" } else if !class_exists && class != "" { result["result"] = "badClass" } else { db := connect.CreateConnect() stmt, _ := db.Prepare("INSERT INTO users(login, password, class) VALUES(?, ?, ?)") defer stmt.Close() res, _ := stmt.Exec(login, pass, class_numb) user_id, _ := res.LastInsertId() stmt, _ = db.Prepare("INSERT INTO users_position(user_id, x, y) VALUES(?, ?, ?)") defer stmt.Close() stmt.Exec(user_id, consts.DEFAULT_PLAYER_POS_X, consts.DEFAULT_PLAYER_POS_Y) } resJSON, _ := json.Marshal(result) return string(resJSON) }