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.

136 lines
4.4KB

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