aboutsummaryrefslogtreecommitdiff
path: root/logic/cmd.go
blob: 7378e376ddc93b0cab0eebed46bdfd4d0d3044af (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2016 Florian Fischer. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.

package logic

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
)

type Cmd struct {
	Cmd  string
	Argv []int
	Argc int
}

var cmdTable = map[string]int{
	"d": 1, "dices": 1,
	"p": 2, "print": 2,
	"q": 3, "quit": 3,
	"h": 4, "help": 4,
	"i": 5, "insert": 5,
	"c": 6, "cancel": 6,
	"r": 7, "reroll": 7,
	"pos": 8, "positions": 8,
}

// we ignore arguments which are to much
func ParseCmd(s string) (Cmd, error) {
	split := strings.Split(s, " ")
	if len(split) == 0 {
		return Cmd{}, errors.New("Empty command")
	}

	argv := make([]int, 0, 5)
	argc := 0

	switch split[0] {
	case "d", "p", "q":
		return Cmd{Cmd: split[0]}, nil
	case "h":
		if len(split) > 1 {
			if i, exists := cmdTable[split[1]]; exists {
				argv = append(argv, i)
				argc++
			}
		}
		if argc < 1 {
			argv = append(argv, 0)
			argc++
		}
	case "i", "c":
		if len(split) < 2 {
			return Cmd{}, errors.New(split[0] + ": missing argument")
		}
		if e, exists := ScorePositions[split[1]]; exists {
			argv = append(argv, e)
		} else if i, err := strconv.Atoi(split[1]); err != nil {
			return Cmd{}, err
		} else if i < 1 || i > 13 {
			return Cmd{}, errors.New(split[0] + ": position out of range")
		} else {
			argv = append(argv, i-1)
		}
		argc++
	case "r":
		for _, v := range split[1:] {
			i, err := strconv.Atoi(v)
			if err != nil {
				return Cmd{}, err
			}
			if i < 1 || i > 5 {
				return Cmd{}, errors.New("r: dice index out of range")
			}
			argv = append(argv, i)
			argc++
			// ignore additional arguments
			if argc == 5 {
				break
			}
		}
	case "pos":
		split[0] = "h"
		argv = append(argv, cmdTable["pos"])
		argc++
	default:
		return Cmd{}, errors.New("Invalid command")
	}

	return Cmd{split[0], argv[0:argc], argc}, nil
}

func CmdHelp(cmd int) string {
	s := ""
	switch cmd {
	case 1:
		s += fmt.Sprintln("d - print dices")
		s += fmt.Sprintln("Print out the dices")
	case 2:
		s += fmt.Sprintln("p - print score")
		s += fmt.Sprintln("Print out your score")
	case 3:
		s += fmt.Sprintln("q - quit the game")
		s += fmt.Sprintln("Terminates the game")
	case 4:
		s += fmt.Sprintln("h [cmd] - print help")
		s += fmt.Sprintln("Print the help for a specific command or gernal help.")
	case 5:
		s += fmt.Sprintln("i [pos] - insert into score")
		s += fmt.Sprintln("Insert your current dices into your score.")
		s += fmt.Sprintln("See \"h pos\" for a explenation of the score entries")
	case 6:
		s += fmt.Sprintln("c [pos] - cancel a entry")
		s += fmt.Sprintln("Write 0 into your score.")
		s += fmt.Sprintln("See \"h pos\" for a explenation of the score entries")
	case 7:
		s += fmt.Sprintln("r [dices] - reroll some dices")
		s += fmt.Sprintln("Reroll the specified dices.")
		s += fmt.Sprintln("The dices to reroll are represented by a list of their indices in the dice set.")
		s += fmt.Sprintln("Example:")
		s += fmt.Sprintln("\t\"r 1 4 5\" rerolls dice number 1, 4 and 5")
		s += fmt.Sprintln("\t\"r\" rerolls all dices")
	case 8:
		s += fmt.Sprintln("Positions:")
		s += fmt.Sprintln("\t1:  Aces")
		s += fmt.Sprintln("\t2:  Twos")
		s += fmt.Sprintln("\t3:  Threes")
		s += fmt.Sprintln("\t4:  Fours")
		s += fmt.Sprintln("\t5:  Fives")
		s += fmt.Sprintln("\t6:  Sixes")
		s += fmt.Sprintln("\t7:  ThreeOfAKind")
		s += fmt.Sprintln("\t8:  FourOfAKind")
		s += fmt.Sprintln("\t9:  FullHouse")
		s += fmt.Sprintln("\t10: SmallStraight")
		s += fmt.Sprintln("\t11: LargeStraight")
		s += fmt.Sprintln("\t12: Yathzze")
		s += fmt.Sprintln("\t13: Chance")
	default:
		s += fmt.Sprintln("h [cmd] - print help")
		s += fmt.Sprintln("p - print score")
		s += fmt.Sprintln("d - print dices")
		s += fmt.Sprintln("i [pos] - insert into score")
		s += fmt.Sprintln("r [dices] - reroll some dices")
		s += fmt.Sprintln("c [pos] - cancel a entry")
	}
	return s
}