can now respawn player from command line

master
Jordan Orelli 5 years ago
parent b408bdf2a2
commit 9200ed8575

@ -61,3 +61,12 @@ func (db *SQLite) ReadBody(body *Body) error {
}
return nil
}
func (db *SQLite) FindBody(id, finder int) error {
q := `update bodies set found_at = CURRENT_TIMESTAMP, found_by = ? where id = ?`
_, err := db.db.Exec(q, finder, id)
if err != nil {
return fmt.Errorf("unable to claim body %d as found by %d: %w", id, finder, err)
}
return nil
}

@ -31,6 +31,10 @@ func OpenSQLite(path string) (*SQLite, error) {
fmt.Fprintf(os.Stderr, "failed to create players table: %v\n", err)
}
db.Exec(`insert or ignore
into players (id, name, phash, psalt)
values (0, "admin", "", "")`)
if _, err := db.Exec(`
create table if not exists bodies (
id integer primary key autoincrement,

@ -149,6 +149,36 @@ func runPlayerStatus(cmd *cobra.Command, args []string) {
}
}
func runPlayerRespawn(cmd *cobra.Command, args []string) {
conn, err := db.OpenSQLite(cmd.Flag("db").Value.String())
if err != nil {
fmt.Fprintf(os.Stderr, "unable to open sqlite database: %v\n", err)
}
defer conn.Close()
player := db.Player{Name: args[0]}
if err := conn.ReadPlayer(&player); err != nil {
fmt.Fprintf(os.Stderr, "unable to read player record: %v\n", err)
return
}
body := db.Body{PlayerID: player.ID}
err = conn.ReadBody(&body)
switch {
case errors.Is(err, sql.ErrNoRows):
fmt.Fprintf(os.Stderr, "player was not dead\n")
return
case err == nil:
break
default:
fmt.Fprintf(os.Stderr, "unable to query for bodies: %v\n", err)
return
}
if err := conn.FindBody(body.ID, 0); err != nil {
fmt.Fprintf(os.Stderr, "unable to respawn player %s: %v\n", args[0], err)
}
}
func main() {
cmd := &cobra.Command{
Use: "kloam",
@ -201,5 +231,13 @@ func main() {
}
player.AddCommand(playerStatus)
playerRespawn := &cobra.Command{
Use: "respawn",
Short: "respawn a player",
Args: cobra.ExactArgs(1),
Run: runPlayerRespawn,
}
player.AddCommand(playerRespawn)
cmd.Execute()
}

Loading…
Cancel
Save