utstats /html/import/import_weapons.php

Language PHP Lines 250
MD5 Hash dadbf1145ca8bf14321d77094d41d2d4 Estimated Cost $5,117 (why?)
Repository https://github.com/graywh/utstats.git View Raw File View Project SPDX
  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
<?php

// Read all available weapons, we'll need them later
if (!isset($weaponnames))
{
  $sql_weaponnames = "SELECT id, name FROM uts_weapons";
  $q_weaponnames = mysql_query($sql_weaponnames);
  while ($r_weaponnames = mysql_fetch_array($q_weaponnames))
  {
    $weaponnames[$r_weaponnames['name']] = $r_weaponnames['id'];
  }
}

// Get all kills by weapon and player
$sql_weapons = "  SELECT  col2 AS player,
                col3 AS weaponname,
                COUNT(*) AS kills
            FROM  uts_temp_$uid 
            WHERE col1 = 'kill'
              OR  col1 = 'teamkill'
            GROUP BY  col3, col2";
            
$q_weapons = mysql_query($sql_weapons) or die(mysql_error());
$weapons = array();
while ($r_weapons = mysql_fetch_array($q_weapons))
{
  
  // Get the wepon's id or assign a new one
  if (empty($r_weapons['weaponname']))
    continue;
    
  if (isset($weaponnames[$r_weapons['weaponname']]))
  {
    $weaponid = $weaponnames[$r_weapons['weaponname']];
  }
  else
  {
    mysql_query("INSERT INTO uts_weapons SET name = '". addslashes($r_weapons['weaponname']) ."'") or die(mysql_error());
    $weaponid = mysql_insert_id();
    $weaponnames[$r_weapons['weaponname']] = $weaponid;
  }
  
  // Get the unique pid of this player
  if (!isset($playerid2pid[$r_weapons['player']]))
  {
    continue;
  }
  else
  {
    $pid = $playerid2pid[$r_weapons['player']];
  }
  
  $weapons[$pid][$weaponid] = array(
                    'weap_kills'    => $r_weapons['kills'],
                    'weap_shotcount'  => 0,
                    'weap_hitcount'   => 0,
                    'weap_damagegiven'  => 0,
                    'weap_accuracy'   => 0
  );

}


// Get the weapon statistics
$sql_weapons = "  SELECT  col1 AS type,
                col2 AS weaponname, 
                col3 AS player,
                col4 AS value
            FROM  uts_temp_$uid 
            WHERE col1 LIKE 'weap_%'";
            
$q_weapons = mysql_query($sql_weapons) or die(mysql_error());
while ($r_weapons = mysql_fetch_array($q_weapons))
{
  // Get the wepon's id or assign a new one
  if (empty($r_weapons['weaponname'])) continue;
  if (isset($weaponnames[$r_weapons['weaponname']]))
  {
    $weaponid = $weaponnames[$r_weapons['weaponname']];
  }
  else
  {
    mysql_query("INSERT INTO uts_weapons SET name = '". addslashes($r_weapons['weaponname']) ."'") or die(mysql_error());
    $weaponid = mysql_insert_id();
    $weaponnames[$r_weapons['weaponname']] = $weaponid;
  }
  
  // Get the unique pid of this player
  if (!isset($playerid2pid[$r_weapons['player']]))
  {
//    Happens if we're ignoring bots or banned players
    continue;
  }
  else
  {
    $pid = $playerid2pid[$r_weapons['player']];
  }
  
  if (!isset($weapons[$pid][$weaponid]['weap_kills']))
  {
    $weapons[$pid][$weaponid] = array(
                      'weap_kills'    => 0,
                      'weap_shotcount'  => 0,
                      'weap_hitcount'   => 0,
                      'weap_damagegiven'  => 0,
                      'weap_accuracy'   => 0
    );  
  }
  
  $weapons[$pid][$weaponid][$r_weapons['type']] = $r_weapons['value'];
}


