aboutsummarylogtreecommitdiffstats
path: root/dom4
blob: 8e381e089ead4a6008fa1e5b97ffd596eb0e7f8d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash

# List all configured games.
function list {
  ls /usr/share/dom4/config | cut -d "." -f 1
}

# Install Dominions 4 via SteamCMD.
function install {
  if [ ! $(find /opt/dom4 -maxdepth 0 -type d -empty 2>/dev/null) ];then
    echo "/opt/dom4 is not empty. Is Dominions 4 already installed?"
    exit 0
  fi

  read -r -p "Steam username: " user
  read -r -s -p "Steam password: " pass

  echo
  echo "Installing Dominions 4 to /opt/dom4..."

  sudo -u dom4 steamcmd +login $user $pass +force_install_dir /opt/dom4 +app_update 259060 validate +quit

  read -r -p "Enter your Dominions 4 license key: " key
  echo $key | sudo -u dom4 tee /opt/dom4/dom4key > /dev/null

  echo "Copied license information to Dominions 4 install directory."
  echo "Installation complete. Use 'dom4 config' to set up a game."
}

# Change the current hosted game by editing /usr/share/dom4/current.
# Params:
#   $1  game name
function change {
  if [[ -s /usr/share/dom4/current ]]; then
    game=$(</usr/share/dom4/current)

    if [[ $1 != $game ]]; then
      read -r -n 2 -p "Currently hosting $game. Set up to host $1 instead? [Y/n] " ovr
      if [[ $ovr != Y ]]; then
        exit 0
      fi
      echo
    fi
  fi

  echo "Setting $1 up for hosting."
  echo
  echo "$1" | sudo -u dom4 tee /usr/share/dom4/current > /dev/null
}

# Load a properties file.
# Params:
#   $1  path to properties file
function config {
  if [[ -z $1 ]]; then
    cat <<EOF
Usage:

  dom4 config mygame
EOF
    exit 1
  fi

  if [[ -s "/usr/share/dom4/config/$1.properties" ]]; then
    read -r -n 2 -p "$1 is already configured. Edit this game? [Y/n] " ovr
    if [[ $ovr != Y ]]; then
      exit 0
    fi

    echo
  else
    newgame=true
    cat << EOF | sudo -u dom4 tee /usr/share/dom4/config/$1.properties > /dev/null
# See http://www.illwinter.com/dom4/startoptions.pdf for a full reference.
# Sections 3-5, 3-6, and 3-7 are especially relevant. Switches should be
# reproduced as given in the documentation, without any leading dashes (so to
# pass the '--era 1' setting to the server, include the line 'era 1' in this
# file).

# SERVER INFO

# Port number must be between 1024 and 65535, and forwarded in your router
# config. Master password is not required but useful if you want to be able to
# set dropped-out players to computer control.

port 6666
masterpass supersecure

# GAME SETTINGS

# The mapfile is required. Any .rgb or image files must have the same filename
# and differ only in extension since the map data is automatically copied over.

mapfile my_pretty_world.map
hours 26
era 1
renaming
storyevents
hofsize 15
requiredap 13
thrones 2 8 1
EOF
  fi

  sudo -u dom4 $EDITOR /usr/share/dom4/config/$1.properties

  mapfile=$(grep -oP "^mapfile\s+\K.+" /usr/share/dom4/config/$1.properties)
  mapname=$(basename $mapfile .map)

  if [ "$newgame" = true ]; then
    if [[ -s "$mapname.map" ]]; then
      echo "Copying $mapname map files..."

      sudo cp "$mapname".* /usr/share/dom4/maps
    elif [[ -s "$HOME/dominions4/maps/$mapname".map ]]; then
      echo "Copying $mapname map files from $HOME/dominions4/maps..."

      sudo cp "$HOME/dominions4/maps/$mapname".* /usr/share/dom4/maps
    else
      echo "Could not find $mapname.map in $(pwd) or $HOME/dominions4/maps. Please ensure the map file(s) exist in one or the other location and try again."

      exit 1;
    fi

    echo
  fi

  change $1

  cat <<EOF
Done. Start the service to let players upload their pretenders.

  sudo systemctl start dom4-server.service

Once all pretenders have been uploaded, stop the service and set the game to ready.

  dom4 ready
EOF
}

# Set game start flag in properties file.
# Params:
#   $1  game name (optional; uses whatever's in current if not supplied)
function ready {
  if [[ ! -z $1 ]]; then
    if [[ ! -s "/usr/share/dom4/config/$1.properties" ]]; then
      echo "No game named '$1' found."

      exit 1
    fi

    change $1
  elif [[ ! -s /usr/share/dom4/current ]]; then
    echo "No game configured. Use 'dom4 config' to set up a game."

    exit 1
  fi

  game=$(</usr/share/dom4/current)

  if [[ ! -d "/usr/share/dom4/savedgames/$game" ]]; then
    echo "No pretenders uploaded for $game. Start the service with systemctl to let players upload."

    exit 1
  fi

  if [[ -s "/usr/share/dom4/savedgames/$game/ftherlnd" ]]; then
    echo "$game has already been started. If the service is not running, start it with systemctl."

    exit 0
  fi

  players=$(ls -1 /usr/share/dom4/savedgames/$game/*.2h | wc -l)

  if grep -qE "uploadtime|uploadmaxp" /usr/share/dom4/config/$game.properties ; then
    echo "Upload flag already set for $game. Start the service with systemctl."

    exit 0
  fi

  echo "Setting start flag for $game ($players players)"

  echo "uploadmaxp $players" | sudo -u dom4 tee -a "/usr/share/dom4/config/$game.properties" > /dev/null

  cat <<EOF

Flag set. Start the dom4-server service to begin the game.

  sudo systemctl start dom4-server.service

You may wish to enable it as well to ensure it restarts on boot.
EOF
}

function delete {
  if [[ -z $1 ]]; then
    echo "Specify a game name to delete."
    exit 0
  fi

  if [[ -s /usr/share/dom4/current ]]; then
    game=$(</usr/share/dom4/current)

    if [[ $game = $1 ]]; then
      read -r -n 2 -p "$game is currently being hosted. Really delete? [Y/n] " ovr
      if [[ $ovr != Y ]]; then
        exit 0
      fi

      sudo -u dom4 rm /usr/share/dom4/current
    fi
  fi

  # leave the mapfile in case something else is using it
  sudo -u dom4 rm /usr/share/dom4/config/$1.properties
  sudo -u dom4 rm -rf /usr/share/dom4/savedgames/$1

  echo "Deleted $1."
}

case $1 in
  "list")
    list
    ;;

  "install")
    install
    ;;

  "config" | "configure")
    config "${@:2}"
    ;;

  "ready")
    ready "${@:2}"
    ;;

  "delete")
    delete "${@:2}"
    ;;

  *)
    cat <<EOF
Install Dominions 4 through SteamCMD:

  dom4 install

List configured games:

  dom4 list

Configure a new game, or edit an existing one:

  dom4 config mygame

Set start flag for current game after all pretenders have been uploaded, prior to running service:

  dom4 ready

Set start flag for mygame after pretenders have been uploaded and set it as current hosted game:

  dom4 ready mygame

Delete a game (this operation is irreversible!):

  dom4 delete mygame

Service commands:

  sudo systemctl start dom4-server.service
  sudo systemctl stop dom4-server.service
  sudo systemctl restart dom4-server.service
  systemctl status dom4-server.service
EOF
    ;;
esac