aboutsummaryrefslogtreecommitdiffstats
path: root/us_maker_edition/src/lua/ULuaParty.pas
blob: 69096e97993dcb631ed0467ec9704fb4cc398ffb (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

unit ULuaParty;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

uses ULua;

{ lua c functions from Party table. Enables creating of party modes w/ lua scripts }

{ Party.Register - register party mode at party manager
  arguments: info: table
                   Name: String;          //< Name used as identifier (language strings, etc.). Has to be set.
                   CanNonParty: Boolean   //< mode is playable when not in party mode. defaulted to false if not set
                   CanParty: Boolean      //< mode is playable in party mode. defaulted to false if not set
                   PlayerCount: Table     //< playable with one, two, three etc. players per team. defaulted to no restrictions if not set. (use table constructor e.g. {1, 2, 3) means playable w/ 1, 2 or three players)
                   TeamCount: Table       //< playable with one, two, three etc. different teams. defaulted to no restrictions if not set. (use table constructor e.g. {1, 2, 3) means playable w/ 1, 2 or three players)

                   BeforeSongSelect: String   //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   AfterSongSelect: String    //< name of global that will be called after song is selected (if nil, not callable or returns true, default action will be executed)

                   BeforeSing: String         //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   OnSing: String             //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   AfterSing: String          //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)}
function ULuaParty_Register(L: Plua_State): Integer; cdecl;

{ Party.GameFinished - returns true if no party game is running or all rounds
  of current game were played }
function ULuaParty_GameFinished(L: Plua_State): Integer; cdecl;

(* Party.SetRoundRanking - sets ranking of current party round,
  arguments: Ranking: table
  ranking of team i is the value (integer from 1 to number of teams) of the
  table with index [i: number].
  you may call this function in the following way:
    Party.SetRoundRanking({3, 1, 2});
    this means: team 1 is ranked third, team 2 is ranked first and team 3 is
    ranked second.
  if no party game is started or party game is finished
  it will raise an error *)
function ULuaParty_SetRoundRanking(L: Plua_State): Integer; cdecl;

{ Party.GetTeams - returns a table with all information and structure as
  in the TPartyGame.Teams array }
function ULuaParty_GetTeams(L: Plua_State): Integer; cdecl;

{ Party.SetTeams - changes all fields from TPartyGame.Teams that have been
  set in the table given as first argument}
function ULuaParty_SetTeams(L: Plua_State): Integer; cdecl;

const
  ULuaParty_Lib_f: array [0..4] of lual_reg = (
    (name:'Register'; func:ULuaParty_Register),
    (name:'GameFinished'; func:ULuaParty_GameFinished),
    (name:'SetRoundRanking'; func:ULuaParty_SetRoundRanking),
    (name:'GetTeams'; func:ULuaParty_GetTeams),
    (name:'SetTeams'; func:ULuaParty_SetTeams)
  );

implementation
uses ULuaCore, ULuaUtils, UParty, SysUtils;


{ Party.Register - register party mode at party manager
  arguments: info: table
                   Name: String;          //< Name used as identifier (language strings, etc.). Has to be set.
                   CanNonParty: Boolean   //< mode is playable when not in party mode. defaulted to false if not set
                   CanParty: Boolean      //< mode is playable in party mode. defaulted to false if not set
                   PlayerCount: Table     //< playable with one, two, three etc. players per team. defaulted to no restrictions if not set. (use table constructor e.g. {1, 2, 3) means playable w/ 1, 2 or three players)
                   TeamCount: Table       //< playable with one, two, three etc. different teams. defaulted to no restrictions if not set. (use table constructor e.g. {1, 2, 3) means playable w/ 1, 2 or three players)

                   BeforeSongSelect: String   //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   AfterSongSelect: String    //< name of global that will be called after song is selected (if nil, not callable or returns true, default action will be executed)

                   BeforeSing: String         //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   OnSing: String             //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)
                   AfterSing: String          //< name of global that will be called before song select screen is shown (if nil, not callable or returns true, default action will be executed)}
function ULuaParty_Register(L: Plua_State): Integer; cdecl;
  var
    Info: TParty_ModeInfo;
    Key: String;
    P: TLuaPlugin;
begin
  Result := 0;
  
  // check for table on stack
  luaL_checkType(L, 1, LUA_TTABLE);

  // get parent id
  P := Lua_GetOwner(L);


  // set mode info to default
  Party.DefaultModeInfo(Info);


  // set parent in info rec and pop it from stack
  Info.Parent := P.Id;

  // go through table elements
  lua_pushNil(L);
  while (lua_Next(L, 1) <> 0) do
  begin
    Key := lowercase(lua_ToString(L, -2));

    if (Key = 'name') and lua_isString(L, -1) then
      Info.Name := lua_toString(L, -1)
    else if (Key = 'cannonparty') and lua_isBoolean(L, -1) then
      Info.CanNonParty := lua_toBoolean(L, -1)
    else if (Key = 'canparty') and lua_isBoolean(L, -1) then
      Info.CanParty := lua_toBoolean(L, -1)
    else if (Key = 'playercount') and lua_isTable(L, -1) then
      Info.PlayerCount := lua_toBinInt(L, -1)
    else if (Key = 'teamcount') and lua_isTable(L, -1) then
      Info.TeamCount := lua_toBinInt(L, -1)
    else if (Key = 'beforesongselect') and lua_isString(L, -1) then
      Info.Functions.BeforeSongSelect := lua_toString(L, -1)
    else if (Key = 'aftersongselect') and lua_isString(L, -1) then
      Info.Functions.AfterSongSelect := lua_toString(L, -1)
    else if (Key = 'beforesing') and lua_isString(L, -1) then
      Info.Functions.BeforeSing := lua_toString(L, -1)
    else if (Key = 'onsing') and lua_isString(L, -1) then
      Info.Functions.OnSing := lua_toString(L, -1)
    else if (Key = 'aftersing') and lua_isString(L, -1) then
      Info.Functions.AfterSing := lua_toString(L, -1);

    // pop value from stack so key is on top
    lua_pop(L, 1);
  end;

  // clear stack from table
  lua_pop(L, lua_gettop(L));

  if not Party.RegisterMode(Info) then
    luaL_error(L, PChar('can''t register party mode at party manager in Party.Register. Is Info.Name defined or is there another mode with this name?'));
end;

{ Party.GameFinished - returns true if no party game is running or all rounds
  of current game were played }
function ULuaParty_GameFinished(L: Plua_State): Integer; cdecl;
begin
  // clear stack
  lua_pop(L, lua_gettop(L));

  // push result
  lua_pushBoolean(L, Party.GameFinished);

  //we return one value
  Result := 1;
end;

{ Party.SetRoundRanking - sets ranking of current party round,
  if no party game is started or party game is finished
  it will raise an error }
function ULuaParty_SetRoundRanking(L: Plua_State): Integer; cdecl;
var
  R: AParty_TeamRanking;
  I: Integer;
  Rank: Integer;
begin
  Result := 0;

  luaL_checktype(L, 1, LUA_TTABLE);

  lua_checkstack(L, 1);

  SetLength(R, Length(Party.Teams));

  for I := 0 to High(R) do
  begin
    lua_pushInteger(L, (I+1));
    lua_gettable(L, 1);

    R[I].Rank := Length(R);
    R[I].Team := I;
    if (lua_isnumber(L, -1)) then
    begin
      Rank := lua_toInteger(L, -1);
      if (Rank >= 1) and (Rank <= Length(R)) then
        R[I].Rank := Rank
    end;

    lua_pop(L, 1);
    
  end;

  // pop table
  lua_pop(L, 1);

  if (not Party.SetRanking(R)) then
    luaL_error(L, PChar('cann''t set party round ranking. Is party started and not finished yet?'));
end;

{ Party.GetTeams - returns a table with all information and structure as
  in the TPartyGame.Teams array }
function ULuaParty_GetTeams(L: Plua_State): Integer; cdecl;
  var
    Team: Integer;
    Player: Integer;
begin
  // clear stack
  lua_pop(L, lua_gettop(L));

  // ensure we have enough stack slots left
  lua_checkstack(L, 7);

  // create the table we want to return
  lua_createtable(L, Length(Party.Teams), 0);

  // add the teams
  for Team := 0 to High(Party.Teams) do
  begin
    // push key for current teams value. lua array beggins at 1
    lua_pushInteger(L, Team + 1);

    // push table containing team info and players table
    lua_createtable(L, 0, 5);

    // team name
    lua_pushString(L, PChar(Party.Teams[Team].Name));
    lua_setField(L, -2, 'Name');

    // team score
    lua_pushInteger(L, Party.Teams[Team].Score);
    lua_setField(L, -2, 'Score');

    // team jokers left
    lua_pushInteger(L, Party.Teams[Team].JokersLeft);
    lua_setField(L, -2, 'JokersLeft');

    // team nextPlayer
    lua_pushInteger(L, Party.Teams[Team].NextPlayer);
    lua_setField(L, -2, 'NextPlayer');

    // team players table
    lua_createtable(L, Length(Party.Teams[Team].Players), 0);

    //add players
    for Player := 0 to High(Party.Teams[Team].Players) do
    begin
      // push key for current players value. lua array beggins at 1
      lua_pushInteger(L, Player + 1);

      // push table containing player info
      lua_createTable(L, 0, 2);

      // player name
      lua_PushString(L, PChar(Party.Teams[Team].Players[Player].Name));
      lua_SetField(L, -2, 'Name');

      // players times played
      lua_PushInteger(L, Party.Teams[Team].Players[Player].TimesPlayed);
      lua_SetField(L, -2, 'TimesPlayed');

      // add value - key - pair to teams player table
      lua_setTable(L, -3);
    end;

    lua_setField(L, -2, 'Players');

    // add value - key - pair to returned table
    lua_setTable(L, -3);
  end;

  // we return 1 value (the first table)
  Result := 1;
end;

{ Party.SetTeams - changes all fields from TPartyGame.Teams that have been
  set in the table given as first argument}
function ULuaParty_SetTeams(L: Plua_State): Integer; cdecl;

  procedure Do_Player(Team, Player: Integer);
    var
      Key: String;
  begin
    if (Player >= 0) and (Player <= High(Party.Teams[Team].Players)) then
    begin
      // go through table elements
      lua_pushNil(L);
      while (lua_Next(L, -2) <> 0) do
      begin
        Key := lowercase(lua_ToString(L, -2));

        if (Key = 'name') and lua_isString(L, -1) then
          Party.Teams[Team].Players[Player].Name := lua_toString(L, -1)
        else if (Key = 'timesplayed') and lua_isNumber(L, -1) then
          Party.Teams[Team].Players[Player].TimesPlayed := lua_toInteger(L, -1);

        // pop value from stack so key is on top
        lua_pop(L, 1);
      end;
    end;
  end;

  procedure Do_Players(Team: Integer);
  begin
    // go through table elements
    lua_pushNil(L);
    while (lua_Next(L, -2) <> 0) do
    begin
      // check if key is a number and value is a table
      if (lua_isNumber(L, -2)) and (lua_isTable(L, -1)) then
        Do_Player(Team, lua_toInteger(L, -2));

      // pop value from stack so key is on top
      lua_pop(L, 1);
    end;
  end;

  procedure Do_Team(Team: Integer);
    var
      Key: String;
  begin
    if (Team >= 0) and (Team <= High(Party.Teams)) then
    begin
      // go through table elements
      lua_pushNil(L);
      while (lua_Next(L, -2) <> 0) do
      begin
        Key := lowercase(lua_ToString(L, -2));

        if (Key = 'name') and lua_isString(L, -1) then
          Party.Teams[Team].Name := lua_toString(L, -1)
        else if (Key = 'score') and lua_isNumber(L, -1) then
          Party.Teams[Team].Score := lua_toInteger(L, -1)
        else if (Key = 'jokersleft') and lua_isNumber(L, -1) then
          Party.Teams[Team].JokersLeft := lua_toInteger(L, -1)
        else if (Key = 'currentplayer') and lua_isNumber(L, -1) then
          Party.Teams[Team].NextPlayer := lua_toInteger(L, -1)
        else if (Key = 'players') and lua_isTable(L, -1) then
          Do_Players(Team);

        // pop value from stack so key is on top
        lua_pop(L, 1);
      end;
    end;
  end;
begin
  Result := 0;
  
  // check for table on stack
  luaL_checkType(L, 1, LUA_TTABLE);


  // go through table elements
  lua_pushNil(L);
  while (lua_Next(L, 1) <> 0) do
  begin
    // check if key is a number and value is a table
    if (lua_isNumber(L, -2)) and (lua_isTable(L, -1)) then
      Do_Team(lua_toInteger(L, -2));

    // pop value from stack so key is on top
    lua_pop(L, 1);
  end;

  // clear stack from table
  lua_pop(L, lua_gettop(L));
end;

end.