Freeman185

SHEMA

Jul 31st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. customSchema = class({})
  2.  
  3. function customSchema:init()
  4.  
  5.     -- Check the schema_examples folder for different implementations
  6.  
  7.     -- Flag Example
  8.     -- statCollection:setFlags({version = GetVersion()})
  9.  
  10.     -- Listen for changes in the current state
  11.     ListenToGameEvent('game_rules_state_change', function(keys)
  12.         local state = GameRules:State_Get()
  13.  
  14.         -- Send custom stats when the game ends
  15.         if state == DOTA_GAMERULES_STATE_POST_GAME then
  16.  
  17.             -- Build game array
  18.             local game = BuildGameArray()
  19.  
  20.             -- Build players array
  21.             local players = BuildPlayersArray()
  22.  
  23.             -- Print the schema data to the console
  24.             if statCollection.TESTING then
  25.                 PrintSchema(game, players)
  26.             end
  27.  
  28.             -- Send custom stats
  29.             if statCollection.HAS_SCHEMA then
  30.                 statCollection:sendCustom({ game = game, players = players })
  31.             end
  32.         end
  33.     end, nil)
  34.  
  35.     -- Write 'test_schema' on the console to test your current functions instead of having to end the game
  36.     if Convars:GetBool('developer') then
  37.         Convars:RegisterCommand("test_schema", function() PrintSchema(BuildGameArray(), BuildPlayersArray()) end, "Test the custom schema arrays", 0)
  38.         Convars:RegisterCommand("test_end_game", function() GameRules:SetGameWinner(DOTA_TEAM_GOODGUYS) end, "Test the end game", 0)
  39.     end
  40. end
  41.  
  42.  
  43.  
  44. -------------------------------------
  45.  
  46. -- In the statcollection/lib/utilities.lua, you'll find many useful functions to build your schema.
  47. -- You are also encouraged to call your custom mod-specific functions
  48.  
  49. -- Returns a table with our custom game tracking.
  50. function BuildGameArray()
  51.     local game = {}
  52.  
  53.     -- Add game values here as game.someValue = GetSomeGameValue()
  54.  
  55.     return game
  56. end
  57.  
  58. -- Returns a table containing data for every player in the game
  59. function BuildPlayersArray()
  60.     local players = {}
  61.     for playerID = 0, DOTA_MAX_PLAYERS do
  62.         if PlayerResource:IsValidPlayerID(playerID) then
  63.             if not PlayerResource:IsBroadcaster(playerID) then
  64.  
  65.                 local hero = PlayerResource:GetSelectedHeroEntity(playerID)
  66.  
  67.                 table.insert(players, {
  68.                     -- steamID32 required in here
  69.                     steamID32 = PlayerResource:GetSteamAccountID(playerID),
  70.  
  71.                     -- Example functions for generic stats are defined in statcollection/lib/utilities.lua
  72.                     -- Add player values here as someValue = GetSomePlayerValue(),
  73.                 })
  74.             end
  75.         end
  76.     end
  77.  
  78.     return players
  79. end
  80.  
  81. -- Prints the custom schema, required to get an schemaID
  82. function PrintSchema(gameArray, playerArray)
  83.     print("-------- GAME DATA --------")
  84.     DeepPrintTable(gameArray)
  85.     print("\n-------- PLAYER DATA --------")
  86.     DeepPrintTable(playerArray)
  87.     print("-------------------------------------")
  88. end
  89.  
  90. -------------------------------------
  91.  
  92. -- If your gamemode is round-based, you can use statCollection:submitRound(bLastRound) at any point of your main game logic code to send a round
  93. -- If you intend to send rounds, make sure your settings.kv has the 'HAS_ROUNDS' set to true. Each round will send the game and player arrays defined earlier
  94. -- The round number is incremented internally, lastRound can be marked to notify that the game ended properly
  95. function customSchema:submitRound()
  96.  
  97.     local winners = BuildRoundWinnerArray()
  98.     local game = BuildGameArray()
  99.     local players = BuildPlayersArray()
  100.  
  101.     statCollection:sendCustom({ game = game, players = players })
  102. end
  103.  
  104. -- A list of players marking who won this round
  105. function BuildRoundWinnerArray()
  106.     local winners = {}
  107.     local current_winner_team = GameRules.Winner or 0 --You'll need to provide your own way of determining which team won the round
  108.     for playerID = 0, DOTA_MAX_PLAYERS do
  109.         if PlayerResource:IsValidPlayerID(playerID) then
  110.             if not PlayerResource:IsBroadcaster(playerID) then
  111.                 winners[PlayerResource:GetSteamAccountID(playerID)] = (PlayerResource:GetTeam(playerID) == current_winner_team) and 1 or 0
  112.             end
  113.         end
  114.     end
  115.     return winners
  116. end
  117.  
  118. -------------------------------------
Add Comment
Please, Sign In to add comment