File: common.sh

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (80 lines) | stat: -rw-r--r-- 2,092 bytes parent folder | download | duplicates (2)
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
#!/bin/bash

function check {
  if [ ! $(command -v netstat) ] && [ ! $(command -v ss) ]; then
    echo "[ERROR] netstat or ss is required to check for available ports"
    exit 1
  fi
  if [ ! -v wz2100cmd ]; then
    echo "[ERROR] wz2100cmd variable is not set."
    exit 1
  fi
  if [ "$players" == "" ] || [ ! $players -gt 0 ]; then
    echo "[ERROR] The number of players is not set."
    exit 1
  fi
  if [ "$hostfile" == "" ]; then
    echo "[ERROR] No hostfile given."
    exit 1
  fi
  if [ "$portmin" == "" ] || [ ! $portmin -gt 0 ]; then
    portmin=2100
  fi
  if [ "$portmax" == "" ] || [ $portmax -lt $portmin ]; then
    portmax=$portmin
  fi
}

function run_host {
  check
  hostpath="$cfgdir/autohost"
  # Find next available port
  port=$((portmin - 1))
  while true; do
    port=$((port + 1))
    if [ $(command -v netstat) ]; then
      ports="$(netstat -nt | grep [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:${port})"
    else
      ports="$(ss -ntO | grep [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*:${port})"
    fi
    if [ "$ports" == "" ] || [ "$port" -gt "$portmax" ]; then
      break
    fi
  done

  if [ "$port" -gt "$portmax" ]; then
    echo "No more port available"
    return 1
  fi
  # Set random map
  if [ ${#maps[@]} -gt 0 ]; then
    if [ "$cfgdir" == "" ]; then
      echo "[ERROR] cfgdir must be set to edit autohost file"
      return 1
    fi
    if [ ! -f "$hostpath/$hostfile" ]; then
      echo "[ERROR] Host file \"$hostpath/$hostfile\" not found."
      return 1
    fi
    rnd=$(($RANDOM % ${#maps[@]}))
    map=${maps[$rnd]}
    sed -i "s/\"map\": \"\(.*\)\",$/\"map\": \"$map\",/g" $hostpath/$hostfile
  fi
  # Set admin list
  admcmd=""
  for adm in ${adminkeys[@]}
  do
     admcmd+=" --addlobbyadminpublickey=${adm}"
  done
  for adm in ${adminhashes[@]}
  do
     admcmd+=" --addlobbyadminhash=${adm}"
  done
  if [ "$cfgdir" == "" ]; then
    configdir=""
  else
    configdir="--configdir=$cfgdir"
  fi
  # Run game
  exec $wz2100cmd $configdir --autohost=$hostfile --gameport=$port --startplayers=$players --enablelobbyslashcmd $admcmd --headless --nosound
}