-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
73 lines (58 loc) · 1.55 KB
/
loader.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
package main
/**
* This module is responsiple for reading data into the
* aplication and deserializing it.
` * @module loader
*/
import (
"bufio"
"fmt"
"os"
"strings"
)
/** Loads data from the file specified in config and returns the collection of
* players as Player objects
* @returns {Array.<String, Player>
*/
func loadData() map[string]Player {
// load the info from the file
data, err := readData()
if (err != nil) {
fmt.Println("There was an issue reading the file " +
fileName +
". Please ensure that this file exsists in the following directory: " +
filePath +
"\nPlease see the config.go file to configure these settings." )
os.Exit(0)
}
// convet the raw data into typed data
return processData(data)
}
// open the file and read in the data, storing the data in a collection
// of lines
func readData() ([]string, error){
root, _ := os.Getwd()
file, err := os.Open(root + filePath + fileName)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// converts a collection of strings into player objects
func processData(data []string) map[string]Player {
players := make(map[string]Player)
for _, d := range data {
// allows for comments
if !strings.Contains(d, "//") {
var newPlayer = newPlayer(d)
players[newPlayer.id] = newPlayer
}
}
return players
}