// Write the weapon statistics for this match
$s_weapons = array();
foreach($weapons as $playerid => $weapon)   // For each player
{
  foreach($weapon as $weaponid => $infos)   // For each weapon
  {
    if ($infos['weap_kills'] == 0 and $infos['weap_shotcount'] == 0 and $infos['damagegiven'] == 0 and $infos['hitcount'] == 0)
      continue;
    
    if ($infos['weap_shotcount'] > 0 and $infos['weap_hitcount'] > 0)
    {
      $infos['weap_accuracy'] = round(100 * $infos['weap_hitcount'] / $infos['weap_shotcount'], 2);
      if ($infos['weap_accuracy'] > 100.0)
        $infos['weap_accuracy'] = 100;
    }
    else
    {
      $infos['weap_accuracy'] = 0;
    }
    
    mysql_query(" INSERT  uts_weaponstats
            SET   matchid = '$matchid',
                pid = '$playerid',
                weapon = '$weaponid',
                kills = '${infos['weap_kills']}',
                shots = '${infos['weap_shotcount']}',
                hits = '${infos['weap_hitcount']}',
                damage = '${infos['weap_damagegiven']}',
                acc = '${infos['weap_accuracy']}';"
    ) or die(mysql_error());
            
    // Update the player's weapon statistics (matchid 0)
    mysql_query(" REPLACE uts_weaponstats
            SELECT  0 AS matchid,
                '$playerid' AS pid,
                '$weaponid' AS weapon,
                SUM(kills) AS kills,
                SUM(shots) AS shots,
                SUM(hits) AS hits,
                SUM(damage) AS damage,
                LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
            FROM  uts_weaponstats
            WHERE pid = '$playerid'
              AND weapon = '$weaponid'
              AND matchid > 0
            GROUP BY pid, weapon;"
    ) or die(mysql_error());
  
  }
  
  // Create the player's match statistics (weapon 0)
  mysql_query(" REPLACE  uts_weaponstats
          SELECT  '$matchid' AS matchid,
              '$playerid' AS pid,
              0 AS weapon,
              SUM(kills) AS kills,
              SUM(shots) AS shots,
              SUM(hits) AS hits,
              SUM(damage) AS damage,
              LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
          FROM  uts_weaponstats
          WHERE matchid = '$matchid'
            AND pid = '$playerid'
            AND weapon > 0
          GROUP BY matchid, pid;"
  ) or die(mysql_error());

  // Update the player's match entry in uts_player
  mysql_query(" UPDATE  uts_player AS p,
              uts_weaponstats AS w
          SET   p.accuracy = w.acc
          WHERE p.matchid = '$matchid'
            AND w.matchid = p.matchid
            AND p.pid = '$playerid'
            AND w.pid = p.pid
            AND w.weapon = 0;"
  ) or die(mysql_error());
  
  // Update the player's career statistics (weapon 0, match 0)
  mysql_query(" REPLACE uts_weaponstats
          SELECT  0 AS matchid,
              '$playerid' AS pid,
              0 AS weapon,
              SUM(kills) AS kills,
              SUM(shots) AS shots,
              SUM(hits) AS hits,
              SUM(damage) AS damage,
              LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
          FROM  uts_weaponstats
          WHERE matchid > 0
            AND pid = '$playerid'
            AND weapon > 0
          GROUP BY pid;"
  ) or die(mysql_error());
}

// Update the global weapon statistics (matchid 0, playerid 0)
foreach($s_weapons as $weaponid => $infos)    // For each weapon
{
  if ($infos['weap_kills'] == 0 and $infos['weap_shotcount'] == 0)
    continue;
  
  // Update the weapon's global record (pid 0, matchid 0)
  mysql_query(" REPLACE uts_weaponstats
          SELECT  0 AS matchid,
              0 AS pid,
              '$weaponid' AS weapon,
              SUM(kills) AS kills,
              SUM(shots) AS shots,
              SUM(hits) AS hits,
              SUM(damage) AS damage,
              LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) AS acc
          FROM  uts_weaponstats
          WHERE matchid > 0
            AND pid > 0
            AND weapon = '$weaponid'
          GROUP BY weapon;"
  ) or die(mysql_error());
}

// Update the global statistics (matchid 0, playerid 0, weaponid 0)
mysql_query(" REPLACE uts_weaponstats
        SELECT  0 as matchid,
            0 as pid,
            0 as weapon,
            SUM(kills) as kills,
            SUM(shots) as shots,
            SUM(hits) as hits,
            SUM(damage) as damage,
            LEAST(ROUND(10000*SUM(hits)/SUM(shots))/100, 100) as acc
        FROM  uts_weaponstats
        WHERE matchid > 0
          AND pid > 0
          AND weapon > 0;"
) or die(mysql_error());
?>
Back to Top