1-- host.lua --
2-- Try to host a game called "Test"
3
4local function plugin()
5
6  local function log(text)
7    std_print("host: " .. text)
8  end
9
10  local counter = 0
11
12  local events, context, info
13
14  local function idle_text(text)
15    counter = counter + 1
16    if counter >= 100 then
17      counter = 0
18      log("idling " .. text)
19    end
20  end
21
22  log("hello world")
23
24  repeat
25    events, context, info = coroutine.yield()
26    idle_text("in " .. info.name .. " waiting for titlescreen or lobby")
27  until info.name == "titlescreen" or info.name == "Multiplayer Lobby"
28
29  local tries = 0
30  while info.name == "titlescreen" and tries < 100 do
31    context.play_multiplayer({})
32    tries = tries + 1
33    log("playing multiplayer...")
34    events, context, info = coroutine.yield()
35  end
36  if info.name == "titlescreen" then
37    context.exit({code = 1})
38    coroutine.yield()
39  end
40
41  repeat
42    events, context, info = coroutine.yield()
43    idle_text("in " .. info.name .. " waiting for lobby")
44  until info.name == "Multiplayer Lobby"
45
46  context.chat({message = "hosting"})
47  log("creating a game")
48  context.create({})
49
50  repeat
51    events, context, info = coroutine.yield()
52    idle_text("in " .. info.name .. " waiting for create")
53  until info.name == "Multiplayer Create"
54
55  context.select_type({type = "scenario"})
56  local s = info.find_level({id = "test1"})
57  if s.index < 0 then
58	log(" error: Could not find scenario with id=test1")
59  end
60  context.select_level({index = s.index})
61
62  log("configuring a game")
63  context.set_name({name = "Test"})
64  context.update_settings({registered_users = false})
65
66  events, context, info = coroutine.yield()
67
68  context.create({})
69
70  ready = nil
71  repeat
72    events, context, info = coroutine.yield()
73    for i,v in ipairs(events) do
74      if v[1] == "chat" then
75        std_print(events[i][2])
76        if v[2].message == "ready" then
77          ready = true
78        end
79      end
80    end
81    idle_text("in " .. info.name .. " waiting for ready in chat")
82  until ready
83
84  log("starting game...")
85  context.chat({message = "starting"})
86  context.launch({})
87
88  repeat
89    events, context, info = coroutine.yield()
90    idle_text("in " .. info.name .. " waiting for game")
91  until info.name == "Game"
92
93  log("got to a game context...")
94
95  repeat
96    events, context, info = coroutine.yield()
97    idle_text("in " .. info.name .. " waiting for not game")
98  until info.name ~= "Game"
99
100  log("left a game context...")
101
102  repeat
103    context.quit({})
104    log("quitting a " .. info.name .. " context...")
105    events, context, info = coroutine.yield()
106  until info.name == "titlescreen"
107
108  context.exit({code = 0})
109  coroutine.yield()
110end
111
112return plugin
113