-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_player.go
90 lines (81 loc) · 1.77 KB
/
db_player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"github.com/tidwall/buntdb"
)
/* ------ */
/* PLAYER */
/* ------ */
func (db *DB) newPlayer(p *tPlayer) {
if p == nil {
panic("newPlayer: Player is NIL")
}
err := db.db.Update(func(tx *buntdb.Tx) error {
index := dbNextIndex(tx)
tx.Set("player:"+index+":name", p.Name, nil)
tx.Set("player:"+index+":pos", fmt.Sprintf("[%v %v]", p.Position.X, p.Position.Z), nil)
p.ID = index
return nil
})
utilsCheck(err)
}
func (db *DB) setPlayerName(p *tPlayer) {
err := db.db.Update(func(tx *buntdb.Tx) error {
tx.Set("player:"+p.ID+":name", p.Name, nil)
return nil
})
utilsCheck(err)
}
func (db *DB) setPlayerPos(p *tPlayer) {
err := db.db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set("player:"+p.ID+":pos", fmt.Sprintf("[%v %v]", p.Position.X, p.Position.Z), nil)
return err
})
utilsCheck(err)
}
func (db *DB) getPlayerName(p *tPlayer) {
db.db.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("player:" + p.ID + ":name")
if err != nil {
return err
}
p.Name = val
return nil
})
}
func (db *DB) getPlayerPos(p *tPlayer) {
err := db.db.View(func(tx *buntdb.Tx) error {
pos, err := tx.Get("player:" + p.ID + ":pos")
if err != nil {
return err
}
var X, Z int64
fmt.Sscanf(pos, "[%d %d]", &X, &Z)
p.Position.X = X
p.Position.Z = Z
return nil
})
utilsCheck(err)
}
func (db *DB) getPlayer(ID string) *tPlayer {
p := &tPlayer{}
err := db.db.View(func(tx *buntdb.Tx) error {
name, err := tx.Get("player:" + ID + ":name")
if err != nil {
return err
}
pos, err := tx.Get("player:" + ID + ":pos")
if err != nil {
return err
}
var X, Z int64
fmt.Sscanf(pos, "[%d %d]", &X, &Z)
p.ID = ID
p.Name = name
p.Position.X = X
p.Position.Z = Z
return nil
})
utilsCheck(err)
return p
}