-
Notifications
You must be signed in to change notification settings - Fork 2
/
import.go
118 lines (100 loc) · 2.79 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"getrankd/api"
)
type ImportPlayerResult struct {
NewRank int
Score int
Name string
}
type ImportMatchResult struct {
Players []ImportPlayerResult
GameName string
Timestamp time.Time
}
func importFromCsv(filename string) {
startTime := time.Now()
fmt.Println("Import from CSV")
fileHandle, err := os.Open(filename)
defer fileHandle.Close()
if err != nil {
panic(err)
}
matches := make([]ImportMatchResult, 0)
reader := bufio.NewReader(fileHandle)
for {
lineContents, readLineErr := reader.ReadString('\n')
if (readLineErr != nil) && (readLineErr != io.EOF) {
panic(readLineErr)
}
lineFields := strings.Split(strings.TrimSpace(lineContents), ",")
playerCount := (len(lineFields) - 2) / 3
if playerCount <= 0 {
break
}
timestampStr := lineFields[0]
gameName := strings.Trim(lineFields[1], "\"")
timestamp, _ := time.Parse("2006-01-02 15:04:05", timestampStr[1:len(timestampStr)-1])
players := make([]ImportPlayerResult, playerCount)
lineFieldIndex := 2
for i := 0; i < playerCount; i++ {
playerRankStr := lineFields[lineFieldIndex+0]
playerScoreStr := lineFields[lineFieldIndex+1]
playerName := strings.Trim(lineFields[lineFieldIndex+2], "\"")
playerRank, _ := strconv.Atoi(playerRankStr)
playerScore, _ := strconv.Atoi(playerScoreStr)
newPlayer := ImportPlayerResult{
NewRank: playerRank,
Score: playerScore,
Name: playerName,
}
players[i] = newPlayer
lineFieldIndex += 3
}
newMatch := ImportMatchResult{
Players: players,
GameName: gameName,
Timestamp: timestamp,
}
fmt.Println(newMatch)
matches = append(matches, newMatch)
if readLineErr != nil {
break
}
}
gameIdMap := make(map[string]int64)
playerIdMap := make(map[string]int64)
for matchIndex := 0; matchIndex < len(matches); matchIndex++ {
match := matches[len(matches)-matchIndex-1] // NOTE: Matches are in reverse chronological order in the file
gameId, hasGameId := gameIdMap[match.GameName]
if !hasGameId {
gameId = api.PersistNewGame(match.GameName)
gameIdMap[match.GameName] = gameId
}
playerResults := make([]api.PlayerMatchResult, 0)
for playerPosition, player := range match.Players {
playerId, hasPlayerId := playerIdMap[player.Name]
if !hasPlayerId {
playerId = api.PersistNewPlayer(player.Name)
playerIdMap[player.Name] = playerId
}
newPlayer := api.PlayerMatchResult{
PlayerId: playerId,
// Score
Position: playerPosition,
}
playerResults = append(playerResults, newPlayer)
}
api.PersistNewMatch(gameId, match.Timestamp, playerResults)
fmt.Printf("Persisted match %d/%d\n", matchIndex+1, len(matches))
}
elapsedTime := time.Since(startTime)
fmt.Printf("Import completed in %s\n", elapsedTime)
}