File: host.lua

package info (click to toggle)
wesnoth-1.14 1:1.14.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 817,348 kB
  • sloc: cpp: 228,458; java: 29,664; python: 22,615; lisp: 7,550; sh: 3,325; xml: 1,767; perl: 1,501; ansic: 1,083; makefile: 264; php: 98; sed: 44
file content (112 lines) | stat: -rw-r--r-- 2,677 bytes parent folder | download | duplicates (3)
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
-- host.lua --
-- Try to host a game called "Test"

local function plugin()

  local function log(text)
    std_print("host: " .. text)
  end

  local counter = 0

  local events, context, info

  local function idle_text(text)
    counter = counter + 1
    if counter >= 100 then
      counter = 0
      log("idling " .. text)
    end
  end

  log("hello world")

  repeat
    events, context, info = coroutine.yield()
    idle_text("in " .. info.name .. " waiting for titlescreen or lobby")
  until info.name == "titlescreen" or info.name == "Multiplayer Lobby"

  local tries = 0
  while info.name == "titlescreen" and tries < 100 do
    context.play_multiplayer({})
    tries = tries + 1
    log("playing multiplayer...")
    events, context, info = coroutine.yield()
  end
  if info.name == "titlescreen" then
    context.exit({code = 1})
    coroutine.yield()
  end

  repeat
    events, context, info = coroutine.yield()
    idle_text("in " .. info.name .. " waiting for lobby")
  until info.name == "Multiplayer Lobby"

  context.chat({message = "hosting"})
  log("creating a game")
  context.create({})

  repeat
    events, context, info = coroutine.yield()
    idle_text("in " .. info.name .. " waiting for create")
  until info.name == "Multiplayer Create"

  context.select_type({type = "scenario"})
  local s = info.find_level({id = "test1"})
  if s.index < 0 then
	log(" error: Could not find scenario with id=test1")
  end
  context.select_level({index = s.index})

  log("configuring a game")
  context.set_name({name = "Test"})
  context.update_settings({registered_users = false})

  events, context, info = coroutine.yield()

  context.create({})

  ready = nil
  repeat
    events, context, info = coroutine.yield()
    for i,v in ipairs(events) do
      if v[1] == "chat" then
        std_print(events[i][2])
        if v[2].message == "ready" then
          ready = true
        end
      end
    end
    idle_text("in " .. info.name .. " waiting for ready in chat")
  until ready

  log("starting game...")
  context.chat({message = "starting"})
  context.launch({})

  repeat
    events, context, info = coroutine.yield()
    idle_text("in " .. info.name .. " waiting for game")
  until info.name == "Game"

  log("got to a game context...")

  repeat
    events, context, info = coroutine.yield()
    idle_text("in " .. info.name .. " waiting for not game")
  until info.name ~= "Game"

  log("left a game context...")

  repeat
    context.quit({})
    log("quitting a " .. info.name .. " context...")
    events, context, info = coroutine.yield()
  until info.name == "titlescreen"

  context.exit({code = 0})
  coroutine.yield()
end

return plugin