mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-04-22 17:27:27 -05:00
87 lines
1.4 KiB
Go
87 lines
1.4 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"os"
|
|
"strconv"
|
|
"sync"
|
|
)
|
|
|
|
type GameInfo struct {
|
|
GameID int
|
|
Name string
|
|
SecretKey string
|
|
Description string
|
|
}
|
|
|
|
var (
|
|
gameList []GameInfo
|
|
gameListIDLookup = map[int]int{}
|
|
gameListNameLookup = map[string]int{}
|
|
mutex = sync.RWMutex{}
|
|
)
|
|
|
|
func GetGameInfoByID(gameId int) *GameInfo {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
|
|
if index, ok := gameListIDLookup[gameId]; ok && index < len(gameList) {
|
|
return &gameList[index]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetGameInfoByName(name string) *GameInfo {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
|
|
if index, ok := gameListNameLookup[name]; ok && index < len(gameList) {
|
|
return &gameList[index]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ReadGameList() {
|
|
file, err := os.Open("game_list.tsv")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
reader := csv.NewReader(file)
|
|
reader.Comma = '\t'
|
|
csvList, err := reader.ReadAll()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
gameList = []GameInfo{}
|
|
gameListIDLookup = map[int]int{}
|
|
gameListNameLookup = map[string]int{}
|
|
|
|
for index, entry := range csvList {
|
|
gameId := -1
|
|
|
|
if entry[2] != "" {
|
|
gameId, err = strconv.Atoi(entry[2])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
gameList = append(gameList, GameInfo{
|
|
GameID: gameId,
|
|
Name: entry[1],
|
|
SecretKey: entry[3],
|
|
Description: entry[0],
|
|
})
|
|
|
|
// Create lookup tables
|
|
if gameId != -1 {
|
|
gameListIDLookup[gameId] = index
|
|
}
|
|
gameListNameLookup[entry[1]] = index
|
|
}
|
|
}
|