...

Source file src/go.formulabun.club/replays/store/structs.go

Documentation: go.formulabun.club/replays/store

     1  package store
     2  
     3  import (
     4  	"database/sql"
     5  	"fmt"
     6  	"os"
     7  )
     8  
     9  type Client struct {
    10  	*sql.DB
    11  }
    12  
    13  type ReplayFile struct {
    14  	FileName     string
    15  	FileCheckSum [16]byte
    16  }
    17  
    18  type Replay struct {
    19  	ReplayID    int64
    20  	GameMap     uint16
    21  	Time        uint32
    22  	BestLap     uint32
    23  	PlayerName  [16]byte
    24  	PlayerSkin  [16]byte
    25  	PlayerColor [16]byte
    26  	Speed       byte
    27  	Weight      byte
    28  }
    29  
    30  func makeClient(databaseName string) (Client, error) {
    31  	passwd, _ := os.LookupEnv("MARIADB_ROOT_PASSWORD")
    32  	db, err := sql.Open("mysql", fmt.Sprintf("root:%s@tcp(replaydb)/%s", passwd, databaseName))
    33  	if err != nil {
    34  		return Client{}, err
    35  	}
    36  	if err = db.Ping(); err != nil {
    37  		return Client{}, err
    38  	}
    39  	return Client{db}, nil
    40  }
    41  
    42  func NewClient() (Client, error) {
    43  	return makeClient("replays")
    44  }
    45  
    46  func newTestClient() (Client, error) {
    47  	return makeClient("testReplays")
    48  }
    49  
    50  func (r Replay) String() string {
    51  	return fmt.Sprintf("Replay %d on map %d with time %d, best lap %d by %s driving a %s %s with stats (%d,%d)", r.ReplayID, r.GameMap, r.Time, r.BestLap, r.PlayerName, r.PlayerSkin, r.PlayerColor, r.Speed, r.Weight)
    52  }
    53  

View as plain text