Follow the leader with help from a server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

150 lines
4.9KB

  1. // NGnius 2020-02-11
  2. package main
  3. import (
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. _ "github.com/mattn/go-sqlite3"
  8. )
  9. const (
  10. sqlServerDefault = "sqlite3"
  11. sqlConnectionDefault = "test.sqlite"
  12. )
  13. var (
  14. // command line arguments
  15. sqlServer string
  16. sqlConnection string
  17. buildTables bool
  18. populateTables bool
  19. // internal variables
  20. db *sql.DB
  21. )
  22. func sqlInit() error {
  23. var dbOpenErr error
  24. db, dbOpenErr = sql.Open(sqlServer, sqlConnection)
  25. if dbOpenErr != nil {
  26. return dbOpenErr
  27. }
  28. if buildTables {
  29. fmt.Println("Building tables in database...")
  30. sqlBuildTables()
  31. }
  32. if populateTables {
  33. fmt.Println("Populating tables in database...")
  34. sqlPopulateTables()
  35. }
  36. return nil
  37. }
  38. func sqlClose() error {
  39. if db != nil {
  40. err := db.Close()
  41. if err != nil {
  42. return err
  43. }
  44. db = nil
  45. }
  46. return nil
  47. }
  48. func boardByName(name string) (*Board, error) {
  49. b := &Board{}
  50. return b, db.QueryRow("SELECT * FROM Boards WHERE name=? LIMIT 1", name).Scan(b.Intake()...)
  51. }
  52. func playerByName(name string) (*Player, error) {
  53. p := &Player{}
  54. return p, db.QueryRow("SELECT * FROM Players WHERE name=? LIMTI 1", name).Scan(p.Intake()...)
  55. }
  56. func newEntrySql(score, player, board int64) error {
  57. tx, _ := db.Begin()
  58. stmt, _ := tx.Prepare("INSERT INTO Entries(score, player, board, time) VALUES (?, ?, ?, ?)")
  59. _, err := stmt.Exec(score, player, board, time.Now().Unix())
  60. if err != nil {
  61. tx.Rollback()
  62. return err
  63. }
  64. tx.Commit()
  65. return nil
  66. }
  67. // internal operations
  68. func sqlBuildTables() {
  69. transaction, txErr := db.Begin()
  70. if txErr != nil {
  71. return
  72. }
  73. // test table
  74. //transaction.Exec("CREATE TABLE IF NOT EXISTS Test (Sometext VARCHAR, Somenumber);")
  75. //transaction.Exec("INSERT INTO Test (Sometext, Somenumber) VALUES (?,?);", "Hello sqlite", 123)
  76. // build real tables
  77. transaction.Exec("CREATE TABLE IF NOT EXISTS Players (id INTEGER PRIMARY KEY, name TEXT NOT NULL);")
  78. transaction.Exec("CREATE TABLE IF NOT EXISTS Boards (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL);")
  79. transaction.Exec("CREATE TABLE IF NOT EXISTS Entries (id INTEGER PRIMARY KEY, rank INTEGER NOT NULL DEFAULT -1, score INTEGER NOT NULL, player INTEGER NOT NULL, board INTEGER NOT NULL, time INTEGER NOT NULL, metadata BLOB NOT NULL DEFAULT 0, FOREIGN KEY(player) REFERENCES Players(id), FOREIGN KEY(board) REFERENCES Boards(id));")
  80. transaction.Commit()
  81. }
  82. func sqlPopulateTables() {
  83. boards := []*Board{
  84. &Board{ID: 1, Name: "main-test", Description: "Overall best (test data)"},
  85. &Board{ID: 2, Name: "coolest-test", Description: "Coolest score (test data)"},
  86. &Board{ID: 3, Name: "fastest-test", Description: "Fastest time (test data)"},
  87. }
  88. for _, b := range boards {
  89. err := b.Commit()
  90. if err != nil {
  91. fmt.Printf("Error creating board %d: %s\n", b.ID, err)
  92. }
  93. }
  94. players := []*Player{
  95. &Player{ID: 1, Name: "NGnius (test)"},
  96. &Player{ID: 2, Name: "Also NGnius (test)"},
  97. &Player{ID: 3, Name: ".xX||eDgY TeeNaGeR||Xx. (test)"},
  98. &Player{ID: 4, Name: "New username who dis? (test)"},
  99. &Player{ID: 5, Name: "Extremely Ridiculously Long Name to break things (test)"},
  100. &Player{ID: 6, Name: "P|P3 |o|z (test)"},
  101. &Player{ID: 7, Name: "Waldo (test)"},
  102. &Player{ID: 8, Name: "Zettagram.com (test)"},
  103. &Player{ID: 9, Name: "The Doctor (test)"},
  104. &Player{ID: 10, Name: "Marvin the Paranoid Android (test)"},
  105. &Player{ID: 11, Name: "IDK HOW (test)"},
  106. &Player{ID: 12, Name: "If you can read this your API may be wrong (test)"},
  107. &Player{ID: 13, Name: "Unlucky 7 (test)"},
  108. }
  109. for _, p := range players {
  110. err := p.Commit()
  111. if err != nil {
  112. fmt.Printf("Error creating player %d: %s\n", p.ID, err)
  113. }
  114. }
  115. now := time.Now().Unix()
  116. entries := []*Entry{
  117. &Entry{ID: 1, Rank: 1, Score: 1000, Player: players[0].ID, Board: boards[0].ID, Time: now},
  118. &Entry{ID: 2, Rank: 2, Score: 900, Player: players[1].ID, Board: boards[0].ID, Time: now},
  119. &Entry{ID: 3, Rank: 3, Score: 400, Player: players[2].ID, Board: boards[0].ID, Time: now},
  120. &Entry{ID: 4, Rank: 4, Score: 350, Player: players[3].ID, Board: boards[0].ID, Time: now},
  121. &Entry{ID: 5, Rank: 5, Score: 350, Player: players[4].ID, Board: boards[0].ID, Time: now},
  122. &Entry{ID: 6, Rank: 6, Score: 250, Player: players[5].ID, Board: boards[0].ID, Time: now},
  123. &Entry{ID: 7, Rank: 7, Score: 200, Player: players[6].ID, Board: boards[0].ID, Time: now},
  124. &Entry{ID: 8, Rank: 8, Score: 175, Player: players[7].ID, Board: boards[0].ID, Time: now},
  125. &Entry{ID: 9, Rank: 9, Score: 150, Player: players[8].ID, Board: boards[0].ID, Time: now},
  126. &Entry{ID: 10, Rank: 10, Score: 140, Player: players[9].ID, Board: boards[0].ID, Time: now},
  127. &Entry{ID: 11, Rank: 11, Score: 10, Player: players[10].ID, Board: boards[0].ID, Time: now},
  128. &Entry{ID: 12, Rank: 12, Score: 60, Player: players[11].ID, Board: boards[0].ID, Time: now},
  129. &Entry{ID: 13, Rank: 13, Score: 13, Player: players[12].ID, Board: boards[0].ID, Time: now},
  130. }
  131. for _, e := range entries {
  132. err := e.Commit()
  133. if err != nil {
  134. fmt.Printf("Error creating entry %d: %s\n", e.ID, err)
  135. }
  136. }
  137. }