Browse Source

Add player autocreate option

master
NGnius 4 years ago
parent
commit
c8ab4d5479
4 changed files with 14 additions and 2 deletions
  1. +1
    -0
      config.go
  2. +1
    -1
      handlers.go
  3. +1
    -0
      json_structs.go
  4. +11
    -1
      sql_service.go

+ 1
- 0
config.go View File

@@ -29,6 +29,7 @@ func initArgs() {
flag.StringVar(&corsHeader, "cors-header", defaultCorsHeader, "Access-Control-Allow-Origin HTTP request header")
flag.StringVar(&password, "entry-pwd", defaultPassword, "Password provided in POST JSON to authenticate")
flag.BoolVar(&reuseTokens, "reuse-tokens", false, "Generate a single token for every player")
flag.BoolVar(&autocreate, "autocreate-players", false, "Create a player during a token request")
}

func parseArgs() {


+ 1
- 1
handlers.go View File

@@ -221,7 +221,7 @@ func newKeyHandler(w http.ResponseWriter, r *http.Request) {
key, sqlErr = keyByPlayer(newKey.PlayerID)
}
if (sqlErr != nil && reuseTokens) || !reuseTokens {
key, sqlErr = newKeySql(newKey.PlayerID)
key, sqlErr = newKeySql(newKey.PlayerID, newKey.PlayerName)
}
if sqlErr != nil {
fmt.Println(sqlErr)


+ 1
- 0
json_structs.go View File

@@ -56,6 +56,7 @@ type KeyJSON struct {
// NewKeyJSON a new API key to be generated
type NewKeyJSON struct {
PlayerID int64
PlayerName string
}

func UnmarshalNewKeyJSON(data []byte) (NewKeyJSON, error) {


+ 11
- 1
sql_service.go View File

@@ -22,6 +22,7 @@ var (
sqlConnection string
buildTables bool
populateTables bool
autocreate bool
// internal variables
db *sql.DB
queryType int
@@ -137,8 +138,17 @@ func newEntrySql(score, player, board int64) error {
return nil
}

func newKeySql(player int64) (*Key, error) {
func newKeySql(player int64, name string) (*Key, error) {
tx, _ := db.Begin()
if autocreate {
sqlPlayer := &Player{ID: player, Name: name}
if (sqlPlayer.Load() != nil) {
playerErr := sqlPlayer.Commit()
if playerErr != nil {
return nil, playerErr
}
}
}
newKey := &Key{Token: "new", Player: player, Time: time.Now().Unix()}
stmt, _ := tx.Prepare(queryStrings[queryType][3])
_, err := stmt.Exec(newKey.Token, newKey.Player, newKey.Time)