master
1package main
2
3import (
4 "sync"
5)
6
7// API_ROOT defines the root path for the web api interface
8const API_ROOT = "/api"
9
10// SUBMISSION_SECRET holds the secret for submitting questions
11const SUBMISSION_SECRET = "shadows"
12
13// mapLock locks the maps to prevent concurrency issues
14var mapLock *sync.Mutex
15
16const CATEGORY_SEPARATOR = ":"
17
18// contains the list of categories
19// as pulled from the server
20var categories []string
21
22// define colors for printing to terminals
23const (
24 COLOR_GREEN = "\x1b[32m"
25 COLOR_BLUE = "\x1b[34m"
26 COLOR_RED = "\x1b[31m"
27 COLOR_BOLD = "\x1b[1m"
28 COLOR_RESET = "\x1b[0m"
29)
30
31// EXIT_CODES define exit error codes
32type EXIT_CODES struct {
33 BAD_CONFIG,
34 FILE_IO_ERROR,
35 NETWORK_IO_ERROR int
36}
37
38// EXIT_CODE declares exit codes
39var EXIT_CODE = EXIT_CODES{
40 BAD_CONFIG: 1,
41 FILE_IO_ERROR: 2,
42 NETWORK_IO_ERROR: 3}
43
44// Record stores information related to a single record
45type Record struct {
46 Question,
47 Reference,
48 Answer,
49 Path string
50 ID int
51}
52
53// ClientRecord stores the client response to a particular record
54type ClientRecord struct {
55 Record
56 ClientAnswer string
57 AnsweredCorrectly bool
58}
59
60// ClientTest stores a full test and its results for the client
61type ClientTest struct {
62 Records []ClientRecord
63 Score float32
64 Username string
65}
66
67// CountingSemaphore implements a counting semaphore
68// using go channels as suggested in the forums
69type CountingSemaphore struct {
70 counter chan bool //store empty structs since they take 0 memory
71 locksTaken int
72}
73
74// P incrememnts the counter here
75// note: race conditions can totally happen
76func (cSem *CountingSemaphore) P() {
77 if cSem.counter != nil {
78 cSem.locksTaken++
79 cSem.counter <- true
80 }
81}
82
83// V decrements the counter here
84// note: race conditions can totally happen
85func (cSem *CountingSemaphore) V() {
86 if cSem.counter != nil && cSem.locksTaken > 0 {
87 <-cSem.counter
88 cSem.locksTaken--
89 }
90}
91
92// Queued returns the number of items in the
93// semaphore, or -1 if the semaphore is unitialized
94// note: race conditions can totally happen
95func (cSem *CountingSemaphore) Queued() int {
96 if cSem.counter != nil {
97 return cSem.locksTaken
98 }
99 return -1
100}
101
102// SetCapacity sets the max number of items accessing this semaphore
103// note: race conditions can totally happen
104func (cSem *CountingSemaphore) SetCapacity(cap int) {
105 cSem.counter = make(chan bool, cap)
106 //cSem.counter <- struct{}{}
107}
108
109func init() {
110 categories = []string{}
111 mapLock = &sync.Mutex{}
112}