Advertisement
gambuzzi

decode.go

May 11th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.83 KB | None | 0 0
  1. package rpgkata
  2.  
  3. import (
  4.   "strconv"
  5.   // "fmt"
  6. )
  7.  
  8. type Player struct {
  9.   Name           string
  10.   Speed          int
  11.   AttackCounter  int
  12.   MaxHealth      int
  13.   Health         int
  14.   Weapon         string
  15.   WeaponType     int
  16.   DealedDamage   int
  17.   DamageFixed    int
  18.   DamageVariable int
  19.   ArmorName      string
  20.   ArmorHealth    int
  21. }
  22.  
  23. func Decode(datain *StringOrArray) []*Player {
  24.   var players []*Player
  25.  
  26.  
  27.   for _, player := range datain.Thearray {
  28.     p := new(Player)
  29.     pieces := player.Thearray
  30.     p.Name = pieces[0].Thestring
  31.     p.Speed, _ = strconv.Atoi(pieces[1].Thestring)
  32.     p.Health, _ = strconv.Atoi(pieces[2].Thestring)
  33.  
  34.     if pieces[3].Type==2 { // Weapon
  35.       // fmt.Println("doppio")
  36.       p.Weapon = pieces[3].Thearray[0].Thestring
  37.       p.WeaponType, _ = strconv.Atoi(pieces[3].Thearray[1].Thestring)
  38.     } else {
  39.       // fmt.Println("singolo")
  40.       p.Weapon = pieces[3].Thestring
  41.       p.WeaponType = 0
  42.     }
  43.  
  44.     if pieces[4].Type==2 { // Damage
  45.       // fmt.Println("doppio")
  46.       p.DamageFixed, _ = strconv.Atoi(pieces[4].Thearray[0].Thestring)
  47.       p.DamageVariable, _ = strconv.Atoi(pieces[4].Thearray[1].Thestring)
  48.       p.DamageVariable -= p.DamageFixed
  49.     } else {
  50.       // fmt.Println("singolo")
  51.       p.DamageFixed, _ = strconv.Atoi(pieces[4].Thestring)
  52.       p.DamageVariable = 0
  53.     }
  54.  
  55.     p.MaxHealth = p.Health
  56.  
  57.     // fmt.Printf("len(pieces) = %d\n", len(pieces))
  58.     if len(pieces) > 6 { // Armor
  59.         p.ArmorName = pieces[5].Thestring
  60.         p.ArmorHealth, _ = strconv.Atoi(pieces[6].Thestring)
  61.     } else {
  62.         p.ArmorName = "'None'"
  63.         p.ArmorHealth = 0
  64.     }
  65.  
  66.     if p.ArmorHealth == 0 {
  67.       p.ArmorName = "'None'"
  68.     }
  69.  
  70.     p.DealedDamage = 0
  71.     p.AttackCounter = 0
  72.  
  73.     players = append(players, p)
  74.   }
  75.  
  76.   return players
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement